From 54c7fc6b0878bbe1c526b7cc5c31ec48f074616a Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 10 Jan 2021 17:01:02 +1000 Subject: [PATCH] AudioStream: Support resampling input --- src/common/audio_stream.cpp | 125 ++++++++++++++++++++++++++++++++++-- src/common/audio_stream.h | 21 +++++- src/common/common.vcxproj | 29 +++++---- src/core/host_interface.cpp | 5 +- 4 files changed, 156 insertions(+), 24 deletions(-) diff --git a/src/common/audio_stream.cpp b/src/common/audio_stream.cpp index 0b35f58a9..e2b01d343 100644 --- a/src/common/audio_stream.cpp +++ b/src/common/audio_stream.cpp @@ -1,17 +1,23 @@ #include "audio_stream.h" #include "assert.h" #include "log.h" +#include "samplerate.h" #include #include Log_SetChannel(AudioStream); AudioStream::AudioStream() = default; -AudioStream::~AudioStream() = default; - -bool AudioStream::Reconfigure(u32 output_sample_rate /*= DefaultOutputSampleRate*/, u32 channels /*= 1*/, - u32 buffer_size /*= DefaultBufferSize*/) +AudioStream::~AudioStream() { + DestroyResampler(); +} + +bool AudioStream::Reconfigure(u32 input_sample_rate /* = DefaultInputSampleRate */, + u32 output_sample_rate /* = DefaultOutputSampleRate */, u32 channels /* = 1 */, + u32 buffer_size /* = DefaultBufferSize */) +{ + DestroyResampler(); if (IsDeviceOpen()) CloseDevice(); @@ -32,9 +38,24 @@ bool AudioStream::Reconfigure(u32 output_sample_rate /*= DefaultOutputSampleRate return false; } + CreateResampler(); + SetInputSampleRate(input_sample_rate); + return true; } +void AudioStream::SetInputSampleRate(u32 sample_rate) +{ + if (m_input_sample_rate == sample_rate) + return; + + std::unique_lock lock(m_buffer_mutex); + m_input_sample_rate = sample_rate; + m_resampler_ratio = static_cast(m_output_sample_rate) / static_cast(sample_rate); + src_set_ratio(static_cast(m_resampler_state), m_resampler_ratio); + ResetResampler(); +} + void AudioStream::SetOutputVolume(u32 volume) { std::unique_lock lock(m_buffer_mutex); @@ -136,9 +157,19 @@ void AudioStream::ReadFrames(SampleType* samples, u32 num_frames, bool apply_vol u32 samples_copied = 0; { std::unique_lock lock(m_buffer_mutex); - samples_copied = std::min(m_buffer.GetSize(), total_samples); - if (samples_copied > 0) - m_buffer.PopRange(samples, samples_copied); + if (m_input_sample_rate == m_output_sample_rate) + { + samples_copied = std::min(m_buffer.GetSize(), total_samples); + if (samples_copied > 0) + m_buffer.PopRange(samples, samples_copied); + } + else + { + ResampleInput(); + samples_copied = std::min(m_resampled_buffer.GetSize(), total_samples); + if (samples_copied > 0) + m_resampled_buffer.PopRange(samples, samples_copied); + } m_buffer_draining_cv.notify_one(); } @@ -222,3 +253,83 @@ void AudioStream::EmptyBuffers() m_buffer.Clear(); m_underflow_flag.store(false); } + +void AudioStream::CreateResampler() +{ + m_resampler_state = src_new(SRC_SINC_MEDIUM_QUALITY, static_cast(m_channels), nullptr); + if (!m_resampler_state) + Panic("Failed to allocate resampler"); +} + +void AudioStream::DestroyResampler() +{ + if (m_resampler_state) + { + src_delete(static_cast(m_resampler_state)); + m_resampler_state = nullptr; + } +} + +void AudioStream::ResetResampler() +{ + m_resampled_buffer.Clear(); + src_reset(static_cast(m_resampler_state)); +} + +void AudioStream::ResampleInput() +{ + const u32 input_space_from_output = (m_resampled_buffer.GetSpace() * m_output_sample_rate) / m_input_sample_rate; + u32 remaining = std::min(m_buffer.GetSize(), input_space_from_output); + if (m_resample_in_buffer.size() < remaining) + { + remaining -= static_cast(m_resample_in_buffer.size()); + m_resample_in_buffer.reserve(m_resample_in_buffer.size() + remaining); + while (remaining > 0) + { + const u32 read_len = std::min(m_buffer.GetContiguousSize(), remaining); + const size_t old_pos = m_resample_in_buffer.size(); + m_resample_in_buffer.resize(m_resample_in_buffer.size() + read_len); + src_short_to_float_array(m_buffer.GetReadPointer(), m_resample_in_buffer.data() + old_pos, + static_cast(read_len)); + m_buffer.Remove(read_len); + remaining -= read_len; + } + } + + const u32 potential_output_size = + (static_cast(m_resample_in_buffer.size()) * m_input_sample_rate) / m_output_sample_rate; + const u32 output_size = std::min(potential_output_size, m_resampled_buffer.GetSpace()); + m_resample_out_buffer.resize(output_size); + + SRC_DATA sd = {}; + sd.data_in = m_resample_in_buffer.data(); + sd.data_out = m_resample_out_buffer.data(); + sd.input_frames = static_cast(m_resample_in_buffer.size()) / m_channels; + sd.output_frames = output_size / m_channels; + sd.src_ratio = m_resampler_ratio; + + const int error = src_process(static_cast(m_resampler_state), &sd); + if (error) + { + Log_ErrorPrintf("Resampler error %d", error); + m_resample_in_buffer.clear(); + m_resample_out_buffer.clear(); + return; + } + + m_resample_in_buffer.erase(m_resample_in_buffer.begin(), + m_resample_in_buffer.begin() + (static_cast(sd.input_frames_used) * m_channels)); + + const float* write_ptr = m_resample_out_buffer.data(); + remaining = static_cast(sd.output_frames_gen) * m_channels; + while (remaining > 0) + { + const u32 samples_to_write = std::min(m_resampled_buffer.GetContiguousSpace(), remaining); + src_float_to_short_array(write_ptr, m_resampled_buffer.GetWritePointer(), static_cast(samples_to_write)); + m_resampled_buffer.AdvanceTail(samples_to_write); + write_ptr += samples_to_write; + remaining -= samples_to_write; + } + m_resample_out_buffer.erase(m_resample_out_buffer.begin(), + m_resample_out_buffer.begin() + (static_cast(sd.output_frames_gen) * m_channels)); +} \ No newline at end of file diff --git a/src/common/audio_stream.h b/src/common/audio_stream.h index e7887792e..c60a5edbe 100644 --- a/src/common/audio_stream.h +++ b/src/common/audio_stream.h @@ -16,6 +16,7 @@ public: enum : u32 { + DefaultInputSampleRate = 44100, DefaultOutputSampleRate = 44100, DefaultBufferSize = 2048, MaxSamples = 32768, @@ -31,10 +32,12 @@ public: s32 GetOutputVolume() const { return m_output_volume; } bool IsSyncing() const { return m_sync; } - bool Reconfigure(u32 output_sample_rate = DefaultOutputSampleRate, u32 channels = 1, - u32 buffer_size = DefaultBufferSize); + bool Reconfigure(u32 input_sample_rate = DefaultInputSampleRate, u32 output_sample_rate = DefaultOutputSampleRate, + u32 channels = 1, u32 buffer_size = DefaultBufferSize); void SetSync(bool enable) { m_sync = enable; } + void SetInputSampleRate(u32 sample_rate); + virtual void SetOutputVolume(u32 volume); void PauseOutput(bool paused); @@ -76,6 +79,7 @@ protected: void ReadFrames(SampleType* samples, u32 num_frames, bool apply_volume); void DropFrames(u32 count); + u32 m_input_sample_rate = 0; u32 m_output_sample_rate = 0; u32 m_channels = 0; u32 m_buffer_size = 0; @@ -87,13 +91,26 @@ private: ALWAYS_INLINE u32 GetBufferSpace() const { return (m_max_samples - m_buffer.GetSize()); } void EnsureBuffer(u32 size); + void CreateResampler(); + void DestroyResampler(); + void ResetResampler(); + void ResampleInput(); + HeapFIFOQueue m_buffer; mutable std::mutex m_buffer_mutex; std::condition_variable m_buffer_draining_cv; std::vector m_resample_buffer; + std::atomic_bool m_underflow_flag{false}; u32 m_max_samples = 0; bool m_output_paused = true; bool m_sync = true; + + // Resampling + double m_resampler_ratio = 1.0; + void* m_resampler_state = nullptr; + HeapFIFOQueue m_resampled_buffer; + std::vector m_resample_in_buffer; + std::vector m_resample_out_buffer; }; \ No newline at end of file diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index be057f88e..1a514c9e3 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -193,6 +193,9 @@ {6a4208ed-e3dc-41e1-81cd-f61025fc285a} + + {39f0adff-3a84-470d-9cf0-ca49e164f2f3} + {EE054E08-3799-4A59-A422-18259C105FFD} @@ -395,7 +398,7 @@ WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 true @@ -422,7 +425,7 @@ _ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) Default false true @@ -452,7 +455,7 @@ WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 true @@ -479,7 +482,7 @@ WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 true @@ -506,7 +509,7 @@ _ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) Default false true @@ -536,7 +539,7 @@ _ITERATOR_DEBUG_LEVEL=1;WIN32;_DEBUGFAST;_DEBUG;_LIB;%(PreprocessorDefinitions) true ProgramDatabase - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) Default false true @@ -567,7 +570,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 false @@ -598,7 +601,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true true stdcpp17 @@ -629,7 +632,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 false @@ -660,7 +663,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true stdcpp17 false @@ -691,7 +694,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true true stdcpp17 @@ -722,7 +725,7 @@ true WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) true - $(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) + $(SolutionDir)dep\libsamplerate\include;$(SolutionDir)dep\glad\include;$(SolutionDir)dep\libcue\include;$(SolutionDir)dep\libchdr\include;$(SolutionDir)dep\stb\include;$(SolutionDir)dep\vulkan-loader\include;$(SolutionDir)dep\glslang;$(SolutionDir)dep\zlib\include;$(SolutionDir)dep\minizip\include;$(SolutionDir)src;%(AdditionalIncludeDirectories) true true stdcpp17 @@ -747,4 +750,4 @@ - + \ No newline at end of file diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index db4deaeda..3f229c00f 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -63,12 +63,13 @@ void HostInterface::CreateAudioStream() m_audio_stream = CreateAudioStream(g_settings.audio_backend); - if (!m_audio_stream || !m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, g_settings.audio_buffer_size)) + if (!m_audio_stream || + !m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, g_settings.audio_buffer_size)) { ReportFormattedError("Failed to create or configure audio stream, falling back to null output."); m_audio_stream.reset(); m_audio_stream = AudioStream::CreateNullAudioStream(); - m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, g_settings.audio_buffer_size); + m_audio_stream->Reconfigure(AUDIO_SAMPLE_RATE, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, g_settings.audio_buffer_size); } m_audio_stream->SetOutputVolume(GetAudioOutputVolume());