source: trunk/src/winmm/waveindart.cpp@ 7788

Last change on this file since 7788 was 7196, checked in by sandervl, 24 years ago

dynamically load MDM.DLL

File size: 19.0 KB
Line 
1/* $Id: waveindart.cpp,v 1.2 2001-10-24 22:47:42 sandervl Exp $ */
2
3/*
4 * Wave record class
5 *
6 * Copyright 2001 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 *
12 */
13
14
15/****************************************************************************
16 * Includes *
17 ****************************************************************************/
18
19
20
21#define INCL_BASE
22#define INCL_OS2MM
23#include <os2wrap.h> //Odin32 OS/2 api wrappers
24#include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers
25#include <stdlib.h>
26#include <string.h>
27#define OS2_ONLY
28#include <win32api.h>
29#include <wprocess.h>
30
31#include "misc.h"
32#include "waveindart.h"
33#include "initwinmm.h"
34
35#define DBG_LOCALLOG DBG_waveindart
36#include "dbglocal.h"
37
38#ifndef min
39#define min(a, b) ((a > b) ? b : a)
40#endif
41
42#ifndef max
43#define max(a, b) ((a > b) ? a : b)
44#endif
45
46LONG APIENTRY WaveInHandler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags);
47
48//TODO: mulaw, alaw & adpcm
49/******************************************************************************/
50/******************************************************************************/
51DartWaveIn::DartWaveIn(LPWAVEFORMATEX pwfx, ULONG fdwOpen, ULONG nCallback, ULONG dwInstance)
52 : WaveInOut(pwfx, fdwOpen, nCallback, dwInstance)
53{
54 MCI_GENERIC_PARMS GenericParms;
55 MCI_AMP_OPEN_PARMS AmpOpenParms;
56 APIRET rc;
57
58 fOverrun = FALSE;
59 fMixerSetup = FALSE;
60
61 MixBuffer = (MCI_MIX_BUFFER *)malloc(PREFILLBUF_DART_REC*sizeof(MCI_MIX_BUFFER));
62 MixSetupParms = (MCI_MIXSETUP_PARMS *)malloc(sizeof(MCI_MIXSETUP_PARMS));
63 BufferParms = (MCI_BUFFER_PARMS *)malloc(sizeof(MCI_BUFFER_PARMS));
64 if(!MixBuffer || !MixSetupParms || !BufferParms) {
65 dprintf(("ERROR: malloc failed!!"));
66 ulError = MMSYSERR_NOMEM;
67 return;
68 }
69
70 ulBufSize = DART_BUFSIZE_REC;
71
72 dprintf(("waveInOpen: samplerate %d, numChan %d bps %d (%d), format %x", SampleRate, nChannels, BitsPerSample, pwfx->nBlockAlign, pwfx->wFormatTag));
73 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
74 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
75
76 AmpOpenParms.usDeviceID = ( USHORT ) 0;
77 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
78
79 rc = mymciSendCommand(0, MCI_OPEN,
80 MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
81 (PVOID) &AmpOpenParms,
82 0);
83 DeviceId = AmpOpenParms.usDeviceID;
84 if(rc) {
85 dprintf(("MCI_OPEN failed\n"));
86 mciError(rc);
87 ulError = MMSYSERR_NODRIVER;
88 }
89 if(rc == 0) {
90 //Grab exclusive rights to device instance (NOT entire device)
91 GenericParms.hwndCallback = 0; //Not needed, so set to 0
92 rc = mymciSendCommand(DeviceId, MCI_ACQUIREDEVICE, MCI_EXCLUSIVE_INSTANCE,
93 (PVOID)&GenericParms, 0);
94 if(rc) {
95 dprintf(("MCI_ACQUIREDEVICE failed\n"));
96 mciError(rc);
97 ulError = MMSYSERR_NOTENABLED;
98 }
99 }
100
101 if(!ulError)
102 callback(WIM_OPEN, 0, 0);
103}
104/******************************************************************************/
105/******************************************************************************/
106DartWaveIn::~DartWaveIn()
107{
108 MCI_GENERIC_PARMS GenericParms;
109
110 State = STATE_STOPPED;
111
112 if(!ulError)
113 {
114 // Generic parameters
115 GenericParms.hwndCallback = 0; //hwndFrame
116
117 // Stop recording.
118 mymciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
119
120 mymciSendCommand(DeviceId,
121 MCI_BUFFER,
122 MCI_WAIT | MCI_DEALLOCATE_MEMORY,
123 (PVOID)&BufferParms,
124 0);
125
126 // Generic parameters
127 GenericParms.hwndCallback = 0; //hwndFrame
128
129 // Close the device
130 mymciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
131 }
132 if(!ulError)
133 {
134 callback(WIM_CLOSE, 0, 0);
135 }
136
137 if(MixBuffer)
138 free(MixBuffer);
139 if(MixSetupParms)
140 free(MixSetupParms);
141 if(BufferParms)
142 free(BufferParms);
143}
144/******************************************************************************/
145/******************************************************************************/
146/******************************************************************************/
147/******************************************************************************/
148MMRESULT DartWaveIn::start()
149{
150 MCI_GENERIC_PARMS GenericParms = {0};
151 MCI_AMP_OPEN_PARMS AmpOpenParms;
152 MCI_CONNECTOR_PARMS ConnectorParms;
153 MCI_AMP_SET_PARMS AmpSetParms ;
154 APIRET rc;
155 int i, curbuf, buflength;
156
157 dprintf(("DartWaveIn::start"));
158 if(State == STATE_RECORDING)
159 return(MMSYSERR_NOERROR);
160
161 if(fMixerSetup == FALSE)
162 {
163 dprintf(("device acquired\n"));
164 /* Set the MixSetupParms data structure to match the loaded file.
165 * This is a global that is used to setup the mixer.
166 */
167 memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
168
169 MixSetupParms->ulBitsPerSample = BitsPerSample;
170 MixSetupParms->ulSamplesPerSec = SampleRate;
171 MixSetupParms->ulFormatTag = MCI_WAVE_FORMAT_PCM;
172 MixSetupParms->ulChannels = nChannels;
173
174 dprintf(("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels));
175
176 /* Setup the mixer for recording of wave data
177 */
178 MixSetupParms->ulFormatMode = MCI_RECORD;
179 MixSetupParms->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
180 MixSetupParms->pmixEvent = WaveInHandler;
181
182 rc = mymciSendCommand(DeviceId,
183 MCI_MIXSETUP,
184 MCI_WAIT | MCI_MIXSETUP_INIT,
185 (PVOID)MixSetupParms,
186 0);
187
188 if ( rc != MCIERR_SUCCESS ) {
189 mciError(rc);
190 mymciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
191 (PVOID)&GenericParms, 0);
192 return(MMSYSERR_NOTSUPPORTED);
193 }
194
195 /*
196 * Set up the BufferParms data structure and allocate
197 * device buffers from the Amp-Mixer
198 */
199
200 int consumerate = getAvgBytesPerSecond();
201 //SvL: Recording usually isn't as sensitive to timing as playback
202 // Dividing by 8 instead of 32 here. Change if necessary.
203 int minbufsize = consumerate/8;
204
205 LPWAVEHDR pwh = wavehdr;
206 if(pwh) {
207 dprintf(("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength));
208
209 ulBufSize = pwh->dwBufferLength/2;
210 if(ulBufSize < minbufsize) {
211 dprintf(("set buffer size to %d bytes (org size = %d)", minbufsize, pwh->dwBufferLength));
212 ulBufSize = minbufsize;
213 }
214 }
215 else ulBufSize = minbufsize;
216
217 MixSetupParms->ulBufferSize = ulBufSize;
218
219 BufferParms->ulNumBuffers = PREFILLBUF_DART_REC;
220 BufferParms->ulBufferSize = MixSetupParms->ulBufferSize;
221 BufferParms->pBufList = MixBuffer;
222
223 for(i=0;i<PREFILLBUF_DART_REC;i++) {
224 MixBuffer[i].ulUserParm = (ULONG)this;
225 }
226
227 rc = mymciSendCommand(DeviceId,
228 MCI_BUFFER,
229 MCI_WAIT | MCI_ALLOCATE_MEMORY,
230 (PVOID)BufferParms,
231 0);
232
233 if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
234 mciError(rc);
235 mymciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
236 (PVOID)&GenericParms, 0);
237 return(MMSYSERR_NOTSUPPORTED);
238 }
239
240 //TODO: don't do this here. Select line or mic depending on aux settings
241 //(until we really support the mixer extensions anyway)
242 /* Set the connector to 'line in' */
243 memset( &ConnectorParms, '\0', sizeof( MCI_CONNECTOR_PARMS ) );
244 ConnectorParms.ulConnectorType = MCI_LINE_IN_CONNECTOR;
245 rc = mymciSendCommand( DeviceId,
246 MCI_CONNECTOR,
247 MCI_WAIT |
248 MCI_ENABLE_CONNECTOR |
249 MCI_CONNECTOR_TYPE,
250 ( PVOID ) &ConnectorParms,
251 0 );
252
253 /* Allow the user to hear what is being recorded
254 * by turning the monitor on
255 */
256 memset( &AmpSetParms, '\0', sizeof( MCI_AMP_SET_PARMS ) );
257 AmpSetParms.ulItem = MCI_AMP_SET_MONITOR;
258 rc = mymciSendCommand(DeviceId,
259 MCI_SET,
260 MCI_WAIT | MCI_SET_ON | MCI_SET_ITEM,
261 ( PVOID ) &AmpSetParms,
262 0 );
263
264 wmutex.enter(VMUTEX_WAIT_FOREVER);
265 fMixerSetup = TRUE;
266 }
267
268 for(i=0;i<PREFILLBUF_DART_REC;i++) {
269 memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
270 }
271 dprintf(("Dart opened, bufsize = %d\n", MixBuffer[0].ulBufferLength));
272
273 dprintf(("MixSetupParms = %X\n", MixSetupParms));
274 State = STATE_RECORDING;
275 fOverrun = FALSE;
276 wmutex.leave();
277
278 dprintf(("Dart recording"));
279
280 // Start recording.
281 USHORT selTIB = GetFS(); // save current FS selector
282 MixSetupParms->pmixRead( MixSetupParms->ulMixHandle,
283 &MixBuffer[0],
284 PREFILLBUF_DART_REC);
285 SetFS(selTIB); // switch back to the saved FS selector
286
287 return(MMSYSERR_NOERROR);
288}
289/******************************************************************************/
290/******************************************************************************/
291MMRESULT DartWaveIn::stop()
292{
293 MCI_GENERIC_PARMS Params;
294
295 wmutex.enter(VMUTEX_WAIT_FOREVER);
296 if(State != STATE_RECORDING) {
297 State = STATE_STOPPED;
298 wmutex.leave();
299 return(MMSYSERR_NOERROR);
300 }
301
302 State = STATE_STOPPED;
303 wmutex.leave();
304
305 memset(&Params, 0, sizeof(Params));
306
307 // Stop recording.
308 mymciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
309
310 return(MMSYSERR_NOERROR);
311}
312/******************************************************************************/
313/******************************************************************************/
314MMRESULT DartWaveIn::reset()
315{
316 MCI_GENERIC_PARMS Params;
317 LPWAVEHDR tmpwavehdr;
318
319 dprintf(("DartWaveIn::reset %s", (State == STATE_RECORDING) ? "recording" : "stopped"));
320 if(State != STATE_RECORDING)
321 return(MMSYSERR_HANDLEBUSY);
322
323 memset(&Params, 0, sizeof(Params));
324
325 // Stop recording
326 mymciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
327
328 dprintf(("Nr of threads blocked on mutex = %d\n", wmutex.getNrBlocked()));
329
330 wmutex.enter(VMUTEX_WAIT_FOREVER);
331 while(wavehdr)
332 {
333 wavehdr->dwFlags |= WHDR_DONE;
334 wavehdr->dwFlags &= ~WHDR_INQUEUE;
335 tmpwavehdr = wavehdr;
336 wavehdr = wavehdr->lpNext;
337 tmpwavehdr->lpNext = NULL;
338
339 wmutex.leave();
340
341 callback(WIM_DATA, (ULONG)tmpwavehdr, 0);
342
343 wmutex.enter(VMUTEX_WAIT_FOREVER);
344 }
345 wavehdr = NULL;
346 State = STATE_STOPPED;
347 fOverrun = FALSE;
348
349 wmutex.leave();
350 return(MMSYSERR_NOERROR);
351}
352/******************************************************************************/
353/******************************************************************************/
354MMRESULT DartWaveIn::addBuffer(LPWAVEHDR pwh, UINT cbwh)
355{
356 int i;
357
358 wmutex.enter(VMUTEX_WAIT_FOREVER);
359 pwh->lpNext = NULL;
360 pwh->dwBytesRecorded = 0;
361 if(wavehdr) {
362 WAVEHDR *chdr = wavehdr;
363 while(chdr->lpNext) {
364 chdr = chdr->lpNext;
365 }
366 chdr->lpNext = pwh;
367 }
368 else wavehdr = pwh;
369 wmutex.leave();
370
371 return(MMSYSERR_NOERROR);
372}
373/******************************************************************************/
374/******************************************************************************/
375ULONG DartWaveIn::getPosition()
376{
377 MCI_STATUS_PARMS mciStatus = {0};
378 ULONG rc, nrbytes;
379
380 mciStatus.ulItem = MCI_STATUS_POSITION;
381 rc = mymciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
382 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
383 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
384 return nrbytes;;
385 }
386 mciError(rc);
387 return 0xFFFFFFFF;
388}
389/******************************************************************************/
390/******************************************************************************/
391int DartWaveIn::getNumDevices()
392{
393 MCI_GENERIC_PARMS GenericParms;
394 MCI_AMP_OPEN_PARMS AmpOpenParms;
395 APIRET rc;
396
397 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
398 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
399
400 AmpOpenParms.usDeviceID = ( USHORT ) 0;
401 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
402
403 rc = mymciSendCommand(0, MCI_OPEN,
404 MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
405 (PVOID) &AmpOpenParms,
406 0);
407
408 if(rc) {
409 return 0; //no devices present
410 }
411
412 // Generic parameters
413 GenericParms.hwndCallback = 0; //hwndFrame
414
415 // Close the device
416 mymciSendCommand(AmpOpenParms.usDeviceID, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
417
418 return 1;
419}
420/******************************************************************************/
421/******************************************************************************/
422BOOL DartWaveIn::queryFormat(ULONG formatTag, ULONG nChannels,
423 ULONG nSamplesPerSec, ULONG wBitsPerSample)
424{
425 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
426 MCI_GENERIC_PARMS GenericParms;
427 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
428 int i, freqbits = 0;
429 ULONG rc, DeviceId;
430 BOOL winrc;
431
432 dprintf(("DartWaveIn::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
433
434 memset(&mciOpenParms, /* Object to fill with zeros. */
435 0, /* Value to place into the object. */
436 sizeof( mciOpenParms ) ); /* How many zero's to use. */
437
438 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
439
440 rc = mymciSendCommand( (USHORT) 0,
441 MCI_OPEN,
442 MCI_WAIT | MCI_OPEN_TYPE_ID,
443 (PVOID) &mciOpenParms,
444 0);
445 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
446 mciError(rc);
447 return(FALSE);
448 }
449 DeviceId = mciOpenParms.usDeviceID;
450
451 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
452
453 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
454 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
455 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
456 mciAudioCaps.ulChannels = nChannels;
457 mciAudioCaps.ulFormatMode = MCI_RECORD;
458 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
459
460 rc = mymciSendCommand(DeviceId, /* Device ID */
461 MCI_GETDEVCAPS,
462 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
463 (PVOID) &mciAudioCaps,
464 0);
465 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
466 mciError(rc);
467 winrc = FALSE;
468 }
469 else winrc = TRUE;
470
471 // Close the device
472 mymciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
473 return(winrc);
474}
475/******************************************************************************/
476/******************************************************************************/
477void DartWaveIn::mciError(ULONG ulError)
478{
479#ifdef DEBUG
480 char szError[256] = "";
481
482 mymciGetErrorString(ulError, szError, sizeof(szError));
483 dprintf(("WINMM: DartWaveIn: %s\n", szError));
484#endif
485}
486/******************************************************************************/
487//Simple implementation of recording. We fill up buffers queued by the application
488//until we run out of data or buffers. If we run out of buffers, the recorded data
489//is thrown away. We will continue when when the applications adds more buffers.
490/******************************************************************************/
491void DartWaveIn::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
492{
493 ULONG buflength, bufpos, bytestocopy;
494 WAVEHDR *whdr, *prevhdr = NULL;
495
496 dprintf2(("WINMM: DartWaveIn handler %x\n", pBuffer));
497 if(ulFlags == MIX_STREAM_ERROR) {
498 if(ulStatus == ERROR_DEVICE_OVERRUN) {
499 dprintf(("WINMM: ERROR: WaveIn handler ERROR_DEVICE_OVERRUN!\n"));
500 if(State == STATE_RECORDING) {
501 fOverrun = TRUE;
502 stop(); //out of buffers, so stop playback
503 }
504 return;
505 }
506 dprintf(("WINMM: WaveIn handler, Unknown error %X\n", ulStatus));
507 return;
508 }
509 wmutex.enter(VMUTEX_WAIT_FOREVER);
510
511 whdr = wavehdr;
512 if(whdr == NULL) {
513 wmutex.leave();
514 //last buffer recorded -> no new ones -> overrun
515 //Windows doesn't care -> just continue
516 dprintf(("WINMM: WARNING: WaveIn handler OVERRUN!\n"));
517 return;
518 }
519
520 buflength = pBuffer->ulBufferLength;
521 bufpos = 0;
522 while(buflength) {
523 if(wavehdr) {
524 bytestocopy = min(buflength, wavehdr->dwBufferLength - wavehdr->dwBytesRecorded);
525 if(bytestocopy) {
526 memcpy(wavehdr->lpData + wavehdr->dwBytesRecorded, (char *)pBuffer->pBuffer + bufpos, bytestocopy);
527 }
528 else DebugInt3(); //should never happen
529
530 bufpos += bytestocopy;
531 wavehdr->dwBytesRecorded += bytestocopy;
532 buflength -= bytestocopy;
533
534 if(wavehdr->dwBytesRecorded == wavehdr->dwBufferLength)
535 {
536 dprintf2(("WINMM: DartWaveIn handler buf %X done\n", whdr));
537 whdr->dwFlags |= WHDR_DONE;
538 whdr->dwFlags &= ~WHDR_INQUEUE;
539
540 wavehdr = whdr->lpNext;
541 whdr->lpNext = NULL;
542 wmutex.leave();
543
544 callback(WIM_DATA, (ULONG)whdr, whdr->dwBytesRecorded);
545
546 wmutex.enter(VMUTEX_WAIT_FOREVER);
547 }
548 }
549 else break;
550 }
551 wmutex.leave();
552
553 //Transfer buffer to DART
554 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
555 USHORT selTIB = GetFS(); // save current FS selector
556 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, pBuffer, 1);
557 SetFS(selTIB); // switch back to the saved FS selector
558}
559/******************************************************************************/
560/******************************************************************************/
561LONG APIENTRY WaveInHandler(ULONG ulStatus,
562 PMCI_MIX_BUFFER pBuffer,
563 ULONG ulFlags)
564{
565 DartWaveIn *dwave;
566
567 if(pBuffer && pBuffer->ulUserParm)
568 {
569 dwave = (DartWaveIn *)pBuffer->ulUserParm;
570 dwave->handler(ulStatus, pBuffer, ulFlags);
571 }
572 return(TRUE);
573}
574/******************************************************************************/
575/******************************************************************************/
576
Note: See TracBrowser for help on using the repository browser.