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>
30 #include <math.h> /* Insomnia - pow() function */
40 #include "wine/windef16.h"
41 #include "wine/debug.h"
44 #include "dsound_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
48 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan
)
52 /* the AmpFactors are expressed in 16.16 fixed point */
53 volpan
->dwVolAmpFactor
= (ULONG
) (pow(2.0, volpan
->lVolume
/ 600.0) * 65536);
54 /* FIXME: dwPan{Left|Right}AmpFactor */
56 /* FIXME: use calculated vol and pan ampfactors */
57 temp
= (double) (volpan
->lVolume
- (volpan
->lPan
> 0 ? volpan
->lPan
: 0));
58 volpan
->dwTotalLeftAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 65536);
59 temp
= (double) (volpan
->lVolume
+ (volpan
->lPan
< 0 ? volpan
->lPan
: 0));
60 volpan
->dwTotalRightAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 65536);
62 TRACE("left = %lx, right = %lx\n", volpan
->dwTotalLeftAmpFactor
, volpan
->dwTotalRightAmpFactor
);
65 void DSOUND_RecalcFormat(IDirectSoundBufferImpl
*dsb
)
69 sw
= dsb
->wfx
.nChannels
* (dsb
->wfx
.wBitsPerSample
/ 8);
70 /* calculate the 10ms write lead */
71 dsb
->writelead
= (dsb
->freq
/ 100) * sw
;
74 void DSOUND_CheckEvent(IDirectSoundBufferImpl
*dsb
, int len
)
78 LPDSBPOSITIONNOTIFY event
;
80 if (dsb
->nrofnotifies
== 0)
83 TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
84 dsb
, dsb
->buflen
, dsb
->playpos
, len
);
85 for (i
= 0; i
< dsb
->nrofnotifies
; i
++) {
86 event
= dsb
->notifies
+ i
;
87 offset
= event
->dwOffset
;
88 TRACE("checking %d, position %ld, event = %d\n",
89 i
, offset
, event
->hEventNotify
);
90 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
91 /* OK. [Inside DirectX, p274] */
93 /* This also means we can't sort the entries by offset, */
94 /* because DSBPN_OFFSETSTOP == -1 */
95 if (offset
== DSBPN_OFFSETSTOP
) {
96 if (dsb
->state
== STATE_STOPPED
) {
97 SetEvent(event
->hEventNotify
);
98 TRACE("signalled event %d (%d)\n", event
->hEventNotify
, i
);
103 if ((dsb
->playpos
+ len
) >= dsb
->buflen
) {
104 if ((offset
< ((dsb
->playpos
+ len
) % dsb
->buflen
)) ||
105 (offset
>= dsb
->playpos
)) {
106 TRACE("signalled event %d (%d)\n", event
->hEventNotify
, i
);
107 SetEvent(event
->hEventNotify
);
110 if ((offset
>= dsb
->playpos
) && (offset
< (dsb
->playpos
+ len
))) {
111 TRACE("signalled event %d (%d)\n", event
->hEventNotify
, i
);
112 SetEvent(event
->hEventNotify
);
118 /* WAV format info can be found at: */
120 /* http://www.cwi.nl/ftp/audio/AudioFormats.part2 */
121 /* ftp://ftp.cwi.nl/pub/audio/RIFF-format */
123 /* Import points to remember: */
125 /* 8-bit WAV is unsigned */
126 /* 16-bit WAV is signed */
128 static inline INT16
cvtU8toS16(BYTE byte
)
130 INT16 s
= (byte
- 128) << 8;
135 static inline BYTE
cvtS16toU8(INT16 word
)
137 BYTE b
= (word
+ 32768) >> 8;
142 static inline void cp_fields(const IDirectSoundBufferImpl
*dsb
, BYTE
*ibuf
, BYTE
*obuf
)
145 if (dsb
->wfx
.nChannels
== 2) {
146 if (dsb
->wfx
.wBitsPerSample
== 8) {
147 /* avoid needless 8->16->8 conversion */
148 if ( (dsound
->wfx
.wBitsPerSample
== 8) && (dsound
->wfx
.nChannels
== 2) ) {
153 fl
= cvtU8toS16(*ibuf
);
154 fr
= cvtU8toS16(*(ibuf
+ 1));
155 } else if (dsb
->wfx
.wBitsPerSample
== 16) {
156 fl
= *((INT16
*)ibuf
);
157 fr
= *(((INT16
*)ibuf
) + 1);
159 } else if (dsb
->wfx
.nChannels
== 1) {
160 if (dsb
->wfx
.wBitsPerSample
== 8) {
161 /* avoid needless 8->16->8 conversion */
162 if ( (dsound
->wfx
.wBitsPerSample
== 8) && (dsound
->wfx
.nChannels
== 1) ) {
166 fl
= cvtU8toS16(*ibuf
);
168 } else if (dsb
->wfx
.wBitsPerSample
== 16) {
169 fl
= *((INT16
*)ibuf
);
173 if (dsound
->wfx
.nChannels
== 2) {
174 if (dsound
->wfx
.wBitsPerSample
== 8) {
175 *obuf
= cvtS16toU8(fl
);
176 *(obuf
+ 1) = cvtS16toU8(fr
);
179 if (dsound
->wfx
.wBitsPerSample
== 16) {
180 *((INT16
*)obuf
) = fl
;
181 *(((INT16
*)obuf
) + 1) = fr
;
185 if (dsound
->wfx
.nChannels
== 1) {
187 if (dsound
->wfx
.wBitsPerSample
== 8) {
188 *obuf
= cvtS16toU8(fl
);
191 if (dsound
->wfx
.wBitsPerSample
== 16) {
192 *((INT16
*)obuf
) = fl
;
198 /* Now with PerfectPitch (tm) technology */
199 static INT
DSOUND_MixerNorm(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
201 INT i
, size
, ipos
, ilen
;
203 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
204 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
206 ibp
= dsb
->buffer
+ dsb
->buf_mixpos
;
209 TRACE("(%p, %p, %p), buf_mixpos=%ld\n", dsb
, ibp
, obp
, dsb
->buf_mixpos
);
210 /* Check for the best case */
211 if ((dsb
->freq
== dsb
->dsound
->wfx
.nSamplesPerSec
) &&
212 (dsb
->wfx
.wBitsPerSample
== dsb
->dsound
->wfx
.wBitsPerSample
) &&
213 (dsb
->wfx
.nChannels
== dsb
->dsound
->wfx
.nChannels
)) {
214 DWORD bytesleft
= dsb
->buflen
- dsb
->buf_mixpos
;
215 TRACE("(%p) Best case\n", dsb
);
216 if (len
<= bytesleft
)
217 memcpy(obp
, ibp
, len
);
219 memcpy(obp
, ibp
, bytesleft
);
220 memcpy(obp
+ bytesleft
, dsb
->buffer
, len
- bytesleft
);
225 /* Check for same sample rate */
226 if (dsb
->freq
== dsb
->dsound
->wfx
.nSamplesPerSec
) {
227 TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb
,
228 dsb
->freq
, dsb
->dsound
->wfx
.nSamplesPerSec
);
230 for (i
= 0; i
< len
; i
+= oAdvance
) {
231 cp_fields(dsb
, ibp
, obp
);
235 if (ibp
>= (BYTE
*)(dsb
->buffer
+ dsb
->buflen
))
236 ibp
= dsb
->buffer
; /* wrap */
241 /* Mix in different sample rates */
243 /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
244 /* Patent Pending :-] */
246 /* Patent enhancements (c) 2000 Ove KÃ¥ven,
247 * TransGaming Technologies Inc. */
249 /* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
250 dsb, dsb->freq, dsb->dsound->wfx.nSamplesPerSec); */
252 size
= len
/ oAdvance
;
254 ipos
= dsb
->buf_mixpos
;
255 for (i
= 0; i
< size
; i
++) {
256 cp_fields(dsb
, (dsb
->buffer
+ ipos
), obp
);
258 dsb
->freqAcc
+= dsb
->freqAdjust
;
259 if (dsb
->freqAcc
>= (1<<DSOUND_FREQSHIFT
)) {
260 ULONG adv
= (dsb
->freqAcc
>>DSOUND_FREQSHIFT
) * iAdvance
;
261 dsb
->freqAcc
&= (1<<DSOUND_FREQSHIFT
)-1;
262 ipos
+= adv
; ilen
+= adv
;
263 while (ipos
>= dsb
->buflen
)
270 static void DSOUND_MixerVol(IDirectSoundBufferImpl
*dsb
, BYTE
*buf
, INT len
)
272 INT i
, inc
= dsb
->dsound
->wfx
.wBitsPerSample
>> 3;
274 INT16
*bps
= (INT16
*) buf
;
276 TRACE("(%p) left = %lx, right = %lx\n", dsb
,
277 dsb
->cvolpan
.dwTotalLeftAmpFactor
, dsb
->cvolpan
.dwTotalRightAmpFactor
);
278 if ((!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) || (dsb
->cvolpan
.lPan
== 0)) &&
279 (!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) || (dsb
->cvolpan
.lVolume
== 0)) &&
280 !(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
281 return; /* Nothing to do */
283 /* If we end up with some bozo coder using panning or 3D sound */
284 /* with a mono primary buffer, it could sound very weird using */
285 /* this method. Oh well, tough patooties. */
287 for (i
= 0; i
< len
; i
+= inc
) {
293 /* 8-bit WAV is unsigned, but we need to operate */
294 /* on signed data for this to work properly */
296 val
= ((val
* (i
& inc
? dsb
->cvolpan
.dwTotalRightAmpFactor
: dsb
->cvolpan
.dwTotalLeftAmpFactor
)) >> 16);
301 /* 16-bit WAV is signed -- much better */
303 val
= ((val
* ((i
& inc
) ? dsb
->cvolpan
.dwTotalRightAmpFactor
: dsb
->cvolpan
.dwTotalLeftAmpFactor
)) >> 16);
309 FIXME("MixerVol had a nasty error\n");
314 static void *tmp_buffer
;
315 static size_t tmp_buffer_len
= 0;
317 static void *DSOUND_tmpbuffer(size_t len
)
319 if (len
>tmp_buffer_len
) {
320 void *new_buffer
= realloc(tmp_buffer
, len
);
322 tmp_buffer
= new_buffer
;
323 tmp_buffer_len
= len
;
330 static DWORD
DSOUND_MixInBuffer(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD fraglen
)
332 INT i
, len
, ilen
, temp
, field
;
333 INT advance
= dsb
->dsound
->wfx
.wBitsPerSample
>> 3;
334 BYTE
*buf
, *ibuf
, *obuf
;
335 INT16
*ibufs
, *obufs
;
338 if (!(dsb
->playflags
& DSBPLAY_LOOPING
)) {
339 temp
= MulDiv(dsb
->dsound
->wfx
.nAvgBytesPerSec
, dsb
->buflen
,
340 dsb
->nAvgBytesPerSec
) -
341 MulDiv(dsb
->dsound
->wfx
.nAvgBytesPerSec
, dsb
->buf_mixpos
,
342 dsb
->nAvgBytesPerSec
);
343 len
= (len
> temp
) ? temp
: len
;
345 len
&= ~3; /* 4 byte alignment */
348 /* This should only happen if we aren't looping and temp < 4 */
350 /* We skip the remainder, so check for possible events */
351 DSOUND_CheckEvent(dsb
, dsb
->buflen
- dsb
->buf_mixpos
);
353 dsb
->state
= STATE_STOPPED
;
355 dsb
->last_playpos
= 0;
358 /* Check for DSBPN_OFFSETSTOP */
359 DSOUND_CheckEvent(dsb
, 0);
363 /* Been seeing segfaults in malloc() for some reason... */
364 TRACE("allocating buffer (size = %d)\n", len
);
365 if ((buf
= ibuf
= (BYTE
*) DSOUND_tmpbuffer(len
)) == NULL
)
368 TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb
, len
, writepos
);
370 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
371 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
372 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
))
373 DSOUND_MixerVol(dsb
, ibuf
, len
);
375 obuf
= dsb
->dsound
->buffer
+ writepos
;
376 for (i
= 0; i
< len
; i
+= advance
) {
377 obufs
= (INT16
*) obuf
;
378 ibufs
= (INT16
*) ibuf
;
379 if (dsb
->dsound
->wfx
.wBitsPerSample
== 8) {
380 /* 8-bit WAV is unsigned */
381 field
= (*ibuf
- 128);
382 field
+= (*obuf
- 128);
383 field
= field
> 127 ? 127 : field
;
384 field
= field
< -128 ? -128 : field
;
387 /* 16-bit WAV is signed */
390 field
= field
> 32767 ? 32767 : field
;
391 field
= field
< -32768 ? -32768 : field
;
396 if (obuf
>= (BYTE
*)(dsb
->dsound
->buffer
+ dsb
->dsound
->buflen
))
397 obuf
= dsb
->dsound
->buffer
;
401 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPOSITIONNOTIFY
)
402 DSOUND_CheckEvent(dsb
, ilen
);
404 if (dsb
->leadin
&& (dsb
->startpos
> dsb
->buf_mixpos
) && (dsb
->startpos
<= dsb
->buf_mixpos
+ ilen
)) {
405 /* HACK... leadin should be reset when the PLAY position reaches the startpos,
406 * not the MIX position... but if the sound buffer is bigger than our prebuffering
407 * (which must be the case for the streaming buffers that need this hack anyway)
408 * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
412 dsb
->buf_mixpos
+= ilen
;
414 if (dsb
->buf_mixpos
>= dsb
->buflen
) {
415 if (!(dsb
->playflags
& DSBPLAY_LOOPING
)) {
416 dsb
->state
= STATE_STOPPED
;
418 dsb
->last_playpos
= 0;
421 DSOUND_CheckEvent(dsb
, 0); /* For DSBPN_OFFSETSTOP */
424 while (dsb
->buf_mixpos
>= dsb
->buflen
)
425 dsb
->buf_mixpos
-= dsb
->buflen
;
426 if (dsb
->leadin
&& (dsb
->startpos
<= dsb
->buf_mixpos
))
427 dsb
->leadin
= FALSE
; /* HACK: see above */
434 static void DSOUND_PhaseCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD len
)
437 INT advance
= dsb
->dsound
->wfx
.wBitsPerSample
>> 3;
438 BYTE
*buf
, *ibuf
, *obuf
;
439 INT16
*ibufs
, *obufs
;
441 len
&= ~3; /* 4 byte alignment */
443 TRACE("allocating buffer (size = %ld)\n", len
);
444 if ((buf
= ibuf
= (BYTE
*) DSOUND_tmpbuffer(len
)) == NULL
)
447 TRACE("PhaseCancel (%p) len = %ld, dest = %ld\n", dsb
, len
, writepos
);
449 ilen
= DSOUND_MixerNorm(dsb
, ibuf
, len
);
450 if ((dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) ||
451 (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
))
452 DSOUND_MixerVol(dsb
, ibuf
, len
);
454 /* subtract instead of add, to phase out premixed data */
455 obuf
= dsb
->dsound
->buffer
+ writepos
;
456 for (i
= 0; i
< len
; i
+= advance
) {
457 obufs
= (INT16
*) obuf
;
458 ibufs
= (INT16
*) ibuf
;
459 if (dsb
->dsound
->wfx
.wBitsPerSample
== 8) {
460 /* 8-bit WAV is unsigned */
461 field
= (*ibuf
- 128);
462 field
-= (*obuf
- 128);
463 field
= field
> 127 ? 127 : field
;
464 field
= field
< -128 ? -128 : field
;
467 /* 16-bit WAV is signed */
470 field
= field
> 32767 ? 32767 : field
;
471 field
= field
< -32768 ? -32768 : field
;
476 if (obuf
>= (BYTE
*)(dsb
->dsound
->buffer
+ dsb
->dsound
->buflen
))
477 obuf
= dsb
->dsound
->buffer
;
482 static void DSOUND_MixCancel(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, BOOL cancel
)
484 DWORD size
, flen
, len
, npos
, nlen
;
485 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
486 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
487 /* determine amount of premixed data to cancel */
489 ((dsb
->primary_mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
490 dsb
->primary_mixpos
- writepos
;
492 TRACE("(%p, %ld), buf_mixpos=%ld\n", dsb
, writepos
, dsb
->buf_mixpos
);
494 /* backtrack the mix position */
495 size
= primary_done
/ oAdvance
;
496 flen
= size
* dsb
->freqAdjust
;
497 len
= (flen
>> DSOUND_FREQSHIFT
) * iAdvance
;
498 flen
&= (1<<DSOUND_FREQSHIFT
)-1;
499 while (dsb
->freqAcc
< flen
) {
501 dsb
->freqAcc
+= 1<<DSOUND_FREQSHIFT
;
504 npos
= ((dsb
->buf_mixpos
< len
) ? dsb
->buflen
: 0) +
505 dsb
->buf_mixpos
- len
;
506 if (dsb
->leadin
&& (dsb
->startpos
> npos
) && (dsb
->startpos
<= npos
+ len
)) {
507 /* stop backtracking at startpos */
508 npos
= dsb
->startpos
;
509 len
= ((dsb
->buf_mixpos
< npos
) ? dsb
->buflen
: 0) +
510 dsb
->buf_mixpos
- npos
;
512 nlen
= len
/ dsb
->wfx
.nBlockAlign
;
513 nlen
= ((nlen
<< DSOUND_FREQSHIFT
) + flen
) / dsb
->freqAdjust
;
514 nlen
*= dsb
->dsound
->wfx
.nBlockAlign
;
516 ((dsb
->primary_mixpos
< nlen
) ? dsb
->dsound
->buflen
: 0) +
517 dsb
->primary_mixpos
- nlen
;
520 dsb
->freqAcc
-= flen
;
521 dsb
->buf_mixpos
= npos
;
522 dsb
->primary_mixpos
= writepos
;
524 TRACE("new buf_mixpos=%ld, primary_mixpos=%ld (len=%ld)\n",
525 dsb
->buf_mixpos
, dsb
->primary_mixpos
, len
);
527 if (cancel
) DSOUND_PhaseCancel(dsb
, writepos
, len
);
530 void DSOUND_MixCancelAt(IDirectSoundBufferImpl
*dsb
, DWORD buf_writepos
)
533 DWORD i
, size
, flen
, len
, npos
, nlen
;
534 INT iAdvance
= dsb
->wfx
.nBlockAlign
;
535 INT oAdvance
= dsb
->dsound
->wfx
.nBlockAlign
;
536 /* determine amount of premixed data to cancel */
538 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
539 dsb
->buf_mixpos
- buf_writepos
;
542 WARN("(%p, %ld), buf_mixpos=%ld\n", dsb
, buf_writepos
, dsb
->buf_mixpos
);
543 /* since this is not implemented yet, just cancel *ALL* prebuffering for now
544 * (which is faster anyway when there's only a single secondary buffer) */
545 dsb
->dsound
->need_remix
= TRUE
;
548 void DSOUND_ForceRemix(IDirectSoundBufferImpl
*dsb
)
550 EnterCriticalSection(&dsb
->lock
);
551 if (dsb
->state
== STATE_PLAYING
) {
552 #if 0 /* this may not be quite reliable yet */
553 dsb
->need_remix
= TRUE
;
555 dsb
->dsound
->need_remix
= TRUE
;
558 LeaveCriticalSection(&dsb
->lock
);
561 static DWORD
DSOUND_MixOne(IDirectSoundBufferImpl
*dsb
, DWORD playpos
, DWORD writepos
, DWORD mixlen
)
564 /* determine this buffer's write position */
565 DWORD buf_writepos
= DSOUND_CalcPlayPosition(dsb
, dsb
->state
& dsb
->dsound
->state
, writepos
,
566 writepos
, dsb
->primary_mixpos
, dsb
->buf_mixpos
);
567 /* determine how much already-mixed data exists */
569 ((dsb
->buf_mixpos
< buf_writepos
) ? dsb
->buflen
: 0) +
570 dsb
->buf_mixpos
- buf_writepos
;
572 ((dsb
->primary_mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
573 dsb
->primary_mixpos
- writepos
;
575 ((dsb
->dsound
->mixpos
< writepos
) ? dsb
->dsound
->buflen
: 0) +
576 dsb
->dsound
->mixpos
- writepos
;
579 TRACE("buf_writepos=%ld, primary_writepos=%ld\n", buf_writepos
, writepos
);
580 TRACE("buf_done=%ld, primary_done=%ld\n", buf_done
, primary_done
);
581 TRACE("buf_mixpos=%ld, primary_mixpos=%ld, mixlen=%ld\n", dsb
->buf_mixpos
, dsb
->primary_mixpos
,
583 TRACE("looping=%ld, startpos=%ld, leadin=%ld\n", dsb
->playflags
, dsb
->startpos
, dsb
->leadin
);
585 /* save write position for non-GETCURRENTPOSITION2... */
586 dsb
->playpos
= buf_writepos
;
588 /* check whether CalcPlayPosition detected a mixing underrun */
589 if ((buf_done
== 0) && (dsb
->primary_mixpos
!= writepos
)) {
590 /* it did, but did we have more to play? */
591 if ((dsb
->playflags
& DSBPLAY_LOOPING
) ||
592 (dsb
->buf_mixpos
< dsb
->buflen
)) {
593 /* yes, have to recover */
594 ERR("underrun on sound buffer %p\n", dsb
);
595 TRACE("recovering from underrun: primary_mixpos=%ld\n", writepos
);
597 dsb
->primary_mixpos
= writepos
;
600 /* determine how far ahead we should mix */
601 if (((dsb
->playflags
& DSBPLAY_LOOPING
) ||
602 (dsb
->leadin
&& (dsb
->probably_valid_to
!= 0))) &&
603 !(dsb
->dsbd
.dwFlags
& DSBCAPS_STATIC
)) {
604 /* if this is a streaming buffer, it typically means that
605 * we should defer mixing past probably_valid_to as long
606 * as we can, to avoid unnecessary remixing */
607 /* the heavy-looking calculations shouldn't be that bad,
608 * as any game isn't likely to be have more than 1 or 2
609 * streaming buffers in use at any time anyway... */
610 DWORD probably_valid_left
=
611 (dsb
->probably_valid_to
== (DWORD
)-1) ? dsb
->buflen
:
612 ((dsb
->probably_valid_to
< buf_writepos
) ? dsb
->buflen
: 0) +
613 dsb
->probably_valid_to
- buf_writepos
;
614 /* check for leadin condition */
615 if ((probably_valid_left
== 0) &&
616 (dsb
->probably_valid_to
== dsb
->startpos
) &&
618 probably_valid_left
= dsb
->buflen
;
619 TRACE("streaming buffer probably_valid_to=%ld, probably_valid_left=%ld\n",
620 dsb
->probably_valid_to
, probably_valid_left
);
621 /* check whether the app's time is already up */
622 if (probably_valid_left
< dsb
->writelead
) {
623 WARN("probably_valid_to now within writelead, possible streaming underrun\n");
624 /* once we pass the point of no return,
625 * no reason to hold back anymore */
626 dsb
->probably_valid_to
= (DWORD
)-1;
627 /* we just have to go ahead and mix what we have,
628 * there's no telling what the app is thinking anyway */
630 /* adjust for our frequency and our sample size */
631 probably_valid_left
= MulDiv(probably_valid_left
,
632 1 << DSOUND_FREQSHIFT
,
633 dsb
->wfx
.nBlockAlign
* dsb
->freqAdjust
) *
634 dsb
->dsound
->wfx
.nBlockAlign
;
635 /* check whether to clip mix_len */
636 if (probably_valid_left
< mixlen
) {
637 TRACE("clipping to probably_valid_left=%ld\n", probably_valid_left
);
638 mixlen
= probably_valid_left
;
642 /* cut mixlen with what's already been mixed */
643 if (mixlen
< primary_done
) {
644 /* huh? and still CalcPlayPosition didn't
645 * detect an underrun? */
646 FIXME("problem with underrun detection (mixlen=%ld < primary_done=%ld)\n", mixlen
, primary_done
);
649 len
= mixlen
- primary_done
;
650 TRACE("remaining mixlen=%ld\n", len
);
652 if (len
< dsb
->dsound
->fraglen
) {
653 /* smaller than a fragment, wait until it gets larger
654 * before we take the mixing overhead */
655 TRACE("mixlen not worth it, deferring mixing\n");
659 /* ok, we know how much to mix, let's go */
660 still_behind
= (adv_done
> primary_done
);
662 slen
= dsb
->dsound
->buflen
- dsb
->primary_mixpos
;
663 if (slen
> len
) slen
= len
;
664 slen
= DSOUND_MixInBuffer(dsb
, dsb
->primary_mixpos
, slen
);
666 if ((dsb
->primary_mixpos
< dsb
->dsound
->mixpos
) &&
667 (dsb
->primary_mixpos
+ slen
>= dsb
->dsound
->mixpos
))
668 still_behind
= FALSE
;
670 dsb
->primary_mixpos
+= slen
; len
-= slen
;
671 while (dsb
->primary_mixpos
>= dsb
->dsound
->buflen
)
672 dsb
->primary_mixpos
-= dsb
->dsound
->buflen
;
674 if ((dsb
->state
== STATE_STOPPED
) || !slen
) break;
676 TRACE("new primary_mixpos=%ld, primary_advbase=%ld\n", dsb
->primary_mixpos
, dsb
->dsound
->mixpos
);
677 TRACE("mixed data len=%ld, still_behind=%d\n", mixlen
-len
, still_behind
);
678 /* return how far we think the primary buffer can
679 * advance its underrun detector...*/
680 if (still_behind
) return 0;
681 if ((mixlen
- len
) < primary_done
) return 0;
682 slen
= ((dsb
->primary_mixpos
< dsb
->dsound
->mixpos
) ?
683 dsb
->dsound
->buflen
: 0) + dsb
->primary_mixpos
-
686 /* the primary_done and still_behind checks above should have worked */
687 FIXME("problem with advancement calculation (advlen=%ld > mixlen=%ld)\n", slen
, mixlen
);
693 static DWORD
DSOUND_MixToPrimary(DWORD playpos
, DWORD writepos
, DWORD mixlen
, BOOL recover
)
695 INT i
, len
, maxlen
= 0;
696 IDirectSoundBufferImpl
*dsb
;
698 TRACE("(%ld,%ld,%ld)\n", playpos
, writepos
, mixlen
);
699 for (i
= dsound
->nrofbuffers
- 1; i
>= 0; i
--) {
700 dsb
= dsound
->buffers
[i
];
702 if (!dsb
|| !(ICOM_VTBL(dsb
)))
704 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
705 TRACE("Checking %p, mixlen=%ld\n", dsb
, mixlen
);
706 EnterCriticalSection(&(dsb
->lock
));
707 if (dsb
->state
== STATE_STOPPING
) {
708 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
709 dsb
->state
= STATE_STOPPED
;
711 if ((dsb
->state
== STATE_STARTING
) || recover
) {
712 dsb
->primary_mixpos
= writepos
;
713 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
714 dsb
->need_remix
= FALSE
;
716 else if (dsb
->need_remix
) {
717 DSOUND_MixCancel(dsb
, writepos
, TRUE
);
718 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
719 dsb
->need_remix
= FALSE
;
721 len
= DSOUND_MixOne(dsb
, playpos
, writepos
, mixlen
);
722 if (dsb
->state
== STATE_STARTING
)
723 dsb
->state
= STATE_PLAYING
;
724 maxlen
= (len
> maxlen
) ? len
: maxlen
;
726 LeaveCriticalSection(&(dsb
->lock
));
733 static void DSOUND_MixReset(DWORD writepos
)
736 IDirectSoundBufferImpl
*dsb
;
739 TRACE("(%ld)\n", writepos
);
741 /* the sound of silence */
742 nfiller
= dsound
->wfx
.wBitsPerSample
== 8 ? 128 : 0;
744 /* reset all buffer mix positions */
745 for (i
= dsound
->nrofbuffers
- 1; i
>= 0; i
--) {
746 dsb
= dsound
->buffers
[i
];
748 if (!dsb
|| !(ICOM_VTBL(dsb
)))
750 if (dsb
->buflen
&& dsb
->state
&& !dsb
->hwbuf
) {
751 TRACE("Resetting %p\n", dsb
);
752 EnterCriticalSection(&(dsb
->lock
));
753 if (dsb
->state
== STATE_STOPPING
) {
754 dsb
->state
= STATE_STOPPED
;
756 else if (dsb
->state
== STATE_STARTING
) {
759 DSOUND_MixCancel(dsb
, writepos
, FALSE
);
760 memcpy(&dsb
->cvolpan
, &dsb
->volpan
, sizeof(dsb
->cvolpan
));
761 dsb
->need_remix
= FALSE
;
763 LeaveCriticalSection(&(dsb
->lock
));
767 /* wipe out premixed data */
768 if (dsound
->mixpos
< writepos
) {
769 memset(dsound
->buffer
+ writepos
, nfiller
, dsound
->buflen
- writepos
);
770 memset(dsound
->buffer
, nfiller
, dsound
->mixpos
);
772 memset(dsound
->buffer
+ writepos
, nfiller
, dsound
->mixpos
- writepos
);
775 /* reset primary mix position */
776 dsound
->mixpos
= writepos
;
779 static void DSOUND_CheckReset(IDirectSoundImpl
*dsound
, DWORD writepos
)
781 if (dsound
->need_remix
) {
782 DSOUND_MixReset(writepos
);
783 dsound
->need_remix
= FALSE
;
784 /* maximize Half-Life performance */
785 dsound
->prebuf
= ds_snd_queue_min
;
786 dsound
->precount
= 0;
789 if (dsound
->precount
>= 4) {
790 if (dsound
->prebuf
< ds_snd_queue_max
)
792 dsound
->precount
= 0;
795 TRACE("premix adjust: %d\n", dsound
->prebuf
);
798 void DSOUND_WaveQueue(IDirectSoundImpl
*dsound
, DWORD mixq
)
800 if (mixq
+ dsound
->pwqueue
> ds_hel_queue
) mixq
= ds_hel_queue
- dsound
->pwqueue
;
801 TRACE("queueing %ld buffers, starting at %d\n", mixq
, dsound
->pwwrite
);
802 for (; mixq
; mixq
--) {
803 waveOutWrite(dsound
->hwo
, dsound
->pwave
[dsound
->pwwrite
], sizeof(WAVEHDR
));
805 if (dsound
->pwwrite
>= DS_HEL_FRAGS
) dsound
->pwwrite
= 0;
810 /* #define SYNC_CALLBACK */
812 void DSOUND_PerformMix(void)
818 RtlAcquireResourceShared(&(dsound
->lock
), TRUE
);
820 if (!dsound
|| !dsound
->ref
) {
821 /* seems the dsound object is currently being released */
822 RtlReleaseResource(&(dsound
->lock
));
826 /* the sound of silence */
827 nfiller
= dsound
->wfx
.wBitsPerSample
== 8 ? 128 : 0;
829 /* whether the primary is forced to play even without secondary buffers */
830 forced
= ((dsound
->state
== STATE_PLAYING
) || (dsound
->state
== STATE_STARTING
));
832 TRACE("entering at %ld\n", GetTickCount());
833 if (dsound
->priolevel
!= DSSCL_WRITEPRIMARY
) {
834 BOOL paused
= ((dsound
->state
== STATE_STOPPED
) || (dsound
->state
== STATE_STARTING
));
835 /* FIXME: document variables */
836 DWORD playpos
, writepos
, inq
, maxq
, frag
;
838 hres
= IDsDriverBuffer_GetPosition(dsound
->hwbuf
, &playpos
, &writepos
);
840 RtlReleaseResource(&(dsound
->lock
));
843 /* Well, we *could* do Just-In-Time mixing using the writepos,
844 * but that's a little bit ambitious and unnecessary... */
845 /* rather add our safety margin to the writepos, if we're playing */
847 writepos
+= dsound
->writelead
;
848 while (writepos
>= dsound
->buflen
)
849 writepos
-= dsound
->buflen
;
850 } else writepos
= playpos
;
853 playpos
= dsound
->pwplay
* dsound
->fraglen
;
856 writepos
+= ds_hel_margin
* dsound
->fraglen
;
857 while (writepos
>= dsound
->buflen
)
858 writepos
-= dsound
->buflen
;
861 TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld\n",
862 playpos
,writepos
,dsound
->playpos
,dsound
->mixpos
);
863 /* wipe out just-played sound data */
864 if (playpos
< dsound
->playpos
) {
865 memset(dsound
->buffer
+ dsound
->playpos
, nfiller
, dsound
->buflen
- dsound
->playpos
);
866 memset(dsound
->buffer
, nfiller
, playpos
);
868 memset(dsound
->buffer
+ dsound
->playpos
, nfiller
, playpos
- dsound
->playpos
);
870 dsound
->playpos
= playpos
;
872 EnterCriticalSection(&(dsound
->mixlock
));
874 /* reset mixing if necessary */
875 DSOUND_CheckReset(dsound
, writepos
);
877 /* check how much prebuffering is left */
878 inq
= dsound
->mixpos
;
880 inq
+= dsound
->buflen
;
883 /* find the maximum we can prebuffer */
887 maxq
+= dsound
->buflen
;
889 } else maxq
= dsound
->buflen
;
891 /* clip maxq to dsound->prebuf */
892 frag
= dsound
->prebuf
* dsound
->fraglen
;
893 if (maxq
> frag
) maxq
= frag
;
895 /* check for consistency */
897 /* the playback position must have passed our last
898 * mixed position, i.e. it's an underrun, or we have
899 * nothing more to play */
900 TRACE("reached end of mixed data (inq=%ld, maxq=%ld)\n", inq
, maxq
);
902 /* stop the playback now, to allow buffers to refill */
903 if (dsound
->state
== STATE_PLAYING
) {
904 dsound
->state
= STATE_STARTING
;
906 else if (dsound
->state
== STATE_STOPPING
) {
907 dsound
->state
= STATE_STOPPED
;
910 /* how can we have an underrun if we aren't playing? */
911 WARN("unexpected primary state (%ld)\n", dsound
->state
);
914 /* DSOUND_callback may need this lock */
915 LeaveCriticalSection(&(dsound
->mixlock
));
917 DSOUND_PrimaryStop(dsound
);
919 EnterCriticalSection(&(dsound
->mixlock
));
922 /* the Stop is supposed to reset play position to beginning of buffer */
923 /* unfortunately, OSS is not able to do so, so get current pointer */
924 hres
= IDsDriverBuffer_GetPosition(dsound
->hwbuf
, &playpos
, NULL
);
926 LeaveCriticalSection(&(dsound
->mixlock
));
927 RtlReleaseResource(&(dsound
->lock
));
931 playpos
= dsound
->pwplay
* dsound
->fraglen
;
934 dsound
->playpos
= playpos
;
935 dsound
->mixpos
= writepos
;
937 maxq
= dsound
->buflen
;
938 if (maxq
> frag
) maxq
= frag
;
939 memset(dsound
->buffer
, nfiller
, dsound
->buflen
);
944 frag
= DSOUND_MixToPrimary(playpos
, writepos
, maxq
, paused
);
945 if (forced
) frag
= maxq
- inq
;
946 dsound
->mixpos
+= frag
;
947 while (dsound
->mixpos
>= dsound
->buflen
)
948 dsound
->mixpos
-= dsound
->buflen
;
951 /* buffers have been filled, restart playback */
952 if (dsound
->state
== STATE_STARTING
) {
953 dsound
->state
= STATE_PLAYING
;
955 else if (dsound
->state
== STATE_STOPPED
) {
956 /* the dsound is supposed to play if there's something to play
957 * even if it is reported as stopped, so don't let this confuse you */
958 dsound
->state
= STATE_STOPPING
;
960 LeaveCriticalSection(&(dsound
->mixlock
));
962 DSOUND_PrimaryPlay(dsound
);
963 TRACE("starting playback\n");
967 LeaveCriticalSection(&(dsound
->mixlock
));
969 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
970 if (dsound
->state
== STATE_STARTING
) {
971 DSOUND_PrimaryPlay(dsound
);
972 dsound
->state
= STATE_PLAYING
;
974 else if (dsound
->state
== STATE_STOPPING
) {
975 DSOUND_PrimaryStop(dsound
);
976 dsound
->state
= STATE_STOPPED
;
979 TRACE("completed processing at %ld\n", GetTickCount());
980 RtlReleaseResource(&(dsound
->lock
));
983 void CALLBACK
DSOUND_timer(UINT timerID
, UINT msg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
986 ERR("dsound died without killing us?\n");
987 timeKillEvent(timerID
);
988 timeEndPeriod(DS_TIME_RES
);
996 void CALLBACK
DSOUND_callback(HWAVEOUT hwo
, UINT msg
, DWORD dwUser
, DWORD dw1
, DWORD dw2
)
998 IDirectSoundImpl
* This
= (IDirectSoundImpl
*)dwUser
;
999 TRACE("entering at %ld, msg=%08x\n", GetTickCount(), msg
);
1000 if (msg
== MM_WOM_DONE
) {
1001 DWORD inq
, mixq
, fraglen
, buflen
, pwplay
, playpos
, mixpos
;
1002 if (This
->pwqueue
== (DWORD
)-1) {
1003 TRACE("completed due to reset\n");
1006 /* it could be a bad idea to enter critical section here... if there's lock contention,
1007 * the resulting scheduling delays might obstruct the winmm player thread */
1008 #ifdef SYNC_CALLBACK
1009 EnterCriticalSection(&(This
->mixlock
));
1011 /* retrieve current values */
1012 fraglen
= dsound
->fraglen
;
1013 buflen
= dsound
->buflen
;
1014 pwplay
= dsound
->pwplay
;
1015 playpos
= pwplay
* fraglen
;
1016 mixpos
= dsound
->mixpos
;
1017 /* check remaining mixed data */
1018 inq
= ((mixpos
< playpos
) ? buflen
: 0) + mixpos
- playpos
;
1019 mixq
= inq
/ fraglen
;
1020 if ((inq
- (mixq
* fraglen
)) > 0) mixq
++;
1021 /* complete the playing buffer */
1022 TRACE("done playing primary pos=%ld\n", playpos
);
1024 if (pwplay
>= DS_HEL_FRAGS
) pwplay
= 0;
1025 /* write new values */
1026 dsound
->pwplay
= pwplay
;
1028 /* queue new buffer if we have data for it */
1029 if (inq
>1) DSOUND_WaveQueue(This
, inq
-1);
1030 #ifdef SYNC_CALLBACK
1031 LeaveCriticalSection(&(This
->lock
));
1034 TRACE("completed\n");