Misc audio tweaks/cleanup

main
Brick 2023-08-21 20:45:52 +01:00 committed by Ryan C. Gordon
parent 72d9d53def
commit 4983638630
2 changed files with 11 additions and 7 deletions

View File

@ -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;

View File

@ -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 }
};