Skip to content

Commit

Permalink
Fix WDM-KS buffer position alignment
Browse files Browse the repository at this point in the history
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 PortAudio#763
  • Loading branch information
dechamps committed Jun 15, 2024
1 parent 18a606e commit 41de4d9
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/hostapi/wdmks/pa_win_wdmks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 41de4d9

Please sign in to comment.