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

Last change on this file since 5120 was 4687, checked in by phaller, 25 years ago

Fix FS: in pmixWrite

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