Browse Source

Fixed ResampleAudio interpolation factor calculation

Brick 1 year ago
parent
commit
e6c878824c
2 changed files with 8 additions and 5 deletions
  1. 5 2
      src/audio/SDL_audiocvt.c
  2. 3 3
      test/testautomation_audio.c

+ 5 - 2
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) {

+ 3 - 3
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 }
   };