Browse Source

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.
Ryan C. Gordon 2 years ago
parent
commit
91cd5478be
1 changed files with 5 additions and 1 deletions
  1. 5 1
      src/audio/SDL_audiocvt.c

+ 5 - 1
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