1 | /* $Id: dwavein.cpp,v 1.2 2001-03-19 19:28:38 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 "dwavein.h"
|
---|
33 |
|
---|
34 | #define DBG_LOCALLOG DBG_dwavein
|
---|
35 | #include "dbglocal.h"
|
---|
36 |
|
---|
37 | #ifndef min
|
---|
38 | #define min(a, b) ((a > b) ? b : a)
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #ifndef max
|
---|
42 | #define max(a, b) ((a > b) ? a : b)
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | LONG APIENTRY WaveInHandler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags);
|
---|
46 |
|
---|
47 | //TODO: mulaw, alaw & adpcm
|
---|
48 | /******************************************************************************/
|
---|
49 | /******************************************************************************/
|
---|
50 | DartWaveIn::DartWaveIn(LPWAVEFORMATEX pwfx)
|
---|
51 | {
|
---|
52 | Init(pwfx);
|
---|
53 | }
|
---|
54 | /******************************************************************************/
|
---|
55 | /******************************************************************************/
|
---|
56 | DartWaveIn::DartWaveIn(LPWAVEFORMATEX pwfx, ULONG nCallback, ULONG dwInstance)
|
---|
57 | {
|
---|
58 | Init(pwfx);
|
---|
59 |
|
---|
60 | mthdCallback = (LPDRVCALLBACK)nCallback; // callback function
|
---|
61 | this->dwInstance = dwInstance;
|
---|
62 | fOverrun = FALSE;
|
---|
63 |
|
---|
64 | if(!ulError)
|
---|
65 | callback((ULONG)this, WIM_OPEN, dwInstance, 0, 0);
|
---|
66 | }
|
---|
67 | /******************************************************************************/
|
---|
68 | /******************************************************************************/
|
---|
69 | DartWaveIn::DartWaveIn(LPWAVEFORMATEX pwfx, HWND hwndCallback)
|
---|
70 | {
|
---|
71 | Init(pwfx);
|
---|
72 |
|
---|
73 | this->hwndCallback = hwndCallback;
|
---|
74 |
|
---|
75 | if(!ulError)
|
---|
76 | PostMessageA(hwndCallback, WIM_OPEN, 0, 0);
|
---|
77 | }
|
---|
78 | /******************************************************************************/
|
---|
79 | /******************************************************************************/
|
---|
80 | void DartWaveIn::callback(HDRVR h, UINT uMessage, DWORD dwUser, DWORD dw1, DWORD dw2)
|
---|
81 | {
|
---|
82 | USHORT selTIB = GetFS(); // save current FS selector
|
---|
83 | USHORT selCallback;
|
---|
84 |
|
---|
85 | dprintf(("WINMM:DartWaveIn::callback(HDRVR h=%08xh, UINT uMessage=%08xh, DWORD dwUser=%08xh, DWORD dw1=%08xh, DWORD dw2=%08xh)\n",
|
---|
86 | h, uMessage, dwUser, dw1, dw2));
|
---|
87 |
|
---|
88 | selCallback = GetProcessTIBSel();
|
---|
89 |
|
---|
90 | //TODO: may not be very safe. perhaps we should allocate a new TIB for the DART thread or let another thread do the actual callback
|
---|
91 | if(selCallback)
|
---|
92 | {
|
---|
93 | SetFS(selCallback); // switch to callback win32 tib selector (stored in waveOutOpen)
|
---|
94 |
|
---|
95 | //@@@PH 1999/12/28 Shockwave Flashes seem to make assumptions on a
|
---|
96 | // specific stack layout. Do we have the correct calling convention here?
|
---|
97 | mthdCallback(h,uMessage,dwUser,dw1,dw2);
|
---|
98 | SetFS(selTIB); // switch back to the saved FS selector
|
---|
99 | }
|
---|
100 | else {
|
---|
101 | dprintf(("WARNING: no valid selector of main thread available (process exiting); skipping waveout notify"));
|
---|
102 | }
|
---|
103 | dprintf(("WINMM:DartWaveOut::callback returned"));
|
---|
104 | }
|
---|
105 | /******************************************************************************/
|
---|
106 | /******************************************************************************/
|
---|
107 | void DartWaveIn::Init(LPWAVEFORMATEX pwfx)
|
---|
108 | {
|
---|
109 | MCI_GENERIC_PARMS GenericParms;
|
---|
110 | MCI_AMP_OPEN_PARMS AmpOpenParms;
|
---|
111 | APIRET rc;
|
---|
112 |
|
---|
113 | fMixerSetup = FALSE;
|
---|
114 | next = NULL;
|
---|
115 | wavehdr = NULL;
|
---|
116 | mthdCallback = NULL;
|
---|
117 | hwndCallback = 0;
|
---|
118 | dwInstance = 0;
|
---|
119 | ulError = 0;
|
---|
120 | State = STATE_STOPPED;
|
---|
121 |
|
---|
122 | MixBuffer = (MCI_MIX_BUFFER *)malloc(PREFILLBUF_DART_REC*sizeof(MCI_MIX_BUFFER));
|
---|
123 | MixSetupParms = (MCI_MIXSETUP_PARMS *)malloc(sizeof(MCI_MIXSETUP_PARMS));
|
---|
124 | BufferParms = (MCI_BUFFER_PARMS *)malloc(sizeof(MCI_BUFFER_PARMS));
|
---|
125 |
|
---|
126 | BitsPerSample = pwfx->wBitsPerSample;
|
---|
127 | SampleRate = pwfx->nSamplesPerSec;
|
---|
128 | this->nChannels = pwfx->nChannels;
|
---|
129 | ulBufSize = DART_BUFSIZE_REC;
|
---|
130 |
|
---|
131 | dprintf(("waveInOpen: samplerate %d, numChan %d bps %d (%d), format %x", SampleRate, nChannels, BitsPerSample, pwfx->nBlockAlign, pwfx->wFormatTag));
|
---|
132 | // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
|
---|
133 | memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
|
---|
134 |
|
---|
135 | AmpOpenParms.usDeviceID = ( USHORT ) 0;
|
---|
136 | AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
|
---|
137 |
|
---|
138 | rc = mciSendCommand(0, MCI_OPEN,
|
---|
139 | MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
|
---|
140 | (PVOID) &AmpOpenParms,
|
---|
141 | 0);
|
---|
142 | DeviceId = AmpOpenParms.usDeviceID;
|
---|
143 | if(rc) {
|
---|
144 | dprintf(("MCI_OPEN failed\n"));
|
---|
145 | mciError(rc);
|
---|
146 | ulError = MMSYSERR_NODRIVER;
|
---|
147 | }
|
---|
148 | if(rc == 0) {
|
---|
149 | //Grab exclusive rights to device instance (NOT entire device)
|
---|
150 | GenericParms.hwndCallback = 0; //Not needed, so set to 0
|
---|
151 | rc = mciSendCommand(DeviceId, MCI_ACQUIREDEVICE, MCI_EXCLUSIVE_INSTANCE,
|
---|
152 | (PVOID)&GenericParms, 0);
|
---|
153 | if(rc) {
|
---|
154 | dprintf(("MCI_ACQUIREDEVICE failed\n"));
|
---|
155 | mciError(rc);
|
---|
156 | ulError = MMSYSERR_NOTENABLED;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | State = STATE_STOPPED;
|
---|
160 |
|
---|
161 | wmutex = new VMutex();
|
---|
162 | if(wmutex == NULL) {
|
---|
163 | DebugInt3();
|
---|
164 | ulError = MMSYSERR_NOTSUPPORTED;
|
---|
165 | }
|
---|
166 | if(wmutex)
|
---|
167 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
168 |
|
---|
169 | if(wavein == NULL) {
|
---|
170 | wavein = this;
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | DartWaveIn *dwave = wavein;
|
---|
174 |
|
---|
175 | while(dwave->next) {
|
---|
176 | dwave = dwave->next;
|
---|
177 | }
|
---|
178 | dwave->next = this;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if(wmutex)
|
---|
182 | wmutex->leave();
|
---|
183 | }
|
---|
184 | /******************************************************************************/
|
---|
185 | /******************************************************************************/
|
---|
186 | DartWaveIn::~DartWaveIn()
|
---|
187 | {
|
---|
188 | MCI_GENERIC_PARMS GenericParms;
|
---|
189 |
|
---|
190 | if(!ulError)
|
---|
191 | {
|
---|
192 | // Generic parameters
|
---|
193 | GenericParms.hwndCallback = 0; //hwndFrame
|
---|
194 |
|
---|
195 | // Stop recording.
|
---|
196 | mciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
|
---|
197 |
|
---|
198 | mciSendCommand(DeviceId,
|
---|
199 | MCI_BUFFER,
|
---|
200 | MCI_WAIT | MCI_DEALLOCATE_MEMORY,
|
---|
201 | (PVOID)&BufferParms,
|
---|
202 | 0);
|
---|
203 |
|
---|
204 | // Generic parameters
|
---|
205 | GenericParms.hwndCallback = 0; //hwndFrame
|
---|
206 |
|
---|
207 | // Close the device
|
---|
208 | mciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
|
---|
209 | }
|
---|
210 |
|
---|
211 | if(wmutex)
|
---|
212 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
213 |
|
---|
214 | State = STATE_STOPPED;
|
---|
215 |
|
---|
216 | if(wavein == this) {
|
---|
217 | wavein = this->next;
|
---|
218 | }
|
---|
219 | else {
|
---|
220 | DartWaveIn *dwave = wavein;
|
---|
221 |
|
---|
222 | while(dwave->next != this) {
|
---|
223 | dwave = dwave->next;
|
---|
224 | }
|
---|
225 | dwave->next = this->next;
|
---|
226 | }
|
---|
227 | if(wmutex)
|
---|
228 | wmutex->leave();
|
---|
229 |
|
---|
230 | if(!ulError) {
|
---|
231 | if(mthdCallback) {
|
---|
232 | callback((ULONG)this, WIM_CLOSE, dwInstance, 0, 0);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | if(hwndCallback)
|
---|
236 | PostMessageA(hwndCallback, WIM_CLOSE, 0, 0);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if(wmutex)
|
---|
240 | delete wmutex;
|
---|
241 |
|
---|
242 | if(MixBuffer)
|
---|
243 | free(MixBuffer);
|
---|
244 | if(MixSetupParms)
|
---|
245 | free(MixSetupParms);
|
---|
246 | if(BufferParms)
|
---|
247 | free(BufferParms);
|
---|
248 | }
|
---|
249 | /******************************************************************************/
|
---|
250 | /******************************************************************************/
|
---|
251 | MMRESULT DartWaveIn::getError()
|
---|
252 | {
|
---|
253 | return(ulError);
|
---|
254 | }
|
---|
255 | /******************************************************************************/
|
---|
256 | /******************************************************************************/
|
---|
257 | int DartWaveIn::getNumDevices()
|
---|
258 | {
|
---|
259 | MCI_GENERIC_PARMS GenericParms;
|
---|
260 | MCI_AMP_OPEN_PARMS AmpOpenParms;
|
---|
261 | APIRET rc;
|
---|
262 |
|
---|
263 | // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
|
---|
264 | memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
|
---|
265 |
|
---|
266 | //TODO: correct for recording??
|
---|
267 | AmpOpenParms.usDeviceID = ( USHORT ) 0;
|
---|
268 | AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
|
---|
269 |
|
---|
270 | rc = mciSendCommand(0, MCI_OPEN,
|
---|
271 | MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
|
---|
272 | (PVOID) &AmpOpenParms,
|
---|
273 | 0);
|
---|
274 |
|
---|
275 | if(rc) {
|
---|
276 | return 0; //no devices present
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Generic parameters
|
---|
280 | GenericParms.hwndCallback = 0; //hwndFrame
|
---|
281 |
|
---|
282 | // Close the device
|
---|
283 | mciSendCommand(AmpOpenParms.usDeviceID, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
|
---|
284 | return 1;
|
---|
285 | }
|
---|
286 | /******************************************************************************/
|
---|
287 | /******************************************************************************/
|
---|
288 | MMRESULT DartWaveIn::start()
|
---|
289 | {
|
---|
290 | MCI_GENERIC_PARMS GenericParms = {0};
|
---|
291 | MCI_AMP_OPEN_PARMS AmpOpenParms;
|
---|
292 | MCI_CONNECTOR_PARMS ConnectorParms;
|
---|
293 | MCI_AMP_SET_PARMS AmpSetParms ;
|
---|
294 | APIRET rc;
|
---|
295 | int i, curbuf, buflength;
|
---|
296 |
|
---|
297 | dprintf(("DartWaveIn::start"));
|
---|
298 | if(State == STATE_RECORDING)
|
---|
299 | return(MMSYSERR_NOERROR);
|
---|
300 |
|
---|
301 | if(fMixerSetup == FALSE)
|
---|
302 | {
|
---|
303 | dprintf(("device acquired\n"));
|
---|
304 | /* Set the MixSetupParms data structure to match the loaded file.
|
---|
305 | * This is a global that is used to setup the mixer.
|
---|
306 | */
|
---|
307 | memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
|
---|
308 |
|
---|
309 | MixSetupParms->ulBitsPerSample = BitsPerSample;
|
---|
310 | MixSetupParms->ulSamplesPerSec = SampleRate;
|
---|
311 | MixSetupParms->ulFormatTag = MCI_WAVE_FORMAT_PCM;
|
---|
312 | MixSetupParms->ulChannels = nChannels;
|
---|
313 |
|
---|
314 | dprintf(("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels));
|
---|
315 |
|
---|
316 | /* Setup the mixer for recording of wave data
|
---|
317 | */
|
---|
318 | MixSetupParms->ulFormatMode = MCI_RECORD;
|
---|
319 | MixSetupParms->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
|
---|
320 | MixSetupParms->pmixEvent = WaveInHandler;
|
---|
321 |
|
---|
322 | rc = mciSendCommand(DeviceId,
|
---|
323 | MCI_MIXSETUP,
|
---|
324 | MCI_WAIT | MCI_MIXSETUP_INIT,
|
---|
325 | (PVOID)MixSetupParms,
|
---|
326 | 0);
|
---|
327 |
|
---|
328 | if ( rc != MCIERR_SUCCESS ) {
|
---|
329 | mciError(rc);
|
---|
330 | mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
|
---|
331 | (PVOID)&GenericParms, 0);
|
---|
332 | return(MMSYSERR_NOTSUPPORTED);
|
---|
333 | }
|
---|
334 |
|
---|
335 | /*
|
---|
336 | * Set up the BufferParms data structure and allocate
|
---|
337 | * device buffers from the Amp-Mixer
|
---|
338 | */
|
---|
339 |
|
---|
340 | int consumerate = getAvgBytesPerSecond();
|
---|
341 | //SvL: Recording usually isn't as sensitive to timing as playback
|
---|
342 | // Dividing by 8 instead of 32 here. Change if necessary.
|
---|
343 | int minbufsize = consumerate/8;
|
---|
344 |
|
---|
345 | LPWAVEHDR pwh = wavehdr;
|
---|
346 | if(pwh) {
|
---|
347 | dprintf(("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength));
|
---|
348 |
|
---|
349 | ulBufSize = pwh->dwBufferLength/2;
|
---|
350 | if(ulBufSize < minbufsize) {
|
---|
351 | dprintf(("set buffer size to %d bytes (org size = %d)", minbufsize, pwh->dwBufferLength));
|
---|
352 | ulBufSize = minbufsize;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | else ulBufSize = minbufsize;
|
---|
356 |
|
---|
357 | MixSetupParms->ulBufferSize = ulBufSize;
|
---|
358 |
|
---|
359 | BufferParms->ulNumBuffers = PREFILLBUF_DART_REC;
|
---|
360 | BufferParms->ulBufferSize = MixSetupParms->ulBufferSize;
|
---|
361 | BufferParms->pBufList = MixBuffer;
|
---|
362 |
|
---|
363 | for(i=0;i<PREFILLBUF_DART_REC;i++) {
|
---|
364 | MixBuffer[i].ulUserParm = (ULONG)this;
|
---|
365 | }
|
---|
366 |
|
---|
367 | rc = mciSendCommand(DeviceId,
|
---|
368 | MCI_BUFFER,
|
---|
369 | MCI_WAIT | MCI_ALLOCATE_MEMORY,
|
---|
370 | (PVOID)BufferParms,
|
---|
371 | 0);
|
---|
372 |
|
---|
373 | if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
|
---|
374 | mciError(rc);
|
---|
375 | mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
|
---|
376 | (PVOID)&GenericParms, 0);
|
---|
377 | return(MMSYSERR_NOTSUPPORTED);
|
---|
378 | }
|
---|
379 |
|
---|
380 | //TODO: don't do this here. Select line or mic depending on aux settings
|
---|
381 | //(until we really support the mixer extensions anyway)
|
---|
382 | /* Set the connector to 'line in' */
|
---|
383 | memset( &ConnectorParms, '\0', sizeof( MCI_CONNECTOR_PARMS ) );
|
---|
384 | ConnectorParms.ulConnectorType = MCI_LINE_IN_CONNECTOR;
|
---|
385 | rc = mciSendCommand( DeviceId,
|
---|
386 | MCI_CONNECTOR,
|
---|
387 | MCI_WAIT |
|
---|
388 | MCI_ENABLE_CONNECTOR |
|
---|
389 | MCI_CONNECTOR_TYPE,
|
---|
390 | ( PVOID ) &ConnectorParms,
|
---|
391 | 0 );
|
---|
392 |
|
---|
393 | /* Allow the user to hear what is being recorded
|
---|
394 | * by turning the monitor on
|
---|
395 | */
|
---|
396 | memset( &AmpSetParms, '\0', sizeof( MCI_AMP_SET_PARMS ) );
|
---|
397 | AmpSetParms.ulItem = MCI_AMP_SET_MONITOR;
|
---|
398 | rc = mciSendCommand(DeviceId,
|
---|
399 | MCI_SET,
|
---|
400 | MCI_WAIT | MCI_SET_ON | MCI_SET_ITEM,
|
---|
401 | ( PVOID ) &AmpSetParms,
|
---|
402 | 0 );
|
---|
403 |
|
---|
404 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
405 | fMixerSetup = TRUE;
|
---|
406 | }
|
---|
407 |
|
---|
408 | for(i=0;i<PREFILLBUF_DART_REC;i++) {
|
---|
409 | memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
|
---|
410 | }
|
---|
411 | dprintf(("Dart opened, bufsize = %d\n", MixBuffer[0].ulBufferLength));
|
---|
412 |
|
---|
413 | dprintf(("MixSetupParms = %X\n", MixSetupParms));
|
---|
414 | State = STATE_RECORDING;
|
---|
415 | fOverrun = FALSE;
|
---|
416 | wmutex->leave();
|
---|
417 |
|
---|
418 | dprintf(("Dart recording"));
|
---|
419 |
|
---|
420 | // Start recording.
|
---|
421 | USHORT selTIB = GetFS(); // save current FS selector
|
---|
422 | MixSetupParms->pmixRead( MixSetupParms->ulMixHandle,
|
---|
423 | &MixBuffer[0],
|
---|
424 | PREFILLBUF_DART_REC);
|
---|
425 | SetFS(selTIB); // switch back to the saved FS selector
|
---|
426 |
|
---|
427 | return(MMSYSERR_NOERROR);
|
---|
428 | }
|
---|
429 | /******************************************************************************/
|
---|
430 | /******************************************************************************/
|
---|
431 | MMRESULT DartWaveIn::stop()
|
---|
432 | {
|
---|
433 | MCI_GENERIC_PARMS Params;
|
---|
434 |
|
---|
435 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
436 | if(State != STATE_RECORDING) {
|
---|
437 | State = STATE_STOPPED;
|
---|
438 | wmutex->leave();
|
---|
439 | return(MMSYSERR_NOERROR);
|
---|
440 | }
|
---|
441 |
|
---|
442 | State = STATE_STOPPED;
|
---|
443 | wmutex->leave();
|
---|
444 |
|
---|
445 | memset(&Params, 0, sizeof(Params));
|
---|
446 |
|
---|
447 | // Stop recording.
|
---|
448 | mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
|
---|
449 |
|
---|
450 | return(MMSYSERR_NOERROR);
|
---|
451 | }
|
---|
452 | /******************************************************************************/
|
---|
453 | /******************************************************************************/
|
---|
454 | MMRESULT DartWaveIn::reset()
|
---|
455 | {
|
---|
456 | MCI_GENERIC_PARMS Params;
|
---|
457 | LPWAVEHDR tmpwavehdr;
|
---|
458 |
|
---|
459 | dprintf(("DartWaveIn::reset %s", (State == STATE_RECORDING) ? "recording" : "stopped"));
|
---|
460 | if(State != STATE_RECORDING)
|
---|
461 | return(MMSYSERR_HANDLEBUSY);
|
---|
462 |
|
---|
463 | memset(&Params, 0, sizeof(Params));
|
---|
464 |
|
---|
465 | // Stop recording
|
---|
466 | mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
|
---|
467 |
|
---|
468 | dprintf(("Nr of threads blocked on mutex = %d\n", wmutex->getNrBlocked()));
|
---|
469 |
|
---|
470 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
471 | while(wavehdr)
|
---|
472 | {
|
---|
473 | wavehdr->dwFlags |= WHDR_DONE;
|
---|
474 | wavehdr->dwFlags &= ~WHDR_INQUEUE;
|
---|
475 | tmpwavehdr = wavehdr;
|
---|
476 | wavehdr = wavehdr->lpNext;
|
---|
477 | tmpwavehdr->lpNext = NULL;
|
---|
478 |
|
---|
479 | wmutex->leave();
|
---|
480 | if(mthdCallback) {
|
---|
481 | callback((ULONG)this, WIM_DATA, dwInstance, (ULONG)tmpwavehdr, 0);
|
---|
482 | }
|
---|
483 | else
|
---|
484 | if(hwndCallback) {
|
---|
485 | dprintf(("Callback (msg) for buffer %x", wavehdr));
|
---|
486 | PostMessageA(hwndCallback, WIM_DATA, (WPARAM)this, (ULONG)tmpwavehdr);
|
---|
487 | }
|
---|
488 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
489 | }
|
---|
490 | wavehdr = NULL;
|
---|
491 | State = STATE_STOPPED;
|
---|
492 | fOverrun = FALSE;
|
---|
493 |
|
---|
494 | wmutex->leave();
|
---|
495 | return(MMSYSERR_NOERROR);
|
---|
496 | }
|
---|
497 | /******************************************************************************/
|
---|
498 | /******************************************************************************/
|
---|
499 | MMRESULT DartWaveIn::addBuffer(LPWAVEHDR pwh, UINT cbwh)
|
---|
500 | {
|
---|
501 | int i;
|
---|
502 |
|
---|
503 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
504 | pwh->lpNext = NULL;
|
---|
505 | pwh->dwBytesRecorded = 0;
|
---|
506 | if(wavehdr) {
|
---|
507 | WAVEHDR *chdr = wavehdr;
|
---|
508 | while(chdr->lpNext) {
|
---|
509 | chdr = chdr->lpNext;
|
---|
510 | }
|
---|
511 | chdr->lpNext = pwh;
|
---|
512 | }
|
---|
513 | else wavehdr = pwh;
|
---|
514 | wmutex->leave();
|
---|
515 |
|
---|
516 | return(MMSYSERR_NOERROR);
|
---|
517 | }
|
---|
518 | /******************************************************************************/
|
---|
519 | /******************************************************************************/
|
---|
520 | ULONG DartWaveIn::getPosition()
|
---|
521 | {
|
---|
522 | MCI_STATUS_PARMS mciStatus = {0};
|
---|
523 | ULONG rc, nrbytes;
|
---|
524 |
|
---|
525 | mciStatus.ulItem = MCI_STATUS_POSITION;
|
---|
526 | rc = mciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
|
---|
527 | if((rc & 0xFFFF) == MCIERR_SUCCESS) {
|
---|
528 | nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
|
---|
529 | return nrbytes;;
|
---|
530 | }
|
---|
531 | mciError(rc);
|
---|
532 | return 0xFFFFFFFF;
|
---|
533 | }
|
---|
534 | /******************************************************************************/
|
---|
535 | /******************************************************************************/
|
---|
536 | BOOL DartWaveIn::queryFormat(ULONG formatTag, ULONG nChannels,
|
---|
537 | ULONG nSamplesPerSec, ULONG wBitsPerSample)
|
---|
538 | {
|
---|
539 | MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
|
---|
540 | MCI_GENERIC_PARMS GenericParms;
|
---|
541 | MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
|
---|
542 | int i, freqbits = 0;
|
---|
543 | ULONG rc, DeviceId;
|
---|
544 | BOOL winrc;
|
---|
545 |
|
---|
546 | dprintf(("DartWaveIn::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
|
---|
547 |
|
---|
548 | memset(&mciOpenParms, /* Object to fill with zeros. */
|
---|
549 | 0, /* Value to place into the object. */
|
---|
550 | sizeof( mciOpenParms ) ); /* How many zero's to use. */
|
---|
551 |
|
---|
552 | mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
|
---|
553 |
|
---|
554 | rc = mciSendCommand( (USHORT) 0,
|
---|
555 | MCI_OPEN,
|
---|
556 | MCI_WAIT | MCI_OPEN_TYPE_ID,
|
---|
557 | (PVOID) &mciOpenParms,
|
---|
558 | 0);
|
---|
559 | if((rc & 0xFFFF) != MCIERR_SUCCESS) {
|
---|
560 | mciError(rc);
|
---|
561 | return(FALSE);
|
---|
562 | }
|
---|
563 | DeviceId = mciOpenParms.usDeviceID;
|
---|
564 |
|
---|
565 | memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
|
---|
566 |
|
---|
567 | mciAudioCaps.ulBitsPerSample = wBitsPerSample;
|
---|
568 | mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
|
---|
569 | mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
|
---|
570 | mciAudioCaps.ulChannels = nChannels;
|
---|
571 | mciAudioCaps.ulFormatMode = MCI_RECORD;
|
---|
572 | mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
|
---|
573 |
|
---|
574 | rc = mciSendCommand(DeviceId, /* Device ID */
|
---|
575 | MCI_GETDEVCAPS,
|
---|
576 | MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
|
---|
577 | (PVOID) &mciAudioCaps,
|
---|
578 | 0);
|
---|
579 | if((rc & 0xFFFF) != MCIERR_SUCCESS) {
|
---|
580 | mciError(rc);
|
---|
581 | winrc = FALSE;
|
---|
582 | }
|
---|
583 | else winrc = TRUE;
|
---|
584 |
|
---|
585 | // Close the device
|
---|
586 | mciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
|
---|
587 | return(winrc);
|
---|
588 | }
|
---|
589 | /******************************************************************************/
|
---|
590 | /******************************************************************************/
|
---|
591 | void DartWaveIn::mciError(ULONG ulError)
|
---|
592 | {
|
---|
593 | #ifdef DEBUG
|
---|
594 | char szError[256] = "";
|
---|
595 |
|
---|
596 | mciGetErrorString(ulError, szError, sizeof(szError));
|
---|
597 | dprintf(("WINMM: DartWaveIn: %s\n", szError));
|
---|
598 | #endif
|
---|
599 | }
|
---|
600 | //******************************************************************************
|
---|
601 | //******************************************************************************
|
---|
602 | BOOL DartWaveIn::find(DartWaveIn *dwave)
|
---|
603 | {
|
---|
604 | DartWaveIn *curwave = wavein;
|
---|
605 |
|
---|
606 | while(curwave) {
|
---|
607 | if(dwave == curwave) {
|
---|
608 | return(TRUE);
|
---|
609 | }
|
---|
610 | curwave = curwave->next;
|
---|
611 | }
|
---|
612 |
|
---|
613 | dprintf2(("WINMM:DartWaveIn not found!"));
|
---|
614 | return(FALSE);
|
---|
615 | }
|
---|
616 | /******************************************************************************/
|
---|
617 | //Simple implementation of recording. We fill up buffers queued by the application
|
---|
618 | //until we run out of data or buffers. If we run out of buffers, the recorded data
|
---|
619 | //is thrown away. We will continue when when the applications adds more buffers.
|
---|
620 | /******************************************************************************/
|
---|
621 | void DartWaveIn::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
|
---|
622 | {
|
---|
623 | ULONG buflength, bufpos, bytestocopy;
|
---|
624 | WAVEHDR *whdr, *prevhdr = NULL;
|
---|
625 |
|
---|
626 | dprintf2(("WINMM: DartWaveIn handler %x\n", pBuffer));
|
---|
627 | if(ulFlags == MIX_STREAM_ERROR) {
|
---|
628 | if(ulStatus == ERROR_DEVICE_OVERRUN) {
|
---|
629 | dprintf(("WINMM: ERROR: WaveIn handler ERROR_DEVICE_OVERRUN!\n"));
|
---|
630 | if(State == STATE_RECORDING) {
|
---|
631 | fOverrun = TRUE;
|
---|
632 | stop(); //out of buffers, so stop playback
|
---|
633 | }
|
---|
634 | return;
|
---|
635 | }
|
---|
636 | dprintf(("WINMM: WaveIn handler, Unknown error %X\n", ulStatus));
|
---|
637 | return;
|
---|
638 | }
|
---|
639 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
640 |
|
---|
641 | whdr = wavehdr;
|
---|
642 | if(whdr == NULL) {
|
---|
643 | wmutex->leave();
|
---|
644 | //last buffer recorded -> no new ones -> overrun
|
---|
645 | //Windows doesn't care -> just continue
|
---|
646 | dprintf(("WINMM: WARNING: WaveIn handler OVERRUN!\n"));
|
---|
647 | return;
|
---|
648 | }
|
---|
649 |
|
---|
650 | buflength = pBuffer->ulBufferLength;
|
---|
651 | bufpos = 0;
|
---|
652 | while(buflength) {
|
---|
653 | if(wavehdr) {
|
---|
654 | bytestocopy = min(buflength, wavehdr->dwBufferLength - wavehdr->dwBytesRecorded);
|
---|
655 | if(bytestocopy) {
|
---|
656 | memcpy(wavehdr->lpData + wavehdr->dwBytesRecorded, (char *)pBuffer->pBuffer + bufpos, bytestocopy);
|
---|
657 | }
|
---|
658 | else DebugInt3(); //should never happen
|
---|
659 |
|
---|
660 | bufpos += bytestocopy;
|
---|
661 | wavehdr->dwBytesRecorded += bytestocopy;
|
---|
662 | buflength -= bytestocopy;
|
---|
663 |
|
---|
664 | if(wavehdr->dwBytesRecorded == wavehdr->dwBufferLength)
|
---|
665 | {
|
---|
666 | dprintf2(("WINMM: DartWaveIn handler buf %X done\n", whdr));
|
---|
667 | whdr->dwFlags |= WHDR_DONE;
|
---|
668 | whdr->dwFlags &= ~WHDR_INQUEUE;
|
---|
669 |
|
---|
670 | wavehdr = whdr->lpNext;
|
---|
671 | whdr->lpNext = NULL;
|
---|
672 | wmutex->leave();
|
---|
673 |
|
---|
674 | if(mthdCallback) {
|
---|
675 | callback((ULONG)this, WIM_DATA, dwInstance, (ULONG)whdr, whdr->dwBytesRecorded);
|
---|
676 | }
|
---|
677 | else
|
---|
678 | if(hwndCallback) {
|
---|
679 | dprintf(("Callback (msg) for buffer %x", whdr));
|
---|
680 | PostMessageA(hwndCallback, WIM_DATA, (WPARAM)this, (ULONG)whdr);
|
---|
681 | }
|
---|
682 | wmutex->enter(VMUTEX_WAIT_FOREVER);
|
---|
683 | }
|
---|
684 | }
|
---|
685 | else break;
|
---|
686 | }
|
---|
687 | wmutex->leave();
|
---|
688 |
|
---|
689 | //Transfer buffer to DART
|
---|
690 | // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
|
---|
691 | USHORT selTIB = GetFS(); // save current FS selector
|
---|
692 | MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, pBuffer, 1);
|
---|
693 | SetFS(selTIB); // switch back to the saved FS selector
|
---|
694 | }
|
---|
695 | /******************************************************************************/
|
---|
696 | /******************************************************************************/
|
---|
697 | LONG APIENTRY WaveInHandler(ULONG ulStatus,
|
---|
698 | PMCI_MIX_BUFFER pBuffer,
|
---|
699 | ULONG ulFlags)
|
---|
700 | {
|
---|
701 | DartWaveIn *dwave;
|
---|
702 |
|
---|
703 | if(pBuffer && pBuffer->ulUserParm)
|
---|
704 | {
|
---|
705 | dwave = (DartWaveIn *)pBuffer->ulUserParm;
|
---|
706 | dwave->handler(ulStatus, pBuffer, ulFlags);
|
---|
707 | }
|
---|
708 | return(TRUE);
|
---|
709 | }
|
---|
710 | /******************************************************************************/
|
---|
711 | /******************************************************************************/
|
---|
712 | DartWaveIn *DartWaveIn::wavein = NULL;
|
---|
713 |
|
---|