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;
116 if ((pwfxe->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) || ((pwfxe->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE)
117 && (IsEqualGUID(&pwfxe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))))
121 * Recalculate FIR step and gain.
123 * firstep says how many points of the FIR exist per one
124 * sample in the secondary buffer. firgain specifies what
125 * to multiply the FIR output by in order to attenuate it correctly.
127 if (dsb->freqAdjust > 1.0f) {
129 * Yes, round it a bit to make sure that the
130 * linear interpolation factor never changes.
132 dsb->firstep = ceil(fir_step / dsb->freqAdjust);
134 dsb->firstep = fir_step;
136 dsb->firgain = (float)dsb->firstep / fir_step;
138 /* calculate the 10ms write lead */
139 dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
143 dsb->get_aux = ieee ? getbpp[4] : getbpp[dsb->pwfx->wBitsPerSample/8 - 1];
144 dsb->put_aux = putieee32;
146 dsb->get = dsb->get_aux;
147 dsb->put = dsb->put_aux;
149 if (ichannels == ochannels)
151 dsb->mix_channels = ichannels;
152 if (ichannels > 32) {
153 FIXME("Copying %u channels is unsupported, limiting to first 32\n", ichannels);
154 dsb->mix_channels = 32;
157 else if (ichannels == 1)
159 dsb->mix_channels = 1;
160 dsb->put = put_mono2stereo;
162 else if (ochannels == 1)
164 dsb->mix_channels = 1;
170 FIXME("Conversion from %u to %u channels is not implemented, falling back to stereo\n", ichannels, ochannels);
171 dsb->mix_channels = 2;
176 * Check for application callback requests for when the play position
177 * reaches certain points.
179 * The offsets that will be triggered will be those between the recorded
180 * "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
181 * beyond that position.
183 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
187 LPDSBPOSITIONNOTIFY event;
188 TRACE("(%p,%d)\n",dsb,len);
190 if (dsb->nrofnotifies == 0)
193 TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
194 dsb, dsb->buflen, playpos, len);
195 for (i = 0; i < dsb->nrofnotifies ; i++) {
196 event = dsb->notifies + i;
197 offset = event->dwOffset;
198 TRACE("checking %d, position %d, event = %p\n",
199 i, offset, event->hEventNotify);
200 /* DSBPN_OFFSETSTOP has to be the last element. So this is */
201 /* OK. [Inside DirectX, p274] */
202 /* Windows does not seem to enforce this, and some apps rely */
203 /* on that, so we can't stop there. */
205 /* This also means we can't sort the entries by offset, */
206 /* because DSBPN_OFFSETSTOP == -1 */
207 if (offset == DSBPN_OFFSETSTOP) {
208 if (dsb->state == STATE_STOPPED) {
209 SetEvent(event->hEventNotify);
210 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
214 if ((playpos + len) >= dsb->buflen) {
215 if ((offset < ((playpos + len) % dsb->buflen)) ||
216 (offset >= playpos)) {
217 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
218 SetEvent(event->hEventNotify);
221 if ((offset >= playpos) && (offset < (playpos + len))) {
222 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
223 SetEvent(event->hEventNotify);
229 static inline float get_current_sample(const IDirectSoundBufferImpl *dsb,
230 DWORD mixpos, DWORD channel)
232 if (mixpos >= dsb->buflen && !(dsb->playflags & DSBPLAY_LOOPING))
234 return dsb->get(dsb, mixpos % dsb->buflen, channel);
237 static UINT cp_fields_noresample(IDirectSoundBufferImpl *dsb, UINT count)
239 UINT istride = dsb->pwfx->nBlockAlign;
240 UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
242 for (i = 0; i < count; i++)
243 for (channel = 0; channel < dsb->mix_channels; channel++)
244 dsb->put(dsb, i * ostride, channel, get_current_sample(dsb,
245 dsb->sec_mixpos + i * istride, channel));
249 static UINT cp_fields_resample(IDirectSoundBufferImpl *dsb, UINT count, float *freqAcc)
252 UINT istride = dsb->pwfx->nBlockAlign;
253 UINT ostride = dsb->device->pwfx->nChannels * sizeof(float);
255 float freqAdjust = dsb->freqAdjust;
256 float freqAcc_start = *freqAcc;
257 float freqAcc_end = freqAcc_start + count * freqAdjust;
258 UINT dsbfirstep = dsb->firstep;
259 UINT channels = dsb->mix_channels;
260 UINT max_ipos = freqAcc_start + count * freqAdjust;
262 UINT fir_cachesize = (fir_len + dsbfirstep - 2) / dsbfirstep;
263 UINT required_input = max_ipos + fir_cachesize;
265 float* intermediate = HeapAlloc(GetProcessHeap(), 0,
266 sizeof(float) * required_input * channels);
268 float* fir_copy = HeapAlloc(GetProcessHeap(), 0,
269 sizeof(float) * fir_cachesize);
271 /* Important: this buffer MUST be non-interleaved
272 * if you want -msse3 to have any effect.
273 * This is good for CPU cache effects, too.
275 float* itmp = intermediate;
276 for (channel = 0; channel < channels; channel++)
277 for (i = 0; i < required_input; i++)
278 *(itmp++) = get_current_sample(dsb,
279 dsb->sec_mixpos + i * istride, channel);
281 for(i = 0; i < count; ++i) {
282 float total_fir_steps = (freqAcc_start + i * freqAdjust) * dsbfirstep;
283 UINT int_fir_steps = total_fir_steps;
284 UINT ipos = int_fir_steps / dsbfirstep;
286 UINT idx = (ipos + 1) * dsbfirstep - int_fir_steps - 1;
287 float rem = int_fir_steps + 1.0 - total_fir_steps;
290 while (idx < fir_len - 1) {
291 fir_copy[fir_used++] = fir[idx] * (1.0 - rem) + fir[idx + 1] * rem;
295 assert(fir_used <= fir_cachesize);
296 assert(ipos + fir_used <= required_input);
298 for (channel = 0; channel < dsb->mix_channels; channel++) {
301 float* cache = &intermediate[channel * required_input + ipos];
302 for (j = 0; j < fir_used; j++)
303 sum += fir_copy[j] * cache[j];
304 dsb->put(dsb, i * ostride, channel, sum * dsb->firgain);
308 freqAcc_end -= (int)freqAcc_end;
309 *freqAcc = freqAcc_end;
311 HeapFree(GetProcessHeap(), 0, fir_copy);
312 HeapFree(GetProcessHeap(), 0, intermediate);
317 static void cp_fields(IDirectSoundBufferImpl *dsb, UINT count, float *freqAcc)
321 if (dsb->freqAdjust == 1.0)
322 adv = cp_fields_noresample(dsb, count); /* *freqAcc is unmodified */
324 adv = cp_fields_resample(dsb, count, freqAcc);
326 ipos = dsb->sec_mixpos + adv * dsb->pwfx->nBlockAlign;
327 if (ipos >= dsb->buflen) {
328 if (dsb->playflags & DSBPLAY_LOOPING)
332 dsb->state = STATE_STOPPED;
336 dsb->sec_mixpos = ipos;
340 * Calculate the distance between two buffer offsets, taking wraparound
343 static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
345 /* If these asserts fail, the problem is not here, but in the underlying code */
346 assert(ptr1 < buflen);
347 assert(ptr2 < buflen);
351 return buflen + ptr1 - ptr2;
355 * Mix at most the given amount of data into the allocated temporary buffer
356 * of the given secondary buffer, starting from the dsb's first currently
357 * unsampled frame (writepos), translating frequency (pitch), stereo/mono
358 * and bits-per-sample so that it is ideal for the primary buffer.
359 * Doesn't perform any mixing - this is a straight copy/convert operation.
361 * dsb = the secondary buffer
362 * writepos = Starting position of changed buffer
363 * len = number of bytes to resample from writepos
365 * NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
367 static void DSOUND_MixToTemporary(IDirectSoundBufferImpl *dsb, DWORD frames)
369 UINT size_bytes = frames * sizeof(float) * dsb->device->pwfx->nChannels;
371 if (dsb->device->tmp_buffer_len < size_bytes || !dsb->device->tmp_buffer)
373 dsb->device->tmp_buffer_len = size_bytes;
374 if (dsb->device->tmp_buffer)
375 dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, size_bytes);
377 dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, size_bytes);
380 cp_fields(dsb, frames, &dsb->freqAcc);
383 static void DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT frames)
387 UINT channels = dsb->device->pwfx->nChannels, chan;
389 TRACE("(%p,%d)\n",dsb,frames);
390 TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
391 dsb->volpan.dwTotalRightAmpFactor);
393 if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
394 (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
395 !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
396 return; /* Nothing to do */
398 if (channels != 1 && channels != 2)
400 FIXME("There is no support for %u channels\n", channels);
404 vLeft = dsb->volpan.dwTotalLeftAmpFactor / ((float)0xFFFF);
405 vRight = dsb->volpan.dwTotalRightAmpFactor / ((float)0xFFFF);
406 for(i = 0; i < frames; ++i){
407 for(chan = 0; chan < channels; ++chan){
409 dsb->device->tmp_buffer[i * channels + chan] *= vLeft;
411 dsb->device->tmp_buffer[i * channels + chan] *= vRight;
417 * Mix (at most) the given number of bytes into the given position of the
418 * device buffer, from the secondary buffer "dsb" (starting at the current
419 * mix position for that buffer).
421 * Returns the number of bytes actually mixed into the device buffer. This
422 * will match fraglen unless the end of the secondary buffer is reached
423 * (and it is not looping).
425 * dsb = the secondary buffer to mix from
426 * writepos = position (offset) in device buffer to write at
427 * fraglen = number of bytes to mix
429 static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
434 UINT frames = fraglen / dsb->device->pwfx->nBlockAlign;
436 TRACE("sec_mixpos=%d/%d\n", dsb->sec_mixpos, dsb->buflen);
437 TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
439 if (len % dsb->device->pwfx->nBlockAlign) {
440 INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
441 ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
442 len -= len % nBlockAlign; /* data alignment */
445 /* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
446 oldpos = dsb->sec_mixpos;
448 DSOUND_MixToTemporary(dsb, frames);
449 ibuf = dsb->device->tmp_buffer;
451 /* Apply volume if needed */
452 DSOUND_MixerVol(dsb, frames);
454 mixieee32(ibuf, dsb->device->mix_buffer, frames * dsb->device->pwfx->nChannels);
456 /* check for notification positions */
457 if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
458 dsb->state != STATE_STARTING) {
459 INT ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
460 DSOUND_CheckEvent(dsb, oldpos, ilen);
467 * Mix some frames from the given secondary buffer "dsb" into the device
470 * dsb = the secondary buffer
471 * playpos = the current play position in the device buffer (primary buffer)
472 * writepos = the current safe-to-write position in the device buffer
473 * mixlen = the maximum number of bytes in the primary buffer to mix, from the
476 * Returns: the number of bytes beyond the writepos that were mixed.
478 static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
480 DWORD primary_done = 0;
482 TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
483 TRACE("writepos=%d, mixlen=%d\n", writepos, mixlen);
484 TRACE("looping=%d, leadin=%d\n", dsb->playflags, dsb->leadin);
486 /* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
487 /* FIXME: Is this needed? */
488 if (dsb->leadin && dsb->state == STATE_STARTING) {
489 if (mixlen > 2 * dsb->device->fraglen) {
490 primary_done = mixlen - 2 * dsb->device->fraglen;
491 mixlen = 2 * dsb->device->fraglen;
492 writepos += primary_done;
493 dsb->sec_mixpos += (primary_done / dsb->device->pwfx->nBlockAlign) *
494 dsb->pwfx->nBlockAlign * dsb->freqAdjust;
500 TRACE("mixlen (primary) = %i\n", mixlen);
502 /* First try to mix to the end of the buffer if possible
503 * Theoretically it would allow for better optimization
505 primary_done += DSOUND_MixInBuffer(dsb, writepos, mixlen);
507 TRACE("total mixed data=%d\n", primary_done);
509 /* Report back the total prebuffered amount for this buffer */
514 * For a DirectSoundDevice, go through all the currently playing buffers and
515 * mix them in to the device buffer.
517 * writepos = the current safe-to-write position in the primary buffer
518 * mixlen = the maximum amount to mix into the primary buffer
519 * (beyond the current writepos)
520 * recover = true if the sound device may have been reset and the write
521 * position in the device buffer changed
522 * all_stopped = reports back if all buffers have stopped
524 * Returns: the length beyond the writepos that was mixed to.
527 static void DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos, DWORD mixlen, BOOL recover, BOOL *all_stopped)
530 IDirectSoundBufferImpl *dsb;
532 /* unless we find a running buffer, all have stopped */
535 TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
536 for (i = 0; i < device->nrofbuffers; i++) {
537 dsb = device->buffers[i];
539 TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
541 if (dsb->buflen && dsb->state) {
542 TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
543 RtlAcquireResourceShared(&dsb->lock, TRUE);
544 /* if buffer is stopping it is stopped now */
545 if (dsb->state == STATE_STOPPING) {
546 dsb->state = STATE_STOPPED;
547 DSOUND_CheckEvent(dsb, 0, 0);
548 } else if (dsb->state != STATE_STOPPED) {
550 /* if the buffer was starting, it must be playing now */
551 if (dsb->state == STATE_STARTING)
552 dsb->state = STATE_PLAYING;
554 /* mix next buffer into the main buffer */
555 DSOUND_MixOne(dsb, writepos, mixlen);
557 *all_stopped = FALSE;
559 RtlReleaseResource(&dsb->lock);
565 * Add buffers to the emulated wave device system.
567 * device = The current dsound playback device
568 * force = If TRUE, the function will buffer up as many frags as possible,
569 * even though and will ignore the actual state of the primary buffer.
574 static void DSOUND_WaveQueue(DirectSoundDevice *device, BOOL force)
576 DWORD prebuf_frames, prebuf_bytes, read_offs_bytes;
580 TRACE("(%p)\n", device);
582 read_offs_bytes = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
584 TRACE("read_offs_bytes = %u, playing_offs_bytes = %u, in_mmdev_bytes: %u, prebuf = %u\n",
585 read_offs_bytes, device->playing_offs_bytes, device->in_mmdev_bytes, device->prebuf);
589 if(device->mixpos < device->playing_offs_bytes)
590 prebuf_bytes = device->mixpos + device->buflen - device->playing_offs_bytes;
592 prebuf_bytes = device->mixpos - device->playing_offs_bytes;
595 /* buffer the maximum amount of frags */
596 prebuf_bytes = device->prebuf * device->fraglen;
598 /* limit to the queue we have left */
599 if(device->in_mmdev_bytes + prebuf_bytes > device->prebuf * device->fraglen)
600 prebuf_bytes = device->prebuf * device->fraglen - device->in_mmdev_bytes;
602 TRACE("prebuf_bytes = %u\n", prebuf_bytes);
607 device->in_mmdev_bytes += prebuf_bytes;
609 if(prebuf_bytes + read_offs_bytes > device->buflen){
610 DWORD chunk_bytes = device->buflen - read_offs_bytes;
611 prebuf_frames = chunk_bytes / device->pwfx->nBlockAlign;
612 prebuf_bytes -= chunk_bytes;
614 prebuf_frames = prebuf_bytes / device->pwfx->nBlockAlign;
618 hr = IAudioRenderClient_GetBuffer(device->render, prebuf_frames, &buffer);
620 WARN("GetBuffer failed: %08x\n", hr);
624 memcpy(buffer, device->buffer + read_offs_bytes,
625 prebuf_frames * device->pwfx->nBlockAlign);
627 hr = IAudioRenderClient_ReleaseBuffer(device->render, prebuf_frames, 0);
629 WARN("ReleaseBuffer failed: %08x\n", hr);
633 /* check if anything wrapped */
634 if(prebuf_bytes > 0){
635 prebuf_frames = prebuf_bytes / device->pwfx->nBlockAlign;
637 hr = IAudioRenderClient_GetBuffer(device->render, prebuf_frames, &buffer);
639 WARN("GetBuffer failed: %08x\n", hr);
643 memcpy(buffer, device->buffer, prebuf_frames * device->pwfx->nBlockAlign);
645 hr = IAudioRenderClient_ReleaseBuffer(device->render, prebuf_frames, 0);
647 WARN("ReleaseBuffer failed: %08x\n", hr);
652 TRACE("in_mmdev_bytes now = %i\n", device->in_mmdev_bytes);
656 * Perform mixing for a Direct Sound device. That is, go through all the
657 * secondary buffers (the sound bites currently playing) and mix them in
658 * to the primary buffer (the device buffer).
660 * The mixing procedure goes:
662 * secondary->buffer (secondary format)
663 * =[Resample]=> device->tmp_buffer (float format)
664 * =[Volume]=> device->tmp_buffer (float format)
665 * =[Mix]=> device->mix_buffer (float format)
666 * =[Reformat]=> device->buffer (device format)
668 static void DSOUND_PerformMix(DirectSoundDevice *device)
670 UINT64 clock_pos, clock_freq, pos_bytes;
674 TRACE("(%p)\n", device);
677 EnterCriticalSection(&device->mixlock);
679 hr = IAudioClock_GetFrequency(device->clock, &clock_freq);
681 WARN("GetFrequency failed: %08x\n", hr);
682 LeaveCriticalSection(&device->mixlock);
686 hr = IAudioClock_GetPosition(device->clock, &clock_pos, NULL);
688 WARN("GetCurrentPadding failed: %08x\n", hr);
689 LeaveCriticalSection(&device->mixlock);
693 pos_bytes = (clock_pos * device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign) / clock_freq;
695 delta_frags = (pos_bytes - device->last_pos_bytes) / device->fraglen;
697 device->playing_offs_bytes += delta_frags * device->fraglen;
698 device->playing_offs_bytes %= device->buflen;
699 device->in_mmdev_bytes -= delta_frags * device->fraglen;
700 device->last_pos_bytes = pos_bytes - (pos_bytes % device->fraglen);
703 if (device->priolevel != DSSCL_WRITEPRIMARY) {
704 BOOL recover = FALSE, all_stopped = FALSE;
705 DWORD playpos, writepos, writelead, maxq, prebuff_max, prebuff_left, size1, size2;
709 /* the sound of silence */
710 nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
712 /* get the position in the primary buffer */
713 if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){
714 LeaveCriticalSection(&(device->mixlock));
718 TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
719 playpos,writepos,device->playpos,device->mixpos,device->buflen);
720 assert(device->playpos < device->buflen);
722 /* calc maximum prebuff */
723 prebuff_max = (device->prebuf * device->fraglen);
725 /* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
726 prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
727 writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos);
729 /* check for underrun. underrun occurs when the write position passes the mix position
730 * also wipe out just-played sound data */
731 if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
732 if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
733 WARN("Probable buffer underrun\n");
734 else TRACE("Buffer starting or buffer underrun\n");
736 /* recover mixing for all buffers */
739 /* reset mix position to write position */
740 device->mixpos = writepos;
742 ZeroMemory(device->buffer, device->buflen);
743 } else if (playpos < device->playpos) {
744 buf1 = device->buffer + device->playpos;
745 buf2 = device->buffer;
746 size1 = device->buflen - device->playpos;
748 FillMemory(buf1, size1, nfiller);
749 if (playpos && (!buf2 || !size2))
750 FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__, device->playpos, device->mixpos, playpos, writepos);
751 FillMemory(buf2, size2, nfiller);
753 buf1 = device->buffer + device->playpos;
755 size1 = playpos - device->playpos;
757 FillMemory(buf1, size1, nfiller);
759 device->playpos = playpos;
761 /* find the maximum we can prebuffer from current write position */
762 maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0;
764 TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
765 prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead);
767 ZeroMemory(device->mix_buffer, device->mix_buffer_len);
770 DSOUND_MixToPrimary(device, writepos, maxq, recover, &all_stopped);
772 if (maxq + writepos > device->buflen)
774 DWORD todo = device->buflen - writepos;
775 DWORD offs_float = (todo / device->pwfx->nBlockAlign) * device->pwfx->nChannels;
776 device->normfunction(device->mix_buffer, device->buffer + writepos, todo);
777 device->normfunction(device->mix_buffer + offs_float, device->buffer, maxq - todo);
780 device->normfunction(device->mix_buffer, device->buffer + writepos, maxq);
782 /* update the mix position, taking wrap-around into account */
783 device->mixpos = writepos + maxq;
784 device->mixpos %= device->buflen;
786 /* update prebuff left */
787 prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
789 /* check if have a whole fragment */
790 if (prebuff_left >= device->fraglen){
792 /* update the wave queue */
793 DSOUND_WaveQueue(device, FALSE);
795 /* buffers are full. start playing if applicable */
796 if(device->state == STATE_STARTING){
797 TRACE("started primary buffer\n");
798 if(DSOUND_PrimaryPlay(device) != DS_OK){
799 WARN("DSOUND_PrimaryPlay failed\n");
802 /* we are playing now */
803 device->state = STATE_PLAYING;
807 /* buffers are full. start stopping if applicable */
808 if(device->state == STATE_STOPPED){
809 TRACE("restarting primary buffer\n");
810 if(DSOUND_PrimaryPlay(device) != DS_OK){
811 WARN("DSOUND_PrimaryPlay failed\n");
814 /* start stopping again. as soon as there is no more data, it will stop */
815 device->state = STATE_STOPPING;
820 /* if device was stopping, its for sure stopped when all buffers have stopped */
821 else if((all_stopped == TRUE) && (device->state == STATE_STOPPING)){
822 TRACE("All buffers have stopped. Stopping primary buffer\n");
823 device->state = STATE_STOPPED;
825 /* stop the primary buffer now */
826 DSOUND_PrimaryStop(device);
831 DSOUND_WaveQueue(device, TRUE);
833 /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
834 if (device->state == STATE_STARTING) {
835 if (DSOUND_PrimaryPlay(device) != DS_OK)
836 WARN("DSOUND_PrimaryPlay failed\n");
838 device->state = STATE_PLAYING;
840 else if (device->state == STATE_STOPPING) {
841 if (DSOUND_PrimaryStop(device) != DS_OK)
842 WARN("DSOUND_PrimaryStop failed\n");
844 device->state = STATE_STOPPED;
848 LeaveCriticalSection(&(device->mixlock));
852 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
853 DWORD_PTR dw1, DWORD_PTR dw2)
855 DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
856 DWORD start_time = GetTickCount();
858 TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
859 TRACE("entering at %d\n", start_time);
861 RtlAcquireResourceShared(&(device->buffer_list_lock), TRUE);
864 DSOUND_PerformMix(device);
866 RtlReleaseResource(&(device->buffer_list_lock));
868 end_time = GetTickCount();
869 TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);