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

Last change on this file since 8508 was 8508, checked in by sandervl, 23 years ago

fixed wave recording; waveIn/OutOpen fixes for WAVE_FORMAT_QUERY flag

File size: 19.1 KB
Line 
1/* $Id: waveindart.cpp,v 1.4 2002-05-28 14:09:25 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();
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();
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 wmutex.enter();
329 while(wavehdr)
330 {
331 wavehdr->dwFlags |= WHDR_DONE;
332 wavehdr->dwFlags &= ~WHDR_INQUEUE;
333 tmpwavehdr = wavehdr;
334 wavehdr = wavehdr->lpNext;
335 tmpwavehdr->lpNext = NULL;
336
337 wmutex.leave();
338
339 callback(WIM_DATA, (ULONG)tmpwavehdr, 0);
340
341 wmutex.enter();
342 }
343 wavehdr = NULL;
344 State = STATE_STOPPED;
345 fOverrun = FALSE;
346
347 wmutex.leave();
348 return(MMSYSERR_NOERROR);
349}
350/******************************************************************************/
351/******************************************************************************/
352MMRESULT DartWaveIn::addBuffer(LPWAVEHDR pwh, UINT cbwh)
353{
354 int i;
355
356 wmutex.enter();
357 pwh->lpNext = NULL;
358 pwh->dwBytesRecorded = 0;
359 if(wavehdr) {
360 WAVEHDR *chdr = wavehdr;
361 while(chdr->lpNext) {
362 chdr = chdr->lpNext;
363 }
364 chdr->lpNext = pwh;
365 }
366 else wavehdr = pwh;
367 wmutex.leave();
368
369 return(MMSYSERR_NOERROR);
370}
371/******************************************************************************/
372/******************************************************************************/
373ULONG DartWaveIn::getPosition()
374{
375 MCI_STATUS_PARMS mciStatus = {0};
376 ULONG rc, nrbytes;
377
378 mciStatus.ulItem = MCI_STATUS_POSITION;
379 rc = mymciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
380 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
381 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
382 return nrbytes;;
383 }
384 mciError(rc);
385 return 0xFFFFFFFF;
386}
387/******************************************************************************/
388/******************************************************************************/
389int DartWaveIn::getNumDevices()
390{
391 MCI_GENERIC_PARMS GenericParms;
392 MCI_AMP_OPEN_PARMS AmpOpenParms;
393 APIRET rc;
394
395 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
396 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
397
398 AmpOpenParms.usDeviceID = ( USHORT ) 0;
399 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
400
401 rc = mymciSendCommand(0, MCI_OPEN,
402 MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
403 (PVOID) &AmpOpenParms,
404 0);
405
406 if(rc) {
407 return 0; //no devices present
408 }
409
410 // Generic parameters
411 GenericParms.hwndCallback = 0; //hwndFrame
412
413 // Close the device
414 mymciSendCommand(AmpOpenParms.usDeviceID, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
415
416 return 1;
417}
418/******************************************************************************/
419/******************************************************************************/
420BOOL DartWaveIn::queryFormat(ULONG formatTag, ULONG nChannels,
421 ULONG nSamplesPerSec, ULONG wBitsPerSample)
422{
423 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
424 MCI_GENERIC_PARMS GenericParms;
425 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
426 int i, freqbits = 0;
427 ULONG rc, DeviceId;
428 BOOL winrc;
429
430 dprintf(("DartWaveIn::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
431
432 memset(&mciOpenParms, /* Object to fill with zeros. */
433 0, /* Value to place into the object. */
434 sizeof( mciOpenParms ) ); /* How many zero's to use. */
435
436 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
437
438 rc = mymciSendCommand( (USHORT) 0,
439 MCI_OPEN,
440 MCI_WAIT | MCI_OPEN_TYPE_ID,
441 (PVOID) &mciOpenParms,
442 0);
443 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
444 mciError(rc);
445 return(FALSE);
446 }
447 DeviceId = mciOpenParms.usDeviceID;
448
449 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
450
451 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
452 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
453 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
454 mciAudioCaps.ulChannels = nChannels;
455 mciAudioCaps.ulFormatMode = MCI_RECORD;
456 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
457
458 rc = mymciSendCommand(DeviceId, /* Device ID */
459 MCI_GETDEVCAPS,
460 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
461 (PVOID) &mciAudioCaps,
462 0);
463 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
464 mciError(rc);
465 winrc = FALSE;
466 }
467 else winrc = TRUE;
468
469 // Close the device
470 mymciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
471 return(winrc);
472}
473/******************************************************************************/
474/******************************************************************************/
475void DartWaveIn::mciError(ULONG ulError)
476{
477#ifdef DEBUG
478 char szError[256] = "";
479
480 mymciGetErrorString(ulError, szError, sizeof(szError));
481 dprintf(("WINMM: DartWaveIn: %s\n", szError));
482#endif
483}
484/******************************************************************************/
485//Simple implementation of recording. We fill up buffers queued by the application
486//until we run out of data or buffers. If we run out of buffers, the recorded data
487//is thrown away. We will continue when when the applications adds more buffers.
488/******************************************************************************/
489void DartWaveIn::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
490{
491 ULONG buflength, bufpos, bytestocopy;
492
493 dprintf2(("WINMM: DartWaveIn handler %x\n", pBuffer));
494 if(ulFlags == MIX_STREAM_ERROR) {
495 if(ulStatus == ERROR_DEVICE_OVERRUN) {
496 dprintf(("WINMM: ERROR: WaveIn handler ERROR_DEVICE_OVERRUN!\n"));
497 if(State == STATE_RECORDING) {
498 fOverrun = TRUE;
499 stop(); //out of buffers, so stop playback
500 }
501 return;
502 }
503 dprintf(("WINMM: WaveIn handler, Unknown error %X\n", ulStatus));
504 return;
505 }
506 wmutex.enter();
507
508 if(wavehdr == NULL) {
509 wmutex.leave();
510 //last buffer recorded -> no new ones -> overrun
511 //Windows doesn't care -> just continue
512 dprintf(("WINMM: WARNING: WaveIn handler OVERRUN!\n"));
513 return;
514 }
515
516 buflength = pBuffer->ulBufferLength;
517 bufpos = 0;
518 while(buflength) {
519 if(wavehdr) {
520 dprintf2(("WINMM: DartWaveIn handler: bytes recorded %d, buffer length %d, room %d", buflength, wavehdr->dwBufferLength, wavehdr->dwBytesRecorded));
521 bytestocopy = min(buflength, wavehdr->dwBufferLength - wavehdr->dwBytesRecorded);
522 if(bytestocopy) {
523 memcpy(wavehdr->lpData + wavehdr->dwBytesRecorded, (char *)pBuffer->pBuffer + bufpos, bytestocopy);
524 }
525 else DebugInt3(); //should never happen
526
527 bufpos += bytestocopy;
528 wavehdr->dwBytesRecorded += bytestocopy;
529 buflength -= bytestocopy;
530
531 if(wavehdr->dwBytesRecorded == wavehdr->dwBufferLength)
532 {
533 WAVEHDR *whdr = wavehdr;
534
535 dprintf2(("WINMM: DartWaveIn handler buf %X done\n", whdr));
536 whdr->dwFlags |= WHDR_DONE;
537 whdr->dwFlags &= ~WHDR_INQUEUE;
538
539 wavehdr = whdr->lpNext;
540 whdr->lpNext = NULL;
541 wmutex.leave();
542
543 callback(WIM_DATA, (ULONG)whdr, whdr->dwBytesRecorded);
544
545 wmutex.enter();
546 }
547 }
548 else break;
549 }
550 wmutex.leave();
551
552 //Transfer buffer to DART
553 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
554 USHORT selTIB = GetFS(); // save current FS selector
555 MixSetupParms->pmixRead(MixSetupParms->ulMixHandle, pBuffer, 1);
556 SetFS(selTIB); // switch back to the saved FS selector
557}
558/******************************************************************************/
559/******************************************************************************/
560LONG APIENTRY WaveInHandler(ULONG ulStatus,
561 PMCI_MIX_BUFFER pBuffer,
562 ULONG ulFlags)
563{
564 DartWaveIn *dwave;
565
566 if(pBuffer && pBuffer->ulUserParm)
567 {
568 dwave = (DartWaveIn *)pBuffer->ulUserParm;
569 dwave->handler(ulStatus, pBuffer, ulFlags);
570 }
571 return(TRUE);
572}
573/******************************************************************************/
574/******************************************************************************/
575
Note: See TracBrowser for help on using the repository browser.