3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <math.h> /* Insomnia - pow() function */
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
34 #include "wine/debug.h"
37 #include "dsound_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
41 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan
)
44 TRACE("(%p)\n",volpan
);
46 TRACE("Vol=%d Pan=%d\n", volpan
->lVolume
, volpan
->lPan
);
47 /* the AmpFactors are expressed in 16.16 fixed point */
48 volpan
->dwVolAmpFactor
= (ULONG
) (pow(2.0, volpan
->lVolume
/ 600.0) * 0xffff);
49 /* FIXME: dwPan{Left|Right}AmpFactor */
51 /* FIXME: use calculated vol and pan ampfactors */
52 temp
= (double) (volpan
->lVolume
- (volpan
->lPan
> 0 ? volpan
->lPan
: 0));
53 volpan
->dwTotalLeftAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 0xffff);
54 temp
= (double) (volpan
->lVolume
+ (volpan
->lPan
< 0 ? volpan
->lPan
: 0));
55 volpan
->dwTotalRightAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 0xffff);
57 TRACE("left = %x, right = %x\n", volpan
->dwTotalLeftAmpFactor
, volpan
->dwTotalRightAmpFactor
);
60 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan
)
63 TRACE("(%p)\n",volpan
);
65 TRACE("left=%x, right=%x\n",volpan
->dwTotalLeftAmpFactor
,volpan
->dwTotalRightAmpFactor
);
66 if (volpan
->dwTotalLeftAmpFactor
==0)
69 left
=600 * log(((double)volpan
->dwTotalLeftAmpFactor
) / 0xffff) / log(2);
70 if (volpan
->dwTotalRightAmpFactor
==0)
73 right
=600 * log(((double)volpan
->dwTotalRightAmpFactor
) / 0xffff) / log(2);
76 volpan
->lVolume
=right
;
77 volpan
->dwVolAmpFactor
=volpan
->dwTotalRightAmpFactor
;
82 volpan
->dwVolAmpFactor
=volpan
->dwTotalLeftAmpFactor
;
84 if (volpan
->lVolume
< -10000)
85 volpan
->lVolume
=-10000;
86 volpan
->lPan
=right
-left
;
87 if (volpan
->lPan
< -10000)
90 TRACE("Vol=%d Pan=%d\n", volpan
->lVolume
, volpan
->lPan
);
93 void DSOUND_RecalcFormat(IDirectSoundBufferImpl
*dsb
)
97 /* calculate the 10ms write lead */
98 dsb
->writelead
= (dsb
->freq
/ 100) * dsb
->pwfx
->nBlockAlign
;
102 * Check for application callback requests for when the play position
103 * reaches certain points.
105 * The offsets that will be triggered will be those between the recorded
106 * "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
107 * beyond that position.
109 void DSOUND_CheckEvent(IDirectSoundBufferImpl
*dsb
, int len
)
113 LPDSBPOSITIONNOTIFY event
;
114 TRACE("(%p,%d)\n",dsb
,len
);
116 if (dsb
->nrofnotifies
== 0)
119 TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
120 dsb
, dsb
->buflen
, dsb
->playpos
, len
);
121 for (i
= 0; i
< dsb
->nrofnotifies
; i
++) {
122 event
= dsb
->notifies
+ i
;
123 offset
= event
->dwOffset
;
124 TRACE("checking %d, position %d, event = %p\n",
125 i
, offset
, event
->hEventNotify
);
126 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
127 /* OK. [Inside DirectX, p274] */
129 /* This also means we can't sort the entries by offset, */
130 /* because DSBPN_OFFSETSTOP == -1 */
131 if (offset
== DSBPN_OFFSETSTOP
) {
132 if (dsb
->state
== STATE_STOPPED
) {
133 SetEvent(event
->hEventNotify
);
134 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
139 if ((dsb
->playpos
+ len
) >= dsb
->buflen
) {
140 if ((offset
< ((dsb
->playpos
+ len
) % dsb
->buflen
)) ||
141 (offset
>= dsb
->playpos
)) {
142 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
143 SetEvent(event
->hEventNotify
);
146 if ((offset
>= dsb
->playpos
) && (offset
< (dsb
->playpos
+ len
))) {
147 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
148 SetEvent(event
->hEventNotify
);
154 /* WAV format info can be found at:
156 * http://www.cwi.nl/ftp/audio/AudioFormats.part2
157 * ftp://ftp.cwi.nl/pub/audio/RIFF-format
159 * Import points to remember:
160 * 8-bit WAV is unsigned
161 * 16-bit WAV is signed
163 /* Use the same formulas as pcmconverter.c */
164 static inline INT16
cvtU8toS16(BYTE b
)
166 return (short)((b
+(b
<< 8))-32768);
169 static inline BYTE
cvtS16toU8(INT16 s
)
171 return (s
>> 8) ^ (unsigned char)0x80;
175 * Copy a single frame from the given input buffer to the given output buffer.
176 * Translate 8 <-> 16 bits and mono <-> stereo
178 static inline void cp_fields(const IDirectSoundBufferImpl
*dsb
, BYTE
*ibuf
, BYTE
*obuf
)
180 DirectSoundDevice
* device
= dsb
->device
;
183 if (dsb
->pwfx
->wBitsPerSample
== 8) {
184 if (device
->pwfx
->wBitsPerSample
== 8 &&
185 device
->pwfx
->nChannels
== dsb
->pwfx
->nChannels
) {
186 /* avoid needless 8->16->8 conversion */
188 if (dsb
->pwfx
->nChannels
==2)
192 fl
= cvtU8toS16(*ibuf
);
193 fr
= (dsb
->pwfx
->nChannels
==2 ? cvtU8toS16(*(ibuf
+ 1)) : fl
);
195 fl
= *((INT16
*)ibuf
);
196 fr
= (dsb
->pwfx
->nChannels
==2 ? *(((INT16
*)ibuf
) + 1) : fl
);
199 if (device
->pwfx
->nChannels
== 2) {
200 if (device
->pwfx
->wBitsPerSample
== 8) {
201 *obuf
= cvtS16toU8(fl
);
202 *(obuf
+ 1) = cvtS16toU8(fr
);
205 if (device
->pwfx
->wBitsPerSample
== 16) {
206 *((INT16
*)obuf
) = fl
;
207 *(((INT16
*)obuf
) + 1) = fr
;
211 if (device
->pwfx
->nChannels
== 1) {
213 if (device
->pwfx
->wBitsPerSample
== 8) {
214 *obuf
= cvtS16toU8(fl
);
217 if (device
->pwfx
->wBitsPerSample
== 16) {
218 *((INT16
*)obuf
) = fl
;
225 * Mix at most the given amount of data into the given device buffer from the
226 * given secondary buffer, starting from the dsb's first currently unmixed
227 * frame (buf_mixpos), translating frequency (pitch), stereo/mono and
228 * bits-per-sample. The secondary buffer sample is looped if it is not
229 * long enough and it is a looping buffer.
230 * (Doesn't perform any mixing - this is a straight copy operation).
232 * Now with PerfectPitch (tm) technology
234 * dsb = the secondary buffer
235 * buf = the device buffer
236 * len = number of bytes to store in the device buffer
238 * Returns: the number of bytes read from the secondary buffer
239 * (ie. len, adjusted for frequency, number of channels and sample size,
240 * and limited by buffer length for non-looping buffers)
242 static INT
DSOUND_MixerNorm(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
244 INT i
, size
, ipos
, ilen
;
246 INT iAdvance
= dsb
->pwfx
->nBlockAlign
;
247 INT oAdvance
= dsb
->device
->pwfx
->nBlockAlign
;
249 ibp
= dsb
->buffer
->memory
+ dsb
->buf_mixpos
;
252 TRACE("(%p, %p, %p), buf_mixpos=%d\n", dsb
, ibp
, obp
, dsb
->buf_mixpos
);
253 /* Check for the best case */
254 if ((dsb
->freq
== dsb
->device
->pwfx
->nSamplesPerSec
) &&
255 (dsb
->pwfx
->wBitsPerSample
== dsb
->device
->pwfx
->wBitsPerSample
) &&
256 (dsb
->pwfx
->nChannels
== dsb
->device
->pwfx
->nChannels
)) {
257 INT bytesleft
= dsb
->buflen
- dsb
->buf_mixpos
;
258 TRACE("(%p) Best case\n", dsb
);
259 if (len
<= bytesleft
)
260 CopyMemory(obp
, ibp
, len
);
262 CopyMemory(obp
, ibp
, bytesleft
);
263 CopyMemory(obp
+ bytesleft
, dsb
->buffer
->memory
, len
- bytesleft
);
268 /* Check for same sample rate */
269 if (dsb
->freq
== dsb
->device
->pwfx
->nSamplesPerSec
) {
270 TRACE("(%p) Same sample rate %d = primary %d\n", dsb
,
271 dsb
->freq
, dsb
->device
->pwfx
->nSamplesPerSec
);
273 for (i
= 0; i
< len
; i
+= oAdvance
) {
274 cp_fields(dsb
, ibp
, obp
);
278 if (ibp
>= (BYTE
*)(dsb
->buffer
->memory
+ dsb
->buflen
))
279 ibp
= dsb
->buffer
->memory
; /* wrap */
284 /* Mix in different sample rates */
286 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
287 /* Patent Pending :-] */
289 /* Patent enhancements (c) 2000 Ove KÃ¥ven,
290 * TransGaming Technologies Inc. */
292 /* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
293 dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec); */
295 size
= len
/ oAdvance
;
297 ipos
= dsb
->buf_mixpos
;
298 for (i
= 0; i
< size
; i
++) {
299 cp_fields(dsb
, (dsb
->buffer
->memory
+ ipos
), obp
);
301 dsb
->freqAcc
+= dsb
->freqAdjust
;
302 if (dsb
->freqAcc
>= (1<<DSOUND_FREQSHIFT
)) {
303 ULONG adv
= (dsb
->freqAcc
>>DSOUND_FREQSHIFT
) * iAdvance
;
304 dsb
->freqAcc
&= (1<<DSOUND_FREQSHIFT
)-1;
305 ipos
+= adv
; ilen
+= adv
;
312 static void DSOUND_MixerVol(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
316 INT16
*bps
= (INT16
*) buf
;
318 TRACE("(%p,%p,%d)\n",dsb
,buf
,len
);
319 TRACE("left = %x, right = %x\n", dsb
->cvolpan
.dwTotalLeftAmpFactor
,
320 dsb
->cvolpan
.dwTotalRightAmpFactor
);
322 if ((!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) || (dsb
->cvolpan
.lPan
== 0)) &&
323 (!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) || (dsb
->cvolpan
.lVolume
== 0)) &&
324 !(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
325 return; /* Nothing to do */
327 /* If we end up with some bozo coder using panning or 3D sound */
328 /* with a mono primary buffer, it could sound very weird using */
329 /* this method. Oh well, tough patooties. */
331 switch (dsb
->device
->pwfx
->wBitsPerSample
) {
333 /* 8-bit WAV is unsigned, but we need to operate */
334 /* on signed data for this to work properly */
335 switch (dsb
->device
->pwfx
->nChannels
) {
337 for (i
= 0; i
< len
; i
++) {
338 INT val
= *bpc
- 128;
339 val
= (val
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
345 for (i
= 0; i
< len
; i
+=2) {
346 INT val
= *bpc
- 128;
347 val
= (val
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
350 val
= (val
* dsb
->cvolpan
.dwTotalRightAmpFactor
) >> 16;
356 FIXME("doesn't support %d channels\n", dsb
->device
->pwfx
->nChannels
);
361 /* 16-bit WAV is signed -- much better */
362 switch (dsb
->device
->pwfx
->nChannels
) {
364 for (i
= 0; i
< len
; i
+= 2) {
365 *bps
= (*bps
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
370 for (i
= 0; i
< len
; i
+= 4) {
371 *bps
= (*bps
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
373 *bps
= (*bps
* dsb
->cvolpan
.dwTotalRightAmpFactor
) >> 16;
378 FIXME("doesn't support %d channels\n", dsb
->device
->pwfx
->nChannels
);
383 FIXME("doesn't support %d bit samples\n", dsb
->device
->pwfx
->wBitsPerSample
);
389 * Make sure the device's tmp_buffer is at least the given size. Return a
392 static LPBYTE
DSOUND_tmpbuffer(DirectSoundDevice
*device
, DWORD len
)
394 TRACE("(%p,%d)\n", device
, len
);
396 if (len
> device
->tmp_buffer_len
) {
397 if (device
->tmp_buffer
)
398 device
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0, device
->tmp_buffer
, len
);
400 device
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0, len
);
402 device
->tmp_buffer_len
= len
;
405 return device
->tmp_buffer
;
409 * Mix (at most) the given number of bytes into the given position of the
410 * device buffer, from the secondary buffer "dsb" (starting at the current
411 * mix position for that buffer).
413 * Returns the number of bytes actually mixed into the device buffer. This
414 * will match fraglen unless the end of the secondary buffer is reached
415 * (and it is not looping).
417 * dsb = the secondary buffer to mix from
418 * writepos = position (offset) in device buffer to write at
419 * fraglen = number of bytes to mix
421 static DWORD
DSOUND_MixInBuffer(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD fraglen
)
423 INT i
, len
, ilen
, field
, todo
;
426 TRACE("(%p,%d,%d)\n",dsb
,writepos
,fraglen
);
429 if (!(dsb
->playflags
& DSBPLAY_LOOPING
)) {
430 /* This buffer is not looping, so make sure the requested
431 * length will not take us past the end of the buffer */
432 int secondary_remainder
= dsb
->buflen
- dsb
->buf_mixpos
;
433 int adjusted_remainder
= MulDiv(dsb
->device
->pwfx
->nAvgBytesPerSec
, secondary_remainder
, dsb
->nAvgBytesPerSec
);
434 assert(adjusted_remainder
>= 0);
435 /* The adjusted remainder must be at least one sample,
436 * otherwise we will never reach the end of the
437 * secondary buffer, as there will perpetually be a
438 * fractional remainder */
439 TRACE("secondary_remainder = %d, adjusted_remainder = %d, len = %d\n", secondary_remainder
, adjusted_remainder
, len
);
440 if (adjusted_remainder
< len
) {
441 TRACE("clipping len to remainder of secondary buffer\n");
442 len
= adjusted_remainder
;
448 if (len
% dsb
->device
->pwfx
->nBlockAlign
) {
449 INT nBlockAlign
= dsb
->device
->pwfx
->nBlockAlign
;
450 ERR("length not a multiple of block size, len = %d, block size = %d\n", len
, nBlockAlign
);
451 len
= (len
/ nBlockAlign
) * nBlockAlign
; /* data alignment */
454 if ((buf
= ibuf
= DSOUND_tmpbuffer(dsb
->device
, len
)) == NULL
)
457 TRACE("MixInBuffer (%p) len = %d, dest = %d\n", dsb
, len
, writepos
);
459 /* first, copy the data from the DirectSoundBuffer into the temporary
460 buffer, translating frequency/bits-per-sample/number-of-channels
461 to match the device settings */
462 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
463 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
464 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) ||
465 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
466 DSOUND_MixerVol(dsb
, ibuf
, len
);
468 /* Now mix the temporary buffer into the devices main buffer */
469 if (dsb
->device
->pwfx
->wBitsPerSample
== 8) {
470 BYTE
*obuf
= dsb
->device
->buffer
+ writepos
;
472 if ((writepos
+ len
) <= dsb
->device
->buflen
)
475 todo
= dsb
->device
->buflen
- writepos
;
477 for (i
= 0; i
< todo
; i
++) {
478 /* 8-bit WAV is unsigned */
479 field
= (*ibuf
++ - 128);
480 field
+= (*obuf
- 128);
481 if (field
> 127) field
= 127;
482 else if (field
< -128) field
= -128;
483 *obuf
++ = field
+ 128;
488 obuf
= dsb
->device
->buffer
;
490 for (i
= 0; i
< todo
; i
++) {
491 /* 8-bit WAV is unsigned */
492 field
= (*ibuf
++ - 128);
493 field
+= (*obuf
- 128);
494 if (field
> 127) field
= 127;
495 else if (field
< -128) field
= -128;
496 *obuf
++ = field
+ 128;
500 INT16
*ibufs
, *obufs
;
502 ibufs
= (INT16
*) ibuf
;
503 obufs
= (INT16
*)(dsb
->device
->buffer
+ writepos
);
505 if ((writepos
+ len
) <= dsb
->device
->buflen
)
508 todo
= (dsb
->device
->buflen
- writepos
) / 2;
510 for (i
= 0; i
< todo
; i
++) {
511 /* 16-bit WAV is signed */
514 if (field
> 32767) field
= 32767;
515 else if (field
< -32768) field
= -32768;
519 if (todo
< (len
/ 2)) {
520 todo
= (len
/ 2) - todo
;
521 obufs
= (INT16
*)dsb
->device
->buffer
;
523 for (i
= 0; i
< todo
; i
++) {
524 /* 16-bit WAV is signed */
527 if (field
> 32767) field
= 32767;
528 else if (field
< -32768) field
= -32768;
534 if (dsb
->leadin
&& (dsb
->startpos
> dsb
->buf_mixpos
) && (dsb
->startpos
<= dsb
->buf_mixpos
+ ilen
)) {
535 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
536 * not the MIX position... but if the sound buffer is bigger than our prebuffering
537 * (which must be the case for the streaming buffers that need this hack anyway)
538 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
542 dsb
->buf_mixpos
+= ilen
;
544 if (dsb
->buf_mixpos
>= dsb
->buflen
) {
545 if (dsb
->playflags
& DSBPLAY_LOOPING
) {
547 dsb
->buf_mixpos
%= dsb
->buflen
;
548 if (dsb
->leadin
&& (dsb
->startpos
<= dsb
->buf_mixpos
))
549 dsb
->leadin
= FALSE
; /* HACK: see above */
550 } else if (dsb
->buf_mixpos
> dsb
->buflen
) {
551 ERR("Mixpos (%u) past buflen (%u), capping...\n", dsb
->buf_mixpos
, dsb
->buflen
);
552 dsb
->buf_mixpos
= dsb
->buflen
;
559 static void DSOUND_PhaseCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD len
)
565 TRACE("(%p,%d,%d)\n",dsb
,writepos
,len
);
567 if (len
% dsb
->device
->pwfx
->nBlockAlign
) {
568 INT nBlockAlign
= dsb
->device
->pwfx
->nBlockAlign
;
569 ERR("length not a multiple of block size, len = %d, block size = %d\n", len
, nBlockAlign
);
570 len
= (len
/ nBlockAlign
) * nBlockAlign
; /* data alignment */
573 if ((buf
= ibuf
= DSOUND_tmpbuffer(dsb
->device
, len
)) == NULL
)
576 TRACE("PhaseCancel (%p) len = %d, dest = %d\n", dsb
, len
, writepos
);
578 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
579 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
580 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) ||
581 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
582 DSOUND_MixerVol(dsb
, ibuf
, len
);
584 /* subtract instead of add, to phase out premixed data */
585 if (dsb
->device
->pwfx
->wBitsPerSample
== 8) {
586 BYTE
*obuf
= dsb
->device
->buffer
+ writepos
;
588 if ((writepos
+ len
) <= dsb
->device
->buflen
)
591 todo
= dsb
->device
->buflen
- writepos
;
593 for (i
= 0; i
< todo
; i
++) {
594 /* 8-bit WAV is unsigned */
595 field
= (*obuf
- 128);
596 field
-= (*ibuf
++ - 128);
597 if (field
> 127) field
= 127;
598 else if (field
< -128) field
= -128;
599 *obuf
++ = field
+ 128;
604 obuf
= dsb
->device
->buffer
;
606 for (i
= 0; i
< todo
; i
++) {
607 /* 8-bit WAV is unsigned */
608 field
= (*obuf
- 128);
609 field
-= (*ibuf
++ - 128);
610 if (field
> 127) field
= 127;
611 else if (field
< -128) field
= -128;
612 *obuf
++ = field
+ 128;
616 INT16
*ibufs
, *obufs
;
618 ibufs
= (INT16
*) ibuf
;
619 obufs
= (INT16
*)(dsb
->device
->buffer
+ writepos
);
621 if ((writepos
+ len
) <= dsb
->device
->buflen
)
624 todo
= (dsb
->device
->buflen
- writepos
) / 2;
626 for (i
= 0; i
< todo
; i
++) {
627 /* 16-bit WAV is signed */
630 if (field
> 32767) field
= 32767;
631 else if (field
< -32768) field
= -32768;
635 if (todo
< (len
/ 2)) {
636 todo
= (len
/ 2) - todo
;
637 obufs
= (INT16
*)dsb
->device
->buffer
;
639 for (i
= 0; i
< todo
; i
++) {
640 /* 16-bit WAV is signed */
643 if (field
> 32767) field
= 32767;
644 else if (field
< -32768) field
= -32768;
651 static void DSOUND_MixCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, BOOL cancel
)
653 DWORD size
, flen
, len
, npos
, nlen
;
654 INT iAdvance
= dsb
->pwfx
->nBlockAlign
;
655 INT oAdvance
= dsb
->device
->pwfx
->nBlockAlign
;
656 /* determine amount of premixed data to cancel */
658 ((dsb
->primary_mixpos
< writepos
) ? dsb
->device
->buflen
: 0) +
659 dsb
->primary_mixpos
- writepos
;
661 TRACE("(%p, %d), buf_mixpos=%d\n", dsb
, writepos
, dsb
->buf_mixpos
);
663 /* backtrack the mix position */
664 size
= primary_done
/ oAdvance
;
665 flen
= size
* dsb
->freqAdjust
;
666 len
= (flen
>> DSOUND_FREQSHIFT
) * iAdvance
;
667 flen
&= (1<<DSOUND_FREQSHIFT
)-1;
668 while (dsb
->freqAcc
< flen
) {
670 dsb
->freqAcc
+= 1<<DSOUND_FREQSHIFT
;
673 npos
= ((dsb
->buf_mixpos
< len
) ? dsb
->buflen
: 0) +
674 dsb
->buf_mixpos
- len
;
675 if (dsb
->leadin
&& (dsb
->startpos
> npos
) && (dsb
->startpos
<= npos
+ len
)) {
676 /* stop backtracking at startpos */
677 npos
= dsb
->startpos
;
678 len
= ((dsb
->buf_mixpos
< npos
) ? dsb
->buflen
: 0) +
679 dsb
->buf_mixpos
- npos
;
681 nlen
= len
/ dsb
->pwfx
->nBlockAlign
;
682 nlen
= ((nlen
<< DSOUND_FREQSHIFT
) + flen
) / dsb
->freqAdjust
;
683 nlen
*= dsb
->device
->pwfx
->nBlockAlign
;
685 ((dsb
->primary_mixpos
< nlen
) ? dsb
->device
->buflen
: 0) +
686 dsb
->primary_mixpos
- nlen
;
689 dsb
->freqAcc
-= flen
;
690 dsb
->buf_mixpos
= npos
;
691 dsb
->primary_mixpos
= writepos
;
693 TRACE("new buf_mixpos=%d, primary_mixpos=%d (len=%d)\n",
694 dsb
->buf_mixpos
, dsb
->primary_mixpos
, len
);
696 if (cancel
) DSOUND_PhaseCancel(dsb
, writepos
, len
);
699 void DSOUND_MixCancelAt(IDirectSoundBufferImpl
*dsb
, DWORD buf_writepos
)
702 DWORD i
, size
, flen
, len
, npos
, nlen
;
703 INT iAdvance
= dsb
->pwfx
->nBlockAlign
;
704 INT oAdvance
= dsb
->device
->pwfx
->nBlockAlign
;
705 /* determine amount of premixed data to cancel */
707 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
708 dsb
->buf_mixpos
- buf_writepos
;
711 WARN("(%p, %d), buf_mixpos=%d\n", dsb
, buf_writepos
, dsb
->buf_mixpos
);
712 /* since this is not implemented yet, just cancel *ALL* prebuffering for now
713 * (which is faster anyway when there's only a single secondary buffer) */
714 dsb
->device
->need_remix
= TRUE
;
717 void DSOUND_ForceRemix(IDirectSoundBufferImpl
*dsb
)
720 EnterCriticalSection(&dsb
->lock
);
721 if (dsb
->state
== STATE_PLAYING
)
722 dsb
->device
->need_remix
= TRUE
;
723 LeaveCriticalSection(&dsb
->lock
);
727 * Mix some frames from the given secondary buffer "dsb" into the device
730 * dsb = the secondary buffer
731 * playpos = the current play position in the device buffer (primary buffer)
732 * writepos = the current safe-to-write position in the device buffer
733 * mixlen = the maximum number of bytes in the primary buffer to mix, from the
736 * Returns: the number of bytes beyond the writepos that were mixed.
738 static DWORD
DSOUND_MixOne(IDirectSoundBufferImpl
*dsb
, DWORD playpos
, DWORD writepos
, DWORD mixlen
)
740 /* The buffer's primary_mixpos may be before or after the the device
741 * buffer's mixpos, but both must be ahead of writepos. */
744 /* determine this buffer's write position */
745 DWORD buf_writepos
= DSOUND_CalcPlayPosition(dsb
, writepos
, writepos
);
746 /* determine how much already-mixed data exists */
748 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
749 dsb
->buf_mixpos
- buf_writepos
;
751 ((dsb
->primary_mixpos
< writepos
) ? dsb
->device
->buflen
: 0) +
752 dsb
->primary_mixpos
- writepos
;
754 ((dsb
->device
->mixpos
< writepos
) ? dsb
->device
->buflen
: 0) +
755 dsb
->device
->mixpos
- writepos
;
757 ((buf_writepos
< dsb
->playpos
) ? dsb
->buflen
: 0) +
758 buf_writepos
- dsb
->playpos
;
759 DWORD buf_left
= dsb
->buflen
- buf_writepos
;
762 TRACE("(%p,%d,%d,%d)\n",dsb
,playpos
,writepos
,mixlen
);
763 TRACE("buf_writepos=%d, primary_writepos=%d\n", buf_writepos
, writepos
);
764 TRACE("buf_done=%d, primary_done=%d\n", buf_done
, primary_done
);
765 TRACE("buf_mixpos=%d, primary_mixpos=%d, mixlen=%d\n", dsb
->buf_mixpos
, dsb
->primary_mixpos
,
767 TRACE("looping=%d, startpos=%d, leadin=%d\n", dsb
->playflags
, dsb
->startpos
, dsb
->leadin
);
769 /* check for notification positions */
770 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPOSITIONNOTIFY
&&
771 dsb
->state
!= STATE_STARTING
) {
772 DSOUND_CheckEvent(dsb
, played
);
775 /* save write position for non-GETCURRENTPOSITION2... */
776 dsb
->playpos
= buf_writepos
;
778 /* check whether CalcPlayPosition detected a mixing underrun */
779 if ((buf_done
== 0) && (dsb
->primary_mixpos
!= writepos
)) {
780 /* it did, but did we have more to play? */
781 if ((dsb
->playflags
& DSBPLAY_LOOPING
) ||
782 (dsb
->buf_mixpos
< dsb
->buflen
)) {
783 /* yes, have to recover */
784 ERR("underrun on sound buffer %p\n", dsb
);
785 TRACE("recovering from underrun: primary_mixpos=%d\n", writepos
);
787 dsb
->primary_mixpos
= writepos
;
790 /* determine how far ahead we should mix */
791 if (((dsb
->playflags
& DSBPLAY_LOOPING
) ||
792 (dsb
->leadin
&& (dsb
->probably_valid_to
!= 0))) &&
793 !(dsb
->dsbd
.dwFlags
& DSBCAPS_STATIC
)) {
794 /* if this is a streaming buffer, it typically means that
795 * we should defer mixing past probably_valid_to as long
796 * as we can, to avoid unnecessary remixing */
797 /* the heavy-looking calculations shouldn't be that bad,
798 * as any game isn't likely to be have more than 1 or 2
799 * streaming buffers in use at any time anyway... */
800 DWORD probably_valid_left
=
801 (dsb
->probably_valid_to
== (DWORD
)-1) ? dsb
->buflen
:
802 ((dsb
->probably_valid_to
< buf_writepos
) ? dsb
->buflen
: 0) +
803 dsb
->probably_valid_to
- buf_writepos
;
804 /* check for leadin condition */
805 if ((probably_valid_left
== 0) &&
806 (dsb
->probably_valid_to
== dsb
->startpos
) &&
808 probably_valid_left
= dsb
->buflen
;
809 TRACE("streaming buffer probably_valid_to=%d, probably_valid_left=%d\n",
810 dsb
->probably_valid_to
, probably_valid_left
);
811 /* check whether the app's time is already up */
812 if (probably_valid_left
< dsb
->writelead
) {
813 WARN("probably_valid_to now within writelead, possible streaming underrun\n");
814 /* once we pass the point of no return,
815 * no reason to hold back anymore */
816 dsb
->probably_valid_to
= (DWORD
)-1;
817 /* we just have to go ahead and mix what we have,
818 * there's no telling what the app is thinking anyway */
820 /* adjust for our frequency and our sample size */
821 probably_valid_left
= MulDiv(probably_valid_left
,
822 1 << DSOUND_FREQSHIFT
,
823 dsb
->pwfx
->nBlockAlign
* dsb
->freqAdjust
) *
824 dsb
->device
->pwfx
->nBlockAlign
;
825 /* check whether to clip mix_len */
826 if (probably_valid_left
< mixlen
) {
827 TRACE("clipping to probably_valid_left=%d\n", probably_valid_left
);
828 mixlen
= probably_valid_left
;
832 /* cut mixlen with what's already been mixed */
833 if (mixlen
< primary_done
) {
834 /* huh? and still CalcPlayPosition didn't
835 * detect an underrun? */
836 FIXME("problem with underrun detection (mixlen=%d < primary_done=%d)\n", mixlen
, primary_done
);
839 len
= mixlen
- primary_done
;
840 TRACE("remaining mixlen=%d\n", len
);
842 if (len
< dsb
->device
->fraglen
) {
843 /* smaller than a fragment, wait until it gets larger
844 * before we take the mixing overhead */
845 TRACE("mixlen not worth it, deferring mixing\n");
850 /* ok, we know how much to mix, let's go */
851 still_behind
= (adv_done
> primary_done
);
853 slen
= dsb
->device
->buflen
- dsb
->primary_mixpos
;
854 if (slen
> len
) slen
= len
;
855 slen
= DSOUND_MixInBuffer(dsb
, dsb
->primary_mixpos
, slen
);
857 if ((dsb
->primary_mixpos
< dsb
->device
->mixpos
) &&
858 (dsb
->primary_mixpos
+ slen
>= dsb
->device
->mixpos
))
859 still_behind
= FALSE
;
861 dsb
->primary_mixpos
+= slen
; len
-= slen
;
862 dsb
->primary_mixpos
%= dsb
->device
->buflen
;
864 if ((dsb
->state
== STATE_STOPPED
) || !slen
) break;
866 TRACE("new primary_mixpos=%d, primary_advbase=%d\n", dsb
->primary_mixpos
, dsb
->device
->mixpos
);
867 TRACE("mixed data len=%d, still_behind=%d\n", mixlen
-len
, still_behind
);
870 /* check if buffer should be considered complete */
871 if (buf_left
< dsb
->writelead
&&
872 !(dsb
->playflags
& DSBPLAY_LOOPING
)) {
873 dsb
->state
= STATE_STOPPED
;
875 dsb
->last_playpos
= 0;
878 dsb
->need_remix
= FALSE
;
879 DSOUND_CheckEvent(dsb
, buf_left
);
882 /* return how far we think the primary buffer can
883 * advance its underrun detector...*/
884 if (still_behind
) return 0;
885 if ((mixlen
- len
) < primary_done
) return 0;
886 slen
= ((dsb
->primary_mixpos
< dsb
->device
->mixpos
) ?
887 dsb
->device
->buflen
: 0) + dsb
->primary_mixpos
-
890 /* the primary_done and still_behind checks above should have worked */
891 FIXME("problem with advancement calculation (advlen=%d > mixlen=%d)\n", slen
, mixlen
);
898 * For a DirectSoundDevice, go through all the currently playing buffers and
899 * mix them in to the device buffer.
901 * playpos = the current play position in the primary buffer
902 * writepos = the current safe-to-write position in the primary buffer
903 * mixlen = the maximum amount to mix into the primary buffer
904 * (beyond the current writepos)
905 * recover = true if the sound device may have been reset and the write
906 * position in the device buffer changed
908 * Returns: the length beyond the writepos that was mixed to.
910 static DWORD
DSOUND_MixToPrimary(DirectSoundDevice
*device
, DWORD playpos
, DWORD writepos
, DWORD mixlen
, BOOL recover
)
912 INT i
, len
, maxlen
= 0;
913 IDirectSoundBufferImpl
*dsb
;
915 TRACE("(%d,%d,%d,%d)\n", playpos
, writepos
, mixlen
, recover
);
916 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
917 dsb
= device
->buffers
[i
];
919 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
920 TRACE("Checking %p, mixlen=%d\n", dsb
, mixlen
);
921 EnterCriticalSection(&(dsb
->lock
));
922 if (dsb
->state
== STATE_STOPPING
) {
923 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
924 dsb
->state
= STATE_STOPPED
;
925 DSOUND_CheckEvent(dsb
, 0);
927 if ((dsb
->state
== STATE_STARTING
) || recover
) {
928 dsb
->primary_mixpos
= writepos
;
929 dsb
->cvolpan
= dsb
->volpan
;
930 dsb
->need_remix
= FALSE
;
932 else if (dsb
->need_remix
) {
933 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
934 dsb
->cvolpan
= dsb
->volpan
;
935 dsb
->need_remix
= FALSE
;
937 len
= DSOUND_MixOne(dsb
, playpos
, writepos
, mixlen
);
938 if (dsb
->state
== STATE_STARTING
)
939 dsb
->state
= STATE_PLAYING
;
940 maxlen
= (len
> maxlen
) ? len
: maxlen
;
942 LeaveCriticalSection(&(dsb
->lock
));
949 static void DSOUND_MixReset(DirectSoundDevice
*device
, DWORD writepos
)
952 IDirectSoundBufferImpl
*dsb
;
955 TRACE("(%p,%d)\n", device
, writepos
);
957 /* the sound of silence */
958 nfiller
= device
->pwfx
->wBitsPerSample
== 8 ? 128 : 0;
960 /* reset all buffer mix positions */
961 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
962 dsb
= device
->buffers
[i
];
964 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
965 TRACE("Resetting %p\n", dsb
);
966 EnterCriticalSection(&(dsb
->lock
));
967 if (dsb
->state
== STATE_STOPPING
) {
968 dsb
->state
= STATE_STOPPED
;
970 else if (dsb
->state
== STATE_STARTING
) {
973 DSOUND_MixCancel(dsb
, writepos
, FALSE
);
974 dsb
->cvolpan
= dsb
->volpan
;
975 dsb
->need_remix
= FALSE
;
977 LeaveCriticalSection(&(dsb
->lock
));
981 /* wipe out premixed data */
982 if (device
->mixpos
< writepos
) {
983 FillMemory(device
->buffer
+ writepos
, device
->buflen
- writepos
, nfiller
);
984 FillMemory(device
->buffer
, device
->mixpos
, nfiller
);
986 FillMemory(device
->buffer
+ writepos
, device
->mixpos
- writepos
, nfiller
);
989 /* reset primary mix position */
990 device
->mixpos
= writepos
;
993 static void DSOUND_CheckReset(DirectSoundDevice
*device
, DWORD writepos
)
995 TRACE("(%p,%d)\n",device
,writepos
);
996 if (device
->need_remix
) {
997 DSOUND_MixReset(device
, writepos
);
998 device
->need_remix
= FALSE
;
999 /* maximize Half-Life performance */
1000 device
->prebuf
= ds_snd_queue_min
;
1001 device
->precount
= 0;
1004 if (device
->precount
>= 4) {
1005 if (device
->prebuf
< ds_snd_queue_max
)
1007 device
->precount
= 0;
1010 TRACE("premix adjust: %d\n", device
->prebuf
);
1013 void DSOUND_WaveQueue(DirectSoundDevice
*device
, DWORD mixq
)
1015 TRACE("(%p,%d)\n", device
, mixq
);
1016 if (mixq
+ device
->pwqueue
> ds_hel_queue
) mixq
= ds_hel_queue
- device
->pwqueue
;
1017 TRACE("queueing %d buffers, starting at %d\n", mixq
, device
->pwwrite
);
1018 for (; mixq
; mixq
--) {
1019 waveOutWrite(device
->hwo
, device
->pwave
[device
->pwwrite
], sizeof(WAVEHDR
));
1021 if (device
->pwwrite
>= DS_HEL_FRAGS
) device
->pwwrite
= 0;
1026 /* #define SYNC_CALLBACK */
1029 * Perform mixing for a Direct Sound device. That is, go through all the
1030 * secondary buffers (the sound bites currently playing) and mix them in
1031 * to the primary buffer (the device buffer).
1033 static void DSOUND_PerformMix(DirectSoundDevice
*device
)
1039 TRACE("(%p)\n", device
);
1041 /* the sound of silence */
1042 nfiller
= device
->pwfx
->wBitsPerSample
== 8 ? 128 : 0;
1044 /* whether the primary is forced to play even without secondary buffers */
1045 forced
= ((device
->state
== STATE_PLAYING
) || (device
->state
== STATE_STARTING
));
1047 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
1048 BOOL paused
= ((device
->state
== STATE_STOPPED
) || (device
->state
== STATE_STARTING
));
1049 /* FIXME: document variables */
1050 DWORD playpos
, writepos
, inq
, maxq
, frag
;
1051 if (device
->hwbuf
) {
1052 hres
= IDsDriverBuffer_GetPosition(device
->hwbuf
, &playpos
, &writepos
);
1054 WARN("IDsDriverBuffer_GetPosition failed\n");
1057 /* Well, we *could* do Just-In-Time mixing using the writepos,
1058 * but that's a little bit ambitious and unnecessary... */
1059 /* rather add our safety margin to the writepos, if we're playing */
1061 writepos
+= device
->writelead
;
1062 writepos
%= device
->buflen
;
1063 } else writepos
= playpos
;
1065 playpos
= device
->pwplay
* device
->fraglen
;
1068 writepos
+= ds_hel_margin
* device
->fraglen
;
1069 writepos
%= device
->buflen
;
1072 TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
1073 playpos
,writepos
,device
->playpos
,device
->mixpos
,device
->buflen
);
1074 assert(device
->playpos
< device
->buflen
);
1075 /* wipe out just-played sound data */
1076 if (playpos
< device
->playpos
) {
1077 FillMemory(device
->buffer
+ device
->playpos
, device
->buflen
- device
->playpos
, nfiller
);
1078 FillMemory(device
->buffer
, playpos
, nfiller
);
1080 FillMemory(device
->buffer
+ device
->playpos
, playpos
- device
->playpos
, nfiller
);
1082 device
->playpos
= playpos
;
1084 EnterCriticalSection(&(device
->mixlock
));
1086 /* reset mixing if necessary */
1087 DSOUND_CheckReset(device
, writepos
);
1089 /* check how much prebuffering is left */
1090 inq
= device
->mixpos
;
1092 inq
+= device
->buflen
;
1095 /* find the maximum we can prebuffer */
1098 if (maxq
< writepos
)
1099 maxq
+= device
->buflen
;
1101 } else maxq
= device
->buflen
;
1103 /* clip maxq to device->prebuf */
1104 frag
= device
->prebuf
* device
->fraglen
;
1105 if (maxq
> frag
) maxq
= frag
;
1107 /* check for consistency */
1109 /* the playback position must have passed our last
1110 * mixed position, i.e. it's an underrun, or we have
1111 * nothing more to play */
1112 TRACE("reached end of mixed data (inq=%d, maxq=%d)\n", inq
, maxq
);
1114 /* stop the playback now, to allow buffers to refill */
1115 if (device
->state
== STATE_PLAYING
) {
1116 device
->state
= STATE_STARTING
;
1118 else if (device
->state
== STATE_STOPPING
) {
1119 device
->state
= STATE_STOPPED
;
1122 /* how can we have an underrun if we aren't playing? */
1123 WARN("unexpected primary state (%d)\n", device
->state
);
1125 #ifdef SYNC_CALLBACK
1126 /* DSOUND_callback may need this lock */
1127 LeaveCriticalSection(&(device
->mixlock
));
1129 if (DSOUND_PrimaryStop(device
) != DS_OK
)
1130 WARN("DSOUND_PrimaryStop failed\n");
1131 #ifdef SYNC_CALLBACK
1132 EnterCriticalSection(&(device
->mixlock
));
1134 if (device
->hwbuf
) {
1135 /* the Stop is supposed to reset play position to beginning of buffer */
1136 /* unfortunately, OSS is not able to do so, so get current pointer */
1137 hres
= IDsDriverBuffer_GetPosition(device
->hwbuf
, &playpos
, NULL
);
1139 LeaveCriticalSection(&(device
->mixlock
));
1140 WARN("IDsDriverBuffer_GetPosition failed\n");
1144 playpos
= device
->pwplay
* device
->fraglen
;
1147 device
->playpos
= playpos
;
1148 device
->mixpos
= writepos
;
1150 maxq
= device
->buflen
;
1151 if (maxq
> frag
) maxq
= frag
;
1152 FillMemory(device
->buffer
, device
->buflen
, nfiller
);
1157 frag
= DSOUND_MixToPrimary(device
, playpos
, writepos
, maxq
, paused
);
1158 if (forced
) frag
= maxq
- inq
;
1159 device
->mixpos
+= frag
;
1160 device
->mixpos
%= device
->buflen
;
1163 /* buffers have been filled, restart playback */
1164 if (device
->state
== STATE_STARTING
) {
1165 device
->state
= STATE_PLAYING
;
1167 else if (device
->state
== STATE_STOPPED
) {
1168 /* the dsound is supposed to play if there's something to play
1169 * even if it is reported as stopped, so don't let this confuse you */
1170 device
->state
= STATE_STOPPING
;
1172 LeaveCriticalSection(&(device
->mixlock
));
1174 if (DSOUND_PrimaryPlay(device
) != DS_OK
)
1175 WARN("DSOUND_PrimaryPlay failed\n");
1177 TRACE("starting playback\n");
1181 LeaveCriticalSection(&(device
->mixlock
));
1183 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
1184 if (device
->state
== STATE_STARTING
) {
1185 if (DSOUND_PrimaryPlay(device
) != DS_OK
)
1186 WARN("DSOUND_PrimaryPlay failed\n");
1188 device
->state
= STATE_PLAYING
;
1190 else if (device
->state
== STATE_STOPPING
) {
1191 if (DSOUND_PrimaryStop(device
) != DS_OK
)
1192 WARN("DSOUND_PrimaryStop failed\n");
1194 device
->state
= STATE_STOPPED
;
1199 void CALLBACK
DSOUND_timer(UINT timerID
, UINT msg
, DWORD_PTR dwUser
,
1200 DWORD_PTR dw1
, DWORD_PTR dw2
)
1202 DirectSoundDevice
* device
= (DirectSoundDevice
*)dwUser
;
1203 DWORD start_time
= GetTickCount();
1205 TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID
,msg
,dwUser
,dw1
,dw2
);
1206 TRACE("entering at %d\n", start_time
);
1208 if (DSOUND_renderer
[device
->drvdesc
.dnDevNode
] != device
) {
1209 ERR("dsound died without killing us?\n");
1210 timeKillEvent(timerID
);
1211 timeEndPeriod(DS_TIME_RES
);
1215 RtlAcquireResourceShared(&(device
->buffer_list_lock
), TRUE
);
1218 DSOUND_PerformMix(device
);
1220 RtlReleaseResource(&(device
->buffer_list_lock
));
1222 end_time
= GetTickCount();
1223 TRACE("completed processing at %d, duration = %d\n", end_time
, end_time
- start_time
);
1226 void CALLBACK
DSOUND_callback(HWAVEOUT hwo
, UINT msg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
1228 DirectSoundDevice
* device
= (DirectSoundDevice
*)dwUser
;
1229 TRACE("(%p,%x,%x,%x,%x)\n",hwo
,msg
,dwUser
,dw1
,dw2
);
1230 TRACE("entering at %d, msg=%08x(%s)\n", GetTickCount(), msg
,
1231 msg
==MM_WOM_DONE
? "MM_WOM_DONE" : msg
==MM_WOM_CLOSE
? "MM_WOM_CLOSE" :
1232 msg
==MM_WOM_OPEN
? "MM_WOM_OPEN" : "UNKNOWN");
1233 if (msg
== MM_WOM_DONE
) {
1234 DWORD inq
, mixq
, fraglen
, buflen
, pwplay
, playpos
, mixpos
;
1235 if (device
->pwqueue
== (DWORD
)-1) {
1236 TRACE("completed due to reset\n");
1239 /* it could be a bad idea to enter critical section here... if there's lock contention,
1240 * the resulting scheduling delays might obstruct the winmm player thread */
1241 #ifdef SYNC_CALLBACK
1242 EnterCriticalSection(&(device
->mixlock
));
1244 /* retrieve current values */
1245 fraglen
= device
->fraglen
;
1246 buflen
= device
->buflen
;
1247 pwplay
= device
->pwplay
;
1248 playpos
= pwplay
* fraglen
;
1249 mixpos
= device
->mixpos
;
1250 /* check remaining mixed data */
1251 inq
= ((mixpos
< playpos
) ? buflen
: 0) + mixpos
- playpos
;
1252 mixq
= inq
/ fraglen
;
1253 if ((inq
- (mixq
* fraglen
)) > 0) mixq
++;
1254 /* complete the playing buffer */
1255 TRACE("done playing primary pos=%d\n", playpos
);
1257 if (pwplay
>= DS_HEL_FRAGS
) pwplay
= 0;
1258 /* write new values */
1259 device
->pwplay
= pwplay
;
1261 /* queue new buffer if we have data for it */
1262 if (inq
>1) DSOUND_WaveQueue(device
, inq
-1);
1263 #ifdef SYNC_CALLBACK
1264 LeaveCriticalSection(&(device
->mixlock
));
1267 TRACE("completed\n");