From 91cd5478be5aaf684636d64662c4a63acb09afdc Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 12 Aug 2023 00:52:11 -0400 Subject: [PATCH] audio: Fix resampler overflowing input buffer. This usually manifests as a clicking sound, because it often produces a value outside the range -1.0f to 1.0f based on whatever random data is past the buffer, which later stages of audio conversion will clamp to a maximum value for the audio format. Since this tends to be a single bad sample generated at the end of the resampled buffer, it sounds like a repeating click in streamed data. I'd like a more efficient means to clamp this value to not overflow the buffer, but this puts out the immediate fire. --- src/audio/SDL_audiocvt.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c index f1517ae6c..d16c1c337 100644 --- a/src/audio/SDL_audiocvt.c +++ b/src/audio/SDL_audiocvt.c @@ -81,7 +81,11 @@ static void ResampleAudio(const int chans, const int inrate, const int outrate, int i, j, chan; for (i = 0; i < outframes; i++) { - const int srcindex = (int)((Sint64)i * inrate / outrate); + int srcindex = (int)((Sint64)i * inrate / outrate); + if (srcindex >= inframes) { // !!! FIXME: can we clamp this without an if statement on each iteration? + srcindex = inframes - 1; + } + /* Calculating the following way avoids subtraction or modulo of large * floats which have low result precision. * interpolation1