- fix some prototypes, remove dxroslayer from dsound
[reactos.git] / reactos / dll / directx / dsound / mixer.c
index 49c8b37..d014c4b 100644 (file)
@@ -3,6 +3,8 @@
  * Copyright 1998 Marcus Meissner
  * Copyright 1998 Rob Riggs
  * Copyright 2000-2002 TransGaming Technologies, Inc.
+ * Copyright 2007 Peter Dons Tychsen
+ * Copyright 2007 Maarten Lankhorst
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -16,7 +18,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include <assert.h>
@@ -28,7 +30,6 @@
 #include "windef.h"
 #include "winbase.h"
 #include "mmsystem.h"
-#include "winreg.h"
 #include "winternl.h"
 #include "wine/debug.h"
 #include "dsound.h"
@@ -42,7 +43,7 @@ void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
        double temp;
        TRACE("(%p)\n",volpan);
 
-       TRACE("Vol=%ld Pan=%ld\n", volpan->lVolume, volpan->lPan);
+       TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
        /* the AmpFactors are expressed in 16.16 fixed point */
        volpan->dwVolAmpFactor = (ULONG) (pow(2.0, volpan->lVolume / 600.0) * 0xffff);
        /* FIXME: dwPan{Left|Right}AmpFactor */
@@ -53,7 +54,7 @@ void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan)
        temp = (double) (volpan->lVolume + (volpan->lPan < 0 ? volpan->lPan : 0));
        volpan->dwTotalRightAmpFactor = (ULONG) (pow(2.0, temp / 600.0) * 0xffff);
 
-       TRACE("left = %lx, right = %lx\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
+       TRACE("left = %x, right = %x\n", volpan->dwTotalLeftAmpFactor, volpan->dwTotalRightAmpFactor);
 }
 
 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
@@ -61,7 +62,7 @@ void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
     double left,right;
     TRACE("(%p)\n",volpan);
 
-    TRACE("left=%lx, right=%lx\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
+    TRACE("left=%x, right=%x\n",volpan->dwTotalLeftAmpFactor,volpan->dwTotalRightAmpFactor);
     if (volpan->dwTotalLeftAmpFactor==0)
         left=-10000;
     else
@@ -86,18 +87,141 @@ void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan)
     if (volpan->lPan < -10000)
         volpan->lPan=-10000;
 
-    TRACE("Vol=%ld Pan=%ld\n", volpan->lVolume, volpan->lPan);
+    TRACE("Vol=%d Pan=%d\n", volpan->lVolume, volpan->lPan);
 }
 
+/** Convert a primary buffer position to a pointer position for device->mix_buffer
+ * device: DirectSoundDevice for which to calculate
+ * pos: Primary buffer position to converts
+ * Returns: Offset for mix_buffer
+ */
+DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos)
+{
+    DWORD ret = pos * 32 / device->pwfx->wBitsPerSample;
+    if (device->pwfx->wBitsPerSample == 32)
+        ret *= 2;
+    return ret;
+}
+
+/* NOTE: Not all secpos have to always be mapped to a bufpos, other way around is always the case
+ * DWORD64 is used here because a single DWORD wouldn't be big enough to fit the freqAcc for big buffers
+ */
+/** This function converts a 'native' sample pointer to a resampled pointer that fits for primary
+ * secmixpos is used to decide which freqAcc is needed
+ * overshot tells what the 'actual' secpos is now (optional)
+ */
+DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot)
+{
+       DWORD64 framelen = secpos / dsb->pwfx->nBlockAlign;
+       DWORD64 freqAdjust = dsb->freqAdjust;
+       DWORD64 acc, freqAcc;
+
+       if (secpos < secmixpos)
+               freqAcc = dsb->freqAccNext;
+       else freqAcc = dsb->freqAcc;
+       acc = (framelen << DSOUND_FREQSHIFT) + (freqAdjust - 1 - freqAcc);
+       acc /= freqAdjust;
+       if (overshot)
+       {
+               DWORD64 oshot = acc * freqAdjust + freqAcc;
+               assert(oshot >= framelen << DSOUND_FREQSHIFT);
+               oshot -= framelen << DSOUND_FREQSHIFT;
+               *overshot = (DWORD)oshot;
+               assert(*overshot < dsb->freqAdjust);
+       }
+       return (DWORD)acc * dsb->device->pwfx->nBlockAlign;
+}
+
+/** Convert a resampled pointer that fits for primary to a 'native' sample pointer
+ * freqAccNext is used here rather than freqAcc: In case the app wants to fill up to
+ * the play position it won't overwrite it
+ */
+static DWORD DSOUND_bufpos_to_secpos(const IDirectSoundBufferImpl *dsb, DWORD bufpos)
+{
+       DWORD oAdv = dsb->device->pwfx->nBlockAlign, iAdv = dsb->pwfx->nBlockAlign, pos;
+       DWORD64 framelen;
+       DWORD64 acc;
+
+       framelen = bufpos/oAdv;
+       acc = framelen * (DWORD64)dsb->freqAdjust + (DWORD64)dsb->freqAccNext;
+       acc = acc >> DSOUND_FREQSHIFT;
+       pos = (DWORD)acc * iAdv;
+       if (pos >= dsb->buflen)
+               /* Because of differences between freqAcc and freqAccNext, this might happen */
+               pos = dsb->buflen - iAdv;
+       TRACE("Converted %d/%d to %d/%d\n", bufpos, dsb->tmp_buffer_len, pos, dsb->buflen);
+       return pos;
+}
+
+/**
+ * Move freqAccNext to freqAcc, and find new values for buffer length and freqAccNext
+ */
+static void DSOUND_RecalcFreqAcc(IDirectSoundBufferImpl *dsb)
+{
+       if (!dsb->freqneeded) return;
+       dsb->freqAcc = dsb->freqAccNext;
+       dsb->tmp_buffer_len = DSOUND_secpos_to_bufpos(dsb, dsb->buflen, 0, &dsb->freqAccNext);
+       TRACE("New freqadjust: %04x, new buflen: %d\n", dsb->freqAccNext, dsb->tmp_buffer_len);
+}
+
+/**
+ * Recalculate the size for temporary buffer, and new writelead
+ * Should be called when one of the following things occur:
+ * - Primary buffer format is changed
+ * - This buffer format (frequency) is changed
+ *
+ * After this, DSOUND_MixToTemporary(dsb, 0, dsb->buflen) should
+ * be called to refill the temporary buffer with data.
+ */
 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb)
 {
+       BOOL needremix = TRUE, needresample = (dsb->freq != dsb->device->pwfx->nSamplesPerSec);
+       DWORD bAlign = dsb->pwfx->nBlockAlign, pAlign = dsb->device->pwfx->nBlockAlign;
+
        TRACE("(%p)\n",dsb);
 
        /* calculate the 10ms write lead */
        dsb->writelead = (dsb->freq / 100) * dsb->pwfx->nBlockAlign;
+
+       if ((dsb->pwfx->wBitsPerSample == dsb->device->pwfx->wBitsPerSample) &&
+           (dsb->pwfx->nChannels == dsb->device->pwfx->nChannels) && !needresample)
+               needremix = FALSE;
+       HeapFree(GetProcessHeap(), 0, dsb->tmp_buffer);
+       dsb->tmp_buffer = NULL;
+       dsb->max_buffer_len = dsb->freqAcc = dsb->freqAccNext = 0;
+       dsb->freqneeded = needresample;
+
+       dsb->convert = convertbpp[dsb->pwfx->wBitsPerSample/8 - 1][dsb->device->pwfx->wBitsPerSample/8 - 1];
+
+       dsb->resampleinmixer = FALSE;
+
+       if (needremix)
+       {
+               if (needresample)
+                       DSOUND_RecalcFreqAcc(dsb);
+               else
+                       dsb->tmp_buffer_len = dsb->buflen / bAlign * pAlign;
+               dsb->max_buffer_len = dsb->tmp_buffer_len;
+               if ((dsb->max_buffer_len <= dsb->device->buflen || dsb->max_buffer_len < ds_snd_shadow_maxsize * 1024 * 1024) && ds_snd_shadow_maxsize >= 0)
+                       dsb->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, dsb->max_buffer_len);
+               if (dsb->tmp_buffer)
+                       FillMemory(dsb->tmp_buffer, dsb->tmp_buffer_len, dsb->device->pwfx->wBitsPerSample == 8 ? 128 : 0);
+               else
+                       dsb->resampleinmixer = TRUE;
+       }
+       else dsb->max_buffer_len = dsb->tmp_buffer_len = dsb->buflen;
+       dsb->buf_mixpos = DSOUND_secpos_to_bufpos(dsb, dsb->sec_mixpos, 0, NULL);
 }
 
