3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
6 * Copyright 2007 Peter Dons Tychsen
7 * Copyright 2007 Maarten Lankhorst
8 * Copyright 2011 Owen Rudge for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <math.h> /* Insomnia - pow() function */
30 #define NONAMELESSSTRUCT
31 #define NONAMELESSUNION
38 #include "wine/debug.h"
42 #include "dsound_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
47 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan
)
50 TRACE("(%p)\n",volpan
);
52 TRACE("Vol=%d Pan=%d\n", volpan
->lVolume
, volpan
->lPan
);
53 /* the AmpFactors are expressed in 16.16 fixed point */
54 volpan
->dwVolAmpFactor
= (ULONG
) (pow(2.0, volpan
->lVolume
/ 600.0) * 0xffff);
55 /* FIXME: dwPan{Left|Right}AmpFactor */
57 /* FIXME: use calculated vol and pan ampfactors */
58 temp
= (double) (volpan
->lVolume
- (volpan
->lPan
> 0 ? volpan
->lPan
: 0));
59 volpan
->dwTotalLeftAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 0xffff);
60 temp
= (double) (volpan
->lVolume
+ (volpan
->lPan
< 0 ? volpan
->lPan
: 0));
61 volpan
->dwTotalRightAmpFactor
= (ULONG
) (pow(2.0, temp
/ 600.0) * 0xffff);
63 TRACE("left = %x, right = %x\n", volpan
->dwTotalLeftAmpFactor
, volpan
->dwTotalRightAmpFactor
);
66 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan
)
69 TRACE("(%p)\n",volpan
);
71 TRACE("left=%x, right=%x\n",volpan
->dwTotalLeftAmpFactor
,volpan
->dwTotalRightAmpFactor
);
72 if (volpan
->dwTotalLeftAmpFactor
==0)
75 left
=600 * log(((double)volpan
->dwTotalLeftAmpFactor
) / 0xffff) / log(2);
76 if (volpan
->dwTotalRightAmpFactor
==0)
79 right
=600 * log(((double)volpan
->dwTotalRightAmpFactor
) / 0xffff) / log(2);
82 volpan
->lVolume
=right
;
83 volpan
->dwVolAmpFactor
=volpan
->dwTotalRightAmpFactor
;
88 volpan
->dwVolAmpFactor
=volpan
->dwTotalLeftAmpFactor
;
90 if (volpan
->lVolume
< -10000)
91 volpan
->lVolume
=-10000;
92 volpan
->lPan
=right
-left
;
93 if (volpan
->lPan
< -10000)
96 TRACE("Vol=%d Pan=%d\n", volpan
->lVolume
, volpan
->lPan
);
100 * Recalculate the size for temporary buffer, and new writelead
101 * Should be called when one of the following things occur:
102 * - Primary buffer format is changed
103 * - This buffer format (frequency) is changed
105 void DSOUND_RecalcFormat(IDirectSoundBufferImpl
*dsb
)
107 DWORD ichannels
= dsb
->pwfx
->nChannels
;
108 DWORD ochannels
= dsb
->device
->pwfx
->nChannels
;
109 WAVEFORMATEXTENSIBLE
*pwfxe
;
114 pwfxe
= (WAVEFORMATEXTENSIBLE
*) dsb
->pwfx
;
115 dsb
->freqAdjust
= (float)dsb
->freq
/ dsb
->device
->pwfx
->nSamplesPerSec
;
117 if ((pwfxe
->Format
.wFormatTag
== WAVE_FORMAT_IEEE_FLOAT
) || ((pwfxe
->Format
.wFormatTag
== WAVE_FORMAT_EXTENSIBLE
)
118 && (IsEqualGUID(&pwfxe
->SubFormat
, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
))))
122 * Recalculate FIR step and gain.
124 * firstep says how many points of the FIR exist per one
125 * sample in the secondary buffer. firgain specifies what
126 * to multiply the FIR output by in order to attenuate it correctly.
128 if (dsb
->freqAdjust
> 1.0f
) {
130 * Yes, round it a bit to make sure that the
131 * linear interpolation factor never changes.
133 dsb
->firstep
= ceil(fir_step
/ dsb
->freqAdjust
);
135 dsb
->firstep
= fir_step
;
137 dsb
->firgain
= (float)dsb
->firstep
/ fir_step
;
139 /* calculate the 10ms write lead */
140 dsb
->writelead
= (dsb
->freq
/ 100) * dsb
->pwfx
->nBlockAlign
;
144 dsb
->get_aux
= ieee
? getbpp
[4] : getbpp
[dsb
->pwfx
->wBitsPerSample
/8 - 1];
145 dsb
->put_aux
= putieee32
;
147 dsb
->get
= dsb
->get_aux
;
148 dsb
->put
= dsb
->put_aux
;
150 if (ichannels
== ochannels
)
152 dsb
->mix_channels
= ichannels
;
153 if (ichannels
> 32) {
154 FIXME("Copying %u channels is unsupported, limiting to first 32\n", ichannels
);
155 dsb
->mix_channels
= 32;
158 else if (ichannels
== 1)
160 dsb
->mix_channels
= 1;
161 dsb
->put
= put_mono2stereo
;
163 else if (ochannels
== 1)
165 dsb
->mix_channels
= 1;
171 FIXME("Conversion from %u to %u channels is not implemented, falling back to stereo\n", ichannels
, ochannels
);
172 dsb
->mix_channels
= 2;
177 * Check for application callback requests for when the play position
178 * reaches certain points.
180 * The offsets that will be triggered will be those between the recorded
181 * "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
182 * beyond that position.
184 void DSOUND_CheckEvent(const IDirectSoundBufferImpl
*dsb
, DWORD playpos
, int len
)
188 LPDSBPOSITIONNOTIFY event
;
189 TRACE("(%p,%d)\n",dsb
,len
);
191 if (dsb
->nrofnotifies
== 0)
194 TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
195 dsb
, dsb
->buflen
, playpos
, len
);
196 for (i
= 0; i
< dsb
->nrofnotifies
; i
++) {
197 event
= dsb
->notifies
+ i
;
198 offset
= event
->dwOffset
;
199 TRACE("checking %d, position %d, event = %p\n",
200 i
, offset
, event
->hEventNotify
);
201 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
202 /* OK. [Inside DirectX, p274] */
203 /* Windows does not seem to enforce this, and some apps rely */
204 /* on that, so we can't stop there. */
206 /* This also means we can't sort the entries by offset, */
207 /* because DSBPN_OFFSETSTOP == -1 */
208 if (offset
== DSBPN_OFFSETSTOP
) {
209 if (dsb
->state
== STATE_STOPPED
) {
210 SetEvent(event
->hEventNotify
);
211 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
215 if ((playpos
+ len
) >= dsb
->buflen
) {
216 if ((offset
< ((playpos
+ len
) % dsb
->buflen
)) ||
217 (offset
>= playpos
)) {
218 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
219 SetEvent(event
->hEventNotify
);
222 if ((offset
>= playpos
) && (offset
< (playpos
+ len
))) {
223 TRACE("signalled event %p (%d)\n", event
->hEventNotify
, i
);
224 SetEvent(event
->hEventNotify
);
230 static inline float get_current_sample(const IDirectSoundBufferImpl
*dsb
,
231 DWORD mixpos
, DWORD channel
)
233 if (mixpos
>= dsb
->buflen
&& !(dsb
->playflags
& DSBPLAY_LOOPING
))
235 return dsb
->get(dsb
, mixpos
% dsb
->buflen
, channel
);
238 static UINT
cp_fields_noresample(IDirectSoundBufferImpl
*dsb
, UINT count
)
240 UINT istride
= dsb
->pwfx
->nBlockAlign
;
241 UINT ostride
= dsb
->device
->pwfx
->nChannels
* sizeof(float);
243 for (i
= 0; i
< count
; i
++)
244 for (channel
= 0; channel
< dsb
->mix_channels
; channel
++)
245 dsb
->put(dsb
, i
* ostride
, channel
, get_current_sample(dsb
,
246 dsb
->sec_mixpos
+ i
* istride
, channel
));
250 static UINT
cp_fields_resample(IDirectSoundBufferImpl
*dsb
, UINT count
, float *freqAcc
)
253 UINT istride
= dsb
->pwfx
->nBlockAlign
;
254 UINT ostride
= dsb
->device
->pwfx
->nChannels
* sizeof(float);
256 float freqAdjust
= dsb
->freqAdjust
;
257 float freqAcc_start
= *freqAcc
;
258 float freqAcc_end
= freqAcc_start
+ count
* freqAdjust
;
259 UINT dsbfirstep
= dsb
->firstep
;
260 UINT channels
= dsb
->mix_channels
;
261 UINT max_ipos
= freqAcc_start
+ count
* freqAdjust
;
263 UINT fir_cachesize
= (fir_len
+ dsbfirstep
- 2) / dsbfirstep
;
264 UINT required_input
= max_ipos
+ fir_cachesize
;
266 float* intermediate
= HeapAlloc(GetProcessHeap(), 0,
267 sizeof(float) * required_input
* channels
);
269 float* fir_copy
= HeapAlloc(GetProcessHeap(), 0,
270 sizeof(float) * fir_cachesize
);
272 /* Important: this buffer MUST be non-interleaved
273 * if you want -msse3 to have any effect.
274 * This is good for CPU cache effects, too.
276 float* itmp
= intermediate
;
277 for (channel
= 0; channel
< channels
; channel
++)
278 for (i
= 0; i
< required_input
; i
++)
279 *(itmp
++) = get_current_sample(dsb
,
280 dsb
->sec_mixpos
+ i
* istride
, channel
);
282 for(i
= 0; i
< count
; ++i
) {
283 float total_fir_steps
= (freqAcc_start
+ i
* freqAdjust
) * dsbfirstep
;
284 UINT int_fir_steps
= total_fir_steps
;
285 UINT ipos
= int_fir_steps
/ dsbfirstep
;
287 UINT idx
= (ipos
+ 1) * dsbfirstep
- int_fir_steps
- 1;
288 float rem
= int_fir_steps
+ 1.0 - total_fir_steps
;
291 while (idx
< fir_len
- 1) {
292 fir_copy
[fir_used
++] = fir
[idx
] * (1.0 - rem
) + fir
[idx
+ 1] * rem
;
296 assert(fir_used
<= fir_cachesize
);
297 assert(ipos
+ fir_used
<= required_input
);
299 for (channel
= 0; channel
< dsb
->mix_channels
; channel
++) {
302 float* cache
= &intermediate
[channel
* required_input
+ ipos
];
303 for (j
= 0; j
< fir_used
; j
++)
304 sum
+= fir_copy
[j
] * cache
[j
];
305 dsb
->put(dsb
, i
* ostride
, channel
, sum
* dsb
->firgain
);
309 freqAcc_end
-= (int)freqAcc_end
;
310 *freqAcc
= freqAcc_end
;
312 HeapFree(GetProcessHeap(), 0, fir_copy
);
313 HeapFree(GetProcessHeap(), 0, intermediate
);
318 static void cp_fields(IDirectSoundBufferImpl
*dsb
, UINT count
, float *freqAcc
)
322 if (dsb
->freqAdjust
== 1.0)
323 adv
= cp_fields_noresample(dsb
, count
); /* *freqAcc is unmodified */
325 adv
= cp_fields_resample(dsb
, count
, freqAcc
);
327 ipos
= dsb
->sec_mixpos
+ adv
* dsb
->pwfx
->nBlockAlign
;
328 if (ipos
>= dsb
->buflen
) {
329 if (dsb
->playflags
& DSBPLAY_LOOPING
)
333 dsb
->state
= STATE_STOPPED
;
337 dsb
->sec_mixpos
= ipos
;
341 * Calculate the distance between two buffer offsets, taking wraparound
344 static inline DWORD
DSOUND_BufPtrDiff(DWORD buflen
, DWORD ptr1
, DWORD ptr2
)
346 /* If these asserts fail, the problem is not here, but in the underlying code */
347 assert(ptr1
< buflen
);
348 assert(ptr2
< buflen
);
352 return buflen
+ ptr1
- ptr2
;
356 * Mix at most the given amount of data into the allocated temporary buffer
357 * of the given secondary buffer, starting from the dsb's first currently
358 * unsampled frame (writepos), translating frequency (pitch), stereo/mono
359 * and bits-per-sample so that it is ideal for the primary buffer.
360 * Doesn't perform any mixing - this is a straight copy/convert operation.
362 * dsb = the secondary buffer
363 * writepos = Starting position of changed buffer
364 * len = number of bytes to resample from writepos
366 * NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
368 static void DSOUND_MixToTemporary(IDirectSoundBufferImpl
*dsb
, DWORD frames
)
370 UINT size_bytes
= frames
* sizeof(float) * dsb
->device
->pwfx
->nChannels
;
372 if (dsb
->device
->tmp_buffer_len
< size_bytes
|| !dsb
->device
->tmp_buffer
)
374 dsb
->device
->tmp_buffer_len
= size_bytes
;
375 if (dsb
->device
->tmp_buffer
)
376 dsb
->device
->tmp_buffer
= HeapReAlloc(GetProcessHeap(), 0, dsb
->device
->tmp_buffer
, size_bytes
);
378 dsb
->device
->tmp_buffer
= HeapAlloc(GetProcessHeap(), 0, size_bytes
);
381 cp_fields(dsb
, frames
, &dsb
->freqAcc
);
384 static void DSOUND_MixerVol(const IDirectSoundBufferImpl
*dsb
, INT frames
)
388 UINT channels
= dsb
->device
->pwfx
->nChannels
, chan
;
390 TRACE("(%p,%d)\n",dsb
,frames
);
391 TRACE("left = %x, right = %x\n", dsb
->volpan
.dwTotalLeftAmpFactor
,
392 dsb
->volpan
.dwTotalRightAmpFactor
);
394 if ((!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPAN
) || (dsb
->volpan
.lPan
== 0)) &&
395 (!(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLVOLUME
) || (dsb
->volpan
.lVolume
== 0)) &&
396 !(dsb
->dsbd
.dwFlags
& DSBCAPS_CTRL3D
))
397 return; /* Nothing to do */
399 if (channels
!= 1 && channels
!= 2)
401 FIXME("There is no support for %u channels\n", channels
);
405 vLeft
= dsb
->volpan
.dwTotalLeftAmpFactor
/ ((float)0xFFFF);
406 vRight
= dsb
->volpan
.dwTotalRightAmpFactor
/ ((float)0xFFFF);
407 for(i
= 0; i
< frames
; ++i
){
408 for(chan
= 0; chan
< channels
; ++chan
){
410 dsb
->device
->tmp_buffer
[i
* channels
+ chan
] *= vLeft
;
412 dsb
->device
->tmp_buffer
[i
* channels
+ chan
] *= vRight
;
418 * Mix (at most) the given number of bytes into the given position of the
419 * device buffer, from the secondary buffer "dsb" (starting at the current
420 * mix position for that buffer).
422 * Returns the number of bytes actually mixed into the device buffer. This
423 * will match fraglen unless the end of the secondary buffer is reached
424 * (and it is not looping).
426 * dsb = the secondary buffer to mix from
427 * writepos = position (offset) in device buffer to write at
428 * fraglen = number of bytes to mix
430 static DWORD
DSOUND_MixInBuffer(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD fraglen
)
435 UINT frames
= fraglen
/ dsb
->device
->pwfx
->nBlockAlign
;
437 TRACE("sec_mixpos=%d/%d\n", dsb
->sec_mixpos
, dsb
->buflen
);
438 TRACE("(%p,%d,%d)\n",dsb
,writepos
,fraglen
);
440 if (len
% dsb
->device
->pwfx
->nBlockAlign
) {
441 INT nBlockAlign
= dsb
->device
->pwfx
->nBlockAlign
;
442 ERR("length not a multiple of block size, len = %d, block size = %d\n", len
, nBlockAlign
);
443 len
-= len
% nBlockAlign
; /* data alignment */
446 /* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
447 oldpos
= dsb
->sec_mixpos
;
449 DSOUND_MixToTemporary(dsb
, frames
);
450 ibuf
= dsb
->device
->tmp_buffer
;
452 /* Apply volume if needed */
453 DSOUND_MixerVol(dsb
, frames
);
455 mixieee32(ibuf
, dsb
->device
->mix_buffer
, frames
* dsb
->device
->pwfx
->nChannels
);
457 /* check for notification positions */
458 if (dsb
->dsbd
.dwFlags
& DSBCAPS_CTRLPOSITIONNOTIFY
&&
459 dsb
->state
!= STATE_STARTING
) {
460 INT ilen
= DSOUND_BufPtrDiff(dsb
->buflen
, dsb
->sec_mixpos
, oldpos
);
461 DSOUND_CheckEvent(dsb
, oldpos
, ilen
);
468 * Mix some frames from the given secondary buffer "dsb" into the device
471 * dsb = the secondary buffer
472 * playpos = the current play position in the device buffer (primary buffer)
473 * writepos = the current safe-to-write position in the device buffer
474 * mixlen = the maximum number of bytes in the primary buffer to mix, from the
477 * Returns: the number of bytes beyond the writepos that were mixed.
479 static DWORD
DSOUND_MixOne(IDirectSoundBufferImpl
*dsb
, DWORD writepos
, DWORD mixlen
)
481 DWORD primary_done
= 0;
483 TRACE("(%p,%d,%d)\n",dsb
,writepos
,mixlen
);
484 TRACE("writepos=%d, mixlen=%d\n", writepos
, mixlen
);
485 TRACE("looping=%d, leadin=%d\n", dsb
->playflags
, dsb
->leadin
);
487 /* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
488 /* FIXME: Is this needed? */
489 if (dsb
->leadin
&& dsb
->state
== STATE_STARTING
) {
490 if (mixlen
> 2 * dsb
->device
->fraglen
) {
491 primary_done
= mixlen
- 2 * dsb
->device
->fraglen
;
492 mixlen
= 2 * dsb
->device
->fraglen
;
493 writepos
+= primary_done
;
494 dsb
->sec_mixpos
+= (primary_done
/ dsb
->device
->pwfx
->nBlockAlign
) *
495 dsb
->pwfx
->nBlockAlign
* dsb
->freqAdjust
;
501 TRACE("mixlen (primary) = %i\n", mixlen
);
503 /* First try to mix to the end of the buffer if possible
504 * Theoretically it would allow for better optimization
506 primary_done
+= DSOUND_MixInBuffer(dsb
, writepos
, mixlen
);
508 TRACE("total mixed data=%d\n", primary_done
);
510 /* Report back the total prebuffered amount for this buffer */
515 * For a DirectSoundDevice, go through all the currently playing buffers and
516 * mix them in to the device buffer.
518 * writepos = the current safe-to-write position in the primary buffer
519 * mixlen = the maximum amount to mix into the primary buffer
520 * (beyond the current writepos)
521 * recover = true if the sound device may have been reset and the write
522 * position in the device buffer changed
523 * all_stopped = reports back if all buffers have stopped
525 * Returns: the length beyond the writepos that was mixed to.
528 static void DSOUND_MixToPrimary(const DirectSoundDevice
*device
, DWORD writepos
, DWORD mixlen
, BOOL recover
, BOOL
*all_stopped
)
531 IDirectSoundBufferImpl
*dsb
;
533 /* unless we find a running buffer, all have stopped */
536 TRACE("(%d,%d,%d)\n", writepos
, mixlen
, recover
);
537 for (i
= 0; i
< device
->nrofbuffers
; i
++) {
538 dsb
= device
->buffers
[i
];
540 TRACE("MixToPrimary for %p, state=%d\n", dsb
, dsb
->state
);
542 if (dsb
->buflen
&& dsb
->state
) {
543 TRACE("Checking %p, mixlen=%d\n", dsb
, mixlen
);
544 RtlAcquireResourceShared(&dsb
->lock
, TRUE
);
545 /* if buffer is stopping it is stopped now */
546 if (dsb
->state
== STATE_STOPPING
) {
547 dsb
->state
= STATE_STOPPED
;
548 DSOUND_CheckEvent(dsb
, 0, 0);
549 } else if (dsb
->state
!= STATE_STOPPED
) {
551 /* if the buffer was starting, it must be playing now */
552 if (dsb
->state
== STATE_STARTING
)
553 dsb
->state
= STATE_PLAYING
;
555 /* mix next buffer into the main buffer */
556 DSOUND_MixOne(dsb
, writepos
, mixlen
);
558 *all_stopped
= FALSE
;
560 RtlReleaseResource(&dsb
->lock
);
566 * Add buffers to the emulated wave device system.
568 * device = The current dsound playback device
569 * force = If TRUE, the function will buffer up as many frags as possible,
570 * even though and will ignore the actual state of the primary buffer.
575 static void DSOUND_WaveQueue(DirectSoundDevice
*device
, BOOL force
)
577 DWORD prebuf_frames
, prebuf_bytes
, read_offs_bytes
;
581 TRACE("(%p)\n", device
);
583 read_offs_bytes
= (device
->playing_offs_bytes
+ device
->in_mmdev_bytes
) % device
->buflen
;
585 TRACE("read_offs_bytes = %u, playing_offs_bytes = %u, in_mmdev_bytes: %u, prebuf = %u\n",
586 read_offs_bytes
, device
->playing_offs_bytes
, device
->in_mmdev_bytes
, device
->prebuf
);
590 if(device
->mixpos
< device
->playing_offs_bytes
)
591 prebuf_bytes
= device
->mixpos
+ device
->buflen
- device
->playing_offs_bytes
;
593 prebuf_bytes
= device
->mixpos
- device
->playing_offs_bytes
;
596 /* buffer the maximum amount of frags */
597 prebuf_bytes
= device
->prebuf
* device
->fraglen
;
599 /* limit to the queue we have left */
600 if(device
->in_mmdev_bytes
+ prebuf_bytes
> device
->prebuf
* device
->fraglen
)
601 prebuf_bytes
= device
->prebuf
* device
->fraglen
- device
->in_mmdev_bytes
;
603 TRACE("prebuf_bytes = %u\n", prebuf_bytes
);
608 if(prebuf_bytes
+ read_offs_bytes
> device
->buflen
){
609 DWORD chunk_bytes
= device
->buflen
- read_offs_bytes
;
610 prebuf_frames
= chunk_bytes
/ device
->pwfx
->nBlockAlign
;
611 prebuf_bytes
-= chunk_bytes
;
613 prebuf_frames
= prebuf_bytes
/ device
->pwfx
->nBlockAlign
;
617 hr
= IAudioRenderClient_GetBuffer(device
->render
, prebuf_frames
, &buffer
);
619 WARN("GetBuffer failed: %08x\n", hr
);
623 memcpy(buffer
, device
->buffer
+ read_offs_bytes
,
624 prebuf_frames
* device
->pwfx
->nBlockAlign
);
626 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, prebuf_frames
, 0);
628 WARN("ReleaseBuffer failed: %08x\n", hr
);
632 device
->in_mmdev_bytes
+= prebuf_frames
* device
->pwfx
->nBlockAlign
;
634 /* check if anything wrapped */
635 if(prebuf_bytes
> 0){
636 prebuf_frames
= prebuf_bytes
/ device
->pwfx
->nBlockAlign
;
638 hr
= IAudioRenderClient_GetBuffer(device
->render
, prebuf_frames
, &buffer
);
640 WARN("GetBuffer failed: %08x\n", hr
);
644 memcpy(buffer
, device
->buffer
, prebuf_frames
* device
->pwfx
->nBlockAlign
);
646 hr
= IAudioRenderClient_ReleaseBuffer(device
->render
, prebuf_frames
, 0);
648 WARN("ReleaseBuffer failed: %08x\n", hr
);
651 device
->in_mmdev_bytes
+= prebuf_frames
* device
->pwfx
->nBlockAlign
;
654 TRACE("in_mmdev_bytes now = %i\n", device
->in_mmdev_bytes
);
658 * Perform mixing for a Direct Sound device. That is, go through all the
659 * secondary buffers (the sound bites currently playing) and mix them in
660 * to the primary buffer (the device buffer).
662 * The mixing procedure goes:
664 * secondary->buffer (secondary format)
665 * =[Resample]=> device->tmp_buffer (float format)
666 * =[Volume]=> device->tmp_buffer (float format)
667 * =[Mix]=> device->mix_buffer (float format)
668 * =[Reformat]=> device->buffer (device format)
670 static void DSOUND_PerformMix(DirectSoundDevice
*device
)
672 UINT32 pad
, to_mix_frags
, to_mix_bytes
;
675 TRACE("(%p)\n", device
);
678 EnterCriticalSection(&device
->mixlock
);
680 hr
= IAudioClient_GetCurrentPadding(device
->client
, &pad
);
682 WARN("GetCurrentPadding failed: %08x\n", hr
);
683 LeaveCriticalSection(&device
->mixlock
);
687 to_mix_frags
= device
->prebuf
- (pad
* device
->pwfx
->nBlockAlign
+ device
->fraglen
- 1) / device
->fraglen
;
689 to_mix_bytes
= to_mix_frags
* device
->fraglen
;
691 if(device
->in_mmdev_bytes
> 0){
692 DWORD delta_bytes
= min(to_mix_bytes
, device
->in_mmdev_bytes
);
693 device
->in_mmdev_bytes
-= delta_bytes
;
694 device
->playing_offs_bytes
+= delta_bytes
;
695 device
->playing_offs_bytes
%= device
->buflen
;
698 if (device
->priolevel
!= DSSCL_WRITEPRIMARY
) {
699 BOOL recover
= FALSE
, all_stopped
= FALSE
;
700 DWORD playpos
, writepos
, writelead
, maxq
, prebuff_max
, prebuff_left
, size1
, size2
;
704 /* the sound of silence */
705 nfiller
= device
->pwfx
->wBitsPerSample
== 8 ? 128 : 0;
707 /* get the position in the primary buffer */
708 if (DSOUND_PrimaryGetPosition(device
, &playpos
, &writepos
) != 0){
709 LeaveCriticalSection(&(device
->mixlock
));
713 TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
714 playpos
,writepos
,device
->playpos
,device
->mixpos
,device
->buflen
);
715 assert(device
->playpos
< device
->buflen
);
717 /* calc maximum prebuff */
718 prebuff_max
= (device
->prebuf
* device
->fraglen
);
720 /* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
721 prebuff_left
= DSOUND_BufPtrDiff(device
->buflen
, device
->mixpos
, playpos
);
722 writelead
= DSOUND_BufPtrDiff(device
->buflen
, writepos
, playpos
);
724 /* check for underrun. underrun occurs when the write position passes the mix position
725 * also wipe out just-played sound data */
726 if((prebuff_left
> prebuff_max
) || (device
->state
== STATE_STOPPED
) || (device
->state
== STATE_STARTING
)){
727 if (device
->state
== STATE_STOPPING
|| device
->state
== STATE_PLAYING
)
728 WARN("Probable buffer underrun\n");
729 else TRACE("Buffer starting or buffer underrun\n");
731 /* recover mixing for all buffers */
734 /* reset mix position to write position */
735 device
->mixpos
= writepos
;
737 ZeroMemory(device
->buffer
, device
->buflen
);
738 } else if (playpos
< device
->playpos
) {
739 buf1
= device
->buffer
+ device
->playpos
;
740 buf2
= device
->buffer
;
741 size1
= device
->buflen
- device
->playpos
;
743 FillMemory(buf1
, size1
, nfiller
);
744 if (playpos
&& (!buf2
|| !size2
))
745 FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__
, device
->playpos
, device
->mixpos
, playpos
, writepos
);
746 FillMemory(buf2
, size2
, nfiller
);
748 buf1
= device
->buffer
+ device
->playpos
;
750 size1
= playpos
- device
->playpos
;
752 FillMemory(buf1
, size1
, nfiller
);
754 device
->playpos
= playpos
;
756 /* find the maximum we can prebuffer from current write position */
757 maxq
= (writelead
< prebuff_max
) ? (prebuff_max
- writelead
) : 0;
759 TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
760 prebuff_left
, device
->prebuf
, device
->fraglen
, prebuff_max
, writelead
);
762 ZeroMemory(device
->mix_buffer
, device
->mix_buffer_len
);
765 DSOUND_MixToPrimary(device
, writepos
, maxq
, recover
, &all_stopped
);
767 if (maxq
+ writepos
> device
->buflen
)
769 DWORD todo
= device
->buflen
- writepos
;
770 DWORD offs_float
= (todo
/ device
->pwfx
->nBlockAlign
) * device
->pwfx
->nChannels
;
771 device
->normfunction(device
->mix_buffer
, device
->buffer
+ writepos
, todo
);
772 device
->normfunction(device
->mix_buffer
+ offs_float
, device
->buffer
, maxq
- todo
);
775 device
->normfunction(device
->mix_buffer
, device
->buffer
+ writepos
, maxq
);
777 /* update the mix position, taking wrap-around into account */
778 device
->mixpos
= writepos
+ maxq
;
779 device
->mixpos
%= device
->buflen
;
781 /* update prebuff left */
782 prebuff_left
= DSOUND_BufPtrDiff(device
->buflen
, device
->mixpos
, playpos
);
784 /* check if have a whole fragment */
785 if (prebuff_left
>= device
->fraglen
){
787 /* update the wave queue */
788 DSOUND_WaveQueue(device
, FALSE
);
790 /* buffers are full. start playing if applicable */
791 if(device
->state
== STATE_STARTING
){
792 TRACE("started primary buffer\n");
793 if(DSOUND_PrimaryPlay(device
) != DS_OK
){
794 WARN("DSOUND_PrimaryPlay failed\n");
797 /* we are playing now */
798 device
->state
= STATE_PLAYING
;
802 /* buffers are full. start stopping if applicable */
803 if(device
->state
== STATE_STOPPED
){
804 TRACE("restarting primary buffer\n");
805 if(DSOUND_PrimaryPlay(device
) != DS_OK
){
806 WARN("DSOUND_PrimaryPlay failed\n");
809 /* start stopping again. as soon as there is no more data, it will stop */
810 device
->state
= STATE_STOPPING
;
815 /* if device was stopping, its for sure stopped when all buffers have stopped */
816 else if((all_stopped
== TRUE
) && (device
->state
== STATE_STOPPING
)){
817 TRACE("All buffers have stopped. Stopping primary buffer\n");
818 device
->state
= STATE_STOPPED
;
820 /* stop the primary buffer now */
821 DSOUND_PrimaryStop(device
);
824 } else if (device
->state
!= STATE_STOPPED
) {
826 DSOUND_WaveQueue(device
, TRUE
);
828 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
829 if (device
->state
== STATE_STARTING
) {
830 if (DSOUND_PrimaryPlay(device
) != DS_OK
)
831 WARN("DSOUND_PrimaryPlay failed\n");
833 device
->state
= STATE_PLAYING
;
835 else if (device
->state
== STATE_STOPPING
) {
836 if (DSOUND_PrimaryStop(device
) != DS_OK
)
837 WARN("DSOUND_PrimaryStop failed\n");
839 device
->state
= STATE_STOPPED
;
843 LeaveCriticalSection(&(device
->mixlock
));
847 DWORD CALLBACK
DSOUND_mixthread(void *p
)
849 DirectSoundDevice
*dev
= p
;
850 TRACE("(%p)\n", dev
);
856 * Some audio drivers are retarded and won't fire after being
857 * stopped, add a timeout to handle this.
859 ret
= WaitForSingleObject(dev
->sleepev
, dev
->sleeptime
);
860 if (ret
== WAIT_FAILED
)
861 WARN("wait returned error %u %08x!\n", GetLastError(), GetLastError());
862 else if (ret
!= WAIT_OBJECT_0
)
863 WARN("wait returned %08x!\n", ret
);
867 RtlAcquireResourceShared(&(dev
->buffer_list_lock
), TRUE
);
868 DSOUND_PerformMix(dev
);
869 RtlReleaseResource(&(dev
->buffer_list_lock
));