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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
26 #include <sys/fcntl.h>
32 #include <math.h> /* Insomnia - pow() function */
42 #include "wine/windef16.h"
43 #include "wine/debug.h"
46 #include "dsound_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
50 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan
)
53 TRACE("(%p)\n",volpan
);
55 /* the AmpFactors are expressed in 16.16 fixed point */
56 volpan
->dwVolAmpFactor
= (ULONG
) (pow(2.0, volpan
->lVolume
/ 600.0) * 65536);
57 /* FIXME: dwPan{Left|Right}AmpFactor */
59 /* FIXME: use calculated vol and pan ampfactors */
60 temp
= (double) (volpan
->lVolume
- (volpan
->lPan
> 0 ? volpan
->lPan
: 0));
61 volpan
->dwTotalLeftAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 65536);
62 temp
= (double) (volpan
->lVolume
+ (volpan
->lPan
< 0 ? volpan
->lPan
: 0));
63 volpan
->dwTotalRightAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 65536);
65 TRACE("left = %lx, right = %lx\n", volpan
->dwTotalLeftAmpFactor
, volpan
->dwTotalRightAmpFactor
);
68 void DSOUND_RecalcFormat(IDirectSoundBufferImpl
*dsb
)
72 sw
= dsb
->wfx
.nChannels
* (dsb
->wfx
.wBitsPerSample
/ 8);
73 /* calculate the 10ms write lead */
74 dsb
->writelead
= (dsb
->freq
/ 100) * sw
;
77 void DSOUND_CheckEvent(IDirectSoundBufferImpl
*dsb
, int len
)
81 LPDSBPOSITIONNOTIFY event
;
83 if (!dsb
->notify
|| dsb
->notify
->nrofnotifies
== 0)
86 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
87 dsb
, dsb
->buflen
, dsb
->playpos
, len
);
88 for (i
= 0; i
< dsb
->notify
->nrofnotifies
; i
++) {
89 event
= dsb
->notify
->notifies
+ i
;
90 offset
= event
->dwOffset
;
91 TRACE("checking %d, position %ld, event = %p\n",
92 i
, offset
, event
->hEventNotify
);
93 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
94 /* OK. [Inside DirectX, p274] */
96 /* This also means we can't sort the entries by offset, */
97 /* because DSBPN_OFFSETSTOP == -1 */
98 if (offset
== DSBPN_OFFSETSTOP
) {
99 if (dsb
->state
== STATE_STOPPED
) {
100 SetEvent(event
->hEventNotify
);
101 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
106 if ((dsb
->playpos
+ len
) >= dsb
->buflen
) {
107 if ((offset
< ((dsb
->playpos
+ len
) % dsb
->buflen
)) ||
108 (offset
>= dsb
->playpos
)) {
109 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
110 SetEvent(event
->hEventNotify
);
113 if ((offset
>= dsb
->playpos
) && (offset
< (dsb
->playpos
+ len
))) {
114 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
115 SetEvent(event
->hEventNotify
);
121 /* WAV format info can be found at:
123 * http://www.cwi.nl/ftp/audio/AudioFormats.part2
124 * ftp://ftp.cwi.nl/pub/audio/RIFF-format
126 * Import points to remember:
127 * 8-bit WAV is unsigned
128 * 16-bit WAV is signed
130 /* Use the same formulas as pcmconverter.c */
131 static inline INT16
cvtU8toS16(BYTE b
)
133 return (short)((b
+(b
<< 8))-32768);
136 static inline BYTE
cvtS16toU8(INT16 s
)
138 return (s
>> 8) ^ (unsigned char)0x80;
141 static inline void cp_fields(const IDirectSoundBufferImpl
*dsb
, BYTE
*ibuf
, BYTE
*obuf
)
145 if (dsb
->wfx
.wBitsPerSample
== 8) {
146 if (dsound
->wfx
.wBitsPerSample
== 8 &&
147 dsound
->wfx
.nChannels
== dsb
->wfx
.nChannels
) {
148 /* avoid needless 8->16->8 conversion */
150 if (dsb
->wfx
.nChannels
==2)
154 fl
= cvtU8toS16(*ibuf
);
155 fr
= (dsb
->wfx
.nChannels
==2 ? cvtU8toS16(*(ibuf
+ 1)) : fl
);
157 fl
= *((INT16
*)ibuf
);
158 fr
= (dsb
->wfx
.nChannels
==2 ? *(((INT16
*)ibuf
) + 1) : fl
);
161 if (dsound
->wfx
.nChannels
== 2) {
162 if (dsound
->wfx
.wBitsPerSample
== 8) {
163 *obuf
= cvtS16toU8(fl
);
164 *(obuf
+ 1) = cvtS16toU8(fr
);
167 if (dsound
->wfx
.wBitsPerSample
== 16) {
168 *((INT16
*)obuf
) = fl
;
169 *(((INT16
*)obuf
) + 1) = fr
;
173 if (dsound
->wfx
.nChannels
== 1) {
175 if (dsound
->wfx
.wBitsPerSample
== 8) {
176 *obuf
= cvtS16toU8(fl
);
179 if (dsound
->wfx
.wBitsPerSample
== 16) {
180 *((INT16
*)obuf
) = fl
;
186 /* Now with PerfectPitch (tm) technology */
187 static INT
DSOUND_MixerNorm(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
189 INT i
, size
, ipos
, ilen
;
191 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
192 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
194 ibp
= dsb
->buffer
->memory
+ dsb
->buf_mixpos
;
197 TRACE("(%p, %p, %p), buf_mixpos=%ld\n", dsb
, ibp
, obp
, dsb
->buf_mixpos
);
198 /* Check for the best case */
199 if ((dsb
->freq
== dsb
->dsound
->wfx
.nSamplesPerSec
) &&
200 (dsb
->wfx
.wBitsPerSample
== dsb
->dsound
->wfx
.wBitsPerSample
) &&
201 (dsb
->wfx
.nChannels
== dsb
->dsound
->wfx
.nChannels
)) {
202 DWORD bytesleft
= dsb
->buflen
- dsb
->buf_mixpos
;
203 TRACE("(%p) Best case\n", dsb
);
204 if (len
<= bytesleft
)
205 memcpy(obp
, ibp
, len
);
207 memcpy(obp
, ibp
, bytesleft
);
208 memcpy(obp
+ bytesleft
, dsb
->buffer
->memory
, len
- bytesleft
);
213 /* Check for same sample rate */
214 if (dsb
->freq
== dsb
->dsound
->wfx
.nSamplesPerSec
) {
215 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb
,
216 dsb
->freq
, dsb
->dsound
->wfx
.nSamplesPerSec
);
218 for (i
= 0; i
< len
; i
+= oAdvance
) {
219 cp_fields(dsb
, ibp
, obp
);
223 if (ibp
>= (BYTE
*)(dsb
->buffer
->memory
+ dsb
->buflen
))
224 ibp
= dsb
->buffer
->memory
; /* wrap */
229 /* Mix in different sample rates */
231 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
232 /* Patent Pending :-] */
234 /* Patent enhancements (c) 2000 Ove KÃ¥ven,
235 * TransGaming Technologies Inc. */
237 /* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
238 dsb, dsb->freq, dsb->dsound->wfx.nSamplesPerSec); */
240 size
= len
/ oAdvance
;
242 ipos
= dsb
->buf_mixpos
;
243 for (i
= 0; i
< size
; i
++) {
244 cp_fields(dsb
, (dsb
->buffer
->memory
+ ipos
), obp
);
246 dsb
->freqAcc
+= dsb
->freqAdjust
;
247 if (dsb
->freqAcc
>= (1<<DSOUND_FREQSHIFT
)) {
248 ULONG adv
= (dsb
->freqAcc
>>DSOUND_FREQSHIFT
) * iAdvance
;
249 dsb
->freqAcc
&= (1<<DSOUND_FREQSHIFT
)-1;
250 ipos
+= adv
; ilen
+= adv
;
251 while (ipos
>= dsb
->buflen
)
258 static void DSOUND_MixerVol(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
262 INT16
*bps
= (INT16
*) buf
;
264 TRACE("(%p,%p,%d)\n",dsb
,buf
,len
);
265 TRACE("left = %lx, right = %lx\n", dsb
->cvolpan
.dwTotalLeftAmpFactor
,
266 dsb
->cvolpan
.dwTotalRightAmpFactor
);
268 if ((!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) || (dsb
->cvolpan
.lPan
== 0)) &&
269 (!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) || (dsb
->cvolpan
.lVolume
== 0)) &&
270 !(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
271 return; /* Nothing to do */
273 /* If we end up with some bozo coder using panning or 3D sound */
274 /* with a mono primary buffer, it could sound very weird using */
275 /* this method. Oh well, tough patooties. */
277 switch (dsb
->dsound
->wfx
.wBitsPerSample
) {
279 /* 8-bit WAV is unsigned, but we need to operate */
280 /* on signed data for this to work properly */
281 switch (dsb
->dsound
->wfx
.nChannels
) {
283 for (i
= 0; i
< len
; i
++) {
284 INT val
= *bpc
- 128;
285 val
= (val
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
291 for (i
= 0; i
< len
; i
+=2) {
292 INT val
= *bpc
- 128;
293 val
= (val
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
296 val
= (val
* dsb
->cvolpan
.dwTotalRightAmpFactor
) >> 16;
302 FIXME("doesn't support %d channels\n", dsb
->dsound
->wfx
.nChannels
);
307 /* 16-bit WAV is signed -- much better */
308 switch (dsb
->dsound
->wfx
.nChannels
) {
310 for (i
= 0; i
< len
; i
+= 2) {
311 *bps
= (*bps
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
316 for (i
= 0; i
< len
; i
+= 4) {
317 *bps
= (*bps
* dsb
->cvolpan
.dwTotalLeftAmpFactor
) >> 16;
319 *bps
= (*bps
* dsb
->cvolpan
.dwTotalRightAmpFactor
) >> 16;
324 FIXME("doesn't support %d channels\n", dsb
->dsound
->wfx
.nChannels
);
329 FIXME("doesn't support %d bit samples\n", dsb
->dsound
->wfx
.wBitsPerSample
);
334 static void *tmp_buffer
;
335 static size_t tmp_buffer_len
= 0;
337 static void *DSOUND_tmpbuffer(size_t len
)
339 if (len
>tmp_buffer_len
) {
340 void *new_buffer
= realloc(tmp_buffer
, len
);
342 tmp_buffer
= new_buffer
;
343 tmp_buffer_len
= len
;
350 static DWORD
DSOUND_MixInBuffer(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD fraglen
)
352 INT i
, len
, ilen
, temp
, field
, nBlockAlign
;
353 INT advance
= dsb
->dsound
->wfx
.wBitsPerSample
>> 3;
354 BYTE
*buf
, *ibuf
, *obuf
;
355 INT16
*ibufs
, *obufs
;
357 TRACE("%p,%ld,%ld)\n",dsb
,writepos
,fraglen
);
360 if (!(dsb
->playflags
& DSBPLAY_LOOPING
)) {
361 temp
= MulDiv(dsb
->dsound
->wfx
.nAvgBytesPerSec
, dsb
->buflen
,
362 dsb
->nAvgBytesPerSec
) -
363 MulDiv(dsb
->dsound
->wfx
.nAvgBytesPerSec
, dsb
->buf_mixpos
,
364 dsb
->nAvgBytesPerSec
);
365 len
= (len
> temp
) ? temp
: len
;
367 nBlockAlign
= dsb
->dsound
->wfx
.nBlockAlign
;
368 len
= len
/ nBlockAlign
* nBlockAlign
; /* data alignment */
371 /* This should only happen if we aren't looping and temp < nBlockAlign */
375 /* Been seeing segfaults in malloc() for some reason... */
376 TRACE("allocating buffer (size = %d)\n", len
);
377 if ((buf
= ibuf
= (BYTE
*) DSOUND_tmpbuffer(len
)) == NULL
)
380 TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb
, len
, writepos
);
382 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
383 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
384 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) ||
385 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
386 DSOUND_MixerVol(dsb
, ibuf
, len
);
388 obuf
= dsb
->dsound
->buffer
+ writepos
;
389 for (i
= 0; i
< len
; i
+= advance
) {
390 obufs
= (INT16
*) obuf
;
391 ibufs
= (INT16
*) ibuf
;
392 if (dsb
->dsound
->wfx
.wBitsPerSample
== 8) {
393 /* 8-bit WAV is unsigned */
394 field
= (*ibuf
- 128);
395 field
+= (*obuf
- 128);
396 field
= field
> 127 ? 127 : field
;
397 field
= field
< -128 ? -128 : field
;
400 /* 16-bit WAV is signed */
403 field
= field
> 32767 ? 32767 : field
;
404 field
= field
< -32768 ? -32768 : field
;
409 if (obuf
>= (BYTE
*)(dsb
->dsound
->buffer
+ dsb
->dsound
->buflen
))
410 obuf
= dsb
->dsound
->buffer
;
414 if (dsb
->leadin
&& (dsb
->startpos
> dsb
->buf_mixpos
) && (dsb
->startpos
<= dsb
->buf_mixpos
+ ilen
)) {
415 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
416 * not the MIX position... but if the sound buffer is bigger than our prebuffering
417 * (which must be the case for the streaming buffers that need this hack anyway)
418 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
422 dsb
->buf_mixpos
+= ilen
;
424 if (dsb
->buf_mixpos
>= dsb
->buflen
) {
425 if (dsb
->playflags
& DSBPLAY_LOOPING
) {
427 while (dsb
->buf_mixpos
>= dsb
->buflen
)
428 dsb
->buf_mixpos
-= dsb
->buflen
;
429 if (dsb
->leadin
&& (dsb
->startpos
<= dsb
->buf_mixpos
))
430 dsb
->leadin
= FALSE
; /* HACK: see above */
437 static void DSOUND_PhaseCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD len
)
439 INT i
, ilen
, field
, nBlockAlign
;
440 INT advance
= dsb
->dsound
->wfx
.wBitsPerSample
>> 3;
441 BYTE
*buf
, *ibuf
, *obuf
;
442 INT16
*ibufs
, *obufs
;
444 nBlockAlign
= dsb
->dsound
->wfx
.nBlockAlign
;
445 len
= len
/ nBlockAlign
* nBlockAlign
; /* data alignment */
447 TRACE("allocating buffer (size = %ld)\n", len
);
448 if ((buf
= ibuf
= (BYTE
*) DSOUND_tmpbuffer(len
)) == NULL
)
451 TRACE("PhaseCancel (%p) len = %ld, dest = %ld\n", dsb
, len
, writepos
);
453 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
454 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
455 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) ||
456 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
457 DSOUND_MixerVol(dsb
, ibuf
, len
);
459 /* subtract instead of add, to phase out premixed data */
460 obuf
= dsb
->dsound
->buffer
+ writepos
;
461 for (i
= 0; i
< len
; i
+= advance
) {
462 obufs
= (INT16
*) obuf
;
463 ibufs
= (INT16
*) ibuf
;
464 if (dsb
->dsound
->wfx
.wBitsPerSample
== 8) {
465 /* 8-bit WAV is unsigned */
466 field
= (*ibuf
- 128);
467 field
-= (*obuf
- 128);
468 field
= field
> 127 ? 127 : field
;
469 field
= field
< -128 ? -128 : field
;
472 /* 16-bit WAV is signed */
475 field
= field
> 32767 ? 32767 : field
;
476 field
= field
< -32768 ? -32768 : field
;
481 if (obuf
>= (BYTE
*)(dsb
->dsound
->buffer
+ dsb
->dsound
->buflen
))
482 obuf
= dsb
->dsound
->buffer
;
487 static void DSOUND_MixCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, BOOL cancel
)
489 DWORD size
, flen
, len
, npos
, nlen
;
490 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
491 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
492 /* determine amount of premixed data to cancel */
494 ((dsb
->primary_mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
495 dsb
->primary_mixpos
- writepos
;
497 TRACE("(%p, %ld), buf_mixpos=%ld\n", dsb
, writepos
, dsb
->buf_mixpos
);
499 /* backtrack the mix position */
500 size
= primary_done
/ oAdvance
;
501 flen
= size
* dsb
->freqAdjust
;
502 len
= (flen
>> DSOUND_FREQSHIFT
) * iAdvance
;
503 flen
&= (1<<DSOUND_FREQSHIFT
)-1;
504 while (dsb
->freqAcc
< flen
) {
506 dsb
->freqAcc
+= 1<<DSOUND_FREQSHIFT
;
509 npos
= ((dsb
->buf_mixpos
< len
) ? dsb
->buflen
: 0) +
510 dsb
->buf_mixpos
- len
;
511 if (dsb
->leadin
&& (dsb
->startpos
> npos
) && (dsb
->startpos
<= npos
+ len
)) {
512 /* stop backtracking at startpos */
513 npos
= dsb
->startpos
;
514 len
= ((dsb
->buf_mixpos
< npos
) ? dsb
->buflen
: 0) +
515 dsb
->buf_mixpos
- npos
;
517 nlen
= len
/ dsb
->wfx
.nBlockAlign
;
518 nlen
= ((nlen
<< DSOUND_FREQSHIFT
) + flen
) / dsb
->freqAdjust
;
519 nlen
*= dsb
->dsound
->wfx
.nBlockAlign
;
521 ((dsb
->primary_mixpos
< nlen
) ? dsb
->dsound
->buflen
: 0) +
522 dsb
->primary_mixpos
- nlen
;
525 dsb
->freqAcc
-= flen
;
526 dsb
->buf_mixpos
= npos
;
527 dsb
->primary_mixpos
= writepos
;
529 TRACE("new buf_mixpos=%ld, primary_mixpos=%ld (len=%ld)\n",
530 dsb
->buf_mixpos
, dsb
->primary_mixpos
, len
);
532 if (cancel
) DSOUND_PhaseCancel(dsb
, writepos
, len
);
535 void DSOUND_MixCancelAt(IDirectSoundBufferImpl
*dsb
, DWORD buf_writepos
)
538 DWORD i
, size
, flen
, len
, npos
, nlen
;
539 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
540 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
541 /* determine amount of premixed data to cancel */
543 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
544 dsb
->buf_mixpos
- buf_writepos
;
547 WARN("(%p, %ld), buf_mixpos=%ld\n", dsb
, buf_writepos
, dsb
->buf_mixpos
);
548 /* since this is not implemented yet, just cancel *ALL* prebuffering for now
549 * (which is faster anyway when there's only a single secondary buffer) */
550 dsb
->dsound
->need_remix
= TRUE
;
553 void DSOUND_ForceRemix(IDirectSoundBufferImpl
*dsb
)
555 EnterCriticalSection(&dsb
->lock
);
556 if (dsb
->state
== STATE_PLAYING
) {
557 #if 0 /* this may not be quite reliable yet */
558 dsb
->need_remix
= TRUE
;
560 dsb
->dsound
->need_remix
= TRUE
;
563 LeaveCriticalSection(&dsb
->lock
);
566 static DWORD
DSOUND_MixOne(IDirectSoundBufferImpl
*dsb
, DWORD playpos
, DWORD writepos
, DWORD mixlen
)
569 /* determine this buffer's write position */
570 DWORD buf_writepos
= DSOUND_CalcPlayPosition(dsb
, dsb
->state
& dsb
->dsound
->state
, writepos
,
571 writepos
, dsb
->primary_mixpos
, dsb
->buf_mixpos
);
572 /* determine how much already-mixed data exists */
574 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
575 dsb
->buf_mixpos
- buf_writepos
;
577 ((dsb
->primary_mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
578 dsb
->primary_mixpos
- writepos
;
580 ((dsb
->dsound
->mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
581 dsb
->dsound
->mixpos
- writepos
;
583 ((buf_writepos
< dsb
->playpos
) ? dsb
->buflen
: 0) +
584 buf_writepos
- dsb
->playpos
;
585 DWORD buf_left
= dsb
->buflen
- buf_writepos
;
588 TRACE("buf_writepos=%ld, primary_writepos=%ld\n", buf_writepos
, writepos
);
589 TRACE("buf_done=%ld, primary_done=%ld\n", buf_done
, primary_done
);
590 TRACE("buf_mixpos=%ld, primary_mixpos=%ld, mixlen=%ld\n", dsb
->buf_mixpos
, dsb
->primary_mixpos
,
592 TRACE("looping=%ld, startpos=%ld, leadin=%ld\n", dsb
->playflags
, dsb
->startpos
, dsb
->leadin
);
594 /* check for notification positions */
595 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPOSITIONNOTIFY
&&
596 dsb
->state
!= STATE_STARTING
) {
597 DSOUND_CheckEvent(dsb
, played
);
600 /* save write position for non-GETCURRENTPOSITION2... */
601 dsb
->playpos
= buf_writepos
;
603 /* check whether CalcPlayPosition detected a mixing underrun */
604 if ((buf_done
== 0) && (dsb
->primary_mixpos
!= writepos
)) {
605 /* it did, but did we have more to play? */
606 if ((dsb
->playflags
& DSBPLAY_LOOPING
) ||
607 (dsb
->buf_mixpos
< dsb
->buflen
)) {
608 /* yes, have to recover */
609 ERR("underrun on sound buffer %p\n", dsb
);
610 TRACE("recovering from underrun: primary_mixpos=%ld\n", writepos
);
612 dsb
->primary_mixpos
= writepos
;
615 /* determine how far ahead we should mix */
616 if (((dsb
->playflags
& DSBPLAY_LOOPING
) ||
617 (dsb
->leadin
&& (dsb
->probably_valid_to
!= 0))) &&
618 !(dsb
->dsbd
.dwFlags
& DSBCAPS_STATIC
)) {
619 /* if this is a streaming buffer, it typically means that
620 * we should defer mixing past probably_valid_to as long
621 * as we can, to avoid unnecessary remixing */
622 /* the heavy-looking calculations shouldn't be that bad,
623 * as any game isn't likely to be have more than 1 or 2
624 * streaming buffers in use at any time anyway... */
625 DWORD probably_valid_left
=
626 (dsb
->probably_valid_to
== (DWORD
)-1) ? dsb
->buflen
:
627 ((dsb
->probably_valid_to
< buf_writepos
) ? dsb
->buflen
: 0) +
628 dsb
->probably_valid_to
- buf_writepos
;
629 /* check for leadin condition */
630 if ((probably_valid_left
== 0) &&
631 (dsb
->probably_valid_to
== dsb
->startpos
) &&
633 probably_valid_left
= dsb
->buflen
;
634 TRACE("streaming buffer probably_valid_to=%ld, probably_valid_left=%ld\n",
635 dsb
->probably_valid_to
, probably_valid_left
);
636 /* check whether the app's time is already up */
637 if (probably_valid_left
< dsb
->writelead
) {
638 WARN("probably_valid_to now within writelead, possible streaming underrun\n");
639 /* once we pass the point of no return,
640 * no reason to hold back anymore */
641 dsb
->probably_valid_to
= (DWORD
)-1;
642 /* we just have to go ahead and mix what we have,
643 * there's no telling what the app is thinking anyway */
645 /* adjust for our frequency and our sample size */
646 probably_valid_left
= MulDiv(probably_valid_left
,
647 1 << DSOUND_FREQSHIFT
,
648 dsb
->wfx
.nBlockAlign
* dsb
->freqAdjust
) *
649 dsb
->dsound
->wfx
.nBlockAlign
;
650 /* check whether to clip mix_len */
651 if (probably_valid_left
< mixlen
) {
652 TRACE("clipping to probably_valid_left=%ld\n", probably_valid_left
);
653 mixlen
= probably_valid_left
;
657 /* cut mixlen with what's already been mixed */
658 if (mixlen
< primary_done
) {
659 /* huh? and still CalcPlayPosition didn't
660 * detect an underrun? */
661 FIXME("problem with underrun detection (mixlen=%ld < primary_done=%ld)\n", mixlen
, primary_done
);
664 len
= mixlen
- primary_done
;
665 TRACE("remaining mixlen=%ld\n", len
);
667 if (len
< dsb
->dsound
->fraglen
) {
668 /* smaller than a fragment, wait until it gets larger
669 * before we take the mixing overhead */
670 TRACE("mixlen not worth it, deferring mixing\n");
675 /* ok, we know how much to mix, let's go */
676 still_behind
= (adv_done
> primary_done
);
678 slen
= dsb
->dsound
->buflen
- dsb
->primary_mixpos
;
679 if (slen
> len
) slen
= len
;
680 slen
= DSOUND_MixInBuffer(dsb
, dsb
->primary_mixpos
, slen
);
682 if ((dsb
->primary_mixpos
< dsb
->dsound
->mixpos
) &&
683 (dsb
->primary_mixpos
+ slen
>= dsb
->dsound
->mixpos
))
684 still_behind
= FALSE
;
686 dsb
->primary_mixpos
+= slen
; len
-= slen
;
687 while (dsb
->primary_mixpos
>= dsb
->dsound
->buflen
)
688 dsb
->primary_mixpos
-= dsb
->dsound
->buflen
;
690 if ((dsb
->state
== STATE_STOPPED
) || !slen
) break;
692 TRACE("new primary_mixpos=%ld, primary_advbase=%ld\n", dsb
->primary_mixpos
, dsb
->dsound
->mixpos
);
693 TRACE("mixed data len=%ld, still_behind=%d\n", mixlen
-len
, still_behind
);
696 /* check if buffer should be considered complete */
697 if (buf_left
< dsb
->writelead
&&
698 !(dsb
->playflags
& DSBPLAY_LOOPING
)) {
699 dsb
->state
= STATE_STOPPED
;
701 dsb
->last_playpos
= 0;
704 DSOUND_CheckEvent(dsb
, buf_left
);
707 /* return how far we think the primary buffer can
708 * advance its underrun detector...*/
709 if (still_behind
) return 0;
710 if ((mixlen
- len
) < primary_done
) return 0;
711 slen
= ((dsb
->primary_mixpos
< dsb
->dsound
->mixpos
) ?
712 dsb
->dsound
->buflen
: 0) + dsb
->primary_mixpos
-
715 /* the primary_done and still_behind checks above should have worked */
716 FIXME("problem with advancement calculation (advlen=%ld > mixlen=%ld)\n", slen
, mixlen
);
722 static DWORD
DSOUND_MixToPrimary(DWORD playpos
, DWORD writepos
, DWORD mixlen
, BOOL recover
)
724 INT i
, len
, maxlen
= 0;
725 IDirectSoundBufferImpl
*dsb
;
727 TRACE("(%ld,%ld,%ld)\n", playpos
, writepos
, mixlen
);
728 for (i
= dsound
->nrofbuffers
- 1; i
>= 0; i
--) {
729 dsb
= dsound
->buffers
[i
];
731 if (!dsb
|| !dsb
->lpVtbl
)
733 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
734 TRACE("Checking %p, mixlen=%ld\n", dsb
, mixlen
);
735 EnterCriticalSection(&(dsb
->lock
));
736 if (dsb
->state
== STATE_STOPPING
) {
737 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
738 dsb
->state
= STATE_STOPPED
;
739 DSOUND_CheckEvent(dsb
, 0);
741 if ((dsb
->state
== STATE_STARTING
) || recover
) {
742 dsb
->primary_mixpos
= writepos
;
743 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
744 dsb
->need_remix
= FALSE
;
746 else if (dsb
->need_remix
) {
747 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
748 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
749 dsb
->need_remix
= FALSE
;
751 len
= DSOUND_MixOne(dsb
, playpos
, writepos
, mixlen
);
752 if (dsb
->state
== STATE_STARTING
)
753 dsb
->state
= STATE_PLAYING
;
754 maxlen
= (len
> maxlen
) ? len
: maxlen
;
756 LeaveCriticalSection(&(dsb
->lock
));
763 static void DSOUND_MixReset(DWORD writepos
)
766 IDirectSoundBufferImpl
*dsb
;
769 TRACE("(%ld)\n", writepos
);
771 /* the sound of silence */
772 nfiller
= dsound
->wfx
.wBitsPerSample
== 8 ? 128 : 0;
774 /* reset all buffer mix positions */
775 for (i
= dsound
->nrofbuffers
- 1; i
>= 0; i
--) {
776 dsb
= dsound
->buffers
[i
];
778 if (!dsb
|| !dsb
->lpVtbl
)
780 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
781 TRACE("Resetting %p\n", dsb
);
782 EnterCriticalSection(&(dsb
->lock
));
783 if (dsb
->state
== STATE_STOPPING
) {
784 dsb
->state
= STATE_STOPPED
;
786 else if (dsb
->state
== STATE_STARTING
) {
789 DSOUND_MixCancel(dsb
, writepos
, FALSE
);
790 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
791 dsb
->need_remix
= FALSE
;
793 LeaveCriticalSection(&(dsb
->lock
));
797 /* wipe out premixed data */
798 if (dsound
->mixpos
< writepos
) {
799 memset(dsound
->buffer
+ writepos
, nfiller
, dsound
->buflen
- writepos
);
800 memset(dsound
->buffer
, nfiller
, dsound
->mixpos
);
802 memset(dsound
->buffer
+ writepos
, nfiller
, dsound
->mixpos
- writepos
);
805 /* reset primary mix position */
806 dsound
->mixpos
= writepos
;
809 static void DSOUND_CheckReset(IDirectSoundImpl
*dsound
, DWORD writepos
)
811 if (dsound
->need_remix
) {
812 DSOUND_MixReset(writepos
);
813 dsound
->need_remix
= FALSE
;
814 /* maximize Half-Life performance */
815 dsound
->prebuf
= ds_snd_queue_min
;
816 dsound
->precount
= 0;
819 if (dsound
->precount
>= 4) {
820 if (dsound
->prebuf
< ds_snd_queue_max
)
822 dsound
->precount
= 0;
825 TRACE("premix adjust: %d\n", dsound
->prebuf
);
828 void DSOUND_WaveQueue(IDirectSoundImpl
*dsound
, DWORD mixq
)
830 if (mixq
+ dsound
->pwqueue
> ds_hel_queue
) mixq
= ds_hel_queue
- dsound
->pwqueue
;
831 TRACE("queueing %ld buffers, starting at %d\n", mixq
, dsound
->pwwrite
);
832 for (; mixq
; mixq
--) {
833 waveOutWrite(dsound
->hwo
, dsound
->pwave
[dsound
->pwwrite
], sizeof(WAVEHDR
));
835 if (dsound
->pwwrite
>= DS_HEL_FRAGS
) dsound
->pwwrite
= 0;
840 /* #define SYNC_CALLBACK */
842 void DSOUND_PerformMix(void)
850 /* the sound of silence */
851 nfiller
= dsound
->wfx
.wBitsPerSample
== 8 ? 128 : 0;
853 /* whether the primary is forced to play even without secondary buffers */
854 forced
= ((dsound
->state
== STATE_PLAYING
) || (dsound
->state
== STATE_STARTING
));
856 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
857 BOOL paused
= ((dsound
->state
== STATE_STOPPED
) || (dsound
->state
== STATE_STARTING
));
858 /* FIXME: document variables */
859 DWORD playpos
, writepos
, inq
, maxq
, frag
;
861 hres
= IDsDriverBuffer_GetPosition(dsound
->hwbuf
, &playpos
, &writepos
);
863 WARN("IDsDriverBuffer_GetPosition failed\n");
866 /* Well, we *could* do Just-In-Time mixing using the writepos,
867 * but that's a little bit ambitious and unnecessary... */
868 /* rather add our safety margin to the writepos, if we're playing */
870 writepos
+= dsound
->writelead
;
871 while (writepos
>= dsound
->buflen
)
872 writepos
-= dsound
->buflen
;
873 } else writepos
= playpos
;
875 playpos
= dsound
->pwplay
* dsound
->fraglen
;
878 writepos
+= ds_hel_margin
* dsound
->fraglen
;
879 while (writepos
>= dsound
->buflen
)
880 writepos
-= dsound
->buflen
;
883 TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld, buflen=%ld\n",
884 playpos
,writepos
,dsound
->playpos
,dsound
->mixpos
,dsound
->buflen
);
885 assert(dsound
->playpos
< dsound
->buflen
);
886 /* wipe out just-played sound data */
887 if (playpos
< dsound
->playpos
) {
888 memset(dsound
->buffer
+ dsound
->playpos
, nfiller
, dsound
->buflen
- dsound
->playpos
);
889 memset(dsound
->buffer
, nfiller
, playpos
);
891 memset(dsound
->buffer
+ dsound
->playpos
, nfiller
, playpos
- dsound
->playpos
);
893 dsound
->playpos
= playpos
;
895 EnterCriticalSection(&(dsound
->mixlock
));
897 /* reset mixing if necessary */
898 DSOUND_CheckReset(dsound
, writepos
);
900 /* check how much prebuffering is left */
901 inq
= dsound
->mixpos
;
903 inq
+= dsound
->buflen
;
906 /* find the maximum we can prebuffer */
910 maxq
+= dsound
->buflen
;
912 } else maxq
= dsound
->buflen
;
914 /* clip maxq to dsound->prebuf */
915 frag
= dsound
->prebuf
* dsound
->fraglen
;
916 if (maxq
> frag
) maxq
= frag
;
918 /* check for consistency */
920 /* the playback position must have passed our last
921 * mixed position, i.e. it's an underrun, or we have
922 * nothing more to play */
923 TRACE("reached end of mixed data (inq=%ld, maxq=%ld)\n", inq
, maxq
);
925 /* stop the playback now, to allow buffers to refill */
926 if (dsound
->state
== STATE_PLAYING
) {
927 dsound
->state
= STATE_STARTING
;
929 else if (dsound
->state
== STATE_STOPPING
) {
930 dsound
->state
= STATE_STOPPED
;
933 /* how can we have an underrun if we aren't playing? */
934 WARN("unexpected primary state (%ld)\n", dsound
->state
);
937 /* DSOUND_callback may need this lock */
938 LeaveCriticalSection(&(dsound
->mixlock
));
940 if (DSOUND_PrimaryStop(dsound
) != DS_OK
)
941 WARN("DSOUND_PrimaryStop failed\n");
943 EnterCriticalSection(&(dsound
->mixlock
));
946 /* the Stop is supposed to reset play position to beginning of buffer */
947 /* unfortunately, OSS is not able to do so, so get current pointer */
948 hres
= IDsDriverBuffer_GetPosition(dsound
->hwbuf
, &playpos
, NULL
);
950 LeaveCriticalSection(&(dsound
->mixlock
));
951 WARN("IDsDriverBuffer_GetPosition failed\n");
955 playpos
= dsound
->pwplay
* dsound
->fraglen
;
958 dsound
->playpos
= playpos
;
959 dsound
->mixpos
= writepos
;
961 maxq
= dsound
->buflen
;
962 if (maxq
> frag
) maxq
= frag
;
963 memset(dsound
->buffer
, nfiller
, dsound
->buflen
);
968 frag
= DSOUND_MixToPrimary(playpos
, writepos
, maxq
, paused
);
969 if (forced
) frag
= maxq
- inq
;
970 dsound
->mixpos
+= frag
;
971 while (dsound
->mixpos
>= dsound
->buflen
)
972 dsound
->mixpos
-= dsound
->buflen
;
975 /* buffers have been filled, restart playback */
976 if (dsound
->state
== STATE_STARTING
) {
977 dsound
->state
= STATE_PLAYING
;
979 else if (dsound
->state
== STATE_STOPPED
) {
980 /* the dsound is supposed to play if there's something to play
981 * even if it is reported as stopped, so don't let this confuse you */
982 dsound
->state
= STATE_STOPPING
;
984 LeaveCriticalSection(&(dsound
->mixlock
));
986 if (DSOUND_PrimaryPlay(dsound
) != DS_OK
)
987 WARN("DSOUND_PrimaryPlay failed\n");
989 TRACE("starting playback\n");
993 LeaveCriticalSection(&(dsound
->mixlock
));
995 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
996 if (dsound
->state
== STATE_STARTING
) {
997 if (DSOUND_PrimaryPlay(dsound
) != DS_OK
)
998 WARN("DSOUND_PrimaryPlay failed\n");
1000 dsound
->state
= STATE_PLAYING
;
1002 else if (dsound
->state
== STATE_STOPPING
) {
1003 if (DSOUND_PrimaryStop(dsound
) != DS_OK
)
1004 WARN("DSOUND_PrimaryStop failed\n");
1006 dsound
->state
= STATE_STOPPED
;
1011 void CALLBACK
DSOUND_timer(UINT timerID
, UINT msg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
1013 TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID
,msg
,dwUser
,dw1
,dw2
);
1014 TRACE("entering at %ld\n", GetTickCount());
1017 ERR("dsound died without killing us?\n");
1018 timeKillEvent(timerID
);
1019 timeEndPeriod(DS_TIME_RES
);
1023 RtlAcquireResourceShared(&(dsound
->lock
), TRUE
);
1026 DSOUND_PerformMix();
1029 RtlReleaseResource(&(dsound
->lock
));
1031 TRACE("completed processing at %ld\n", GetTickCount());
1034 void CALLBACK
DSOUND_callback(HWAVEOUT hwo
, UINT msg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
1036 IDirectSoundImpl
* This
= (IDirectSoundImpl
*)dwUser
;
1037 TRACE("entering at %ld, msg=%08x(%s)\n", GetTickCount(), msg
,
1038 msg
==MM_WOM_DONE
? "MM_WOM_DONE" : msg
==MM_WOM_CLOSE
? "MM_WOM_CLOSE" :
1039 msg
==MM_WOM_OPEN
? "MM_WOM_OPEN" : "UNKNOWN");
1040 if (msg
== MM_WOM_DONE
) {
1041 DWORD inq
, mixq
, fraglen
, buflen
, pwplay
, playpos
, mixpos
;
1042 if (This
->pwqueue
== (DWORD
)-1) {
1043 TRACE("completed due to reset\n");
1046 /* it could be a bad idea to enter critical section here... if there's lock contention,
1047 * the resulting scheduling delays might obstruct the winmm player thread */
1048 #ifdef SYNC_CALLBACK
1049 EnterCriticalSection(&(This
->mixlock
));
1051 /* retrieve current values */
1052 fraglen
= dsound
->fraglen
;
1053 buflen
= dsound
->buflen
;
1054 pwplay
= dsound
->pwplay
;
1055 playpos
= pwplay
* fraglen
;
1056 mixpos
= dsound
->mixpos
;
1057 /* check remaining mixed data */
1058 inq
= ((mixpos
< playpos
) ? buflen
: 0) + mixpos
- playpos
;
1059 mixq
= inq
/ fraglen
;
1060 if ((inq
- (mixq
* fraglen
)) > 0) mixq
++;
1061 /* complete the playing buffer */
1062 TRACE("done playing primary pos=%ld\n", playpos
);
1064 if (pwplay
>= DS_HEL_FRAGS
) pwplay
= 0;
1065 /* write new values */
1066 dsound
->pwplay
= pwplay
;
1068 /* queue new buffer if we have data for it */
1069 if (inq
>1) DSOUND_WaveQueue(This
, inq
-1);
1070 #ifdef SYNC_CALLBACK
1071 LeaveCriticalSection(&(This
->mixlock
));
1074 TRACE("completed\n");