From: Eduard Permyakov Subject: [PATCH v2 1/2] dsound: Commit next audio chunk between play cursor and write cursor to playing. Message-Id: <20210818151546.13209-1-epermyakov@codeweavers.com> Date: Wed, 18 Aug 2021 18:15:45 +0300 This region of the audio buffer is forbidden to be written to by the DirectSound specification. The documentation states: "The write cursor is the point after which it is safe to write data into the buffer. The block between the play cursor and the write cursor is already committed to be played, and cannot be changed safely." However, some applications still do this, which has lead to audio glitches only when using the Wine DirectSound implementation. Experiments showed that the native DirctSound implementation will still play the old audio the first time around when the buffer region gets overwritten. Use an approach of copying the next forbidden region into a "committed buffer" to add the same behavior to the Wine implementation. Signed-off-by: Eduard Permyakov --- dlls/dsound/buffer.c | 8 ++++++++ dlls/dsound/dsound_convert.c | 30 ++++++++++++++++++++---------- dlls/dsound/dsound_private.h | 5 ++++- dlls/dsound/mixer.c | 19 +++++++++++++++++++ 4 files changed, 51 insertions(+), 11 deletions(-) diff --git a/dlls/dsound/buffer.c b/dlls/dsound/buffer.c index dc3b54906ce..df8962f1c0f 100644 --- a/dlls/dsound/buffer.c +++ b/dlls/dsound/buffer.c @@ -1081,6 +1081,13 @@ HRESULT secondarybuffer_create(DirectSoundDevice *device, const DSBUFFERDESC *ds /* calculate fragment size and write lead */ DSOUND_RecalcFormat(dsb); + dsb->committedbuff = HeapAlloc(GetProcessHeap(), 0, dsb->writelead); + if(!dsb->committedbuff) { + IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface); + return DSERR_OUTOFMEMORY; + } + FillMemory(dsb->committedbuff, dsb->writelead, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0); + if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) { dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER); dsb->ds3db_ds3db.vPosition.x = 0.0; @@ -1135,6 +1142,7 @@ void secondarybuffer_destroy(IDirectSoundBufferImpl *This) HeapFree(GetProcessHeap(), 0, This->notifies); HeapFree(GetProcessHeap(), 0, This->pwfx); + HeapFree(GetProcessHeap(), 0, This->committedbuff); if (This->filters) { int i; diff --git a/dlls/dsound/dsound_convert.c b/dlls/dsound/dsound_convert.c index 4735178a9a8..fc2b0cd0797 100644 --- a/dlls/dsound/dsound_convert.c +++ b/dlls/dsound/dsound_convert.c @@ -55,17 +55,27 @@ WINE_DEFAULT_DEBUG_CHANNEL(dsound); #define le32(x) (x) #endif +static BYTE *buff_byte(const IDirectSoundBufferImpl *dsb, DWORD offset) +{ + if(offset >= dsb->sec_mixpos && offset < dsb->sec_mixpos + dsb->writelead) { + return (BYTE*)dsb->committedbuff + (offset - dsb->sec_mixpos); + }else if(offset < (dsb->sec_mixpos + dsb->writelead) % dsb->buflen) { + return (BYTE*)dsb->committedbuff + (dsb->buflen - dsb->sec_mixpos + offset); + }else{ + return dsb->buffer->memory + offset; + } +} + static float get8(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) { - const BYTE* buf = dsb->buffer->memory; - buf += pos + channel; + const BYTE *buf = buff_byte(dsb, pos + channel); return (buf[0] - 0x80) / (float)0x80; } static float get16(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) { - const BYTE* buf = dsb->buffer->memory; - const SHORT *sbuf = (const SHORT*)(buf + pos + 2 * channel); + const BYTE *buf = buff_byte(dsb, pos + 2 * channel); + const SHORT *sbuf = (const SHORT*)(buf); SHORT sample = (SHORT)le16(*sbuf); return sample / (float)0x8000; } @@ -73,8 +83,8 @@ static float get16(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) static float get24(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) { LONG sample; - const BYTE* buf = dsb->buffer->memory; - buf += pos + 3 * channel; + const BYTE *buf = buff_byte(dsb, pos + 3 * channel); + /* The next expression deliberately has an overflow for buf[2] >= 0x80, this is how negative values are made. */ @@ -84,16 +94,16 @@ static float get24(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) static float get32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) { - const BYTE* buf = dsb->buffer->memory; - const LONG *sbuf = (const LONG*)(buf + pos + 4 * channel); + const BYTE *buf = buff_byte(dsb, pos + 4 * channel); + const LONG *sbuf = (const LONG*)(buf); LONG sample = le32(*sbuf); return sample / (float)0x80000000U; } static float getieee32(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) { - const BYTE* buf = dsb->buffer->memory; - const float *sbuf = (const float*)(buf + pos + 4 * channel); + const BYTE *buf = buff_byte(dsb, pos + 4 * channel); + const float *sbuf = (const float*)(buf); /* The value will be clipped later, when put into some non-float buffer */ return *sbuf; } diff --git a/dlls/dsound/dsound_private.h b/dlls/dsound/dsound_private.h index bdb9ebee544..a79d62c54da 100644 --- a/dlls/dsound/dsound_private.h +++ b/dlls/dsound/dsound_private.h @@ -153,7 +153,10 @@ struct IDirectSoundBufferImpl LONG64 freqAccNum; /* used for mixing */ DWORD sec_mixpos; - + /* Holds a copy of the next 'writelead' bytes, to be used for mixing. This makes it + * so that these bytes get played once even if this region of the buffer gets overwritten, + * which is more in-line with native DirectSound behavior. */ + LPVOID committedbuff; /* IDirectSoundNotify fields */ LPDSBPOSITIONNOTIFY notifies; int nrofnotifies; diff --git a/dlls/dsound/mixer.c b/dlls/dsound/mixer.c index 124d1e4fba1..32656fbd618 100644 --- a/dlls/dsound/mixer.c +++ b/dlls/dsound/mixer.c @@ -391,6 +391,18 @@ static void cp_fields(IDirectSoundBufferImpl *dsb, UINT count, LONG64 *freqAccNu dsb->sec_mixpos = ipos; } +static void copy_circular(VOID *dstbuff, VOID *srcbuff, DWORD srcoff, DWORD srcsize, DWORD cpysize) +{ + DWORD overflow; + if(cpysize > srcsize - srcoff) { + overflow = cpysize - (srcsize - srcoff); + memcpy(dstbuff, (BYTE*)srcbuff + srcoff, srcsize - srcoff); + memcpy((BYTE*)dstbuff + (srcsize - srcoff), srcbuff, overflow); + }else{ + memcpy(dstbuff, (BYTE*)srcbuff + srcoff, cpysize); + } +} + /** * Calculate the distance between two buffer offsets, taking wraparound * into account. @@ -518,6 +530,8 @@ static DWORD DSOUND_MixInBuffer(IDirectSoundBufferImpl *dsb, float *mix_buffer, INT ilen = DSOUND_BufPtrDiff(dsb->buflen, dsb->sec_mixpos, oldpos); DSOUND_CheckEvent(dsb, oldpos, ilen); } + TRACE("committing %u bytes from offset %u\n", dsb->writelead, dsb->sec_mixpos); + copy_circular(dsb->committedbuff, dsb->buffer->memory, dsb->sec_mixpos, dsb->buflen, dsb->writelead); return frames; } @@ -551,6 +565,11 @@ static DWORD DSOUND_MixOne(IDirectSoundBufferImpl *dsb, float *mix_buffer, DWORD } } + if(dsb->leadin) { + TRACE("committing %u bytes from offset %u\n", dsb->writelead, dsb->sec_mixpos); + copy_circular(dsb->committedbuff, dsb->buffer->memory, dsb->sec_mixpos, dsb->buflen, dsb->writelead); + } + dsb->leadin = FALSE; TRACE("frames (primary) = %i\n", frames); -- 2.17.1