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

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

fix for getPosition (overflow)

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