diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/audio_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/audio_core/time_stretch.cpp | 64 | ||||
| -rw-r--r-- | src/audio_core/time_stretch.h | 35 |
3 files changed, 101 insertions, 0 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index de5c291ce..c381dbe1d 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt | |||
| @@ -17,6 +17,8 @@ add_library(audio_core STATIC | |||
| 17 | sink_stream.h | 17 | sink_stream.h |
| 18 | stream.cpp | 18 | stream.cpp |
| 19 | stream.h | 19 | stream.h |
| 20 | time_stretch.cpp | ||
| 21 | time_stretch.h | ||
| 20 | 22 | ||
| 21 | $<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h> | 23 | $<$<BOOL:${ENABLE_CUBEB}>:cubeb_sink.cpp cubeb_sink.h> |
| 22 | ) | 24 | ) |
diff --git a/src/audio_core/time_stretch.cpp b/src/audio_core/time_stretch.cpp new file mode 100644 index 000000000..17e128323 --- /dev/null +++ b/src/audio_core/time_stretch.cpp | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include "audio_core/time_stretch.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | |||
| 11 | namespace AudioCore { | ||
| 12 | |||
| 13 | TimeStretcher::TimeStretcher(u32 sample_rate, u32 channel_count) | ||
| 14 | : m_sample_rate(sample_rate), m_channel_count(channel_count) { | ||
| 15 | m_sound_touch.setChannels(channel_count); | ||
| 16 | m_sound_touch.setSampleRate(sample_rate); | ||
| 17 | m_sound_touch.setPitch(1.0); | ||
| 18 | m_sound_touch.setTempo(1.0); | ||
| 19 | } | ||
| 20 | |||
| 21 | void TimeStretcher::Clear() { | ||
| 22 | m_sound_touch.clear(); | ||
| 23 | } | ||
| 24 | |||
| 25 | size_t TimeStretcher::Process(const s16* in, size_t num_in, s16* out, size_t num_out) { | ||
| 26 | const double time_delta = static_cast<double>(num_out) / m_sample_rate; // seconds | ||
| 27 | |||
| 28 | // We were given actual_samples number of samples, and num_samples were requested from us. | ||
| 29 | double current_ratio = static_cast<double>(num_in) / static_cast<double>(num_out); | ||
| 30 | |||
| 31 | const double max_latency = 0.3; // seconds | ||
| 32 | const double max_backlog = m_sample_rate * max_latency / 1000.0 / m_stretch_ratio; | ||
| 33 | const double backlog_fullness = m_sound_touch.numSamples() / max_backlog; | ||
| 34 | if (backlog_fullness > 5.0) { | ||
| 35 | // Too many samples in backlog: Don't push anymore on | ||
| 36 | num_in = 0; | ||
| 37 | } | ||
| 38 | |||
| 39 | // We ideally want the backlog to be about 50% full. | ||
| 40 | // This gives some headroom both ways to prevent underflow and overflow. | ||
| 41 | // We tweak current_ratio to encourage this. | ||
| 42 | constexpr double tweak_time_scale = 0.05; // seconds | ||
| 43 | const double tweak_correction = (backlog_fullness - 0.5) * (time_delta / tweak_time_scale); | ||
| 44 | current_ratio *= std::pow(1.0 + 2.0 * tweak_correction, tweak_correction < 0 ? 3.0 : 1.0); | ||
| 45 | |||
| 46 | // This low-pass filter smoothes out variance in the calculated stretch ratio. | ||
| 47 | // The time-scale determines how responsive this filter is. | ||
| 48 | constexpr double lpf_time_scale = 2.0; // seconds | ||
| 49 | const double lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale); | ||
| 50 | m_stretch_ratio += lpf_gain * (current_ratio - m_stretch_ratio); | ||
| 51 | |||
| 52 | // Place a lower limit of 10% speed. When a game boots up, there will be | ||
| 53 | // many silence samples. These do not need to be timestretched. | ||
| 54 | m_stretch_ratio = std::max(m_stretch_ratio, 0.1); | ||
| 55 | m_sound_touch.setTempo(m_stretch_ratio); | ||
| 56 | |||
| 57 | LOG_DEBUG(Audio, "Audio Stretching: samples:{}/{} ratio:{} backlog:{} gain: {}", num_in, num_out, | ||
| 58 | m_stretch_ratio, backlog_fullness, lpf_gain); | ||
| 59 | |||
| 60 | m_sound_touch.putSamples(in, num_in); | ||
| 61 | return m_sound_touch.receiveSamples(out, num_out); | ||
| 62 | } | ||
| 63 | |||
| 64 | } // namespace AudioCore | ||
diff --git a/src/audio_core/time_stretch.h b/src/audio_core/time_stretch.h new file mode 100644 index 000000000..cdead34a2 --- /dev/null +++ b/src/audio_core/time_stretch.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <SoundTouch.h> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace AudioCore { | ||
| 13 | |||
| 14 | class TimeStretcher { | ||
| 15 | public: | ||
| 16 | TimeStretcher(u32 sample_rate, u32 channel_count); | ||
| 17 | |||
| 18 | /// @param in Input sample buffer | ||
| 19 | /// @param num_in Number of input frames in `in` | ||
| 20 | /// @param out Output sample buffer | ||
| 21 | /// @param num_out Desired number of output frames in `out` | ||
| 22 | /// @returns Actual number of frames written to `out` | ||
| 23 | size_t Process(const s16* in, size_t num_in, s16* out, size_t num_out); | ||
| 24 | |||
| 25 | void Clear(); | ||
| 26 | |||
| 27 | private: | ||
| 28 | u32 m_sample_rate; | ||
| 29 | u32 m_channel_count; | ||
| 30 | std::array<s16, 2> m_last_stretched_sample = {}; | ||
| 31 | soundtouch::SoundTouch m_sound_touch; | ||
| 32 | double m_stretch_ratio = 1.0; | ||
| 33 | }; | ||
| 34 | |||
| 35 | } // namespace AudioCore | ||