SDL: The history buffer should always have the maximum possible padding frames

From 88123a5109b8c6a951cb2e7b582a76777e603fbd Mon Sep 17 00:00:00 2001
From: Brick <[EMAIL REDACTED]>
Date: Mon, 21 Aug 2023 10:14:59 +0100
Subject: [PATCH] The history buffer should always have the maximum possible
 padding frames

---
 src/audio/SDL_audiocvt.c | 14 +++++++++-----
 src/audio/SDL_sysaudio.h |  3 ++-
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index d5e5a64c835f..7024aa939023 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -32,6 +32,8 @@
 
 #include "SDL_audio_resampler_filter.h"
 
+#define RESAMPLER_MAX_PADDING_FRAMES (RESAMPLER_ZERO_CROSSINGS + 1)
+
 static Sint64 GetResampleRate(const int src_rate, const int dst_rate)
 {
     SDL_assert(src_rate > 0);
@@ -59,12 +61,14 @@ static int GetResamplerNeededInputFrames(const int output_frames, const Sint64 r
 
 static int GetResamplerPaddingFrames(const Sint64 resample_rate)
 {
-    return resample_rate ? (RESAMPLER_ZERO_CROSSINGS + 1) : 0;
+    return resample_rate ? RESAMPLER_MAX_PADDING_FRAMES : 0;
 }
 
-static int GetHistoryBufferSampleFrames(const Sint32 required_resampler_frames)
+static int GetHistoryBufferSampleFrames(const int required_resampler_frames)
 {
-    return required_resampler_frames;
+    SDL_assert(required_resampler_frames <= RESAMPLER_MAX_PADDING_FRAMES);
+
+    return RESAMPLER_MAX_PADDING_FRAMES;
 }
 
 // lpadding and rpadding are expected to be buffers of (GetResamplerPaddingFrames(resample_rate) * chans * sizeof (float)) bytes.
@@ -98,7 +102,7 @@ static void ResampleAudio(const int chans, const float *lpadding, const float *r
 
             // do this twice to calculate the sample, once for the "left wing" and then same for the right.
             for (j = 0; j < RESAMPLER_ZERO_CROSSINGS; j++) {
-                const int filt_ind = j + filterindex1;
+                const int filt_ind = filterindex1 + j;
                 const int srcframe = srcindex - j;
                 /* !!! FIXME: we can bubble this conditional out of here by doing a pre loop. */
                 const float insample = (srcframe < 0) ? lpadding[((paddinglen + srcframe) * chans) + chan] : inbuf[(srcframe * chans) + chan];
@@ -107,7 +111,7 @@ static void ResampleAudio(const int chans, const float *lpadding, const float *r
 
             // Do the right wing!
             for (j = 0; j < RESAMPLER_ZERO_CROSSINGS; j++) {
-                const int filt_ind = j + filterindex2;
+                const int filt_ind = filterindex2 + j;
                 const int srcframe = srcindex + 1 + j;
                 // !!! FIXME: we can bubble this conditional out of here by doing a post loop.
                 const float insample = (srcframe >= inframes) ? rpadding[((srcframe - inframes) * chans) + chan] : inbuf[(srcframe * chans) + chan];
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 5f48a359d361..3ab01f3cff97 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -179,11 +179,12 @@ struct SDL_AudioStream
     int resampler_padding_frames;
     int history_buffer_frames;
     int future_buffer_filled_frames;
-    Sint64 resample_offset;
 
     SDL_AudioSpec src_spec;
     SDL_AudioSpec dst_spec;
+
     Sint64 resample_rate;
+    Sint64 resample_offset;
 
     int src_sample_frame_size;
     int dst_sample_frame_size;