source: trunk/src/winmm/waveoutdart.cpp@ 8706

Last change on this file since 8706 was 8574, checked in by sandervl, 23 years ago

Fixed DART waveout resume

File size: 23.6 KB
Line 
1/* $Id: waveoutdart.cpp,v 1.10 2002-06-05 11:44:11 sandervl Exp $ */
2
3/*
4 * Wave playback class (DART)
5 *
6 * Copyright 1998-2001 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#define INCL_DOSPROCESS
26#include <os2wrap.h> //Odin32 OS/2 api wrappers
27#include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers
28#include <stdlib.h>
29#include <stddef.h>
30#include <string.h>
31#define OS2_ONLY
32#include <win32api.h>
33#include <wprocess.h>
34
35#include "misc.h"
36#include "waveoutdart.h"
37#include "initwinmm.h"
38
39#define DBG_LOCALLOG DBG_waveoutdart
40#include "dbglocal.h"
41
42#ifndef min
43#define min(a, b) ((a > b) ? b : a)
44#endif
45
46#ifndef max
47#define max(a, b) ((a > b) ? a : b)
48#endif
49
50LONG APIENTRY WaveOutHandler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags);
51
52//TODO: mulaw, alaw & adpcm
53/******************************************************************************/
54/******************************************************************************/
55DartWaveOut::DartWaveOut(LPWAVEFORMATEX pwfx, ULONG fdwOpen, ULONG nCallback, ULONG dwInstance)
56 : WaveOut(pwfx, fdwOpen, nCallback, dwInstance)
57{
58 MCI_GENERIC_PARMS GenericParms;
59 MCI_AMP_OPEN_PARMS AmpOpenParms;
60 APIRET rc;
61
62 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
63 fMixerSetup = FALSE;
64 fUnderrun = FALSE;
65
66 ulBufSize = DART_BUFSIZE;
67
68 MixBuffer = (MCI_MIX_BUFFER *)malloc(PREFILLBUF_DART*sizeof(MCI_MIX_BUFFER));
69 MixSetupParms = (MCI_MIXSETUP_PARMS *)malloc(sizeof(MCI_MIXSETUP_PARMS));
70 BufferParms = (MCI_BUFFER_PARMS *)malloc(sizeof(MCI_BUFFER_PARMS));
71 if(!MixBuffer || !MixSetupParms || !BufferParms) {
72 dprintf(("ERROR: malloc failed!!"));
73 ulError = MMSYSERR_NOMEM;
74 return;
75 }
76
77 // Setup the open structure, pass the playlist and tell MCI_OPEN to use it
78 memset(&AmpOpenParms,0,sizeof(AmpOpenParms));
79
80 AmpOpenParms.usDeviceID = ( USHORT ) 0;
81 AmpOpenParms.pszDeviceType = ( PSZ ) MCI_DEVTYPE_AUDIO_AMPMIX;
82
83 rc = mymciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE,
84 (PVOID) &AmpOpenParms, 0);
85
86 DeviceId = AmpOpenParms.usDeviceID;
87 if(rc) {
88 dprintf(("MCI_OPEN failed\n"));
89 mciError(rc);
90 ulError = MMSYSERR_NODRIVER;
91 }
92 if(rc == 0) {
93 //Grab exclusive rights to device instance (NOT entire device)
94 GenericParms.hwndCallback = 0; //Not needed, so set to 0
95 rc = mymciSendCommand(DeviceId, MCI_ACQUIREDEVICE, MCI_EXCLUSIVE_INSTANCE,
96 (PVOID)&GenericParms, 0);
97 if(rc) {
98 dprintf(("MCI_ACQUIREDEVICE failed\n"));
99 mciError(rc);
100 ulError = MMSYSERR_NOTENABLED;
101 }
102 }
103 setVolume(volume);
104
105 if(!ulError)
106 callback(WOM_OPEN, 0, 0);
107}
108/******************************************************************************/
109/******************************************************************************/
110DartWaveOut::~DartWaveOut()
111{
112 MCI_GENERIC_PARMS GenericParms;
113
114 State = STATE_STOPPED;
115
116 if(!ulError) {
117 // Generic parameters
118 GenericParms.hwndCallback = 0; //hwndFrame
119
120 // Stop the playback.
121 mymciSendCommand(DeviceId, MCI_STOP,MCI_WAIT, (PVOID)&GenericParms,0);
122
123 mymciSendCommand(DeviceId, MCI_BUFFER,
124 MCI_WAIT | MCI_DEALLOCATE_MEMORY,
125 (PVOID)&BufferParms, 0);
126
127 // Generic parameters
128 GenericParms.hwndCallback = 0; //hwndFrame
129
130 // Close the device
131 mymciSendCommand(DeviceId, MCI_CLOSE, MCI_WAIT, (PVOID)&GenericParms, 0);
132 }
133
134 if(!ulError)
135 {
136 callback(WOM_CLOSE, 0, 0);
137 }
138
139 if(MixBuffer)
140 free(MixBuffer);
141 if(MixSetupParms)
142 free(MixSetupParms);
143 if(BufferParms)
144 free(BufferParms);
145}
146/******************************************************************************/
147/******************************************************************************/
148MMRESULT DartWaveOut::write(LPWAVEHDR pwh, UINT cbwh)
149{
150 MCI_GENERIC_PARMS GenericParms = {0};
151 APIRET rc;
152 int i, buflength;
153
154 queuedbuffers++;
155 if(fMixerSetup == FALSE)
156 {
157 dprintf(("device acquired\n"));
158 /* Set the MixSetupParms data structure to match the loaded file.
159 * This is a global that is used to setup the mixer.
160 */
161 memset(MixSetupParms, 0, sizeof( MCI_MIXSETUP_PARMS ) );
162
163 MixSetupParms->ulBitsPerSample = BitsPerSample;
164 MixSetupParms->ulSamplesPerSec = SampleRate;
165 MixSetupParms->ulFormatTag = MCI_WAVE_FORMAT_PCM;
166 MixSetupParms->ulChannels = nChannels;
167
168 dprintf(("bps %d, sps %d chan %d\n", BitsPerSample, SampleRate, nChannels));
169
170 /* Setup the mixer for playback of wave data
171 */
172 MixSetupParms->ulFormatMode = MCI_PLAY;
173 MixSetupParms->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
174 MixSetupParms->pmixEvent = WaveOutHandler;
175
176 rc = mymciSendCommand(DeviceId,
177 MCI_MIXSETUP,
178 MCI_WAIT | MCI_MIXSETUP_INIT,
179 (PVOID)MixSetupParms,
180 0);
181
182 if ( rc != MCIERR_SUCCESS ) {
183 mciError(rc);
184 mymciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
185 (PVOID)&GenericParms, 0);
186 return(MMSYSERR_NOTSUPPORTED);
187 }
188
189 /*
190 * Set up the BufferParms data structure and allocate
191 * device buffers from the Amp-Mixer
192 */
193 dprintf(("mix setup %d, %d\n", pwh->dwBufferLength, pwh->dwBufferLength));
194
195#if 1
196 int consumerate = getAvgBytesPerSecond();
197 int minbufsize = consumerate/32;
198
199 ulBufSize = pwh->dwBufferLength/2;
200 if(ulBufSize > minbufsize) {
201 dprintf(("set buffer size to %d bytes (org size = %d)", minbufsize, pwh->dwBufferLength));
202 ulBufSize = minbufsize;
203 }
204#else
205 if(pwh->dwBufferLength >= 512 && pwh->dwBufferLength <= 1024)
206 ulBufSize = pwh->dwBufferLength;
207 else ulBufSize = 1024;
208#endif
209
210 MixSetupParms->ulBufferSize = ulBufSize;
211
212 BufferParms->ulNumBuffers = PREFILLBUF_DART;
213 BufferParms->ulBufferSize = MixSetupParms->ulBufferSize;
214 BufferParms->pBufList = MixBuffer;
215
216 for(i=0;i<PREFILLBUF_DART;i++) {
217 MixBuffer[i].ulUserParm = (ULONG)this;
218 }
219
220 rc = mymciSendCommand(DeviceId,
221 MCI_BUFFER,
222 MCI_WAIT | MCI_ALLOCATE_MEMORY,
223 (PVOID)BufferParms,
224 0);
225
226 if(ULONG_LOWD(rc) != MCIERR_SUCCESS) {
227 mciError(rc);
228 mymciSendCommand(DeviceId, MCI_RELEASEDEVICE, MCI_WAIT,
229 (PVOID)&GenericParms, 0);
230 return(MMSYSERR_NOTSUPPORTED);
231 }
232
233 wmutex.enter();
234 fMixerSetup = TRUE;
235
236 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
237 bytesPlayed = bytesCopied = bytesReturned = 0;
238
239 for(i=0;i<PREFILLBUF_DART;i++) {
240 memset(MixBuffer[i].pBuffer, 0, MixBuffer[i].ulBufferLength);
241 }
242 dprintf(("Dart opened, bufsize = %d\n", MixBuffer[0].ulBufferLength));
243
244 wavehdr = pwh;
245 curhdr = pwh;
246 pwh->lpNext = NULL;
247 pwh->reserved = 0;
248
249 if(State != STATE_STOPPED) {//don't start playback if paused
250 wmutex.leave();
251 return(MMSYSERR_NOERROR);
252 }
253
254 writeBuffer(); //must be called before (re)starting playback
255
256 dprintf(("MixSetupParms = %X\n", MixSetupParms));
257 State = STATE_PLAYING;
258 fUnderrun = FALSE;
259 wmutex.leave();
260
261 //write buffers to DART; starts playback
262 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
263 USHORT selTIB = RestoreOS2FS(); // save current FS selector
264
265 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
266 MixBuffer,
267 PREFILLBUF_DART);
268 SetFS(selTIB); // switch back to the saved FS selector
269 dprintf(("Dart playing\n"));
270 }
271 else
272 {
273 pwh->lpNext = NULL;
274 pwh->reserved = 0;
275 wmutex.enter();
276 if(wavehdr) {
277 WAVEHDR *chdr = wavehdr;
278 while(chdr->lpNext) {
279 chdr = chdr->lpNext;
280 }
281 chdr->lpNext = pwh;
282 }
283 else wavehdr = pwh;
284
285 if(!fUnderrun && State != STATE_STOPPED) {//don't start playback if paused
286 wmutex.leave();
287 return(MMSYSERR_NOERROR);
288 }
289
290 writeBuffer(); //must be called before (re)starting playback
291
292 State = STATE_PLAYING;
293 fUnderrun = FALSE;
294 wmutex.leave();
295
296 //write buffers to DART; starts playback
297 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
298 USHORT selTIB = RestoreOS2FS(); // save current FS selector
299
300 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle,
301 MixBuffer,
302 PREFILLBUF_DART);
303 SetFS(selTIB); // switch back to the saved FS selector
304
305 dprintf(("Dart playing\n"));
306 }
307 return(MMSYSERR_NOERROR);
308}
309/******************************************************************************/
310/******************************************************************************/
311MMRESULT DartWaveOut::pause()
312{
313 MCI_GENERIC_PARMS Params;
314
315 dprintf(("WINMM: DartWaveOut::pause"));
316
317 wmutex.enter();
318 if(State != STATE_PLAYING) {
319 State = STATE_PAUSED;
320 wmutex.leave();
321 return(MMSYSERR_NOERROR);
322 }
323
324 State = STATE_PAUSED;
325 wmutex.leave();
326
327 memset(&Params, 0, sizeof(Params));
328
329 // Pause the playback.
330 mymciSendCommand(DeviceId, MCI_PAUSE, MCI_WAIT, (PVOID)&Params, 0);
331
332 return(MMSYSERR_NOERROR);
333}
334/******************************************************************************/
335/******************************************************************************/
336MMRESULT DartWaveOut::resume()
337{
338 MCI_GENERIC_PARMS Params;
339 int i, curbuf;
340
341 dprintf(("DartWaveOut::resume"));
342
343 wmutex.enter();
344 if(State != STATE_PAUSED) {
345 wmutex.leave();
346 return(MMSYSERR_NOERROR);
347 }
348 wmutex.leave();
349
350 //Only write buffers to dart if mixer has been initialized; if not, then
351 //the first buffer write will do this for us.
352 if(fMixerSetup == TRUE)
353 {
354 wmutex.enter();
355 State = STATE_PLAYING;
356 fUnderrun = FALSE;
357 wmutex.leave();
358 curbuf = curPlayBuf;
359
360 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
361 USHORT selTIB = GetFS(); // save current FS selector
362
363 for(i=0;i<PREFILLBUF_DART;i++)
364 {
365 dprintf(("restart: write buffer at %x size %d", MixBuffer[curbuf].pBuffer, MixBuffer[curbuf].ulBufferLength));
366 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curbuf], 1);
367 if(++curbuf == PREFILLBUF_DART)
368 curbuf = 0;
369 }
370 SetFS(selTIB); // switch back to the saved FS selector
371 }
372
373 return(MMSYSERR_NOERROR);
374}
375/******************************************************************************/
376/******************************************************************************/
377MMRESULT DartWaveOut::stop()
378{
379 MCI_GENERIC_PARMS Params;
380
381 dprintf(("DartWaveOut::stop %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
382 if(State != STATE_PLAYING)
383 return(MMSYSERR_HANDLEBUSY);
384
385 memset(&Params, 0, sizeof(Params));
386
387 // Stop the playback.
388 mymciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
389
390 State = STATE_STOPPED;
391 fUnderrun = FALSE;
392
393 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
394 bytesPlayed = bytesCopied = bytesReturned = 0;
395
396 return(MMSYSERR_NOERROR);
397}
398/******************************************************************************/
399/******************************************************************************/
400MMRESULT DartWaveOut::reset()
401{
402 MCI_GENERIC_PARMS Params;
403 LPWAVEHDR tmpwavehdr;
404
405 dprintf(("DartWaveOut::reset %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
406 if(State != STATE_PLAYING)
407 return(MMSYSERR_HANDLEBUSY);
408
409 memset(&Params, 0, sizeof(Params));
410
411 // Stop the playback.
412 mymciSendCommand(DeviceId, MCI_STOP, MCI_WAIT, (PVOID)&Params, 0);
413
414 wmutex.enter();
415 while(wavehdr)
416 {
417 wavehdr->dwFlags |= WHDR_DONE;
418 wavehdr->dwFlags &= ~WHDR_INQUEUE;
419 wavehdr->reserved = 0;
420 tmpwavehdr = wavehdr;
421 wavehdr = wavehdr->lpNext;
422 tmpwavehdr->lpNext = NULL;
423 wmutex.leave();
424
425 callback(WOM_DONE, (ULONG)tmpwavehdr, 0);
426 wmutex.enter();
427 }
428 wavehdr = NULL;
429 State = STATE_STOPPED;
430 fUnderrun = FALSE;
431
432 curPlayBuf = curFillBuf = curFillPos = curPlayPos = 0;
433 bytesPlayed = bytesCopied = bytesReturned = 0;
434 queuedbuffers = 0;
435
436 wmutex.leave();
437 return(MMSYSERR_NOERROR);
438}
439/******************************************************************************/
440/******************************************************************************/
441ULONG DartWaveOut::getPosition()
442{
443 MCI_STATUS_PARMS mciStatus = {0};
444 ULONG rc, nrbytes;
445
446 mciStatus.ulItem = MCI_STATUS_POSITION;
447 rc = mymciSendCommand(DeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_WAIT, (PVOID)&mciStatus, 0);
448 if((rc & 0xFFFF) == MCIERR_SUCCESS) {
449 nrbytes = (mciStatus.ulReturn * (getAvgBytesPerSecond()/1000));
450 return nrbytes;;
451 }
452 mciError(rc);
453 return 0xFFFFFFFF;
454}
455/******************************************************************************/
456/******************************************************************************/
457BOOL DartWaveOut::queryFormat(ULONG formatTag, ULONG nChannels,
458 ULONG nSamplesPerSec, ULONG wBitsPerSample)
459{
460 MCI_WAVE_GETDEVCAPS_PARMS mciAudioCaps;
461 MCI_GENERIC_PARMS GenericParms;
462 MCI_OPEN_PARMS mciOpenParms; /* open parms for MCI_OPEN */
463 int i, freqbits = 0;
464 ULONG rc, DeviceId;
465 BOOL winrc;
466
467 dprintf(("DartWaveOut::queryFormat %x srate=%d, nchan=%d, bps=%d", formatTag, nSamplesPerSec, nChannels, wBitsPerSample));
468
469 memset(&mciOpenParms, /* Object to fill with zeros. */
470 0, /* Value to place into the object. */
471 sizeof( mciOpenParms ) ); /* How many zero's to use. */
472
473 mciOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_WAVEFORM_AUDIO;
474
475 rc = mymciSendCommand( (USHORT) 0,
476 MCI_OPEN,
477 MCI_WAIT | MCI_OPEN_TYPE_ID,
478 (PVOID) &mciOpenParms,
479 0);
480 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
481 mciError(rc);
482 return(FALSE);
483 }
484 DeviceId = mciOpenParms.usDeviceID;
485
486 memset( &mciAudioCaps , 0, sizeof(MCI_WAVE_GETDEVCAPS_PARMS));
487
488 mciAudioCaps.ulBitsPerSample = wBitsPerSample;
489 mciAudioCaps.ulFormatTag = DATATYPE_WAVEFORM;
490 mciAudioCaps.ulSamplesPerSec = nSamplesPerSec;
491 mciAudioCaps.ulChannels = nChannels;
492 mciAudioCaps.ulFormatMode = MCI_PLAY;
493 mciAudioCaps.ulItem = MCI_GETDEVCAPS_WAVE_FORMAT;
494
495 rc = mymciSendCommand(DeviceId, /* Device ID */
496 MCI_GETDEVCAPS,
497 MCI_WAIT | MCI_GETDEVCAPS_EXTENDED | MCI_GETDEVCAPS_ITEM,
498 (PVOID) &mciAudioCaps,
499 0);
500 if((rc & 0xFFFF) != MCIERR_SUCCESS) {
501 mciError(rc);
502 winrc = FALSE;
503 }
504 else winrc = TRUE;
505
506 // Close the device
507 mymciSendCommand(DeviceId,MCI_CLOSE,MCI_WAIT,(PVOID)&GenericParms,0);
508 return(winrc);
509}
510/******************************************************************************/
511/******************************************************************************/
512void DartWaveOut::mciError(ULONG ulError)
513{
514#ifdef DEBUG
515 char szError[256] = "";
516
517 mymciGetErrorString(ulError, szError, sizeof(szError));
518 dprintf(("WINMM: DartWaveOut: %s\n", szError));
519#endif
520}
521//******************************************************************************
522//******************************************************************************
523void DartWaveOut::handler(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
524{
525 ULONG buflength;
526 WAVEHDR *whdr, *prevhdr = NULL;
527
528 dprintf2(("WINMM: handler %d; buffers left %d", curPlayBuf, queuedbuffers));
529 if(ulFlags == MIX_STREAM_ERROR) {
530 if(ulStatus == ERROR_DEVICE_UNDERRUN) {
531 dprintf(("WINMM: WaveOut handler UNDERRUN! state %s", (State == STATE_PLAYING) ? "playing" : "stopped"));
532 if(State == STATE_PLAYING) {
533 fUnderrun = TRUE;
534 stop(); //out of buffers, so stop playback
535 }
536 return;
537 }
538 dprintf(("WINMM: WaveOut handler, Unknown error %X\n", ulStatus));
539 return;
540 }
541 if(State != STATE_PLAYING) {
542 return;
543 }
544
545 wmutex.enter();
546
547 bytesPlayed += MixBuffer[curPlayBuf].ulBufferLength;
548
549 if(curPlayBuf == PREFILLBUF_DART-1)
550 curPlayBuf = 0;
551 else curPlayBuf++;
552
553 fUnderrun = FALSE;
554
555 whdr = wavehdr;
556 while(whdr) {
557 if(whdr->reserved == WHDR_DONE)
558 {
559 if(bytesPlayed < bytesReturned + whdr->dwBufferLength) {
560 dprintf2(("Buffer marked done, but not yet played completely (play %d/%d, cop %d, ret %d)", bytesPlayed, getPosition(), bytesCopied, bytesReturned));
561 break; //not yet done
562 }
563
564 dprintf2(("WINMM: handler buf %X done (play %d/%d, cop %d, ret %d)", whdr, bytesPlayed, getPosition(), bytesCopied, bytesReturned));
565 queuedbuffers--;
566
567 whdr->dwFlags &= ~WHDR_INQUEUE;
568 whdr->dwFlags |= WHDR_DONE;
569 whdr->reserved = 0;
570
571 if(prevhdr == NULL)
572 wavehdr = whdr->lpNext;
573 else prevhdr->lpNext = whdr->lpNext;
574
575 whdr->lpNext = NULL;
576
577 bytesReturned += whdr->dwBufferLength;
578 wmutex.leave();
579
580 callback(WOM_DONE, (ULONG)whdr, 0);
581
582 wmutex.enter();
583 }
584 else break;
585
586 prevhdr = whdr;
587 whdr = whdr->lpNext;
588 }
589
590 if(wavehdr == NULL) {
591 //last buffer played -> no new ones -> silence buffer & continue
592 dprintf(("WINMM: WaveOut handler LAST BUFFER PLAYED! state %s (play %d (%d), cop %d, ret %d)", (State == STATE_PLAYING) ? "playing" : "stopped", bytesPlayed, getPosition(), bytesCopied, bytesReturned));
593 if(State == STATE_PLAYING) {
594 fUnderrun = TRUE;
595 pause();
596//// memset(MixBuffer[curPlayBuf].pBuffer, 0, MixBuffer[curPlayBuf].ulBufferLength);
597//// goto sendbuffer;
598 }
599 wmutex.leave();
600 return;
601 }
602
603 writeBuffer();
604
605sendbuffer:
606 wmutex.leave();
607
608 //Transfer buffer to DART
609 // MCI_MIXSETUP_PARMS->pMixWrite does alter FS: selector!
610 USHORT selTIB = RestoreOS2FS(); // save current FS selector
611 MixSetupParms->pmixWrite(MixSetupParms->ulMixHandle, &MixBuffer[curPlayBuf], 1);
612 SetFS(selTIB); // switch back to the saved FS selector
613
614 dprintf2(("WINMM: handler DONE"));
615}
616/******************************************************************************/
617/******************************************************************************/
618void DartWaveOut::writeBuffer()
619{
620 ULONG buflength;
621
622 if(!fUnderrun && State == STATE_PLAYING && curFillBuf == curPlayBuf) {
623 dprintf2(("writeBuffer: no more room for more audio data"));
624 return; //no room left
625 }
626
627 if(curhdr == NULL)
628 curhdr = wavehdr;
629
630 while(curhdr && (curhdr->reserved == WHDR_DONE)) {
631 curhdr = curhdr->lpNext;
632 }
633
634 if(curhdr == NULL) {
635 return; //no unprocessed buffers left
636 }
637
638 dprintf2(("WINMM: handler cur (%d,%d), fill (%d,%d)\n", curPlayBuf, curPlayPos, curFillBuf, curFillPos));
639
640 while(curhdr) {
641 buflength = min((ULONG)MixBuffer[curFillBuf].ulBufferLength - curPlayPos,
642 (ULONG)curhdr->dwBufferLength - curFillPos);
643 dprintf2(("WINMM: copied %d bytes, cufFillPos = %d, curPlayPos = %d, dwBufferLength = %d\n", buflength, curFillPos, curPlayPos, curhdr->dwBufferLength));
644
645 memcpy((char *)MixBuffer[curFillBuf].pBuffer + curPlayPos,
646 curhdr->lpData + curFillPos, buflength);
647 curPlayPos += buflength;
648 curFillPos += buflength;
649 bytesCopied += buflength;
650
651 if(curFillPos == curhdr->dwBufferLength) {
652 dprintf2(("Buffer %d done ptr %x size %d %x %x %x %x %x %x", curFillBuf, curhdr->lpData, curhdr->dwBufferLength, curhdr->dwBytesRecorded, curhdr->dwUser, curhdr->dwFlags, curhdr->dwLoops, curhdr->lpNext, curhdr->reserved));
653
654 curFillPos = 0;
655 curhdr->reserved = WHDR_DONE;
656 //search for next unprocessed buffer
657 while(curhdr && (curhdr->reserved == WHDR_DONE))
658 curhdr = curhdr->lpNext;
659 }
660 if(curPlayPos == MixBuffer[curFillBuf].ulBufferLength) {
661 curPlayPos = 0;
662 if(++curFillBuf == PREFILLBUF_DART) {
663 curFillBuf = 0;
664 }
665 if(curFillBuf == curPlayBuf)
666 break; //no more room left
667 }
668 }
669}
670/******************************************************************************/
671/******************************************************************************/
672LONG APIENTRY WaveOutHandler(ULONG ulStatus,
673 PMCI_MIX_BUFFER pBuffer,
674 ULONG ulFlags)
675{
676 PTIB2 ptib2;
677 DartWaveOut *dwave;
678
679 dprintf2(("WaveOutHandler %x %x %x", ulStatus, pBuffer, ulFlags));
680
681 ptib2 = (PTIB2)_getTIBvalue(offsetof(TIB, tib_ptib2));
682 if(ptib2 && HIBYTE(ptib2->tib2_ulpri) != PRTYC_TIMECRITICAL) {
683 dprintf(("Setting priority of DART thread to PRTYC_TIMECRITICAL"));
684 DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
685 }
686 if(pBuffer && pBuffer->ulUserParm)
687 {
688 dwave = (DartWaveOut *)pBuffer->ulUserParm;
689 dwave->handler(ulStatus, pBuffer, ulFlags);
690 }
691 return(TRUE);
692}
693
694/******************************************************************************/
695/******************************************************************************/
696MMRESULT DartWaveOut::setVolume(ULONG ulVol)
697{
698 ULONG ulVolR = (((ulVol & 0xffff0000) >> 16 )*100)/0xFFFF; // Right Volume
699 ULONG ulVolL = ((ulVol& 0x0000ffff)*100)/0xFFFF; // Left Volume
700 MCI_SET_PARMS msp = {0};
701
702 dprintf(("DartWaveOut::setVolume %d %d", ulVolL, ulVolR));
703 volume = ulVol;
704
705// PD: My card (ESS 1868 PnP) driver can't change only
706// one channel Left or Right :-(
707//
708#ifdef GOOD_AUDIO_CARD_DRIVER
709
710 msp.ulAudio = MCI_SET_AUDIO_LEFT;
711 msp.ulLevel = ulVolL;
712
713 mymciSendCommand(DeviceId, MCI_SET,
714 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
715 &msp, 0);
716
717 msp.ulAudio = MCI_SET_AUDIO_RIGHT;
718 msp.ulLevel = ulVolR;
719
720#else
721 msp.ulAudio = MCI_SET_AUDIO_ALL;
722 msp.ulLevel = max(ulVolR,ulVolL);
723#endif
724
725 mymciSendCommand(DeviceId, MCI_SET,
726 MCI_WAIT | MCI_SET_AUDIO | MCI_SET_VOLUME,
727 &msp, 0);
728 return 0;
729}
730/******************************************************************************/
731/******************************************************************************/
732
Note: See TracBrowser for help on using the repository browser.