From 49836386304c7e18c14e3078060bc2fd773cb8b7 Mon Sep 17 00:00:00 2001
From: Brick <[EMAIL REDACTED]>
Date: Mon, 21 Aug 2023 20:45:52 +0100
Subject: [PATCH] Misc audio tweaks/cleanup
---
src/audio/SDL_audiocvt.c | 16 ++++++++++------
test/testautomation_audio.c | 2 +-
2 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 0cb85008da1d..5496d238f67d 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -32,8 +32,12 @@
#include "SDL_audio_resampler_filter.h"
+/* For a given srcpos, `srcpos + frame` are sampled, where `-RESAMPLER_ZERO_CROSSINGS < frame <= RESAMPLER_ZERO_CROSSINGS`.
+ * Note, when upsampling, it is also possible to start sampling from `srcpos = -1`. */
#define RESAMPLER_MAX_PADDING_FRAMES (RESAMPLER_ZERO_CROSSINGS + 1)
+/* The source position is tracked using 32:32 fixed-point arithmetic.
+ * This gives high precision and avoids lots of divides in ResampleAudio. */
static Sint64 GetResampleRate(const int src_rate, const int dst_rate)
{
SDL_assert(src_rate > 0);
@@ -47,16 +51,16 @@ static Sint64 GetResampleRate(const int src_rate, const int dst_rate)
static size_t GetResamplerAvailableOutputFrames(const size_t input_frames, const Sint64 resample_rate, const Sint64 resample_offset)
{
- const Sint64 frames = ((((Sint64)input_frames << 32) - resample_offset - 1) / resample_rate) + 1;
+ const Sint64 output_frames = ((((Sint64)input_frames << 32) - resample_offset - 1) / resample_rate) + 1;
- return (size_t) SDL_max(frames, 0);
+ return (size_t) SDL_max(output_frames, 0);
}
static int GetResamplerNeededInputFrames(const int output_frames, const Sint64 resample_rate, const Sint64 resample_offset)
{
- const Sint32 frames = (Sint32)((((output_frames - 1) * resample_rate) + resample_offset) >> 32) + 1;
+ const Sint32 input_frames = (Sint32)((((output_frames - 1) * resample_rate) + resample_offset) >> 32) + 1;
- return (int) SDL_max(frames, 0);
+ return (int) SDL_max(input_frames, 0);
}
static int GetResamplerPaddingFrames(const Sint64 resample_rate)
@@ -68,6 +72,7 @@ static int GetHistoryBufferSampleFrames(const int required_resampler_frames)
{
SDL_assert(required_resampler_frames <= RESAMPLER_MAX_PADDING_FRAMES);
+ // Even if we aren't currently resampling, make sure to keep enough history in case we need to later.
return RESAMPLER_MAX_PADDING_FRAMES;
}
@@ -88,8 +93,7 @@ static void ResampleAudio(const int chans, const float *lpadding, const float *r
Uint32 srcfraction = (Uint32)(srcpos & 0xFFFFFFFF);
srcpos += resample_rate;
- SDL_assert(srcindex >= -1);
- SDL_assert(srcindex < inframes);
+ SDL_assert(srcindex >= -1 && srcindex < inframes);
const int filterindex = (int)(srcfraction >> (32 - RESAMPLER_BITS_PER_ZERO_CROSSING)) * RESAMPLER_ZERO_CROSSINGS;
diff --git a/test/testautomation_audio.c b/test/testautomation_audio.c
index 0454d986ed93..6785b5e06cf3 100644
--- a/test/testautomation_audio.c
+++ b/test/testautomation_audio.c
@@ -796,7 +796,7 @@ static int audio_resampleLoss(void *arg)
} test_specs[] = {
{ 50, 440, 0, 44100, 48000, 79, 0.0008 },
{ 50, 5000, SDL_PI_D / 2, 20000, 10000, 999, 0.0001 },
- { 50, 440, 0, 22050, 96000, 76, 0.0120 }, /* I have no idea how to tune these values */
+ { 50, 440, 0, 22050, 96000, 76, 0.0120 },
{ 50, 440, 0, 96000, 22050, 80, 0.0014 },
{ 0 }
};