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

Last change on this file since 3831 was 3780, checked in by sandervl, 25 years ago

MN: resume on underrun & min. buffer size fixes

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