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

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

more logging

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