summaryrefslogtreecommitdiff
path: root/src/audio_core/time_stretch.cpp
diff options
context:
space:
mode:
authorGravatar MerryMage2018-09-08 16:48:13 +0100
committerGravatar MerryMage2018-09-08 18:56:38 +0100
commite51bd49f87052d0706a016fab88d18ffef05b8b1 (patch)
tree76eccf3f2b428ada51c8025bc3ab7c512263854d /src/audio_core/time_stretch.cpp
parentcubeb_sink: Hold last available value instead of writing zeros (diff)
downloadyuzu-e51bd49f87052d0706a016fab88d18ffef05b8b1.tar.gz
yuzu-e51bd49f87052d0706a016fab88d18ffef05b8b1.tar.xz
yuzu-e51bd49f87052d0706a016fab88d18ffef05b8b1.zip
audio_core: Add audio stretcher
Diffstat (limited to 'src/audio_core/time_stretch.cpp')
-rw-r--r--src/audio_core/time_stretch.cpp64
1 files changed, 64 insertions, 0 deletions
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
11namespace AudioCore {
12
13TimeStretcher::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
21void TimeStretcher::Clear() {
22 m_sound_touch.clear();
23}
24
25size_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