From 41de4d924d1c05232f142385cd16befeaf8a5a6a Mon Sep 17 00:00:00 2001 From: Etienne Dechamps Date: Sat, 15 Jun 2024 17:39:20 +0100 Subject: [PATCH] Fix WDM-KS buffer position alignment The previous code is only correct if `bytesPerFrame` is a power of two. If it's not (e.g. 2-channel 24-bit = 6 bytes per frame), then the computed buffer position is corrupted. This can lead the code to use the wrong buffer half, resulting in glitchy audio. Fixes #763 --- src/hostapi/wdmks/pa_win_wdmks.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hostapi/wdmks/pa_win_wdmks.c b/src/hostapi/wdmks/pa_win_wdmks.c index 36cb396a8..c19afa98e 100644 --- a/src/hostapi/wdmks/pa_win_wdmks.c +++ b/src/hostapi/wdmks/pa_win_wdmks.c @@ -6651,7 +6651,7 @@ static PaError PaPinCaptureEventHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, pos += pin->hwLatency; pos %= pCapture->hostBufferSize; /* Need to align position on frame boundary */ - pos &= ~(pCapture->bytesPerFrame - 1); + pos = (pos / pCapture->bytesPerFrame) * pCapture->bytesPerFrame; /* Call barrier (or dummy) */ pin->fnMemBarrier(); @@ -6703,7 +6703,7 @@ static PaError PaPinRenderEventHandler_WaveRTEvent(PaProcessThreadInfo* pInfo, u /* Wrap it */ pos %= pRender->hostBufferSize; /* And align it, not sure its really needed though */ - pos &= ~(pRender->bytesPerFrame - 1); + pos = (pos / pRender->bytesPerFrame) * pRender->bytesPerFrame; /* Then realOutBuf will point to "other" half of double buffer */ realOutBuf = pos < halfOutputBuffer ? 1U : 0U; @@ -6740,7 +6740,7 @@ static PaError PaPinRenderEventHandler_WaveRTPolled(PaProcessThreadInfo* pInfo, /* Wrap it */ pos %= pRender->hostBufferSize; /* And align it, not sure its really needed though */ - pos &= ~(pRender->bytesPerFrame - 1); + pos = (pos / pRender->bytesPerFrame) * pRender->bytesPerFrame; if (pInfo->priming) {