Simplify logic for audio resampling, fixes it on less ideal input/output sample rate ratios

This commit is contained in:
Mr-Wiseguy 2023-12-23 23:57:22 -05:00
parent f2eeb00e09
commit bce34bfe0f

View File

@ -2,8 +2,10 @@
#include <cassert> #include <cassert>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <array>
#include <filesystem> #include <filesystem>
#include <numeric> #include <numeric>
#include <stdexcept>
#include "../../ultramodern/ultra64.h" #include "../../ultramodern/ultra64.h"
#include "../../ultramodern/ultramodern.hpp" #include "../../ultramodern/ultramodern.hpp"
@ -84,11 +86,8 @@ static uint32_t output_channels = 2;
// Terminology: a frame is a collection of samples for each channel. e.g. 2 input samples is one input frame. This is unrelated to graphical frames. // Terminology: a frame is a collection of samples for each channel. e.g. 2 input samples is one input frame. This is unrelated to graphical frames.
// In order to prevent resampling discontinuities, the last few frames of the previous audio chunk are prepended to the current chunk before // Number of frames to duplicate for fixing interpolation at the start and end of a chunk.
// resampling it so there's enough information for interpolation. constexpr uint32_t duplicated_input_frames = 4;
constexpr uint32_t min_duplicated_frames = 32;
// The number of input frames to duplicate for interpolation to prevent discontinuities.
static uint32_t duplicated_input_frames;
// The number of output frames to skip for playback (to avoid playing duplicate inputs twice). // The number of output frames to skip for playback (to avoid playing duplicate inputs twice).
static uint32_t discarded_output_frames; static uint32_t discarded_output_frames;
@ -96,17 +95,11 @@ void queue_samples(int16_t* audio_data, size_t sample_count) {
// Buffer for holding the output of swapping the audio channels. This is reused across // Buffer for holding the output of swapping the audio channels. This is reused across
// calls to reduce runtime allocations. // calls to reduce runtime allocations.
static std::vector<float> swap_buffer; static std::vector<float> swap_buffer;
static std::vector<float> duplicated_sample_buffer; static std::array<float, duplicated_input_frames * input_channels> duplicated_sample_buffer;
assert((sample_count / input_channels) / duplicated_input_frames * duplicated_input_frames == (sample_count / input_channels)); // Make sure the swap buffer is large enough to hold the audio data, including any extra space needed for resampling.
size_t resampled_sample_count = sample_count + duplicated_input_frames * input_channels;
if (duplicated_input_frames * input_channels > duplicated_sample_buffer.size()) { size_t max_sample_count = std::max(resampled_sample_count, resampled_sample_count * audio_convert.len_mult);
duplicated_sample_buffer.resize(duplicated_input_frames * input_channels);
}
size_t max_sample_count = std::max(sample_count, sample_count * audio_convert.len_mult);
// Make sure the swap buffer is large enough to hold the audio data.
if (max_sample_count > swap_buffer.size()) { if (max_sample_count > swap_buffer.size()) {
swap_buffer.resize(max_sample_count); swap_buffer.resize(max_sample_count);
} }
@ -123,21 +116,28 @@ void queue_samples(int16_t* audio_data, size_t sample_count) {
swap_buffer[i + 1 + duplicated_input_frames * input_channels] = audio_data[i + 0] * (0.5f / 32768.0f); swap_buffer[i + 1 + duplicated_input_frames * input_channels] = audio_data[i + 0] * (0.5f / 32768.0f);
} }
// TODO handle cases where a chunk is smaller than the duplicated frame count.
assert(sample_count > duplicated_input_frames * input_channels); assert(sample_count > duplicated_input_frames * input_channels);
// Copy the last converted samples into the duplicated sample buffer to reuse in resampling the next queued chunk. // Copy the last converted samples into the duplicated sample buffer to reuse in resampling the next queued chunk.
for (size_t i = 0; i < duplicated_input_frames * 2; i++) { for (size_t i = 0; i < duplicated_input_frames * input_channels; i++) {
duplicated_sample_buffer[i] = swap_buffer[i + sample_count]; duplicated_sample_buffer[i] = swap_buffer[i + sample_count];
} }
audio_convert.buf = reinterpret_cast<Uint8*>(swap_buffer.data()); audio_convert.buf = reinterpret_cast<Uint8*>(swap_buffer.data());
audio_convert.len = (sample_count + duplicated_input_frames * input_channels) * sizeof(swap_buffer[0]); audio_convert.len = (sample_count + duplicated_input_frames * input_channels) * sizeof(swap_buffer[0]);
SDL_ConvertAudio(&audio_convert); int ret = SDL_ConvertAudio(&audio_convert);
if (ret < 0) {
printf("Error using SDL audio converter: %s\n", SDL_GetError());
throw std::runtime_error("Error using SDL audio converter");
}
// Queue the swapped audio data. // Queue the swapped audio data.
SDL_QueueAudio(audio_device, swap_buffer.data() + output_channels * discarded_output_frames, // Offset the data start by only half the discarded frame count as the other half of the discarded frames are at the end of the buffer.
sample_count * sizeof(swap_buffer[0]) * output_sample_rate * output_channels / (sample_rate * input_channels)); SDL_QueueAudio(audio_device, swap_buffer.data() + output_channels * discarded_output_frames / 2,
audio_convert.len_cvt - output_channels * discarded_output_frames * sizeof(swap_buffer[0]));
} }
constexpr uint32_t bytes_per_frame = input_channels * sizeof(float); constexpr uint32_t bytes_per_frame = input_channels * sizeof(float);
@ -166,23 +166,18 @@ size_t get_frames_remaining() {
} }
void update_audio_converter() { void update_audio_converter() {
SDL_BuildAudioCVT(&audio_convert, AUDIO_F32, 2, sample_rate, AUDIO_F32, output_channels, output_sample_rate); int ret = SDL_BuildAudioCVT(&audio_convert, AUDIO_F32, input_channels, sample_rate, AUDIO_F32, output_channels, output_sample_rate);
// Calculate the number of samples to duplicate and discard based on the greatest common denominator fo the input and output sample rates. if (ret < 0) {
// Keeping them at the same ratio as the sample rates themselves ensures an integer number of output samples are produced from an printf("Error creating SDL audio converter: %s\n", SDL_GetError());
// integer number of input samples. throw std::runtime_error("Error creating SDL audio converter");
size_t rate_gcd = std::gcd(sample_rate, output_sample_rate); }
size_t gcd_input_samples = sample_rate / rate_gcd;
size_t gcd_output_samples = output_sample_rate / rate_gcd; // Calculate the number of samples to discard based on the sample rate ratio and the duplicate frame count.
size_t num_duplicated_chunks = (gcd_input_samples + min_duplicated_frames - 1) / min_duplicated_frames; discarded_output_frames = duplicated_input_frames * output_sample_rate / sample_rate;
// Duplicate twice as many input frames as the corresponding skipped input frames as we need to prevent discontinuities at
// both the start and end of a given chunk.
duplicated_input_frames = num_duplicated_chunks * gcd_input_samples * 2;
discarded_output_frames = num_duplicated_chunks * gcd_output_samples;
} }
void set_frequency(uint32_t freq) { void set_frequency(uint32_t freq) {
assert(freq == 32000 || freq == 48000);
sample_rate = freq; sample_rate = freq;
update_audio_converter(); update_audio_converter();