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

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

JvdH: Clear INQUEUE flag for returned buffers in WaveOutReset

File size: 24.5 KB
Line 
1/* $Id: dwaveout.cpp,v 1.26 2000-06-03 16:40:52 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 ulBufSize = pwh->dwBufferLength/2;
338 if(ulBufSize < 128) {
339 dprintf(("set buffer size to 128 bytes (org size = %d)", pwh->dwBufferLength));
340 ulBufSize = 128;
341 }
342#else
343 if(pwh->dwBufferLength >= 512 && pwh->dwBufferLength <= 1024)
344 ulBufSize = pwh->dwBufferLength;
345 else ulBufSize = 1024;
346#endif
347
348 MixSetupParms->ulBufferSize = ulBufSize;
349
350 BufferParms->ulNumBuffers = PREFILLBUF_DART;
351 BufferParms->ulBufferSize = MixSetupParms->ulBufferSize;
352 BufferParms->pBufList = MixBuffer;
353
354 for(i=0;i<PREFILLBUF_DART;i++) {
355 MixBuffer[i].ulUserParm = (ULONG)this;
356 }
357
358 rc = mciSendCommand(DeviceId,
359 MCI_BUFFER,
360 MCI_WAIT | MCI_ALLOCATE_MEMORY,
361 (PVOID)BufferParms,
362 0);
363
364 if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
365 mciError(rc);
366 mciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
367 (PVOID)&GenericParms, 0);
368 return(MMSYSERR_NOTSUPPORTED);
369 }
370
371 wmutex->enter(VMUTEX_WAIT_FOREVER);
372 fMixerSetup = TRUE;
373
374 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
375
376 for(i=0;i<PREFILLBUF_DART;i++) {
377 memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
378 }
379 dprintf(("Dart opened, bufsize = %d\n", MixBuffer[0].ulBufferLength));
380
381 wavehdr = pwh;
382 curhdr = pwh;
383 pwh->lpNext = NULL;
384
385 if(State != STATE_STOPPED) {//don't start playback if paused
386 wmutex->leave();
387 return(MMSYSERR_NOERROR);
388 }
389
390 while(TRUE) {
391 buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
392 (ULONG)wavehdr->dwBufferLength - curFillPos);
393 dprintf(("Copying %d data; curPlayPos = %d curFillPos = %d\n", buflength, curPlayPos, curFillPos));
394
395 memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
396 wavehdr->lpData + curFillPos,
397 buflength);
398
399 curPlayPos += buflength;
400 curFillPos += buflength;
401 if(curFillPos == wavehdr->dwBufferLength) {
402 dprintf(("Processed first win32 buffer\n"));
403 curFillPos = 0;
404 wavehdr->dwFlags |= WHDR_DONE;
405 curhdr = NULL;
406 }
407 if(curPlayPos == MixBuffer[curPlayBuf].ulBufferLength) {
408 if(++curPlayBuf == PREFILLBUF_DART) {
409 curPlayBuf = 0;
410 break;
411 }
412 curPlayPos = 0;
413 }
414 if(curFillPos == 0)
415 break;
416 }
417 dprintf(("MixSetupParms = %X\n", MixSetupParms));
418 State = STATE_PLAYING;
419 fUnderrun = FALSE;
420 wmutex->leave();
421
422 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
423 MixBuffer,
424 PREFILLBUF_DART);
425 dprintf(("Dart playing\n"));
426 }
427 else
428 {
429 wmutex->enter(VMUTEX_WAIT_FOREVER);
430 pwh->lpNext = NULL;
431 if(wavehdr) {
432 WAVEHDR *chdr = wavehdr;
433 while(chdr->lpNext) {
434 chdr = chdr->lpNext;
435 }
436 chdr->lpNext = pwh;
437 }
438 else wavehdr = pwh;
439 wmutex->leave();
440 if(State == STATE_STOPPED) {//continue playback
441 restart();
442 }
443 }
444
445 return(MMSYSERR_NOERROR);
446}
447/******************************************************************************/
448/******************************************************************************/
449MMRESULT DartWaveOut::pause()
450{
451 MCI_GENERIC_PARMS Params;
452
453 wmutex->enter(VMUTEX_WAIT_FOREVER);
454 if(State != STATE_PLAYING) {
455 State = STATE_PAUSED;
456 wmutex->leave();
457 return(MMSYSERR_NOERROR);
458 }
459
460 State = STATE_PAUSED;
461 wmutex->leave();
462
463 memset(&Params, 0, sizeof(Params));
464
465 // Stop the playback.
466 mciSendCommand(DeviceId, MCI_PAUSE, MCI_WAIT, (PVOID)&Params, 0);
467
468 return(MMSYSERR_NOERROR);
469}
470/******************************************************************************/
471/******************************************************************************/
472MMRESULT DartWaveOut::reset()
473{
474 MCI_GENERIC_PARMS Params;
475
476 dprintf(("DartWaveOut::reset %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
477 if(State != STATE_PLAYING)
478 return(MMSYSERR_HANDLEBUSY);
479
480 memset(&Params, 0, sizeof(Params));
481
482 // Stop the playback.
483 mciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
484
485 dprintf(("Nr of threads blocked on mutex = %d\n", wmutex->getNrBlocked()));
486
487 wmutex->enter(VMUTEX_WAIT_FOREVER);
488 while(wavehdr) {
489 wavehdr->dwFlags |= WHDR_DONE;
490 wavehdr->dwFlags &= ~WHDR_INQUEUE;
491 wmutex->leave();
492 if(mthdCallback) {
493 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)wavehdr, 0);
494 }
495 else
496 if(hwndCallback) {
497 dprintf(("Callback (msg) for buffer %x", wavehdr));
498 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)wavehdr);
499 }
500 wmutex->enter(VMUTEX_WAIT_FOREVER);
501 wavehdr = wavehdr->lpNext;
502 }
503 wavehdr = NULL;
504 State = STATE_STOPPED;
505 fUnderrun = FALSE;
506
507 wmutex->leave();
508 return(MMSYSERR_NOERROR);
509}
510/******************************************************************************/
511/******************************************************************************/
512MMRESULT DartWaveOut::restart()
513{
514 int i, curbuf;
515
516 dprintf(("DartWaveOut::restart"));
517 if(State == STATE_PLAYING) {
518 return(MMSYSERR_NOERROR);
519 }
520 //Only write buffers to dart if mixer has been initialized; if not, then
521 //the first buffer write will do this for us.
522 if(fMixerSetup == TRUE) {
523 wmutex->enter(VMUTEX_WAIT_FOREVER);
524 State = STATE_PLAYING;
525 fUnderrun = FALSE;
526 wmutex->leave();
527 curbuf = curPlayBuf;
528 for(i=0;i<PREFILLBUF_DART;i++) {
529 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curbuf], 1);
530 if(++curbuf == PREFILLBUF_DART) {
531 curbuf = 0;
532 }
533 }
534 }
535 return(MMSYSERR_NOERROR);
536}
537/******************************************************************************/
538/******************************************************************************/
539ULONG DartWaveOut::getPosition()
540{
541 MCI_STATUS_PARMS mciStatus = {0};
542 ULONG rc, nrbytes;
543
544 mciStatus.ulItem = MCI_STATUS_POSITION;
545 rc = mciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
546 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
547 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
548 return nrbytes;;
549 }
550 mciError(rc);
551 return 0xFFFFFFFF;
552}
553/******************************************************************************/
554/******************************************************************************/
555BOOL DartWaveOut::queryFormat(ULONG formatTag, ULONG nChannels,
556 ULONG nSamplesPerSec, ULONG wBitsPerSample)
557{
558 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
559 MCI_GENERIC_PARMS GenericParms;
560 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
561 int i, freqbits = 0;
562 ULONG rc, DeviceId;
563 BOOL winrc;
564
565 dprintf(("DartWaveOut::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
566
567 memset(&mciOpenParms, /* Object to fill with zeros. */
568 0, /* Value to place into the object. */
569 sizeof( mciOpenParms ) ); /* How many zero's to use. */
570
571 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
572
573 rc = mciSendCommand( (USHORT) 0,
574 MCI_OPEN,
575 MCI_WAIT | MCI_OPEN_TYPE_ID,
576 (PVOID) &mciOpenParms,
577 0);
578 if (rc != 0) {
579 return(FALSE);
580 }
581 DeviceId = mciOpenParms.usDeviceID;
582
583 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
584
585 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
586 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
587 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
588 mciAudioCaps.ulChannels = nChannels;
589 mciAudioCaps.ulFormatMode = MCI_PLAY;
590 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
591
592 rc = mciSendCommand(DeviceId, /* Device ID */
593 MCI_GETDEVCAPS,
594 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
595 (PVOID) &mciAudioCaps,
596 0);
597 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
598 mciError(rc);
599 winrc = FALSE;
600 }
601 else winrc = TRUE;
602
603 // Close the device
604 mciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
605 return(winrc);
606}
607/******************************************************************************/
608/******************************************************************************/
609void DartWaveOut::mciError(ULONG ulError)
610{
611#ifdef DEBUG
612 char szError[256] = "";
613
614 mciGetErrorString(ulError, szError, sizeof(szError));
615 dprintf(("WINMM: DartWaveOut: %s\n", szError));
616#endif
617}
618//******************************************************************************
619//******************************************************************************
620BOOL DartWaveOut::find(DartWaveOut *dwave)
621{
622 DartWaveOut *curwave = waveout;
623
624 while(curwave) {
625 if(dwave == curwave) {
626 return(TRUE);
627 }
628 curwave = curwave->next;
629 }
630
631#ifdef DEBUG
632// WriteLog("WINMM:DartWaveOut not found!\n");
633#endif
634 return(FALSE);
635}
636/******************************************************************************/
637/******************************************************************************/
638void DartWaveOut::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
639{
640 ULONG buflength;
641 WAVEHDR *whdr, *prevhdr = NULL;
642
643#ifdef DEBUG1
644 dprintf(("WINMM: handler %d\n", curPlayBuf));
645#endif
646 if(ulFlags == MIX_STREAM_ERROR) {
647 if(ulStatus == ERROR_DEVICE_UNDERRUN) {
648 dprintf(("WINMM: WaveOut handler UNDERRUN!\n"));
649 if(State == STATE_PLAYING) {
650 fUnderrun = TRUE;
651 pause(); //out of buffers, so pause playback
652 }
653 return;
654 }
655 dprintf(("WINMM: WaveOut handler, Unknown error %X\n", ulStatus));
656 return;
657 }
658 wmutex->enter(VMUTEX_WAIT_FOREVER);
659
660 whdr = wavehdr;
661 if(whdr == NULL) {
662 wmutex->leave();
663 //last buffer played -> no new ones -> underrun; pause playback
664 dprintf(("WINMM: WaveOut handler UNDERRUN!\n"));
665 if(State == STATE_PLAYING) {
666 fUnderrun = TRUE;
667 pause(); //out of buffers, so pause playback
668 }
669 return;
670 }
671 while(whdr) {
672 if(whdr->dwFlags & WHDR_DONE) {
673#ifdef DEBUG1
674 dprintf(("WINMM: handler buf %X done\n", whdr));
675#endif
676 whdr->dwFlags &= ~WHDR_INQUEUE;
677
678 if(prevhdr == NULL)
679 wavehdr = whdr->lpNext;
680 else prevhdr->lpNext = whdr->lpNext;
681
682 whdr->lpNext = NULL;
683 wmutex->leave();
684
685 if(mthdCallback) {
686 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)whdr, 0);
687 }
688 else
689 if(hwndCallback) {
690 dprintf(("Callback (msg) for buffer %x", whdr));
691 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)whdr);
692 }
693
694 wmutex->enter(VMUTEX_WAIT_FOREVER);
695 }
696 prevhdr = whdr;
697 whdr = whdr->lpNext;
698 }
699
700 if(curhdr == NULL)
701 curhdr = wavehdr;
702
703#ifdef DEBUG1
704 dprintf(("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos));
705#endif
706
707 while(curhdr) {
708 buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
709 (ULONG)curhdr->dwBufferLength - curFillPos);
710 memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
711 curhdr->lpData + curFillPos,
712 buflength);
713 curPlayPos += buflength;
714 curFillPos += buflength;
715#ifdef DEBUG1
716 dprintf(("WINMM: copied %d bytes, cufFillPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curhdr->dwBufferLength));
717#endif
718 if(curFillPos == curhdr->dwBufferLength) {
719#ifdef DEBUG1
720 dprintf(("Buffer %d done\n", curFillBuf));
721#endif
722 curFillPos = 0;
723 curhdr->dwFlags |= WHDR_DONE;
724 //search for next unprocessed buffer
725 while(curhdr && curhdr->dwFlags & WHDR_DONE)
726 curhdr = curhdr->lpNext;
727 }
728 if(curPlayPos == MixBuffer[curFillBuf].ulBufferLength) {
729 curPlayPos = 0;
730 if(++curFillBuf == PREFILLBUF_DART) {
731 curFillBuf = 0;
732 }
733 if(curFillBuf == curPlayBuf)
734 break; //no more room left
735 }
736 }
737
738 if(curPlayBuf == PREFILLBUF_DART-1)
739 curPlayBuf = 0;
740 else curPlayBuf++;
741
742 wmutex->leave();
743 //Transfer buffer to DART
744 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curPlayBuf], 1);
745}
746/******************************************************************************/
747/******************************************************************************/
748LONG APIENTRY WaveOutHandler(ULONG ulStatus,
749 PMCI_MIX_BUFFER pBuffer,
750 ULONG ulFlags)
751{
752 // PTIB ptib;
753 // PPIB ppib;
754 // DosGetInfoBlocks(&ptib, &ppib);
755 // dprintf(("WaveOutHandler: thread %d prio %X", ptib->tib_ptib2->tib2_ultid, ptib->tib_ptib2->tib2_ulpri));
756
757 DartWaveOut *dwave;
758
759 if(pBuffer && pBuffer->ulUserParm)
760 {
761 dwave = (DartWaveOut *)pBuffer->ulUserParm;
762 dwave->handler(ulStatus, pBuffer, ulFlags);
763 }
764 return(TRUE);
765}
766
767/******************************************************************************/
768/******************************************************************************/
769MMRESULT DartWaveOut::setVolume(ULONG ulVol)
770{
771 ULONG ulVolR = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume
772 ULONG ulVolL = ((ulVol& 0x0000ffff)*100)/0xFFFF; // Left Volume
773 MCI_SET_PARMS msp = {0};
774
775 volume = ulVol;
776
777// PD: My card (ESS 1868 PnP) driver can't change only
778// one channel Left or Right :-(
779//
780#ifdef GOOD_AUDIO_CARD_DRIVER
781
782 msp.ulAudio = MCI_SET_AUDIO_LEFT;
783 msp.ulLevel = ulVolL;
784
785 mciSendCommand(DeviceId, MCI_SET,
786 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
787 &msp, 0);
788
789 msp.ulAudio = MCI_SET_AUDIO_RIGHT;
790 msp.ulLevel = ulVolR;
791
792#else
793 msp.ulAudio = MCI_SET_AUDIO_ALL;
794 msp.ulLevel = max(ulVolR,ulVolL);
795#endif
796
797 mciSendCommand(DeviceId, MCI_SET,
798 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
799 &msp, 0);
800 return 0;
801}
802/******************************************************************************/
803/******************************************************************************/
804DartWaveOut *DartWaveOut::waveout = NULL;
805
Note: See TracBrowser for help on using the repository browser.