From 14c3d4716fa4abfa7cc70f056676f935ba302c8a Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Sun, 23 Apr 2017 19:11:43 -0700 Subject: [PATCH] AudioStretcher: split StretchAudio into ProcessSamples / GetStretchedSamples --- Source/Core/AudioCommon/AudioStretcher.cpp | 8 ++++++-- Source/Core/AudioCommon/AudioStretcher.h | 5 +++-- Source/Core/AudioCommon/Mixer.cpp | 17 ++++++++++------- Source/Core/AudioCommon/Mixer.h | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Source/Core/AudioCommon/AudioStretcher.cpp b/Source/Core/AudioCommon/AudioStretcher.cpp index 3414934a81..88721b0e35 100644 --- a/Source/Core/AudioCommon/AudioStretcher.cpp +++ b/Source/Core/AudioCommon/AudioStretcher.cpp @@ -2,7 +2,9 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include +#include #include "AudioCommon/AudioStretcher.h" #include "Common/Logging/Log.h" @@ -27,8 +29,7 @@ void AudioStretcher::Clear() m_sound_touch.clear(); } -void AudioStretcher::StretchAudio(const short* in, unsigned int num_in, short* out, - unsigned int num_out) +void AudioStretcher::ProcessSamples(const short* in, unsigned int num_in, unsigned int num_out) { const double time_delta = static_cast(num_out) / m_sample_rate; // seconds @@ -65,7 +66,10 @@ void AudioStretcher::StretchAudio(const short* in, unsigned int num_in, short* o m_stretch_ratio, backlog_fullness, lpf_gain); m_sound_touch.putSamples(in, num_in); +} +void AudioStretcher::GetStretchedSamples(short* out, unsigned int num_out) +{ const size_t samples_received = m_sound_touch.receiveSamples(out, num_out); if (samples_received != 0) diff --git a/Source/Core/AudioCommon/AudioStretcher.h b/Source/Core/AudioCommon/AudioStretcher.h index 0fdc2f09f5..b914fb924b 100644 --- a/Source/Core/AudioCommon/AudioStretcher.h +++ b/Source/Core/AudioCommon/AudioStretcher.h @@ -13,8 +13,9 @@ namespace AudioCommon class AudioStretcher { public: - AudioStretcher(unsigned int sample_rate); - void StretchAudio(const short* in, unsigned int num_in, short* out, unsigned int num_out); + explicit AudioStretcher(unsigned int sample_rate); + void ProcessSamples(const short* in, unsigned int num_in, unsigned int num_out); + void GetStretchedSamples(short* out, unsigned int num_out); void Clear(); private: diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp index 8d4ef3bc81..659deb819f 100644 --- a/Source/Core/AudioCommon/Mixer.cpp +++ b/Source/Core/AudioCommon/Mixer.cpp @@ -129,18 +129,19 @@ unsigned int CMixer::Mix(short* samples, unsigned int num_samples) unsigned int available_samples = std::min(m_dma_mixer.AvailableSamples(), m_streaming_mixer.AvailableSamples()); - m_stretch_buffer.fill(0); + m_scratch_buffer.fill(0); - m_dma_mixer.Mix(m_stretch_buffer.data(), available_samples, false); - m_streaming_mixer.Mix(m_stretch_buffer.data(), available_samples, false); - m_wiimote_speaker_mixer.Mix(m_stretch_buffer.data(), available_samples, false); + m_dma_mixer.Mix(m_scratch_buffer.data(), available_samples, false); + m_streaming_mixer.Mix(m_scratch_buffer.data(), available_samples, false); + m_wiimote_speaker_mixer.Mix(m_scratch_buffer.data(), available_samples, false); if (!m_is_stretching) { m_stretcher.Clear(); m_is_stretching = true; } - m_stretcher.StretchAudio(m_stretch_buffer.data(), available_samples, samples, num_samples); + m_stretcher.ProcessSamples(m_scratch_buffer.data(), available_samples, num_samples); + m_stretcher.GetStretchedSamples(samples, num_samples); } else { @@ -160,11 +161,13 @@ unsigned int CMixer::MixSurround(float* samples, unsigned int num_samples) memset(samples, 0, num_samples * 6 * sizeof(float)); - unsigned int available_samples = Mix(m_stretch_buffer.data(), num_samples); + // Mix() may also use m_scratch_buffer internally, but is safe because it alternates reads and + // writes. + unsigned int available_samples = Mix(m_scratch_buffer.data(), num_samples); for (size_t i = 0; i < static_cast(available_samples) * 2; ++i) { m_float_conversion_buffer[i] = - m_stretch_buffer[i] / static_cast(std::numeric_limits::max()); + m_scratch_buffer[i] / static_cast(std::numeric_limits::max()); } DPL2Decode(m_float_conversion_buffer.data(), available_samples, samples); diff --git a/Source/Core/AudioCommon/Mixer.h b/Source/Core/AudioCommon/Mixer.h index 813268c903..2707df3383 100644 --- a/Source/Core/AudioCommon/Mixer.h +++ b/Source/Core/AudioCommon/Mixer.h @@ -81,7 +81,7 @@ private: bool m_is_stretching = false; AudioCommon::AudioStretcher m_stretcher; - std::array m_stretch_buffer; + std::array m_scratch_buffer; std::array m_float_conversion_buffer; WaveFileWriter m_wave_writer_dtk;