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

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

getposition fix

File size: 24.2 KB
Line 
1/* $Id: dwaveout.cpp,v 1.24 2000-04-09 11:29:07 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 wmutex->leave();
491 if(mthdCallback) {
492 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)wavehdr, 0);
493 }
494 else
495 if(hwndCallback) {
496 dprintf(("Callback (msg) for buffer %x", wavehdr));
497 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)wavehdr);
498 }
499 wmutex->enter(VMUTEX_WAIT_FOREVER);
500 wavehdr = wavehdr->lpNext;
501 }
502 wavehdr = NULL;
503 State = STATE_STOPPED;
504 fUnderrun = FALSE;
505
506 wmutex->leave();
507 return(MMSYSERR_NOERROR);
508}
509/******************************************************************************/
510/******************************************************************************/
511MMRESULT DartWaveOut::restart()
512{
513 int i, curbuf;
514
515 dprintf(("DartWaveOut::restart"));
516 if(State == STATE_PLAYING) {
517 return(MMSYSERR_NOERROR);
518 }
519 wmutex->enter(VMUTEX_WAIT_FOREVER);
520 State = STATE_PLAYING;
521 fUnderrun = FALSE;
522 wmutex->leave();
523 curbuf = curPlayBuf;
524 for(i=0;i<PREFILLBUF_DART;i++) {
525 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curbuf], 1);
526 if(++curbuf == PREFILLBUF_DART) {
527 curbuf = 0;
528 }
529 }
530 return(MMSYSERR_NOERROR);
531}
532/******************************************************************************/
533/******************************************************************************/
534ULONG DartWaveOut::getPosition()
535{
536 MCI_STATUS_PARMS mciStatus = {0};
537 ULONG rc, nrbytes;
538
539 mciStatus.ulItem = MCI_STATUS_POSITION;
540 rc = mciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
541 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
542 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
543 return nrbytes;;
544 }
545 mciError(rc);
546 return 0xFFFFFFFF;
547}
548/******************************************************************************/
549/******************************************************************************/
550BOOL DartWaveOut::queryFormat(ULONG formatTag, ULONG nChannels,
551 ULONG nSamplesPerSec, ULONG wBitsPerSample)
552{
553 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
554 MCI_GENERIC_PARMS GenericParms;
555 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
556 int i, freqbits = 0;
557 ULONG rc, DeviceId;
558 BOOL winrc;
559
560 dprintf(("DartWaveOut::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
561
562 memset(&mciOpenParms, /* Object to fill with zeros. */
563 0, /* Value to place into the object. */
564 sizeof( mciOpenParms ) ); /* How many zero's to use. */
565
566 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
567
568 rc = mciSendCommand( (USHORT) 0,
569 MCI_OPEN,
570 MCI_WAIT | MCI_OPEN_TYPE_ID,
571 (PVOID) &mciOpenParms,
572 0);
573 if (rc != 0) {
574 return(FALSE);
575 }
576 DeviceId = mciOpenParms.usDeviceID;
577
578 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
579
580 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
581 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
582 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
583 mciAudioCaps.ulChannels = nChannels;
584 mciAudioCaps.ulFormatMode = MCI_PLAY;
585 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
586
587 rc = mciSendCommand(DeviceId, /* Device ID */
588 MCI_GETDEVCAPS,
589 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
590 (PVOID) &mciAudioCaps,
591 0);
592 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
593 mciError(rc);
594 winrc = FALSE;
595 }
596 else winrc = TRUE;
597
598 // Close the device
599 mciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
600 return(winrc);
601}
602/******************************************************************************/
603/******************************************************************************/
604void DartWaveOut::mciError(ULONG ulError)
605{
606#ifdef DEBUG
607 char szError[256] = "";
608
609 mciGetErrorString(ulError, szError, sizeof(szError));
610 dprintf(("WINMM: DartWaveOut: %s\n", szError));
611#endif
612}
613//******************************************************************************
614//******************************************************************************
615BOOL DartWaveOut::find(DartWaveOut *dwave)
616{
617 DartWaveOut *curwave = waveout;
618
619 while(curwave) {
620 if(dwave == curwave) {
621 return(TRUE);
622 }
623 curwave = curwave->next;
624 }
625
626#ifdef DEBUG
627// WriteLog("WINMM:DartWaveOut not found!\n");
628#endif
629 return(FALSE);
630}
631/******************************************************************************/
632/******************************************************************************/
633void DartWaveOut::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
634{
635 ULONG buflength;
636 WAVEHDR *whdr, *prevhdr = NULL;
637
638#ifdef DEBUG1
639 dprintf(("WINMM: handler %d\n", curPlayBuf));
640#endif
641 if(ulFlags == MIX_STREAM_ERROR) {
642 if(ulStatus == ERROR_DEVICE_UNDERRUN) {
643 dprintf(("WINMM: WaveOut handler UNDERRUN!\n"));
644 if(State == STATE_PLAYING) {
645 fUnderrun = TRUE;
646 pause(); //out of buffers, so pause playback
647 }
648 return;
649 }
650 dprintf(("WINMM: WaveOut handler, Unknown error %X\n", ulStatus));
651 return;
652 }
653 wmutex->enter(VMUTEX_WAIT_FOREVER);
654
655 whdr = wavehdr;
656 if(whdr == NULL) {
657 wmutex->leave();
658 //last buffer played -> no new ones -> underrun; pause playback
659 dprintf(("WINMM: WaveOut handler UNDERRUN!\n"));
660 if(State == STATE_PLAYING) {
661 fUnderrun = TRUE;
662 pause(); //out of buffers, so pause playback
663 }
664 return;
665 }
666 while(whdr) {
667 if(whdr->dwFlags & WHDR_DONE) {
668#ifdef DEBUG1
669 dprintf(("WINMM: handler buf %X done\n", whdr));
670#endif
671 whdr->dwFlags &= ~WHDR_INQUEUE;
672
673 if(prevhdr == NULL)
674 wavehdr = whdr->lpNext;
675 else prevhdr->lpNext = whdr->lpNext;
676
677 whdr->lpNext = NULL;
678 wmutex->leave();
679
680 if(mthdCallback) {
681 callback((ULONG)this, WOM_DONE, dwInstance, (ULONG)whdr, 0);
682 }
683 else
684 if(hwndCallback) {
685 dprintf(("Callback (msg) for buffer %x", whdr));
686 PostMessageA(hwndCallback, WOM_DONE, (WPARAM)this, (ULONG)whdr);
687 }
688
689 wmutex->enter(VMUTEX_WAIT_FOREVER);
690 }
691 prevhdr = whdr;
692 whdr = whdr->lpNext;
693 }
694
695 if(curhdr == NULL)
696 curhdr = wavehdr;
697
698#ifdef DEBUG1
699 dprintf(("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos));
700#endif
701
702 while(curhdr) {
703 buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
704 (ULONG)curhdr->dwBufferLength - curFillPos);
705 memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
706 curhdr->lpData + curFillPos,
707 buflength);
708 curPlayPos += buflength;
709 curFillPos += buflength;
710#ifdef DEBUG1
711 dprintf(("WINMM: copied %d bytes, cufFillPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curhdr->dwBufferLength));
712#endif
713 if(curFillPos == curhdr->dwBufferLength) {
714#ifdef DEBUG1
715 dprintf(("Buffer %d done\n", curFillBuf));
716#endif
717 curFillPos = 0;
718 curhdr->dwFlags |= WHDR_DONE;
719 //search for next unprocessed buffer
720 while(curhdr && curhdr->dwFlags & WHDR_DONE)
721 curhdr = curhdr->lpNext;
722 }
723 if(curPlayPos == MixBuffer[curFillBuf].ulBufferLength) {
724 curPlayPos = 0;
725 if(++curFillBuf == PREFILLBUF_DART) {
726 curFillBuf = 0;
727 }
728 if(curFillBuf == curPlayBuf)
729 break; //no more room left
730 }
731 }
732
733 if(curPlayBuf == PREFILLBUF_DART-1)
734 curPlayBuf = 0;
735 else curPlayBuf++;
736
737 wmutex->leave();
738 //Transfer buffer to DART
739 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curPlayBuf], 1);
740}
741/******************************************************************************/
742/******************************************************************************/
743LONG APIENTRY WaveOutHandler(ULONG ulStatus,
744 PMCI_MIX_BUFFER pBuffer,
745 ULONG ulFlags)
746{
747 // PTIB ptib;
748 // PPIB ppib;
749 // DosGetInfoBlocks(&ptib, &ppib);
750 // dprintf(("WaveOutHandler: thread %d prio %X", ptib->tib_ptib2->tib2_ultid, ptib->tib_ptib2->tib2_ulpri));
751
752 DartWaveOut *dwave;
753
754 if(pBuffer && pBuffer->ulUserParm)
755 {
756 dwave = (DartWaveOut *)pBuffer->ulUserParm;
757 dwave->handler(ulStatus, pBuffer, ulFlags);
758 }
759 return(TRUE);
760}
761
762/******************************************************************************/
763/******************************************************************************/
764MMRESULT DartWaveOut::setVolume(ULONG ulVol)
765{
766 ULONG ulVolR = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume
767 ULONG ulVolL = ((ulVol& 0x0000ffff)*100)/0xFFFF; // Left Volume
768 MCI_SET_PARMS msp = {0};
769
770 volume = ulVol;
771
772// PD: My card (ESS 1868 PnP) driver can't change only
773// one channel Left or Right :-(
774//
775#ifdef GOOD_AUDIO_CARD_DRIVER
776
777 msp.ulAudio = MCI_SET_AUDIO_LEFT;
778 msp.ulLevel = ulVolL;
779
780 mciSendCommand(DeviceId, MCI_SET,
781 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
782 &msp, 0);
783
784 msp.ulAudio = MCI_SET_AUDIO_RIGHT;
785 msp.ulLevel = ulVolR;
786
787#else
788 msp.ulAudio = MCI_SET_AUDIO_ALL;
789 msp.ulLevel = max(ulVolR,ulVolL);
790#endif
791
792 mciSendCommand(DeviceId, MCI_SET,
793 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
794 &msp, 0);
795 return 0;
796}
797/******************************************************************************/
798/******************************************************************************/
799DartWaveOut *DartWaveOut::waveout = NULL;
800
Note: See TracBrowser for help on using the repository browser.