diff --git a/CMakeLists.txt b/CMakeLists.txt index aba2ac8768..cc3fe60dbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -648,18 +648,11 @@ else() endif() find_package(OpenAL) -if(OPENAL_FOUND) - if(NOT APPLE) - check_lib(SOUNDTOUCH soundtouch SoundTouch soundtouch/SoundTouch.h QUIET) - endif() - if (SOUNDTOUCH_FOUND) - message(STATUS "Using shared soundtouch") - else() - message(STATUS "Using static soundtouch from Externals") - add_subdirectory(Externals/soundtouch) - include_directories(Externals) - endif() -endif() + +# Using static soundtouch from Externals +# Unable to use system soundtouch library: We require shorts, not floats. +add_subdirectory(Externals/soundtouch) +include_directories(Externals) if(NOT ANDROID) add_definitions(-D__LIBUSB__) diff --git a/Source/Core/AudioCommon/CMakeLists.txt b/Source/Core/AudioCommon/CMakeLists.txt index f09b7ef823..8f0a283db8 100644 --- a/Source/Core/AudioCommon/CMakeLists.txt +++ b/Source/Core/AudioCommon/CMakeLists.txt @@ -37,7 +37,7 @@ if(ENABLE_OPENAL) if(OPENAL_FOUND) message(STATUS "OpenAL found, enabling OpenAL sound backend") target_sources(audiocommon PRIVATE OpenALStream.cpp aldlist.cpp) - target_link_libraries(audiocommon PRIVATE OpenAL::OpenAL SoundTouch) + target_link_libraries(audiocommon PRIVATE OpenAL::OpenAL) target_compile_definitions(audiocommon PRIVATE HAVE_OPENAL=1) else() message(STATUS "OpenAL NOT found, disabling OpenAL sound backend") @@ -76,4 +76,4 @@ elseif(APPLE) target_sources(audiocommon PRIVATE CoreAudioSoundStream.cpp) endif() - +target_link_libraries(audiocommon PRIVATE SoundTouch) diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp index bdfbbfe5a0..bebdd2abbe 100644 --- a/Source/Core/AudioCommon/Mixer.cpp +++ b/Source/Core/AudioCommon/Mixer.cpp @@ -4,6 +4,7 @@ #include "AudioCommon/Mixer.h" +#include #include #include "Common/CommonTypes.h" @@ -15,6 +16,15 @@ CMixer::CMixer(unsigned int BackendSampleRate) : m_sampleRate(BackendSampleRate) { INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized"); + + m_sound_touch.setChannels(2); + m_sound_touch.setSampleRate(BackendSampleRate); + m_sound_touch.setPitch(1.0); + m_sound_touch.setTempo(1.0); + m_sound_touch.setSetting(SETTING_USE_QUICKSEEK, 0); + m_sound_touch.setSetting(SETTING_SEQUENCE_MS, 62); + m_sound_touch.setSetting(SETTING_SEEKWINDOW_MS, 28); + m_sound_touch.setSetting(SETTING_OVERLAP_MS, 8); } CMixer::~CMixer() @@ -119,12 +129,73 @@ unsigned int CMixer::Mix(short* samples, unsigned int num_samples, bool consider memset(samples, 0, num_samples * 2 * sizeof(short)); - m_dma_mixer.Mix(samples, num_samples, consider_framelimit); - m_streaming_mixer.Mix(samples, num_samples, consider_framelimit); - m_wiimote_speaker_mixer.Mix(samples, num_samples, consider_framelimit); + unsigned int actual_samples = m_dma_mixer.Mix(samples, num_samples, false); + m_streaming_mixer.Mix(samples, num_samples, false); + m_wiimote_speaker_mixer.Mix(samples, num_samples, false); + + StretchAudio(samples, actual_samples, num_samples); + return num_samples; } +void CMixer::StretchAudio(short* samples, unsigned int actual_samples, unsigned int num_samples) +{ + const double time_delta = static_cast(num_samples) / m_sampleRate; // seconds + + // We were given actual_samples number of samples, and num_samples were requested from us. + double current_ratio = static_cast(actual_samples) / static_cast(num_samples); + + constexpr double MAXIMUM_LATENCY = 0.080; // seconds + const double max_backlog = m_sampleRate * MAXIMUM_LATENCY; + double backlog_fullness = m_sound_touch.numSamples() / max_backlog; + if (backlog_fullness > 1.0) + { + // Exceeded latency budget: Do not add more samples into FIFO. + actual_samples = 0; + } + + // We ideally want the backlog to be about 50% full. + // This gives some headroom both ways to prevent underflow and overflow. + // We tweak current_ratio to encourage this. + constexpr double tweak_time_scale = 0.1; // seconds + current_ratio *= 1.0 + 2.0 * (backlog_fullness - 0.5) * (time_delta / tweak_time_scale); + + // This low-pass filter smoothes out variance in the calculated stretch ratio. + // The time-scale determines how responsive this filter is. + constexpr double lpf_time_scale = 0.3; // seconds + const double m_lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale); + m_stretch_ratio += m_lpf_gain * (current_ratio - m_stretch_ratio); + + // Place a lower limit of 10% speed. When a game boots up, there will be + // many silence samples. These do not need to be timestretched. + m_sound_touch.setTempo(std::max(m_stretch_ratio, 0.1)); + + if (actual_samples != num_samples) + { + DEBUG_LOG(AUDIO, "Audio stretching: samples:%u/%u ratio:%f backlog:%f gain: %f", actual_samples, + num_samples, m_stretch_ratio, backlog_fullness, m_lpf_gain); + } + + m_sound_touch.putSamples(samples, actual_samples); + + memset(samples, 0, num_samples * 2 * sizeof(short)); + + const size_t samples_received = m_sound_touch.receiveSamples(samples, num_samples); + + if (samples_received != 0) + { + m_last_stretched_sample[0] = samples[samples_received * 2 - 2]; + m_last_stretched_sample[1] = samples[samples_received * 2 - 1]; + } + + // Preform padding if we've run out of samples. + for (size_t i = samples_received; i < num_samples; i++) + { + samples[i * 2 + 0] = m_last_stretched_sample[0]; + samples[i * 2 + 1] = m_last_stretched_sample[1]; + } +} + void CMixer::MixerFifo::PushSamples(const short* samples, unsigned int num_samples) { // Cache access in non-volatile variable diff --git a/Source/Core/AudioCommon/Mixer.h b/Source/Core/AudioCommon/Mixer.h index dd879462ab..762a872d50 100644 --- a/Source/Core/AudioCommon/Mixer.h +++ b/Source/Core/AudioCommon/Mixer.h @@ -10,6 +10,9 @@ #include "AudioCommon/WaveFile.h" #include "Common/CommonTypes.h" +#include +#include + class CMixer final { public: @@ -70,11 +73,19 @@ private: float m_numLeftI = 0.0f; u32 m_frac = 0; }; + + void StretchAudio(short* samples, unsigned int actual_samples, unsigned int num_samples); + MixerFifo m_dma_mixer{this, 32000}; MixerFifo m_streaming_mixer{this, 48000}; MixerFifo m_wiimote_speaker_mixer{this, 3000}; unsigned int m_sampleRate; + bool m_is_stretching = false; + soundtouch::SoundTouch m_sound_touch; + double m_stretch_ratio = 1.0; + std::array m_last_stretched_sample = {}; + WaveFileWriter m_wave_writer_dtk; WaveFileWriter m_wave_writer_dsp;