-void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
+/**
+ * Check for application callback requests for when the play position
+ * reaches certain points.
+ *
+ * The offsets that will be triggered will be those between the recorded
+ * "last played" position for the buffer (i.e. dsb->playpos) and "len" bytes
+ * beyond that position.
+ */
+void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len)
 {
        int                     i;
        DWORD                   offset;
@@ -107,15 +231,17 @@ void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
        if (dsb->nrofnotifies == 0)
                return;
 
-       TRACE("(%p) buflen = %ld, playpos = %ld, len = %d\n",
-               dsb, dsb->buflen, dsb->playpos, len);
+       TRACE("(%p) buflen = %d, playpos = %d, len = %d\n",
+               dsb, dsb->buflen, playpos, len);
        for (i = 0; i < dsb->nrofnotifies ; i++) {
                event = dsb->notifies + i;
                offset = event->dwOffset;
-               TRACE("checking %d, position %ld, event = %p\n",
+               TRACE("checking %d, position %d, event = %p\n",
                        i, offset, event->hEventNotify);
                /* DSBPN_OFFSETSTOP has to be the last element. So this is */
                /* OK. [Inside DirectX, p274] */
+               /* Windows does not seem to enforce this, and some apps rely */
+               /* on that, so we can't stop there. */
                /*  */
                /* This also means we can't sort the entries by offset, */
                /* because DSBPN_OFFSETSTOP == -1 */
@@ -123,18 +249,17 @@ void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
                        if (dsb->state == STATE_STOPPED) {
                                SetEvent(event->hEventNotify);
                                TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
-                               return;
-                       } else
-                               return;
+                       }
+                        continue;
                }
-               if ((dsb->playpos + len) >= dsb->buflen) {
-                       if ((offset < ((dsb->playpos + len) % dsb->buflen)) ||
-                           (offset >= dsb->playpos)) {
+               if ((playpos + len) >= dsb->buflen) {
+                       if ((offset < ((playpos + len) % dsb->buflen)) ||
+                           (offset >= playpos)) {
                                TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
                                SetEvent(event->hEventNotify);
                        }
                } else {
-                       if ((offset >= dsb->playpos) && (offset < (dsb->playpos + len))) {
+                       if ((offset >= playpos) && (offset < (playpos + len))) {
                                TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
                                SetEvent(event->hEventNotify);
                        }
@@ -142,954 +267,705 @@ void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len)
        }
 }
 
-/* WAV format info can be found at:
- *
- *    http://www.cwi.nl/ftp/audio/AudioFormats.part2
- *    ftp://ftp.cwi.nl/pub/audio/RIFF-format
- *
- * Import points to remember:
- *    8-bit WAV is unsigned
- *    16-bit WAV is signed
+/**
+ * Copy a single frame from the given input buffer to the given output buffer.
+ * Translate 8 <-> 16 bits and mono <-> stereo
  */
- /* Use the same formulas as pcmconverter.c */
-static inline INT16 cvtU8toS16(BYTE b)
+static inline void cp_fields(const IDirectSoundBufferImpl *dsb, const BYTE *ibuf, BYTE *obuf,
+        UINT istride, UINT ostride, UINT count, UINT freqAcc, UINT adj)
 {
-    return (short)((b+(b << 8))-32768);
-}
+    DirectSoundDevice *device = dsb->device;
+    INT istep = dsb->pwfx->wBitsPerSample / 8, ostep = device->pwfx->wBitsPerSample / 8;
 
-static inline BYTE cvtS16toU8(INT16 s)
-{
-    return (s >> 8) ^ (unsigned char)0x80;
+    if (device->pwfx->nChannels == dsb->pwfx->nChannels) {
+        dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
+        if (device->pwfx->nChannels == 2)
+            dsb->convert(ibuf + istep, obuf + ostep, istride, ostride, count, freqAcc, adj);
+    }
+
+    if (device->pwfx->nChannels == 1 && dsb->pwfx->nChannels == 2)
+    {
+        dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
+    }
+
+    if (device->pwfx->nChannels == 2 && dsb->pwfx->nChannels == 1)
+    {
+        dsb->convert(ibuf, obuf, istride, ostride, count, freqAcc, adj);
+        dsb->convert(ibuf, obuf + ostep, istride, ostride, count, freqAcc, adj);
+    }
 }
 
-static inline void cp_fields(const IDirectSoundBufferImpl *dsb, BYTE *ibuf, BYTE *obuf )
+/**
+ * Calculate the distance between two buffer offsets, taking wraparound
+ * into account.
+ */
+static inline DWORD DSOUND_BufPtrDiff(DWORD buflen, DWORD ptr1, DWORD ptr2)
 {
-       DirectSoundDevice * device = dsb->dsound->device;
-        INT fl,fr;
-
-        if (dsb->pwfx->wBitsPerSample == 8)  {
-                if (device->pwfx->wBitsPerSample == 8 &&
-                    device->pwfx->nChannels == dsb->pwfx->nChannels) {
-                        /* avoid needless 8->16->8 conversion */
-                        *obuf=*ibuf;
-                        if (dsb->pwfx->nChannels==2)
-                                *(obuf+1)=*(ibuf+1);
-                        return;
-                }
-                fl = cvtU8toS16(*ibuf);
-                fr = (dsb->pwfx->nChannels==2 ? cvtU8toS16(*(ibuf + 1)) : fl);
-        } else {
-                fl = *((INT16 *)ibuf);
-                fr = (dsb->pwfx->nChannels==2 ? *(((INT16 *)ibuf) + 1)  : fl);
-        }
-
-        if (device->pwfx->nChannels == 2) {
-                if (device->pwfx->wBitsPerSample == 8) {
-                        *obuf = cvtS16toU8(fl);
-                        *(obuf + 1) = cvtS16toU8(fr);
-                        return;
-                }
-                if (device->pwfx->wBitsPerSample == 16) {
-                        *((INT16 *)obuf) = fl;
-                        *(((INT16 *)obuf) + 1) = fr;
-                        return;
-                }
-        }
-        if (device->pwfx->nChannels == 1) {
-                fl = (fl + fr) >> 1;
-                if (device->pwfx->wBitsPerSample == 8) {
-                        *obuf = cvtS16toU8(fl);
-                        return;
-                }
-                if (device->pwfx->wBitsPerSample == 16) {
-                        *((INT16 *)obuf) = fl;
-                        return;
-                }
-        }
+/* If these asserts fail, the problem is not here, but in the underlying code */
+       assert(ptr1 < buflen);
+       assert(ptr2 < buflen);
+       if (ptr1 >= ptr2) {
+               return ptr1 - ptr2;
+       } else {
+               return buflen + ptr1 - ptr2;
+       }
 }
-
-/* Now with PerfectPitch (tm) technology */
-static INT DSOUND_MixerNorm(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
+/**
+ * Mix at most the given amount of data into the allocated temporary buffer
+ * of the given secondary buffer, starting from the dsb's first currently
+ * unsampled frame (writepos), translating frequency (pitch), stereo/mono
+ * and bits-per-sample so that it is ideal for the primary buffer.
+ * Doesn't perform any mixing - this is a straight copy/convert operation.
+ *
+ * dsb = the secondary buffer
+ * writepos = Starting position of changed buffer
+ * len = number of bytes to resample from writepos
+ *
+ * NOTE: writepos + len <= buflen. When called by mixer, MixOne makes sure of this.
+ */
+void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD len, BOOL inmixer)
 {
-       INT     i, size, ipos, ilen;
-       BYTE    *ibp, *obp;
+       INT     size;
+       BYTE    *ibp, *obp, *obp_begin;
        INT     iAdvance = dsb->pwfx->nBlockAlign;
-       INT     oAdvance = dsb->dsound->device->pwfx->nBlockAlign;
-
-       ibp = dsb->buffer->memory + dsb->buf_mixpos;
-       obp = buf;
-
-       TRACE("(%p, %p, %p), buf_mixpos=%ld\n", dsb, ibp, obp, dsb->buf_mixpos);
-       /* Check for the best case */
-       if ((dsb->freq == dsb->dsound->device->pwfx->nSamplesPerSec) &&
-           (dsb->pwfx->wBitsPerSample == dsb->dsound->device->pwfx->wBitsPerSample) &&
-           (dsb->pwfx->nChannels == dsb->dsound->device->pwfx->nChannels)) {
-               INT bytesleft = dsb->buflen - dsb->buf_mixpos;
-               TRACE("(%p) Best case\n", dsb);
-               if (len <= bytesleft )
-                       CopyMemory(obp, ibp, len);
-               else { /* wrap */
-                       CopyMemory(obp, ibp, bytesleft);
-                       CopyMemory(obp + bytesleft, dsb->buffer->memory, len - bytesleft);
-               }
-               return len;
+       INT     oAdvance = dsb->device->pwfx->nBlockAlign;
+       DWORD freqAcc, target_writepos = 0, overshot, maxlen;
+
+       /* We resample only when needed */
+       if ((dsb->tmp_buffer && inmixer) || (!dsb->tmp_buffer && !inmixer) || dsb->resampleinmixer != inmixer)
+               return;
+
+       assert(writepos + len <= dsb->buflen);
+       if (inmixer && writepos + len < dsb->buflen)
+               len += dsb->pwfx->nBlockAlign;
+
+       maxlen = DSOUND_secpos_to_bufpos(dsb, len, 0, NULL);
+
+       ibp = dsb->buffer->memory + writepos;
+       if (!inmixer)
+               obp_begin = dsb->tmp_buffer;
+       else if (dsb->device->tmp_buffer_len < maxlen || !dsb->device->tmp_buffer)
+       {
+               dsb->device->tmp_buffer_len = maxlen;
+               if (dsb->device->tmp_buffer)
+                       dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, maxlen);
+               else
+                       dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, maxlen);
+               obp_begin = dsb->device->tmp_buffer;
        }
+       else
+               obp_begin = dsb->device->tmp_buffer;
+
+       TRACE("(%p, %p)\n", dsb, ibp);
+       size = len / iAdvance;
 
        /* Check for same sample rate */
-       if (dsb->freq == dsb->dsound->device->pwfx->nSamplesPerSec) {
-               TRACE("(%p) Same sample rate %ld = primary %ld\n", dsb,
-                       dsb->freq, dsb->dsound->device->pwfx->nSamplesPerSec);
-               ilen = 0;
-               for (i = 0; i < len; i += oAdvance) {
-                       cp_fields(dsb, ibp, obp );
-                       ibp += iAdvance;
-                       ilen += iAdvance;
-                       obp += oAdvance;
-                       if (ibp >= (BYTE *)(dsb->buffer->memory + dsb->buflen))
-                               ibp = dsb->buffer->memory;      /* wrap */
-               }
-               return (ilen);
+       if (dsb->freq == dsb->device->pwfx->nSamplesPerSec) {
+               TRACE("(%p) Same sample rate %d = primary %d\n", dsb,
+                       dsb->freq, dsb->device->pwfx->nSamplesPerSec);
+               obp = obp_begin;
+               if (!inmixer)
+                        obp += writepos/iAdvance*oAdvance;
+
+               cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, 0, 1 << DSOUND_FREQSHIFT);
+               return;
        }
 
        /* Mix in different sample rates */
-       /* */
-       /* New PerfectPitch(tm) Technology (c) 1998 Rob Riggs */
-       /* Patent Pending :-] */
-
-       /* Patent enhancements (c) 2000 Ove K√•ven,
-        * TransGaming Technologies Inc. */
-
-       /* FIXME("(%p) Adjusting frequency: %ld -> %ld (need optimization)\n",
-          dsb, dsb->freq, dsb->dsound->device->pwfx->nSamplesPerSec); */
-
-       size = len / oAdvance;
-       ilen = 0;
-       ipos = dsb->buf_mixpos;
-       for (i = 0; i < size; i++) {
-                cp_fields(dsb, (dsb->buffer->memory + ipos), obp);
-               obp += oAdvance;
-               dsb->freqAcc += dsb->freqAdjust;
-               if (dsb->freqAcc >= (1<<DSOUND_FREQSHIFT)) {
-                       ULONG adv = (dsb->freqAcc>>DSOUND_FREQSHIFT) * iAdvance;
-                       dsb->freqAcc &= (1<<DSOUND_FREQSHIFT)-1;
-                       ipos += adv; ilen += adv;
-                       ipos %= dsb->buflen;
-               }
+       TRACE("(%p) Adjusting frequency: %d -> %d\n", dsb, dsb->freq, dsb->device->pwfx->nSamplesPerSec);
+
+       target_writepos = DSOUND_secpos_to_bufpos(dsb, writepos, dsb->sec_mixpos, &freqAcc);
+       overshot = freqAcc >> DSOUND_FREQSHIFT;
+       if (overshot)
+       {
+               if (overshot >= size)
+                       return;
+               size -= overshot;
+               writepos += overshot * iAdvance;
+               if (writepos >= dsb->buflen)
+                       return;
+               ibp = dsb->buffer->memory + writepos;
+               freqAcc &= (1 << DSOUND_FREQSHIFT) - 1;
+               TRACE("Overshot: %d, freqAcc: %04x\n", overshot, freqAcc);
        }
-       return ilen;
+
+       if (!inmixer)
+               obp = obp_begin + target_writepos;
+       else obp = obp_begin;
+
+       /* FIXME: Small problem here when we're overwriting buf_mixpos, it then STILL uses old freqAcc, not sure if it matters or not */
+       cp_fields(dsb, ibp, obp, iAdvance, oAdvance, size, freqAcc, dsb->freqAdjust);
 }
 
-static void DSOUND_MixerVol(IDirectSoundBufferImpl *dsb, BYTE *buf, INT len)
+/** Apply volume to the given soundbuffer from (primary) position writepos and length len
+ * Returns: NULL if no volume needs to be applied
+ * or else a memory handle that holds 'len' volume adjusted buffer */
+static LPBYTE DSOUND_MixerVol(const IDirectSoundBufferImpl *dsb, INT len)
 {
        INT     i;
-       BYTE    *bpc = buf;
-       INT16   *bps = (INT16 *) buf;
+       BYTE    *bpc;
+       INT16   *bps, *mems;
+       DWORD vLeft, vRight;
+       INT nChannels = dsb->device->pwfx->nChannels;
+       LPBYTE mem = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos;
 
-       TRACE("(%p,%p,%d)\n",dsb,buf,len);
-       TRACE("left = %lx, right = %lx\n", dsb->cvolpan.dwTotalLeftAmpFactor,
-               dsb->cvolpan.dwTotalRightAmpFactor);
+       if (dsb->resampleinmixer)
+               mem = dsb->device->tmp_buffer;
 
-       if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->cvolpan.lPan == 0)) &&
-           (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->cvolpan.lVolume == 0)) &&
-           !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
-               return;         /* Nothing to do */
+       TRACE("(%p,%d)\n",dsb,len);
+       TRACE("left = %x, right = %x\n", dsb->volpan.dwTotalLeftAmpFactor,
+               dsb->volpan.dwTotalRightAmpFactor);
+
+       if ((!(dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) || (dsb->volpan.lPan == 0)) &&
+           (!(dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) || (dsb->volpan.lVolume == 0)) &&
+            !(dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
+               return NULL; /* Nothing to do */
+
+       if (nChannels != 1 && nChannels != 2)
+       {
+               FIXME("There is no support for %d channels\n", nChannels);
+               return NULL;
+       }
 
-       /* If we end up with some bozo coder using panning or 3D sound */
-       /* with a mono primary buffer, it could sound very weird using */
-       /* this method. Oh well, tough patooties. */
+       if (dsb->device->pwfx->wBitsPerSample != 8 && dsb->device->pwfx->wBitsPerSample != 16)
+       {
+               FIXME("There is no support for %d bpp\n", dsb->device->pwfx->wBitsPerSample);
+               return NULL;
+       }
 
-       switch (dsb->dsound->device->pwfx->wBitsPerSample) {
+       if (dsb->device->tmp_buffer_len < len || !dsb->device->tmp_buffer)
+       {
+               /* If we just resampled in DSOUND_MixToTemporary, we shouldn't need to resize here */
+               assert(!dsb->resampleinmixer);
+               dsb->device->tmp_buffer_len = len;
+               if (dsb->device->tmp_buffer)
+                       dsb->device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, dsb->device->tmp_buffer, len);
+               else
+                       dsb->device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
+       }
+
+       bpc = dsb->device->tmp_buffer;
+       bps = (INT16 *)bpc;
+       mems = (INT16 *)mem;
+       vLeft = dsb->volpan.dwTotalLeftAmpFactor;
+       if (nChannels > 1)
+               vRight = dsb->volpan.dwTotalRightAmpFactor;
+       else
+               vRight = vLeft;
+
+       switch (dsb->device->pwfx->wBitsPerSample) {
        case 8:
                /* 8-bit WAV is unsigned, but we need to operate */
                /* on signed data for this to work properly */
-               switch (dsb->dsound->device->pwfx->nChannels) {
-               case 1:
-                       for (i = 0; i < len; i++) {
-                               INT val = *bpc - 128;
-                               val = (val * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
-                               *bpc = val + 128;
-                               bpc++;
-                       }
-                       break;
-               case 2:
-                       for (i = 0; i < len; i+=2) {
-                               INT val = *bpc - 128;
-                               val = (val * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
-                               *bpc++ = val + 128;
-                               val = *bpc - 128;
-                               val = (val * dsb->cvolpan.dwTotalRightAmpFactor) >> 16;
-                               *bpc = val + 128;
-                               bpc++;
-                       }
-                       break;
-               default:
-                       FIXME("doesn't support %d channels\n", dsb->dsound->device->pwfx->nChannels);
-                       break;
+               for (i = 0; i < len-1; i+=2) {
+                       *(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
+                       *(bpc++) = (((*(mem++) - 128) * vRight) >> 16) + 128;
                }
+               if (len % 2 == 1 && nChannels == 1)
+                       *(bpc++) = (((*(mem++) - 128) * vLeft) >> 16) + 128;
                break;
        case 16:
                /* 16-bit WAV is signed -- much better */
-               switch (dsb->dsound->device->pwfx->nChannels) {
-               case 1:
-                       for (i = 0; i < len; i += 2) {
-                               *bps = (*bps * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
-                               bps++;
-                       }
-                       break;
-               case 2:
-                       for (i = 0; i < len; i += 4) {
-                               *bps = (*bps * dsb->cvolpan.dwTotalLeftAmpFactor) >> 16;
-                               bps++;
-                               *bps = (*bps * dsb->cvolpan.dwTotalRightAmpFactor) >> 16;
-                               bps++;
-                       }
-                       break;
-               default:
-                       FIXME("doesn't support %d channels\n", dsb->dsound->device->pwfx->nChannels);
-                       break;
+               for (i = 0; i < len-3; i += 4) {
+                       *(bps++) = (*(mems++) * vLeft) >> 16;
+                       *(bps++) = (*(mems++) * vRight) >> 16;
                }
-               break;
-       default:
-               FIXME("doesn't support %d bit samples\n", dsb->dsound->device->pwfx->wBitsPerSample);
+               if (len % 4 == 2 && nChannels == 1)
+                       *(bps++) = ((INT)*(mems++) * vLeft) >> 16;
                break;
        }
+       return dsb->device->tmp_buffer;
 }
 
-static LPBYTE DSOUND_tmpbuffer(DirectSoundDevice *device, DWORD len)
+/**
+ * Mix (at most) the given number of bytes into the given position of the
+ * device buffer, from the secondary buffer "dsb" (starting at the current
+ * mix position for that buffer).
+ *
+ * Returns the number of bytes actually mixed into the device buffer. This
+ * will match fraglen unless the end of the secondary buffer is reached
+ * (and it is not looping).
+ *
+ * dsb  = the secondary buffer to mix from
+ * writepos = position (offset) in device buffer to write at
+ * fraglen = number of bytes to mix
+ */
+static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
 {
-    TRACE("(%p,%ld)\n", device, len);
+       INT len = fraglen, ilen;
+       BYTE *ibuf = (dsb->tmp_buffer ? dsb->tmp_buffer : dsb->buffer->memory) + dsb->buf_mixpos, *volbuf;
+       DWORD oldpos, mixbufpos;
 
-    if (len > device->tmp_buffer_len) {
-        if (device->tmp_buffer)
-            device->tmp_buffer = HeapReAlloc(GetProcessHeap(), 0, device->tmp_buffer, len);
-        else
-            device->tmp_buffer = HeapAlloc(GetProcessHeap(), 0, len);
+       TRACE("buf_mixpos=%d/%d sec_mixpos=%d/%d\n", dsb->buf_mixpos, dsb->tmp_buffer_len, dsb->sec_mixpos, dsb->buflen);
+       TRACE("(%p,%d,%d)\n",dsb,writepos,fraglen);
 
-        device->tmp_buffer_len = len;
-    }
-
-    return device->tmp_buffer;
-}
+       assert(dsb->buf_mixpos + len <= dsb->tmp_buffer_len);
 
-static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD fraglen)
-{
-       INT     i, len, ilen, field, todo;
-       BYTE    *buf, *ibuf;
-
-       TRACE("(%p,%ld,%ld)\n",dsb,writepos,fraglen);
-
-       len = fraglen;
-       if (!(dsb->playflags & DSBPLAY_LOOPING)) {
-               int secondary_remainder = dsb->buflen - dsb->buf_mixpos;
-               int adjusted_remainder = MulDiv(dsb->dsound->device->pwfx->nAvgBytesPerSec, secondary_remainder, dsb->nAvgBytesPerSec);
-               assert(adjusted_remainder >= 0);
-               TRACE("secondary_remainder = %d, adjusted_remainder = %d, len = %d\n", secondary_remainder, adjusted_remainder, len);
-               if (adjusted_remainder < len) {
-                       TRACE("clipping len to remainder of secondary buffer\n");
-                       len = adjusted_remainder;
-               }
-               if (len == 0)
-                       return 0;
-       }
-
-       if (len % dsb->dsound->device->pwfx->nBlockAlign) {
-               INT nBlockAlign = dsb->dsound->device->pwfx->nBlockAlign;
+       if (len % dsb->device->pwfx->nBlockAlign) {
+               INT nBlockAlign = dsb->device->pwfx->nBlockAlign;
                ERR("length not a multiple of block size, len = %d, block size = %d\n", len, nBlockAlign);
-               len = (len / nBlockAlign) * nBlockAlign;        /* data alignment */
+               len -= len % nBlockAlign; /* data alignment */
        }
 
-       if ((buf = ibuf = DSOUND_tmpbuffer(dsb->dsound->device, len)) == NULL)
-               return 0;
-
-       TRACE("MixInBuffer (%p) len = %d, dest = %ld\n", dsb, len, writepos);
-
-       ilen = DSOUND_MixerNorm(dsb, ibuf, len);
-       if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
-           (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) ||
-           (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
-               DSOUND_MixerVol(dsb, ibuf, len);
-
-       if (dsb->dsound->device->pwfx->wBitsPerSample == 8) {
-               BYTE    *obuf = dsb->dsound->device->buffer + writepos;
-
-               if ((writepos + len) <= dsb->dsound->device->buflen)
-                       todo = len;
-               else
-                       todo = dsb->dsound->device->buflen - writepos;
-
-               for (i = 0; i < todo; i++) {
-                       /* 8-bit WAV is unsigned */
-                       field = (*ibuf++ - 128);
-                       field += (*obuf - 128);
-                       if (field > 127) field = 127;
-                       else if (field < -128) field = -128;
-                       *obuf++ = field + 128;
-               }
-
-               if (todo < len) {
-                       todo = len - todo;
-                       obuf = dsb->dsound->device->buffer;
-
-                       for (i = 0; i < todo; i++) {
-                               /* 8-bit WAV is unsigned */
-                               field = (*ibuf++ - 128);
-                               field += (*obuf - 128);
-                               if (field > 127) field = 127;
-                               else if (field < -128) field = -128;
-                               *obuf++ = field + 128;
-                       }
-               }
-        } else {
-               INT16   *ibufs, *obufs;
-
-               ibufs = (INT16 *) ibuf;
-               obufs = (INT16 *)(dsb->dsound->device->buffer + writepos);
-
-               if ((writepos + len) <= dsb->dsound->device->buflen)
-                       todo = len / 2;
-               else
-                       todo = (dsb->dsound->device->buflen - writepos) / 2;
-
-               for (i = 0; i < todo; i++) {
-                       /* 16-bit WAV is signed */
-                       field = *ibufs++;
-                       field += *obufs;
-                       if (field > 32767) field = 32767;
-                       else if (field < -32768) field = -32768;
-                       *obufs++ = field;
-               }
-
-               if (todo < (len / 2)) {
-                       todo = (len / 2) - todo;
-                       obufs = (INT16 *)dsb->dsound->device->buffer;
-
-                       for (i = 0; i < todo; i++) {
-                               /* 16-bit WAV is signed */
-                               field = *ibufs++;
-                               field += *obufs;
-                               if (field > 32767) field = 32767;
-                               else if (field < -32768) field = -32768;
-                               *obufs++ = field;
-                       }
-               }
-        }
-
-       if (dsb->leadin && (dsb->startpos > dsb->buf_mixpos) && (dsb->startpos <= dsb->buf_mixpos + ilen)) {
-               /* HACK... leadin should be reset when the PLAY position reaches the startpos,
-                * not the MIX position... but if the sound buffer is bigger than our prebuffering
-                * (which must be the case for the streaming buffers that need this hack anyway)
-                * plus DS_HEL_MARGIN or equivalent, then this ought to work anyway. */
-               dsb->leadin = FALSE;
+       /* Resample buffer to temporary buffer specifically allocated for this purpose, if needed */
+       DSOUND_MixToTemporary(dsb, dsb->sec_mixpos, DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos+len) - dsb->sec_mixpos, TRUE);
+       if (dsb->resampleinmixer)
+               ibuf = dsb->device->tmp_buffer;
+
+       /* Apply volume if needed */
+       volbuf = DSOUND_MixerVol(dsb, len);
+       if (volbuf)
+               ibuf = volbuf;
+
+       mixbufpos = DSOUND_bufpos_to_mixpos(dsb->device, writepos);
+       /* Now mix the temporary buffer into the devices main buffer */
+       if ((writepos + len) <= dsb->device->buflen)
+               dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, len);
+       else
+       {
+               DWORD todo = dsb->device->buflen - writepos;
+               dsb->device->mixfunction(ibuf, dsb->device->mix_buffer + mixbufpos, todo);
+               dsb->device->mixfunction(ibuf + todo, dsb->device->mix_buffer, len - todo);
        }
 
-       dsb->buf_mixpos += ilen;
+       oldpos = dsb->sec_mixpos;
+       dsb->buf_mixpos += len;
 
-       if (dsb->buf_mixpos >= dsb->buflen) {
+       if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
+               if (dsb->buf_mixpos > dsb->tmp_buffer_len)
+                       ERR("Mixpos (%u) past buflen (%u), capping...\n", dsb->buf_mixpos, dsb->tmp_buffer_len);
                if (dsb->playflags & DSBPLAY_LOOPING) {
-                       /* wrap */
-                       dsb->buf_mixpos %= dsb->buflen;
-                       if (dsb->leadin && (dsb->startpos <= dsb->buf_mixpos))
-                               dsb->leadin = FALSE; /* HACK: see above */
-               } else if (dsb->buf_mixpos > dsb->buflen) {
-                       ERR("Mixpos (%lu) past buflen (%lu), capping...\n", dsb->buf_mixpos, dsb->buflen);
-                       dsb->buf_mixpos = dsb->buflen;
+                       dsb->buf_mixpos -= dsb->tmp_buffer_len;
+               } else if (dsb->buf_mixpos >= dsb->tmp_buffer_len) {
+                       dsb->buf_mixpos = dsb->sec_mixpos = 0;
+                       dsb->state = STATE_STOPPED;
                }
+               DSOUND_RecalcFreqAcc(dsb);
        }
 
-       return len;
-}
-
-static void DSOUND_PhaseCancel(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD len)
-{
-       INT     ilen, field;
-       UINT    i, todo;
-       BYTE    *buf, *ibuf;
-
-       TRACE("(%p,%ld,%ld)\n",dsb,writepos,len);
-
-       if (len % dsb->dsound->device->pwfx->nBlockAlign) {
-               INT nBlockAlign = dsb->dsound->device->pwfx->nBlockAlign;
-               ERR("length not a multiple of block size, len = %ld, block size = %d\n", len, nBlockAlign);
-               len = (len / nBlockAlign) * nBlockAlign;        /* data alignment */
+       dsb->sec_mixpos = DSOUND_bufpos_to_secpos(dsb, dsb->buf_mixpos);
+       ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos);
+       /* check for notification positions */
+       if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
+           dsb->state != STATE_STARTING) {
+               DSOUND_CheckEvent(dsb, oldpos, ilen);
        }
 
-       if ((buf = ibuf = DSOUND_tmpbuffer(dsb->dsound->device, len)) == NULL)
-               return;
-
-       TRACE("PhaseCancel (%p) len = %ld, dest = %ld\n", dsb, len, writepos);
-
-       ilen = DSOUND_MixerNorm(dsb, ibuf, len);
-       if ((dsb->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
-           (dsb->dsbd.dwFlags & DSBCAPS_CTRLVOLUME) ||
-           (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D))
-               DSOUND_MixerVol(dsb, ibuf, len);
-
-       /* subtract instead of add, to phase out premixed data */
-       if (dsb->dsound->device->pwfx->wBitsPerSample == 8) {
-               BYTE    *obuf = dsb->dsound->device->buffer + writepos;
-
-               if ((writepos + len) <= dsb->dsound->device->buflen)
-                       todo = len;
-               else
-                       todo = dsb->dsound->device->buflen - writepos;
-
-               for (i = 0; i < todo; i++) {
-                       /* 8-bit WAV is unsigned */
-                       field = (*ibuf++ - 128);
-                       field -= (*obuf - 128);
-                       if (field > 127) field = 127;
-                       else if (field < -128) field = -128;
-                       *obuf++ = field + 128;
-               }
-
-               if (todo < len) {
-                       todo = len - todo;
-                       obuf = dsb->dsound->device->buffer;
-
-                       for (i = 0; i < todo; i++) {
-                               /* 8-bit WAV is unsigned */
-                               field = (*ibuf++ - 128);
-                               field -= (*obuf - 128);
-                               if (field > 127) field = 127;
-                               else if (field < -128) field = -128;
-                               *obuf++ = field + 128;
-                       }
-               }
-        } else {
-               INT16   *ibufs, *obufs;
-
-               ibufs = (INT16 *) ibuf;
-               obufs = (INT16 *)(dsb->dsound->device->buffer + writepos);
-
-               if ((writepos + len) <= dsb->dsound->device->buflen)
-                       todo = len / 2;
-               else
-                       todo = (dsb->dsound->device->buflen - writepos) / 2;
-
-               for (i = 0; i < todo; i++) {
-                       /* 16-bit WAV is signed */
-                       field = *ibufs++;
-                       field -= *obufs;
-                       if (field > 32767) field = 32767;
-                       else if (field < -32768) field = -32768;
-                       *obufs++ = field;
-               }
-
-               if (todo < (len / 2)) {
-                       todo = (len / 2) - todo;
-                       obufs = (INT16 *)dsb->dsound->device->buffer;
-
-                       for (i = 0; i < todo; i++) {
-                               /* 16-bit WAV is signed */
-                               field = *ibufs++;
-                               field -= *obufs;
-                               if (field > 32767) field = 32767;
-                               else if (field < -32768) field = -32768;
-                               *obufs++ = field;
-                       }
-               }
-        }
+       /* increase mix position */
+       dsb->primary_mixpos += len;
+       if (dsb->primary_mixpos >= dsb->device->buflen)
+               dsb->primary_mixpos -= dsb->device->buflen;
+       return len;
 }
 
-static void DSOUND_MixCancel(IDirectSoundBufferImpl *dsb, DWORD writepos, BOOL cancel)
+/**
+ * Mix some frames from the given secondary buffer "dsb" into the device
+ * primary buffer.
+ *
+ * dsb = the secondary buffer
+ * playpos = the current play position in the device buffer (primary buffer)
+ * writepos = the current safe-to-write position in the device buffer
+ * mixlen = the maximum number of bytes in the primary buffer to mix, from the
+ *          current writepos.
+ *
+ * Returns: the number of bytes beyond the writepos that were mixed.
+ */
+static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen)
 {
-       DWORD   size, flen, len, npos, nlen;
-       INT     iAdvance = dsb->pwfx->nBlockAlign;
-       INT     oAdvance = dsb->dsound->device->pwfx->nBlockAlign;
-       /* determine amount of premixed data to cancel */
-       DWORD primary_done =
-               ((dsb->primary_mixpos < writepos) ? dsb->dsound->device->buflen : 0) +
-               dsb->primary_mixpos - writepos;
-
-       TRACE("(%p, %ld), buf_mixpos=%ld\n", dsb, writepos, dsb->buf_mixpos);
-
-       /* backtrack the mix position */
-       size = primary_done / oAdvance;
-       flen = size * dsb->freqAdjust;
-       len = (flen >> DSOUND_FREQSHIFT) * iAdvance;
-       flen &= (1<<DSOUND_FREQSHIFT)-1;
-       while (dsb->freqAcc < flen) {
-               len += iAdvance;
-               dsb->freqAcc += 1<<DSOUND_FREQSHIFT;
-       }
-       len %= dsb->buflen;
-       npos = ((dsb->buf_mixpos < len) ? dsb->buflen : 0) +
-               dsb->buf_mixpos - len;
-       if (dsb->leadin && (dsb->startpos > npos) && (dsb->startpos <= npos + len)) {
-               /* stop backtracking at startpos */
-               npos = dsb->startpos;
-               len = ((dsb->buf_mixpos < npos) ? dsb->buflen : 0) +
-                       dsb->buf_mixpos - npos;
-               flen = dsb->freqAcc;
-               nlen = len / dsb->pwfx->nBlockAlign;
-               nlen = ((nlen << DSOUND_FREQSHIFT) + flen) / dsb->freqAdjust;
-               nlen *= dsb->dsound->device->pwfx->nBlockAlign;
-               writepos =
-                       ((dsb->primary_mixpos < nlen) ? dsb->dsound->device->buflen : 0) +
-                       dsb->primary_mixpos - nlen;
+       /* The buffer's primary_mixpos may be before or after the device
+        * buffer's mixpos, but both must be ahead of writepos. */
+       DWORD primary_done;
+
+       TRACE("(%p,%d,%d)\n",dsb,writepos,mixlen);
+       TRACE("writepos=%d, buf_mixpos=%d, primary_mixpos=%d, mixlen=%d\n", writepos, dsb->buf_mixpos, dsb->primary_mixpos, mixlen);
+       TRACE("looping=%d, leadin=%d, buflen=%d\n", dsb->playflags, dsb->leadin, dsb->tmp_buffer_len);
+
+       /* If leading in, only mix about 20 ms, and 'skip' mixing the rest, for more fluid pointer advancement */
+       if (dsb->leadin && dsb->state == STATE_STARTING)
+       {
+               if (mixlen > 2 * dsb->device->fraglen)
+               {
+                       dsb->primary_mixpos += mixlen - 2 * dsb->device->fraglen;
+                       dsb->primary_mixpos %= dsb->device->buflen;
+               }
        }
+       dsb->leadin = FALSE;
 
-       dsb->freqAcc -= flen;
-       dsb->buf_mixpos = npos;
-       dsb->primary_mixpos = writepos;
+       /* calculate how much pre-buffering has already been done for this buffer */
+       primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
 
-       TRACE("new buf_mixpos=%ld, primary_mixpos=%ld (len=%ld)\n",
-             dsb->buf_mixpos, dsb->primary_mixpos, len);
-
-       if (cancel) DSOUND_PhaseCancel(dsb, writepos, len);
-}
-
-void DSOUND_MixCancelAt(IDirectSoundBufferImpl *dsb, DWORD buf_writepos)
-{
-#if 0
-       DWORD   i, size, flen, len, npos, nlen;
-       INT     iAdvance = dsb->pwfx->nBlockAlign;
-       INT     oAdvance = dsb->dsound->device->pwfx->nBlockAlign;
-       /* determine amount of premixed data to cancel */
-       DWORD buf_done =
-               ((dsb->buf_mixpos < buf_writepos) ? dsb->buflen : 0) +
-               dsb->buf_mixpos - buf_writepos;
-#endif
-
-       WARN("(%p, %ld), buf_mixpos=%ld\n", dsb, buf_writepos, dsb->buf_mixpos);
-       /* since this is not implemented yet, just cancel *ALL* prebuffering for now
-        * (which is faster anyway when there's only a single secondary buffer) */
-       dsb->dsound->device->need_remix = TRUE;
-}
-
-void DSOUND_ForceRemix(IDirectSoundBufferImpl *dsb)
-{
-       TRACE("(%p)\n",dsb);
-       EnterCriticalSection(&dsb->lock);
-       if (dsb->state == STATE_PLAYING)
-               dsb->dsound->device->need_remix = TRUE;
-       LeaveCriticalSection(&dsb->lock);
-}
-
-static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, DWORD playpos, DWORD writepos, DWORD mixlen)
-{
-       DWORD len, slen;
-       /* determine this buffer's write position */
-       DWORD buf_writepos = DSOUND_CalcPlayPosition(dsb, writepos, writepos);
-       /* determine how much already-mixed data exists */
-       DWORD buf_done =
-               ((dsb->buf_mixpos < buf_writepos) ? dsb->buflen : 0) +
-               dsb->buf_mixpos - buf_writepos;
-       DWORD primary_done =
-               ((dsb->primary_mixpos < writepos) ? dsb->dsound->device->buflen : 0) +
-               dsb->primary_mixpos - writepos;
-       DWORD adv_done =
-               ((dsb->dsound->device->mixpos < writepos) ? dsb->dsound->device->buflen : 0) +
-               dsb->dsound->device->mixpos - writepos;
-       DWORD played =
-               ((buf_writepos < dsb->playpos) ? dsb->buflen : 0) +
-               buf_writepos - dsb->playpos;
-       DWORD buf_left = dsb->buflen - buf_writepos;
-       int still_behind;
-
-       TRACE("(%p,%ld,%ld,%ld)\n",dsb,playpos,writepos,mixlen);
-       TRACE("buf_writepos=%ld, primary_writepos=%ld\n", buf_writepos, writepos);
-       TRACE("buf_done=%ld, primary_done=%ld\n", buf_done, primary_done);
-       TRACE("buf_mixpos=%ld, primary_mixpos=%ld, mixlen=%ld\n", dsb->buf_mixpos, dsb->primary_mixpos,
-             mixlen);
-       TRACE("looping=%ld, startpos=%ld, leadin=%ld\n", dsb->playflags, dsb->startpos, dsb->leadin);
-
-       /* check for notification positions */
-       if (dsb->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY &&
-           dsb->state != STATE_STARTING) {
-               DSOUND_CheckEvent(dsb, played);
+       /* sanity */
+       if(mixlen < primary_done)
+       {
+               /* Should *NEVER* happen */
+               ERR("Fatal error. Under/Overflow? primary_done=%d, mixpos=%d/%d (%d/%d), primary_mixpos=%d, writepos=%d, mixlen=%d\n", primary_done,dsb->buf_mixpos,dsb->tmp_buffer_len,dsb->sec_mixpos, dsb->buflen, dsb->primary_mixpos, writepos, mixlen);
+               return 0;
        }
 
-       /* save write position for non-GETCURRENTPOSITION2... */
-       dsb->playpos = buf_writepos;
-
-       /* check whether CalcPlayPosition detected a mixing underrun */
-       if ((buf_done == 0) && (dsb->primary_mixpos != writepos)) {
-               /* it did, but did we have more to play? */
-               if ((dsb->playflags & DSBPLAY_LOOPING) ||
-                   (dsb->buf_mixpos < dsb->buflen)) {
-                       /* yes, have to recover */
-                       ERR("underrun on sound buffer %p\n", dsb);
-                       TRACE("recovering from underrun: primary_mixpos=%ld\n", writepos);
-               }
-               dsb->primary_mixpos = writepos;
-               primary_done = 0;
-       }
-       /* determine how far ahead we should mix */
-       if (((dsb->playflags & DSBPLAY_LOOPING) ||
-            (dsb->leadin && (dsb->probably_valid_to != 0))) &&
-           !(dsb->dsbd.dwFlags & DSBCAPS_STATIC)) {
-               /* if this is a streaming buffer, it typically means that
-                * we should defer mixing past probably_valid_to as long
-                * as we can, to avoid unnecessary remixing */
-               /* the heavy-looking calculations shouldn't be that bad,
-                * as any game isn't likely to be have more than 1 or 2
-                * streaming buffers in use at any time anyway... */
-               DWORD probably_valid_left =
-                       (dsb->probably_valid_to == (DWORD)-1) ? dsb->buflen :
-                       ((dsb->probably_valid_to < buf_writepos) ? dsb->buflen : 0) +
-                       dsb->probably_valid_to - buf_writepos;
-               /* check for leadin condition */
-               if ((probably_valid_left == 0) &&
-                   (dsb->probably_valid_to == dsb->startpos) &&
-                   dsb->leadin)
-                       probably_valid_left = dsb->buflen;
-               TRACE("streaming buffer probably_valid_to=%ld, probably_valid_left=%ld\n",
-                     dsb->probably_valid_to, probably_valid_left);
-               /* check whether the app's time is already up */
-               if (probably_valid_left < dsb->writelead) {
-                       WARN("probably_valid_to now within writelead, possible streaming underrun\n");
-                       /* once we pass the point of no return,
-                        * no reason to hold back anymore */
-                       dsb->probably_valid_to = (DWORD)-1;
-                       /* we just have to go ahead and mix what we have,
-                        * there's no telling what the app is thinking anyway */
-               } else {
-                       /* adjust for our frequency and our sample size */
-                       probably_valid_left = MulDiv(probably_valid_left,
-                                                    1 << DSOUND_FREQSHIFT,
-                                                    dsb->pwfx->nBlockAlign * dsb->freqAdjust) *
-                                             dsb->dsound->device->pwfx->nBlockAlign;
-                       /* check whether to clip mix_len */
-                       if (probably_valid_left < mixlen) {
-                               TRACE("clipping to probably_valid_left=%ld\n", probably_valid_left);
-                               mixlen = probably_valid_left;
+       /* take into account already mixed data */
+       mixlen -= primary_done;
+
+       TRACE("primary_done=%d, mixlen (primary) = %i\n", primary_done, mixlen);
+
+       if (!mixlen)
+               return primary_done;
+
+       /* First try to mix to the end of the buffer if possible
+        * Theoretically it would allow for better optimization
+       */
+       if (mixlen + dsb->buf_mixpos >= dsb->tmp_buffer_len)
+       {
+               DWORD newmixed, mixfirst = dsb->tmp_buffer_len - dsb->buf_mixpos;
+               newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
+               mixlen -= newmixed;
+
+               if (dsb->playflags & DSBPLAY_LOOPING)
+                       while (newmixed && mixlen)
+                       {
+                               mixfirst = (dsb->tmp_buffer_len < mixlen ? dsb->tmp_buffer_len : mixlen);
+                               newmixed = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixfirst);
+                               mixlen -= newmixed;
                        }
-               }
-       }
-       /* cut mixlen with what's already been mixed */
-       if (mixlen < primary_done) {
-               /* huh? and still CalcPlayPosition didn't
-                * detect an underrun? */
-               FIXME("problem with underrun detection (mixlen=%ld < primary_done=%ld)\n", mixlen, primary_done);
-               return 0;
        }
-       len = mixlen - primary_done;
-       TRACE("remaining mixlen=%ld\n", len);
-
-       if (len < dsb->dsound->device->fraglen) {
-               /* smaller than a fragment, wait until it gets larger
-                * before we take the mixing overhead */
-               TRACE("mixlen not worth it, deferring mixing\n");
-               still_behind = 1;
-               goto post_mix;
-       }
-
-       /* ok, we know how much to mix, let's go */
-       still_behind = (adv_done > primary_done);
-       while (len) {
-               slen = dsb->dsound->device->buflen - dsb->primary_mixpos;
-               if (slen > len) slen = len;
-               slen = DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, slen);
+       else DSOUND_MixInBuffer(dsb, dsb->primary_mixpos, mixlen);
 
-               if ((dsb->primary_mixpos < dsb->dsound->device->mixpos) &&
-                   (dsb->primary_mixpos + slen >= dsb->dsound->device->mixpos))
-                       still_behind = FALSE;
+       /* re-calculate the primary done */
+       primary_done = DSOUND_BufPtrDiff(dsb->device->buflen, dsb->primary_mixpos, writepos);
 
-               dsb->primary_mixpos += slen; len -= slen;
-               dsb->primary_mixpos %= dsb->dsound->device->buflen;
+       TRACE("new primary_mixpos=%d, total mixed data=%d\n", dsb->primary_mixpos, primary_done);
 
-               if ((dsb->state == STATE_STOPPED) || !slen) break;
-       }
-       TRACE("new primary_mixpos=%ld, primary_advbase=%ld\n", dsb->primary_mixpos, dsb->dsound->device->mixpos);
-       TRACE("mixed data len=%ld, still_behind=%d\n", mixlen-len, still_behind);
-
-post_mix:
-       /* check if buffer should be considered complete */
-       if (buf_left < dsb->writelead &&
-           !(dsb->playflags & DSBPLAY_LOOPING)) {
-               dsb->state = STATE_STOPPED;
-               dsb->playpos = 0;
-               dsb->last_playpos = 0;
-               dsb->buf_mixpos = 0;
-               dsb->leadin = FALSE;
-               dsb->need_remix = FALSE;
-               DSOUND_CheckEvent(dsb, buf_left);
-       }
-
-       /* return how far we think the primary buffer can
-        * advance its underrun detector...*/
-       if (still_behind) return 0;
-       if ((mixlen - len) < primary_done) return 0;
-       slen = ((dsb->primary_mixpos < dsb->dsound->device->mixpos) ?
-               dsb->dsound->device->buflen : 0) + dsb->primary_mixpos -
-               dsb->dsound->device->mixpos;
-       if (slen > mixlen) {
-               /* the primary_done and still_behind checks above should have worked */
-               FIXME("problem with advancement calculation (advlen=%ld > mixlen=%ld)\n", slen, mixlen);
-               slen = 0;
-       }
-       return slen;
+       /* Report back the total prebuffered amount for this buffer */
+       return primary_done;
 }
 
-static DWORD DSOUND_MixToPrimary(DirectSoundDevice *device, DWORD playpos, DWORD writepos, DWORD mixlen, BOOL recover)
+/**
+ * For a DirectSoundDevice, go through all the currently playing buffers and
+ * mix them in to the device buffer.
+ *
+ * writepos = the current safe-to-write position in the primary buffer
+ * mixlen = the maximum amount to mix into the primary buffer
+ *          (beyond the current writepos)
+ * mustlock = Do we have to fight for lock because we otherwise risk an underrun?
+ * recover = true if the sound device may have been reset and the write
+ *           position in the device buffer changed
+ * all_stopped = reports back if all buffers have stopped
+ *
+ * Returns:  the length beyond the writepos that was mixed to.
+ */
+
+static DWORD DSOUND_MixToPrimary(const DirectSoundDevice *device, DWORD writepos, DWORD mixlen, BOOL mustlock, BOOL recover, BOOL *all_stopped)
 {
-       INT                     i, len, maxlen = 0;
+       INT i, len;
+       DWORD minlen = 0;
        IDirectSoundBufferImpl  *dsb;
+       BOOL gotall = TRUE;
 
-       TRACE("(%ld,%ld,%ld,%d)\n", playpos, writepos, mixlen, recover);
+       /* unless we find a running buffer, all have stopped */
+       *all_stopped = TRUE;
+
+       TRACE("(%d,%d,%d)\n", writepos, mixlen, recover);
        for (i = 0; i < device->nrofbuffers; i++) {
                dsb = device->buffers[i];
 
+               TRACE("MixToPrimary for %p, state=%d\n", dsb, dsb->state);
+
                if (dsb->buflen && dsb->state && !dsb->hwbuf) {
-                       TRACE("Checking %p, mixlen=%ld\n", dsb, mixlen);
-                       EnterCriticalSection(&(dsb->lock));
+                       TRACE("Checking %p, mixlen=%d\n", dsb, mixlen);
+                       if (!RtlAcquireResourceShared(&dsb->lock, mustlock))
+                       {
+                               gotall = FALSE;
+                               continue;
+                       }
+                       /* if buffer is stopping it is stopped now */
                        if (dsb->state == STATE_STOPPING) {
-                               DSOUND_MixCancel(dsb, writepos, TRUE);
                                dsb->state = STATE_STOPPED;
-                               DSOUND_CheckEvent(dsb, 0);
-                       } else {
+                               DSOUND_CheckEvent(dsb, 0, 0);
+                       } else if (dsb->state != STATE_STOPPED) {
+
+                               /* if recovering, reset the mix position */
                                if ((dsb->state == STATE_STARTING) || recover) {
                                        dsb->primary_mixpos = writepos;
-                                       dsb->cvolpan = dsb->volpan;
-                                       dsb->need_remix = FALSE;
-                               }
-                               else if (dsb->need_remix) {
-                                       DSOUND_MixCancel(dsb, writepos, TRUE);
-                                       dsb->cvolpan = dsb->volpan;
-                                       dsb->need_remix = FALSE;
                                }
-                               len = DSOUND_MixOne(dsb, playpos, writepos, mixlen);
+
+                               /* if the buffer was starting, it must be playing now */
                                if (dsb->state == STATE_STARTING)
                                        dsb->state = STATE_PLAYING;
-                               maxlen = (len > maxlen) ? len : maxlen;
+
+                               /* mix next buffer into the main buffer */
+                               len = DSOUND_MixOne(dsb, writepos, mixlen);
+
+                               if (!minlen) minlen = len;
+
+                               /* record the minimum length mixed from all buffers */
+                               /* we only want to return the length which *all* buffers have mixed */
+                               else if (len) minlen = (len < minlen) ? len : minlen;
+
+                               *all_stopped = FALSE;
                        }
-                       LeaveCriticalSection(&(dsb->lock));
+                       RtlReleaseResource(&dsb->lock);
                }
        }
 
-       return maxlen;
+       TRACE("Mixed at least %d from all buffers\n", minlen);
+       if (!gotall) return 0;
+       return minlen;
 }
 
-static void DSOUND_MixReset(DirectSoundDevice *device, DWORD writepos)
+/**
+ * Add buffers to the emulated wave device system.
+ *
+ * device = The current dsound playback device
+ * force = If TRUE, the function will buffer up as many frags as possible,
+ *         even though and will ignore the actual state of the primary buffer.
+ *
+ * Returns:  None
+ */
+
+static void DSOUND_WaveQueue(DirectSoundDevice *device, BOOL force)
 {
-       INT                     i;
-       IDirectSoundBufferImpl  *dsb;
-       int nfiller;
+       DWORD prebuf_frags, wave_writepos, wave_fragpos, i;
+       TRACE("(%p)\n", device);
 
-       TRACE("(%p,%ld)\n", device, writepos);
+       /* calculate the current wave frag position */
+       wave_fragpos = (device->pwplay + device->pwqueue) % device->helfrags;
+
+       /* calculate the current wave write position */
+       wave_writepos = wave_fragpos * device->fraglen;
+
+       TRACE("wave_fragpos = %i, wave_writepos = %i, pwqueue = %i, prebuf = %i\n",
+               wave_fragpos, wave_writepos, device->pwqueue, device->prebuf);
+
+       if (!force)
+       {
+               /* check remaining prebuffered frags */
+               prebuf_frags = device->mixpos / device->fraglen;
+               if (prebuf_frags == device->helfrags)
+                       --prebuf_frags;
+               TRACE("wave_fragpos = %d, mixpos_frags = %d\n", wave_fragpos, prebuf_frags);
+               if (prebuf_frags < wave_fragpos)
+                       prebuf_frags += device->helfrags;
+               prebuf_frags -= wave_fragpos;
+               TRACE("wanted prebuf_frags = %d\n", prebuf_frags);
+       }
+       else
+               /* buffer the maximum amount of frags */
+               prebuf_frags = device->prebuf;
 
-       /* the sound of silence */
-       nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
+       /* limit to the queue we have left */
+       if ((prebuf_frags + device->pwqueue) > device->prebuf)
+               prebuf_frags = device->prebuf - device->pwqueue;
 
-       /* reset all buffer mix positions */
-       for (i = 0; i < device->nrofbuffers; i++) {
-               dsb = device->buffers[i];
+       TRACE("prebuf_frags = %i\n", prebuf_frags);
 
-               if (dsb->buflen && dsb->state && !dsb->hwbuf) {
-                       TRACE("Resetting %p\n", dsb);
-                       EnterCriticalSection(&(dsb->lock));
-                       if (dsb->state == STATE_STOPPING) {
-                               dsb->state = STATE_STOPPED;
-                       }
-                       else if (dsb->state == STATE_STARTING) {
-                               /* nothing */
-                       } else {
-                               DSOUND_MixCancel(dsb, writepos, FALSE);
-                               dsb->cvolpan = dsb->volpan;
-                               dsb->need_remix = FALSE;
-                       }
-                       LeaveCriticalSection(&(dsb->lock));
-               }
-       }
+       /* adjust queue */
+       device->pwqueue += prebuf_frags;
 
-       /* wipe out premixed data */
-       if (device->mixpos < writepos) {
-               FillMemory(device->buffer + writepos, device->buflen - writepos, nfiller);
-               FillMemory(device->buffer, device->mixpos, nfiller);
-       } else {
-               FillMemory(device->buffer + writepos, device->mixpos - writepos, nfiller);
-       }
+       /* get out of CS when calling the wave system */
+       LeaveCriticalSection(&(device->mixlock));
+       /* **** */
 
-       /* reset primary mix position */
-       device->mixpos = writepos;
-}
-
-static void DSOUND_CheckReset(DirectSoundDevice *device, DWORD writepos)
-{
-       TRACE("(%p,%ld)\n",device,writepos);
-       if (device->need_remix) {
-               DSOUND_MixReset(device, writepos);
-               device->need_remix = FALSE;
-               /* maximize Half-Life performance */
-               device->prebuf = ds_snd_queue_min;
-               device->precount = 0;
-       } else {
-               device->precount++;
-               if (device->precount >= 4) {
-                       if (device->prebuf < ds_snd_queue_max)
-                               device->prebuf++;
-                       device->precount = 0;
-               }
+       /* queue up the new buffers */
+       for(i=0; i<prebuf_frags; i++){
+               TRACE("queueing wave buffer %i\n", wave_fragpos);
+               waveOutWrite(device->hwo, &device->pwave[wave_fragpos], sizeof(WAVEHDR));
+               wave_fragpos++;
+               wave_fragpos %= device->helfrags;
        }
-       TRACE("premix adjust: %d\n", device->prebuf);
-}
 
-void DSOUND_WaveQueue(DirectSoundDevice *device, DWORD mixq)
-{
-       TRACE("(%p,%ld)\n", device, mixq);
-       if (mixq + device->pwqueue > ds_hel_queue) mixq = ds_hel_queue - device->pwqueue;
-       TRACE("queueing %ld buffers, starting at %d\n", mixq, device->pwwrite);
-       for (; mixq; mixq--) {
-               waveOutWrite(device->hwo, device->pwave[device->pwwrite], sizeof(WAVEHDR));
-               device->pwwrite++;
-               if (device->pwwrite >= DS_HEL_FRAGS) device->pwwrite = 0;
-               device->pwqueue++;
-       }
-}
+       /* **** */
+       EnterCriticalSection(&(device->mixlock));
 
-/* #define SYNC_CALLBACK */
+       TRACE("queue now = %i\n", device->pwqueue);
+}
 
-void DSOUND_PerformMix(DirectSoundDevice *device)
+/**
+ * Perform mixing for a Direct Sound device. That is, go through all the
+ * secondary buffers (the sound bites currently playing) and mix them in
+ * to the primary buffer (the device buffer).
+ */
+static void DSOUND_PerformMix(DirectSoundDevice *device)
 {
-       int nfiller;
-       BOOL forced;
-       HRESULT hres;
-
        TRACE("(%p)\n", device);
 
-       /* the sound of silence */
-       nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
-
-       /* whether the primary is forced to play even without secondary buffers */
-       forced = ((device->state == STATE_PLAYING) || (device->state == STATE_STARTING));
+       /* **** */
+       EnterCriticalSection(&(device->mixlock));
 
        if (device->priolevel != DSSCL_WRITEPRIMARY) {
-               BOOL paused = ((device->state == STATE_STOPPED) || (device->state == STATE_STARTING));
-               /* FIXME: document variables */
-               DWORD playpos, writepos, inq, maxq, frag;
-               if (device->hwbuf) {
-                       hres = IDsDriverBuffer_GetPosition(device->hwbuf, &playpos, &writepos);
-                       if (hres) {
-                           WARN("IDsDriverBuffer_GetPosition failed\n");
-                           return;
-                       }
-                       /* Well, we *could* do Just-In-Time mixing using the writepos,
-                        * but that's a little bit ambitious and unnecessary... */
-                       /* rather add our safety margin to the writepos, if we're playing */
-                       if (!paused) {
-                               writepos += device->writelead;
-                               writepos %= device->buflen;
-                       } else writepos = playpos;
-               } else {
-                       playpos = device->pwplay * device->fraglen;
-                       writepos = playpos;
-                       if (!paused) {
-                               writepos += ds_hel_margin * device->fraglen;
-                               writepos %= device->buflen;
-                       }
+               BOOL recover = FALSE, all_stopped = FALSE;
+               DWORD playpos, writepos, writelead, maxq, frag, prebuff_max, prebuff_left, size1, size2, mixplaypos, mixplaypos2;
+               LPVOID buf1, buf2;
+               BOOL lock = (device->hwbuf && !(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK));
+               BOOL mustlock = FALSE;
+               int nfiller;
+
+               /* the sound of silence */
+               nfiller = device->pwfx->wBitsPerSample == 8 ? 128 : 0;
+
+               /* get the position in the primary buffer */
+               if (DSOUND_PrimaryGetPosition(device, &playpos, &writepos) != 0){
+                       LeaveCriticalSection(&(device->mixlock));
+                       return;
                }
-               TRACE("primary playpos=%ld, writepos=%ld, clrpos=%ld, mixpos=%ld, buflen=%ld\n",
+
+               TRACE("primary playpos=%d, writepos=%d, clrpos=%d, mixpos=%d, buflen=%d\n",
                      playpos,writepos,device->playpos,device->mixpos,device->buflen);
                assert(device->playpos < device->buflen);
-               /* wipe out just-played sound data */
-               if (playpos < device->playpos) {
-                       FillMemory(device->buffer + device->playpos, device->buflen - device->playpos, nfiller);
-                       FillMemory(device->buffer, playpos, nfiller);
+
+               mixplaypos = DSOUND_bufpos_to_mixpos(device, device->playpos);
+               mixplaypos2 = DSOUND_bufpos_to_mixpos(device, playpos);
+
+               /* calc maximum prebuff */
+               prebuff_max = (device->prebuf * device->fraglen);
+               if (!device->hwbuf && playpos + prebuff_max >= device->helfrags * device->fraglen)
+                       prebuff_max += device->buflen - device->helfrags * device->fraglen;
+
+               /* check how close we are to an underrun. It occurs when the writepos overtakes the mixpos */
+               prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
+               writelead = DSOUND_BufPtrDiff(device->buflen, writepos, playpos);
+
+               /* check for underrun. underrun occurs when the write position passes the mix position
+                * also wipe out just-played sound data */
+               if((prebuff_left > prebuff_max) || (device->state == STATE_STOPPED) || (device->state == STATE_STARTING)){
+                       if (device->state == STATE_STOPPING || device->state == STATE_PLAYING)
+                               WARN("Probable buffer underrun\n");
+                       else TRACE("Buffer starting or buffer underrun\n");
+
+                       /* recover mixing for all buffers */
+                       recover = TRUE;
+
+                       /* reset mix position to write position */
+                       device->mixpos = writepos;
+
+                       ZeroMemory(device->mix_buffer, device->mix_buffer_len);
+                       ZeroMemory(device->buffer, device->buflen);
+               } else if (playpos < device->playpos) {
+                       buf1 = device->buffer + device->playpos;
+                       buf2 = device->buffer;
+                       size1 = device->buflen - device->playpos;
+                       size2 = playpos;
+                       FillMemory(device->mix_buffer + mixplaypos, device->mix_buffer_len - mixplaypos, 0);
+                       FillMemory(device->mix_buffer, mixplaypos2, 0);
+                       if (lock)
+                               IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
+                       FillMemory(buf1, size1, nfiller);
+                       if (playpos && (!buf2 || !size2))
+                               FIXME("%d: (%d, %d)=>(%d, %d) There should be an additional buffer here!!\n", __LINE__, device->playpos, device->mixpos, playpos, writepos);
+                       FillMemory(buf2, size2, nfiller);
+                       if (lock)
+                               IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
                } else {
-                       FillMemory(device->buffer + device->playpos, playpos - device->playpos, nfiller);
+                       buf1 = device->buffer + device->playpos;
+                       buf2 = NULL;
+                       size1 = playpos - device->playpos;
+                       size2 = 0;
+                       FillMemory(device->mix_buffer + mixplaypos, mixplaypos2 - mixplaypos, 0);
+                       if (lock)
+                               IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, device->playpos, size1+size2, 0);
+                       FillMemory(buf1, size1, nfiller);
+                       if (buf2 && size2)
+                       {
+                               FIXME("%d: There should be no additional buffer here!!\n", __LINE__);
+                               FillMemory(buf2, size2, nfiller);
+                       }
+                       if (lock)
+                               IDsDriverBuffer_Unlock(device->hwbuf, buf1, size1, buf2, size2);
                }
                device->playpos = playpos;
 
-               EnterCriticalSection(&(device->mixlock));
+               /* find the maximum we can prebuffer from current write position */
+               maxq = (writelead < prebuff_max) ? (prebuff_max - writelead) : 0;
 
-               /* reset mixing if necessary */
-               DSOUND_CheckReset(device, writepos);
-
-               /* check how much prebuffering is left */
-               inq = device->mixpos;
-               if (inq < writepos)
-                       inq += device->buflen;
-               inq -= writepos;
-
-               /* find the maximum we can prebuffer */
-               if (!paused) {
-                       maxq = playpos;
-                       if (maxq < writepos)
-                               maxq += device->buflen;
-                       maxq -= writepos;
-               } else maxq = device->buflen;
-
-               /* clip maxq to device->prebuf */
-               frag = device->prebuf * device->fraglen;
-               if (maxq > frag) maxq = frag;
-
-               /* check for consistency */
-               if (inq > maxq) {
-                       /* the playback position must have passed our last
-                        * mixed position, i.e. it's an underrun, or we have
-                        * nothing more to play */
-                       TRACE("reached end of mixed data (inq=%ld, maxq=%ld)\n", inq, maxq);
-                       inq = 0;
-                       /* stop the playback now, to allow buffers to refill */
-                       if (device->state == STATE_PLAYING) {
-                               device->state = STATE_STARTING;
-                       }
-                       else if (device->state == STATE_STOPPING) {
-                               device->state = STATE_STOPPED;
-                       }
-                       else {
-                               /* how can we have an underrun if we aren't playing? */
-                               WARN("unexpected primary state (%ld)\n", device->state);
-                       }
-#ifdef SYNC_CALLBACK
-                       /* DSOUND_callback may need this lock */
-                       LeaveCriticalSection(&(device->mixlock));
-#endif
-                       if (DSOUND_PrimaryStop(device) != DS_OK)
-                               WARN("DSOUND_PrimaryStop failed\n");
-#ifdef SYNC_CALLBACK
-                       EnterCriticalSection(&(device->mixlock));
-#endif
-                       if (device->hwbuf) {
-                               /* the Stop is supposed to reset play position to beginning of buffer */
-                               /* unfortunately, OSS is not able to do so, so get current pointer */
-                               hres = IDsDriverBuffer_GetPosition(device->hwbuf, &playpos, NULL);
-                               if (hres) {
-                                       LeaveCriticalSection(&(device->mixlock));
-                                       WARN("IDsDriverBuffer_GetPosition failed\n");
-                                       return;
-                               }
-                       } else {
-                               playpos = device->pwplay * device->fraglen;
-                       }
-                       writepos = playpos;
-                       device->playpos = playpos;
-                       device->mixpos = writepos;
-                       inq = 0;
-                       maxq = device->buflen;
-                       if (maxq > frag) maxq = frag;
-                       FillMemory(device->buffer, device->buflen, nfiller);
-                       paused = TRUE;
-               }
+               TRACE("prebuff_left = %d, prebuff_max = %dx%d=%d, writelead=%d\n",
+                       prebuff_left, device->prebuf, device->fraglen, prebuff_max, writelead);
+
+               /* Do we risk an 'underrun' if we don't advance pointer? */
+               if (writelead/device->fraglen <= ds_snd_queue_min || recover)
+                       mustlock = TRUE;
+
+               if (lock)
+                       IDsDriverBuffer_Lock(device->hwbuf, &buf1, &size1, &buf2, &size2, writepos, maxq, 0);
 
                /* do the mixing */
-               frag = DSOUND_MixToPrimary(device, playpos, writepos, maxq, paused);
-               if (forced) frag = maxq - inq;
-               device->mixpos += frag;
+               frag = DSOUND_MixToPrimary(device, writepos, maxq, mustlock, recover, &all_stopped);
+
+               if (frag + writepos > device->buflen)
+               {
+                       DWORD todo = device->buflen - writepos;
+                       device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, todo);
+                       device->normfunction(device->mix_buffer, device->buffer, frag - todo);
+               }
+               else
+                       device->normfunction(device->mix_buffer + DSOUND_bufpos_to_mixpos(device, writepos), device->buffer + writepos, frag);
+
+               /* update the mix position, taking wrap-around into account */
+               device->mixpos = writepos + frag;
                device->mixpos %= device->buflen;
 
-               if (frag) {
-                       /* buffers have been filled, restart playback */
-                       if (device->state == STATE_STARTING) {
-                               device->state = STATE_PLAYING;
+               if (lock)
+               {
+                       DWORD frag2 = (frag > size1 ? frag - size1 : 0);
+                       frag -= frag2;
+                       if (frag2 > size2)
+                       {
+                               FIXME("Buffering too much! (%d, %d, %d, %d)\n", maxq, frag, size2, frag2 - size2);
+                               frag2 = size2;
                        }
-                       else if (device->state == STATE_STOPPED) {
-                               /* the dsound is supposed to play if there's something to play
-                                * even if it is reported as stopped, so don't let this confuse you */
-                               device->state = STATE_STOPPING;
+                       IDsDriverBuffer_Unlock(device->hwbuf, buf1, frag, buf2, frag2);
+               }
+
+               /* update prebuff left */
+               prebuff_left = DSOUND_BufPtrDiff(device->buflen, device->mixpos, playpos);
+
+               /* check if have a whole fragment */
+               if (prebuff_left >= device->fraglen){
+
+                       /* update the wave queue if using wave system */
+                       if (!device->hwbuf)
+                               DSOUND_WaveQueue(device, FALSE);
+
+                       /* buffers are full. start playing if applicable */
+                       if(device->state == STATE_STARTING){
+                               TRACE("started primary buffer\n");
+                               if(DSOUND_PrimaryPlay(device) != DS_OK){
+                                       WARN("DSOUND_PrimaryPlay failed\n");
+                               }
+                               else{
+                                       /* we are playing now */
+                                       device->state = STATE_PLAYING;
+                               }
                        }
-                       LeaveCriticalSection(&(device->mixlock));
-                       if (paused) {
-                               if (DSOUND_PrimaryPlay(device) != DS_OK)
+
+                       /* buffers are full. start stopping if applicable */
+                       if(device->state == STATE_STOPPED){
+                               TRACE("restarting primary buffer\n");
+                               if(DSOUND_PrimaryPlay(device) != DS_OK){
                                        WARN("DSOUND_PrimaryPlay failed\n");
-                               else
-                                       TRACE("starting playback\n");
+                               }
+                               else{
+                                       /* start stopping again. as soon as there is no more data, it will stop */
+                                       device->state = STATE_STOPPING;
+                               }
                        }
                }
-               else
-                       LeaveCriticalSection(&(device->mixlock));
+
+               /* if device was stopping, its for sure stopped when all buffers have stopped */
+               else if((all_stopped == TRUE) && (device->state == STATE_STOPPING)){
+                       TRACE("All buffers have stopped. Stopping primary buffer\n");
+                       device->state = STATE_STOPPED;
+
+                       /* stop the primary buffer now */
+                       DSOUND_PrimaryStop(device);
+               }
+
        } else {
+
+               /* update the wave queue if using wave system */
+               if (!device->hwbuf)
+                       DSOUND_WaveQueue(device, TRUE);
+               else
+                       /* Keep alsa happy, which needs GetPosition called once every 10 ms */
+                       IDsDriverBuffer_GetPosition(device->hwbuf, NULL, NULL);
+
                /* in the DSSCL_WRITEPRIMARY mode, the app is totally in charge... */
                if (device->state == STATE_STARTING) {
                        if (DSOUND_PrimaryPlay(device) != DS_OK)
@@ -1104,15 +980,19 @@ void DSOUND_PerformMix(DirectSoundDevice *device)
                                device->state = STATE_STOPPED;
                }
        }
+
+       LeaveCriticalSection(&(device->mixlock));
+       /* **** */
 }
 
-void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
+void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser,
+                           DWORD_PTR dw1, DWORD_PTR dw2)
 {
-        DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
+       DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
        DWORD start_time =  GetTickCount();
-        DWORD end_time;
+       DWORD end_time;
        TRACE("(%d,%d,0x%lx,0x%lx,0x%lx)\n",timerID,msg,dwUser,dw1,dw2);
-        TRACE("entering at %ld\n", start_time);
+       TRACE("entering at %d\n", start_time);
 
        if (DSOUND_renderer[device->drvdesc.dnDevNode] != device) {
                ERR("dsound died without killing us?\n");
@@ -1129,49 +1009,39 @@ void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWOR
        RtlReleaseResource(&(device->buffer_list_lock));
 
        end_time = GetTickCount();
-       TRACE("completed processing at %ld, duration = %ld\n", end_time, end_time - start_time);
+       TRACE("completed processing at %d, duration = %d\n", end_time, end_time - start_time);
 }
 
-void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2)
+void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
 {
-        DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
+       DirectSoundDevice * device = (DirectSoundDevice*)dwUser;
        TRACE("(%p,%x,%lx,%lx,%lx)\n",hwo,msg,dwUser,dw1,dw2);
-       TRACE("entering at %ld, msg=%08x(%s)\n", GetTickCount(), msg,
-               msg==MM_WOM_DONE ? "MM_WOM_DONE" : msg==MM_WOM_CLOSE ? "MM_WOM_CLOSE" :
+       TRACE("entering at %d, msg=%08x(%s)\n", GetTickCount(), msg,
+               msg==MM_WOM_DONE ? "MM_WOM_DONE" : msg==MM_WOM_CLOSE ? "MM_WOM_CLOSE" : 
                msg==MM_WOM_OPEN ? "MM_WOM_OPEN" : "UNKNOWN");
+
+       /* check if packet completed from wave driver */
        if (msg == MM_WOM_DONE) {
-               DWORD inq, mixq, fraglen, buflen, pwplay, playpos, mixpos;
-               if (device->pwqueue == (DWORD)-1) {
-                       TRACE("completed due to reset\n");
-                       return;
-               }
-/* it could be a bad idea to enter critical section here... if there's lock contention,
- * the resulting scheduling delays might obstruct the winmm player thread */
-#ifdef SYNC_CALLBACK
+
+               /* **** */
                EnterCriticalSection(&(device->mixlock));
-#endif
-               /* retrieve current values */
-               fraglen = device->fraglen;
-               buflen = device->buflen;
-               pwplay = device->pwplay;
-               playpos = pwplay * fraglen;
-               mixpos = device->mixpos;
-               /* check remaining mixed data */
-               inq = ((mixpos < playpos) ? buflen : 0) + mixpos - playpos;
-               mixq = inq / fraglen;
-               if ((inq - (mixq * fraglen)) > 0) mixq++;
-               /* complete the playing buffer */
-               TRACE("done playing primary pos=%ld\n", playpos);
-               pwplay++;
-               if (pwplay >= DS_HEL_FRAGS) pwplay = 0;
-               /* write new values */
-               device->pwplay = pwplay;
+
+               TRACE("done playing primary pos=%d\n", device->pwplay * device->fraglen);
+
+               /* update playpos */
+               device->pwplay++;
+               device->pwplay %= device->helfrags;
+
+               /* sanity */
+               if(device->pwqueue == 0){
+                       ERR("Wave queue corrupted!\n");
+               }
+
+               /* update queue */
                device->pwqueue--;
-               /* queue new buffer if we have data for it */
-               if (inq>1) DSOUND_WaveQueue(device, inq-1);
-#ifdef SYNC_CALLBACK
+
                LeaveCriticalSection(&(device->mixlock));
-#endif
+               /* **** */
        }
        TRACE("completed\n");
 }