source: trunk/src/winmm/dwaveout.cpp@ 5334

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

many wave playback & recording fixes

File size: 28.3 KB
Line 
1/* $Id: dwaveout.cpp,v 1.32 2001-03-19 19:28:38 sandervl Exp $ */
2
3/*
4 * Wave playback class
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
7 *
8 *
9 * Project Odin Software License can be found in LICENSE.TXT
10 *
11 * Note:
12 * 2000/11/24 PH MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
13 *
14 */
15
16
17/****************************************************************************
18 * Includes *
19 ****************************************************************************/
20
21
22
23#define INCL_BASE
24#define INCL_OS2MM
25#include <os2wrap.h> //Odin32 OS/2 api wrappers
26#include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers
27#include <stdlib.h>
28#include <string.h>
29#define OS2_ONLY
30#include <win32api.h>
31#include <wprocess.h>
32
33#include "misc.h"
34#include "dwaveout.h"
35
36#define DBG_LOCALLOG DBG_dwaveout
37#include "dbglocal.h"
38
39#ifndef min
40#define min(a, b) ((a > b) ? b : a)
41#endif
42
43#ifndef max
44#define max(a, b) ((a > b) ? a : b)
45#endif
46
47LONG APIENTRY WaveOutHandler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags);
48
49//TODO: mulaw, alaw & adpcm
50/******************************************************************************/
51/******************************************************************************/
52DartWaveOut::DartWaveOut(LPWAVEFORMATEX pwfx)
53{
54 Init(pwfx);
55}
56/******************************************************************************/
57/******************************************************************************/
58DartWaveOut::DartWaveOut(LPWAVEFORMATEX pwfx, ULONG nCallback, ULONG dwInstance)
59{
60 Init(pwfx);
61
62 mthdCallback = (LPDRVCALLBACK)nCallback; // callback function
63 this->dwInstance = dwInstance;
64 fUnderrun = FALSE;
65
66 if(!ulError)
67 callback((ULONG)this, WOM_OPEN, dwInstance, 0, 0);
68}
69/******************************************************************************/
70/******************************************************************************/
71DartWaveOut::DartWaveOut(LPWAVEFORMATEX pwfx, HWND hwndCallback)
72{
73 Init(pwfx);
74
75 this->hwndCallback = hwndCallback;
76
77 if(!ulError)
78 PostMessageA(hwndCallback, WOM_OPEN, 0, 0);
79}
80/******************************************************************************/
81/******************************************************************************/
82void DartWaveOut::callback(HDRVR h, UINT uMessage, DWORD dwUser, DWORD dw1, DWORD dw2)
83{
84 USHORT selTIB = GetFS(); // save current FS selector
85 USHORT selCallback;
86
87 dprintf(("WINMM:DartWaveOut::callback(HDRVR h=%08xh, UINT uMessage=%08xh, DWORD dwUser=%08xh, DWORD dw1=%08xh, DWORD dw2=%08xh)\n",
88 h,
89 uMessage,
90 dwUser,
91 dw1,
92 dw2));
93
94 selCallback = GetProcessTIBSel();
95
96 //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
97 if(selCallback)
98 {
99 SetFS(selCallback); // switch to callback win32 tib selector (stored in waveOutOpen)
100
101 //@@@PH 1999/12/28 Shockwave Flashes seem to make assumptions on a
102 // specific stack layout. Do we have the correct calling convention here?
103 mthdCallback(h,uMessage,dwUser,dw1,dw2);
104 SetFS(selTIB); // switch back to the saved FS selector
105 }
106 else {
107 dprintf(("WARNING: no valid selector of main thread available (process exiting); skipping waveout notify"));
108 }
109 dprintf(("WINMM:DartWaveOut::callback returned"));
110}
111/******************************************************************************/
112/******************************************************************************/
113void DartWaveOut::Init(LPWAVEFORMATEX pwfx)
114{
115 MCI_GENERIC_PARMS GenericParms;
116 MCI_AMP_OPEN_PARMS AmpOpenParms;
117 APIRET rc;
118
119 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
120 readPos = writePos = 0;
121
122 fMixerSetup = FALSE;
123 next = NULL;
124 wavehdr = NULL;
125 curhdr = NULL;
126 mthdCallback = NULL;
127 hwndCallback = 0;
128 dwInstance = 0;
129 ulError = 0;
130 volume = defvolume;
131 State = STATE_STOPPED;
132
133 MixBuffer = (MCI_MIX_BUFFER *)malloc(PREFILLBUF_DART*sizeof(MCI_MIX_BUFFER));
134 MixSetupParms = (MCI_MIXSETUP_PARMS *)malloc(sizeof(MCI_MIXSETUP_PARMS));
135 BufferParms = (MCI_BUFFER_PARMS *)malloc(sizeof(MCI_BUFFER_PARMS));
136
137 BitsPerSample = pwfx->wBitsPerSample;
138 SampleRate = pwfx->nSamplesPerSec;
139 this->nChannels = pwfx->nChannels;
140 ulBufSize = DART_BUFSIZE;
141
142 dprintf(("waveOutOpen: samplerate %d, numChan %d bps %d (%d), format %x", SampleRate, nChannels, BitsPerSample, pwfx->nBlockAlign, pwfx->wFormatTag));
143 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
144 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
145
146 AmpOpenParms.usDeviceID = ( USHORT ) 0;
147 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
148
149 rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
150 (PVOID) &AmpOpenParms, 0);
151
152 DeviceId = AmpOpenParms.usDeviceID;
153 if(rc) {
154 dprintf(("MCI_OPEN failed\n"));
155 mciError(rc);
156 ulError = MMSYSERR_NODRIVER;
157 }
158 if(rc == 0) {
159 //Grab exclusive rights to device instance (NOT entire device)
160 GenericParms.hwndCallback = 0; //Not needed, so set to 0
161 rc = mciSendCommand(DeviceId, MCI_ACQUIREDEVICE, MCI_EXCLUSIVE_INSTANCE,
162 (PVOID)&GenericParms, 0);
163 if(rc) {
164 dprintf(("MCI_ACQUIREDEVICE failed\n"));
165 mciError(rc);
166 ulError = MMSYSERR_NOTENABLED;
167 }
168 }
169 State = STATE_STOPPED;
170
171 setVolume(volume);
172
173 wmutex = new VMutex();
174 if(wmutex == NULL) {
175 DebugInt3();
176 ulError = MMSYSERR_NOTSUPPORTED;
177 }
178 if(wmutex)
179 wmutex->enter(VMUTEX_WAIT_FOREVER);
180
181 if(waveout == NULL) {
182 waveout = this;
183 }
184 else {
185 DartWaveOut *dwave = waveout;
186
187 while(dwave->next) {
188 dwave = dwave->next;
189 }
190 dwave->next = this;
191 }
192
193 if(wmutex)
194 wmutex->leave();
195}
196/******************************************************************************/
197/******************************************************************************/
198DartWaveOut::~DartWaveOut()
199{
200 MCI_GENERIC_PARMS GenericParms;
201
202 if(!ulError) {
203 // Generic parameters
204 GenericParms.hwndCallback = 0; //hwndFrame
205
206 // Stop the playback.
207 mciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
208
209 mciSendCommand(DeviceId, MCI_BUFFER,
210 MCI_WAIT | MCI_DEALLOCATE_MEMORY,
211 (PVOID)&BufferParms, 0);
212
213 // Generic parameters
214 GenericParms.hwndCallback = 0; //hwndFrame
215
216 // Close the device
217 mciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
218 }
219
220 if(wmutex)
221 wmutex->enter(VMUTEX_WAIT_FOREVER);
222
223 State = STATE_STOPPED;
224
225 if(waveout == this) {
226 waveout = this->next;
227 }
228 else {
229 DartWaveOut *dwave = waveout;
230
231 while(dwave->next != this) {
232 dwave = dwave->next;
233 }
234 dwave->next = this->next;
235 }
236 if(wmutex)
237 wmutex->leave();
238
239 if(!ulError) {
240 if(mthdCallback) {
241 callback((ULONG)this, WOM_CLOSE, dwInstance, 0, 0);
242 }
243 else
244 if(hwndCallback)
245 PostMessageA(hwndCallback, WOM_CLOSE, 0, 0);
246 }
247
248 if(wmutex)
249 delete wmutex;
250
251 if(MixBuffer)
252 free(MixBuffer);
253 if(MixSetupParms)
254 free(MixSetupParms);
255 if(BufferParms)
256 free(BufferParms);
257}
258/******************************************************************************/
259/******************************************************************************/
260MMRESULT DartWaveOut::getError()
261{
262 return(ulError);
263}
264/******************************************************************************/
265/******************************************************************************/
266int DartWaveOut::getNumDevices()
267{
268 MCI_GENERIC_PARMS GenericParms;
269 MCI_AMP_OPEN_PARMS AmpOpenParms;
270 APIRET rc;
271
272 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
273 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
274
275 AmpOpenParms.usDeviceID = ( USHORT ) 0;
276 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
277
278 rc = mciSendCommand(0, MCI_OPEN,
279 MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
280 (PVOID) &AmpOpenParms,
281 0);
282
283 if(rc) {
284 return 0; //no devices present
285 }
286
287 // Generic parameters
288 GenericParms.hwndCallback = 0; //hwndFrame
289
290 // Close the device
291 mciSendCommand(AmpOpenParms.usDeviceID, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
292
293 return 1;
294}
295/******************************************************************************/
296/******************************************************************************/
297MMRESULT DartWaveOut::write(LPWAVEHDR pwh, UINT cbwh)
298{
299 MCI_GENERIC_PARMS GenericParms = {0};
300 APIRET rc;
301 int i, buflength;
302
303 if(fMixerSetup == FALSE)
304 {
305 dprintf(("device acquired\n"));
306 /* Set the MixSetupParms data structure to match the loaded file.
307 * This is a global that is used to setup the mixer.
308 */
309 memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
310
311 MixSetupParms->ulBitsPerSample = BitsPerSample;
312 MixSetupParms->ulSamplesPerSec = SampleRate;
313 MixSetupParms->ulFormatTag = MCI_WAVE_FORMAT_PCM;
314 MixSetupParms->ulChannels = nChannels;
315
316 dprintf(("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels));
317
318 /* Setup the mixer for playback of wave data
319 */
320 MixSetupParms->ulFormatMode = MCI_PLAY;
321 MixSetupParms->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
322 MixSetupParms->pmixEvent = WaveOutHandler;
323
324 rc = mciSendCommand(DeviceId,
325 MCI_MIXSETUP,
326 MCI_WAIT | MCI_MIXSETUP_INIT,
327 (PVOID)MixSetupParms,
328 0);
329
330 if ( rc != MCIERR_SUCCESS ) {
331 mciError(rc);
332 mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
333 (PVOID)&GenericParms, 0);
334 return(MMSYSERR_NOTSUPPORTED);
335 }
336
337 /*
338 * Set up the BufferParms data structure and allocate
339 * device buffers from the Amp-Mixer
340 */
341 dprintf(("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength));
342
343#if 1
344 int consumerate = getAvgBytesPerSecond();
345 int minbufsize = consumerate/32;
346
347 ulBufSize = pwh->dwBufferLength/2;
348 if(ulBufSize > minbufsize) {
349 dprintf(("set buffer size to %d bytes (org size = %d)", minbufsize, pwh->dwBufferLength));
350 ulBufSize = minbufsize;
351 }
352#else
353 if(pwh->dwBufferLength >= 512 && pwh->dwBufferLength <= 1024)
354 ulBufSize = pwh->dwBufferLength;
355 else ulBufSize = 1024;
356#endif
357
358 MixSetupParms->ulBufferSize = ulBufSize;
359
360 BufferParms->ulNumBuffers = PREFILLBUF_DART;
361 BufferParms->ulBufferSize = MixSetupParms->ulBufferSize;
362 BufferParms->pBufList = MixBuffer;
363
364 for(i=0;i<PREFILLBUF_DART;i++) {
365 MixBuffer[i].ulUserParm = (ULONG)this;
366 }
367
368 rc = mciSendCommand(DeviceId,
369 MCI_BUFFER,
370 MCI_WAIT | MCI_ALLOCATE_MEMORY,
371 (PVOID)BufferParms,
372 0);
373
374 if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
375 mciError(rc);
376 mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
377 (PVOID)&GenericParms, 0);
378 return(MMSYSERR_NOTSUPPORTED);
379 }
380
381 wmutex->enter(VMUTEX_WAIT_FOREVER);
382 fMixerSetup = TRUE;
383
384 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
385 readPos = writePos = 0;
386
387 for(i=0;i<PREFILLBUF_DART;i++) {
388 memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
389 }
390 dprintf(("Dart opened, bufsize = %d\n", MixBuffer[0].ulBufferLength));
391
392 wavehdr = pwh;
393 curhdr = pwh;
394 pwh->lpNext = NULL;
395 pwh->reserved = 0;
396
397 if(State != STATE_STOPPED) {//don't start playback if paused
398 wmutex->leave();
399 return(MMSYSERR_NOERROR);
400 }
401
402 writeBuffer(); //must be called before (re)starting playback
403
404 dprintf(("MixSetupParms = %X\n", MixSetupParms));
405 State = STATE_PLAYING;
406 fUnderrun = FALSE;
407 wmutex->leave();
408
409 //write buffers to DART; starts playback
410 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
411 USHORT selTIB = GetFS(); // save current FS selector
412
413 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
414 MixBuffer,
415 PREFILLBUF_DART);
416 SetFS(selTIB); // switch back to the saved FS selector
417
418 dprintf(("Dart playing\n"));
419 }
420 else
421 {
422 pwh->lpNext = NULL;
423 pwh->reserved = 0;
424 wmutex->enter(VMUTEX_WAIT_FOREVER);
425 if(wavehdr) {
426 WAVEHDR *chdr = wavehdr;
427 while(chdr->lpNext) {
428 chdr = chdr->lpNext;
429 }
430 chdr->lpNext = pwh;
431 }
432 else wavehdr = pwh;
433
434 writeBuffer(); //must be called before (re)starting playback
435
436 wmutex->leave();
437 if(State == STATE_STOPPED) {//continue playback
438 restart();
439 }
440 }
441 return(MMSYSERR_NOERROR);
442}
443/******************************************************************************/
444/******************************************************************************/
445MMRESULT DartWaveOut::pause()
446{
447 MCI_GENERIC_PARMS Params;
448
449 wmutex->enter(VMUTEX_WAIT_FOREVER);
450 if(State != STATE_PLAYING) {
451 State = STATE_PAUSED;
452 wmutex->leave();
453 return(MMSYSERR_NOERROR);
454 }
455
456 State = STATE_PAUSED;
457 wmutex->leave();
458
459 memset(&Params, 0, sizeof(Params));
460
461 // Stop the playback.
462 mciSendCommand(DeviceId, MCI_PAUSE, MCI_WAIT, (PVOID)&Params, 0);
463
464 return(MMSYSERR_NOERROR);
465}
466/******************************************************************************/
467/******************************************************************************/
468MMRESULT DartWaveOut::stop()
469{
470 MCI_GENERIC_PARMS Params;
471
472 dprintf(("DartWaveOut::stop %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
473 if(State != STATE_PLAYING)
474 return(MMSYSERR_HANDLEBUSY);
475
476 memset(&Params, 0, sizeof(Params));
477
478 // Stop the playback.
479 mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
480
481 State = STATE_STOPPED;
482 fUnderrun = FALSE;
483
484 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
485 readPos = writePos = 0;
486
487 return(MMSYSERR_NOERROR);
488}
489/******************************************************************************/
490/******************************************************************************/
491MMRESULT DartWaveOut::reset()
492{
493 MCI_GENERIC_PARMS Params;
494 LPWAVEHDR tmpwavehdr;
495
496 dprintf(("DartWaveOut::reset %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
497 if(State != STATE_PLAYING)
498 return(MMSYSERR_HANDLEBUSY);
499
500 memset(&Params, 0, sizeof(Params));
501
502 // Stop the playback.
503 mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
504
505 dprintf(("Nr of threads blocked on mutex = %d\n", wmutex->getNrBlocked()));
506
507 wmutex->enter(VMUTEX_WAIT_FOREVER);
508 while(wavehdr)
509 {
510 wavehdr->dwFlags |= WHDR_DONE;
511 wavehdr->dwFlags &= ~WHDR_INQUEUE;
512 tmpwavehdr = wavehdr;
513 wavehdr = wavehdr->lpNext;
514 tmpwavehdr->lpNext = NULL;
515 wmutex->leave();
516
517 if(mthdCallback) {
518 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)tmpwavehdr, 0);
519 }
520 else
521 if(hwndCallback) {
522 dprintf(("Callback (msg) for buffer %x", tmpwavehdr));
523 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)tmpwavehdr);
524 }
525 wmutex->enter(VMUTEX_WAIT_FOREVER);
526 }
527 wavehdr = NULL;
528 State = STATE_STOPPED;
529 fUnderrun = FALSE;
530
531 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
532 readPos = writePos = 0;
533
534 wmutex->leave();
535 return(MMSYSERR_NOERROR);
536}
537/******************************************************************************/
538/******************************************************************************/
539MMRESULT DartWaveOut::restart()
540{
541 int i, curbuf;
542
543 dprintf(("DartWaveOut::restart"));
544 if(State == STATE_PLAYING)
545 return(MMSYSERR_NOERROR);
546
547 //Only write buffers to dart if mixer has been initialized; if not, then
548 //the first buffer write will do this for us.
549 if(fMixerSetup == TRUE)
550 {
551 wmutex->enter(VMUTEX_WAIT_FOREVER);
552 State = STATE_PLAYING;
553 fUnderrun = FALSE;
554 wmutex->leave();
555 curbuf = curPlayBuf;
556
557 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
558 USHORT selTIB = GetFS(); // save current FS selector
559
560 for(i=0;i<PREFILLBUF_DART;i++)
561 {
562 dprintf(("restart: write buffer at %x size %d", MixBuffer[curbuf].pBuffer, MixBuffer[curbuf].ulBufferLength));
563 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curbuf], 1);
564 if(++curbuf == PREFILLBUF_DART)
565 curbuf = 0;
566 }
567 SetFS(selTIB); // switch back to the saved FS selector
568 }
569 return(MMSYSERR_NOERROR);
570}
571/******************************************************************************/
572/******************************************************************************/
573ULONG DartWaveOut::getPosition()
574{
575 MCI_STATUS_PARMS mciStatus = {0};
576 ULONG rc, nrbytes;
577
578 mciStatus.ulItem = MCI_STATUS_POSITION;
579 rc = mciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
580 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
581 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
582 return nrbytes;;
583 }
584 mciError(rc);
585 return 0xFFFFFFFF;
586}
587/******************************************************************************/
588/******************************************************************************/
589BOOL DartWaveOut::queryFormat(ULONG formatTag, ULONG nChannels,
590 ULONG nSamplesPerSec, ULONG wBitsPerSample)
591{
592 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
593 MCI_GENERIC_PARMS GenericParms;
594 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
595 int i, freqbits = 0;
596 ULONG rc, DeviceId;
597 BOOL winrc;
598
599 dprintf(("DartWaveOut::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
600
601 memset(&mciOpenParms, /* Object to fill with zeros. */
602 0, /* Value to place into the object. */
603 sizeof( mciOpenParms ) ); /* How many zero's to use. */
604
605 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
606
607 rc = mciSendCommand( (USHORT) 0,
608 MCI_OPEN,
609 MCI_WAIT | MCI_OPEN_TYPE_ID,
610 (PVOID) &mciOpenParms,
611 0);
612 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
613 mciError(rc);
614 return(FALSE);
615 }
616 DeviceId = mciOpenParms.usDeviceID;
617
618 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
619
620 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
621 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
622 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
623 mciAudioCaps.ulChannels = nChannels;
624 mciAudioCaps.ulFormatMode = MCI_PLAY;
625 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
626
627 rc = mciSendCommand(DeviceId, /* Device ID */
628 MCI_GETDEVCAPS,
629 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
630 (PVOID) &mciAudioCaps,
631 0);
632 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
633 mciError(rc);
634 winrc = FALSE;
635 }
636 else winrc = TRUE;
637
638 // Close the device
639 mciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
640 return(winrc);
641}
642/******************************************************************************/
643/******************************************************************************/
644void DartWaveOut::mciError(ULONG ulError)
645{
646#ifdef DEBUG
647 char szError[256] = "";
648
649 mciGetErrorString(ulError, szError, sizeof(szError));
650 dprintf(("WINMM: DartWaveOut: %s\n", szError));
651#endif
652}
653//******************************************************************************
654//******************************************************************************
655BOOL DartWaveOut::find(DartWaveOut *dwave)
656{
657 DartWaveOut *curwave = waveout;
658
659 while(curwave) {
660 if(dwave == curwave) {
661 return(TRUE);
662 }
663 curwave = curwave->next;
664 }
665
666 dprintf2(("WINMM:DartWaveOut not found!\n"));
667 return(FALSE);
668}
669/******************************************************************************/
670/******************************************************************************/
671void DartWaveOut::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
672{
673 ULONG buflength;
674 WAVEHDR *whdr, *prevhdr = NULL;
675
676 dprintf2(("WINMM: handler %d\n", curPlayBuf));
677 if(ulFlags == MIX_STREAM_ERROR) {
678 if(ulStatus == ERROR_DEVICE_UNDERRUN) {
679 dprintf(("WINMM: WaveOut handler UNDERRUN! state %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
680 if(State == STATE_PLAYING) {
681 fUnderrun = TRUE;
682 stop(); //out of buffers, so stop playback
683 }
684 return;
685 }
686 dprintf(("WINMM: WaveOut handler, Unknown error %X\n", ulStatus));
687 return;
688 }
689 if(State != STATE_PLAYING) {
690 return;
691 }
692
693 wmutex->enter(VMUTEX_WAIT_FOREVER);
694
695 readPos += MixBuffer[curPlayBuf].ulBufferLength;
696
697 if(curPlayBuf == PREFILLBUF_DART-1)
698 curPlayBuf = 0;
699 else curPlayBuf++;
700
701 whdr = wavehdr;
702 if(whdr == NULL) {
703 //last buffer played -> no new ones -> silence buffer & continue
704 dprintf(("WINMM: WaveOut handler LAST BUFFER PLAYED! state %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
705 if(State == STATE_PLAYING) {
706 fUnderrun = TRUE;
707//// memset(MixBuffer[curPlayBuf].pBuffer, 0, MixBuffer[curPlayBuf].ulBufferLength);
708 goto sendbuffer;
709 }
710 wmutex->leave();
711 return;
712 }
713 fUnderrun = FALSE;
714 while(whdr) {
715 if(whdr->reserved == WHDR_DONE)
716 {
717 if(readPos < writePos + whdr->dwBufferLength) {
718 dprintf2(("Buffer marked done, but not yet played completely (%d,%d)", readPos, writePos));
719 break; //not yet done
720 }
721
722 dprintf2(("WINMM: handler buf %X done (read %d, write %d)", whdr, readPos, writePos));
723 whdr->dwFlags &= ~WHDR_INQUEUE;
724 whdr->dwFlags |= WHDR_DONE;
725
726 if(prevhdr == NULL)
727 wavehdr = whdr->lpNext;
728 else prevhdr->lpNext = whdr->lpNext;
729
730 whdr->lpNext = NULL;
731
732 writePos += whdr->dwBufferLength;
733 wmutex->leave();
734
735 if(mthdCallback) {
736 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)whdr, 0);
737 }
738 else
739 if(hwndCallback) {
740 dprintf(("Callback (msg) for buffer %x", whdr));
741 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)whdr);
742 }
743
744 wmutex->enter(VMUTEX_WAIT_FOREVER);
745 }
746 else break;
747
748 prevhdr = whdr;
749 whdr = whdr->lpNext;
750 }
751
752 writeBuffer();
753
754sendbuffer:
755 wmutex->leave();
756
757 //Transfer buffer to DART
758 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
759 USHORT selTIB = GetFS(); // save current FS selector
760 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curPlayBuf], 1);
761 SetFS(selTIB); // switch back to the saved FS selector
762}
763/******************************************************************************/
764/******************************************************************************/
765void DartWaveOut::writeBuffer()
766{
767 ULONG buflength;
768
769 if(!fUnderrun && State == STATE_PLAYING && curFillBuf == curPlayBuf) {
770 dprintf2(("writeBuffer: no more room for more audio data"));
771 return; //no room left
772 }
773
774 if(curhdr == NULL)
775 curhdr = wavehdr;
776
777 while(curhdr && (curhdr->reserved == WHDR_DONE)) {
778 curhdr = curhdr->lpNext;
779 }
780
781 if(curhdr == NULL) {
782 return; //no unprocessed buffers left
783 }
784
785 dprintf2(("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos));
786
787 while(curhdr) {
788 buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
789 (ULONG)curhdr->dwBufferLength - curFillPos);
790 dprintf2(("WINMM: copied %d bytes, cufFillPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curhdr->dwBufferLength));
791
792 memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
793 curhdr->lpData + curFillPos, buflength);
794 curPlayPos += buflength;
795 curFillPos += buflength;
796
797
798 if(curFillPos == curhdr->dwBufferLength) {
799 dprintf2(("Buffer %d done (%x)", curFillBuf, curhdr));
800 curFillPos = 0;
801 curhdr->reserved = WHDR_DONE;
802 //search for next unprocessed buffer
803 while(curhdr && (curhdr->reserved == WHDR_DONE))
804 curhdr = curhdr->lpNext;
805 }
806 if(curPlayPos == MixBuffer[curFillBuf].ulBufferLength) {
807 curPlayPos = 0;
808 if(++curFillBuf == PREFILLBUF_DART) {
809 curFillBuf = 0;
810 }
811 if(curFillBuf == curPlayBuf)
812 break; //no more room left
813 }
814 }
815}
816/******************************************************************************/
817/******************************************************************************/
818LONG APIENTRY WaveOutHandler(ULONG ulStatus,
819 PMCI_MIX_BUFFER pBuffer,
820 ULONG ulFlags)
821{
822 PTIB2 ptib2;
823 DartWaveOut *dwave;
824
825 ptib2 = (PTIB2)_getTIBvalue(offsetof(TIB, tib_ptib2));
826 if(ptib2 && HIBYTE(ptib2->tib2_ulpri) != PRTYC_TIMECRITICAL) {
827 dprintf(("Setting priority of DART thread to PRTYC_TIMECRITICAL"));
828 DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
829 }
830 if(pBuffer && pBuffer->ulUserParm)
831 {
832 dwave = (DartWaveOut *)pBuffer->ulUserParm;
833 dwave->handler(ulStatus, pBuffer, ulFlags);
834 }
835 return(TRUE);
836}
837
838/******************************************************************************/
839/******************************************************************************/
840MMRESULT DartWaveOut::setVolume(ULONG ulVol)
841{
842 ULONG ulVolR = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume
843 ULONG ulVolL = ((ulVol& 0x0000ffff)*100)/0xFFFF; // Left Volume
844 MCI_SET_PARMS msp = {0};
845
846 dprintf(("DartWaveOut::setVolume %x %x", ulVolL, ulVolR));
847 volume = ulVol;
848
849// PD: My card (ESS 1868 PnP) driver can't change only
850// one channel Left or Right :-(
851//
852#ifdef GOOD_AUDIO_CARD_DRIVER
853
854 msp.ulAudio = MCI_SET_AUDIO_LEFT;
855 msp.ulLevel = ulVolL;
856
857 mciSendCommand(DeviceId, MCI_SET,
858 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
859 &msp, 0);
860
861 msp.ulAudio = MCI_SET_AUDIO_RIGHT;
862 msp.ulLevel = ulVolR;
863
864#else
865 msp.ulAudio = MCI_SET_AUDIO_ALL;
866 msp.ulLevel = max(ulVolR,ulVolL);
867#endif
868
869 mciSendCommand(DeviceId, MCI_SET,
870 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
871 &msp, 0);
872 return 0;
873}
874/******************************************************************************/
875//Called if waveOutSetVolume is called by the application with waveout handle NULL
876//Sets the default volume of each waveout stream (until it's volume is changed
877//with an appropriate waveOutSetVolume call)
878/******************************************************************************/
879void DartWaveOut::setDefaultVolume(ULONG volume)
880{
881 defvolume = volume;
882}
883/******************************************************************************/
884/******************************************************************************/
885DartWaveOut *DartWaveOut::waveout = NULL;
886ULONG DartWaveOut::defvolume = 0xFFFFFFFF;
887
Note: See TracBrowser for help on using the repository browser.