SDL: Fixed ResampleAudio interpolation factor calculation

From e6c878824c1afce8a20ce2468a7da23b1b7d5443 Mon Sep 17 00:00:00 2001
From: Brick <[EMAIL REDACTED]>
Date: Tue, 22 Aug 2023 11:26:39 +0100
Subject: [PATCH] Fixed ResampleAudio interpolation factor calculation

---
 src/audio/SDL_audiocvt.c    | 7 +++++--
 test/testautomation_audio.c | 6 +++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 5496d238f67d..c93957f8ecab 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -76,6 +76,9 @@ static int GetHistoryBufferSampleFrames(const int required_resampler_frames)
     return RESAMPLER_MAX_PADDING_FRAMES;
 }
 
+#define RESAMPLER_FILTER_INTERP_BITS (32 - RESAMPLER_BITS_PER_ZERO_CROSSING)
+#define RESAMPLER_FILTER_INTERP_RANGE (1 << RESAMPLER_FILTER_INTERP_BITS)
+
 // lpadding and rpadding are expected to be buffers of (GetResamplerPaddingFrames(resample_rate) * chans * sizeof (float)) bytes.
 static void ResampleAudio(const int chans, const float *lpadding, const float *rpadding,
                          const float *inbuf, const int inframes, float *outbuf, const int outframes,
@@ -95,9 +98,9 @@ static void ResampleAudio(const int chans, const float *lpadding, const float *r
 
         SDL_assert(srcindex >= -1 && srcindex < inframes);
 
-        const int filterindex = (int)(srcfraction >> (32 - RESAMPLER_BITS_PER_ZERO_CROSSING)) * RESAMPLER_ZERO_CROSSINGS;
+        const int filterindex = (int)(srcfraction >> RESAMPLER_FILTER_INTERP_BITS) * RESAMPLER_ZERO_CROSSINGS;
 
-        const float interpolation1 = (float)srcfraction * 0x1p-32f;
+        const float interpolation1 = (float)(srcfraction & (RESAMPLER_FILTER_INTERP_RANGE - 1)) * (1.0f / RESAMPLER_FILTER_INTERP_RANGE);
         const float interpolation2 = 1.0f - interpolation1;
 
         for (chan = 0; chan < chans; ++chan) {
diff --git a/test/testautomation_audio.c b/test/testautomation_audio.c
index 6785b5e06cf3..84b6055123da 100644
--- a/test/testautomation_audio.c
+++ b/test/testautomation_audio.c
@@ -794,10 +794,10 @@ static int audio_resampleLoss(void *arg)
     double signal_to_noise;
     double max_error;
   } test_specs[] = {
-    { 50, 440, 0, 44100, 48000, 79, 0.0008 },
+    { 50, 440, 0, 44100, 48000, 80, 0.0009 },
     { 50, 5000, SDL_PI_D / 2, 20000, 10000, 999, 0.0001 },
-    { 50, 440, 0, 22050, 96000, 76, 0.0120 },
-    { 50, 440, 0, 96000, 22050, 80, 0.0014 },
+    { 50, 440, 0, 22050, 96000, 79, 0.0120 },
+    { 50, 440, 0, 96000, 22050, 80, 0.0002 },
     { 0 }
   };