2018-09-08 15:48:13 +00:00
|
|
|
// Copyright 2018 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstddef>
|
|
|
|
#include "audio_core/time_stretch.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2018-10-24 04:44:32 +00:00
|
|
|
TimeStretcher::TimeStretcher(u32 sample_rate, u32 channel_count) : m_sample_rate{sample_rate} {
|
2018-09-08 15:48:13 +00:00
|
|
|
m_sound_touch.setChannels(channel_count);
|
|
|
|
m_sound_touch.setSampleRate(sample_rate);
|
|
|
|
m_sound_touch.setPitch(1.0);
|
|
|
|
m_sound_touch.setTempo(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeStretcher::Clear() {
|
|
|
|
m_sound_touch.clear();
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:07:16 +00:00
|
|
|
void TimeStretcher::Flush() {
|
|
|
|
m_sound_touch.flush();
|
|
|
|
}
|
|
|
|
|
2018-09-15 13:21:06 +00:00
|
|
|
std::size_t TimeStretcher::Process(const s16* in, std::size_t num_in, s16* out,
|
|
|
|
std::size_t num_out) {
|
2018-09-08 15:48:13 +00:00
|
|
|
const double time_delta = static_cast<double>(num_out) / m_sample_rate; // seconds
|
|
|
|
|
|
|
|
// We were given actual_samples number of samples, and num_samples were requested from us.
|
|
|
|
double current_ratio = static_cast<double>(num_in) / static_cast<double>(num_out);
|
|
|
|
|
2018-10-29 13:49:36 +00:00
|
|
|
const double max_latency = 0.25; // seconds
|
2018-09-08 15:49:04 +00:00
|
|
|
const double max_backlog = m_sample_rate * max_latency;
|
2018-09-08 15:48:13 +00:00
|
|
|
const double backlog_fullness = m_sound_touch.numSamples() / max_backlog;
|
2018-10-29 13:49:36 +00:00
|
|
|
if (backlog_fullness > 4.0) {
|
2018-09-08 15:48:13 +00:00
|
|
|
// Too many samples in backlog: Don't push anymore on
|
|
|
|
num_in = 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.05; // seconds
|
|
|
|
const double tweak_correction = (backlog_fullness - 0.5) * (time_delta / tweak_time_scale);
|
|
|
|
current_ratio *= std::pow(1.0 + 2.0 * tweak_correction, tweak_correction < 0 ? 3.0 : 1.0);
|
|
|
|
|
|
|
|
// This low-pass filter smoothes out variance in the calculated stretch ratio.
|
|
|
|
// The time-scale determines how responsive this filter is.
|
2018-10-29 13:49:36 +00:00
|
|
|
constexpr double lpf_time_scale = 0.712; // seconds
|
2018-09-08 15:48:13 +00:00
|
|
|
const double lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale);
|
|
|
|
m_stretch_ratio += lpf_gain * (current_ratio - m_stretch_ratio);
|
|
|
|
|
2018-09-08 15:49:04 +00:00
|
|
|
// Place a lower limit of 5% speed. When a game boots up, there will be
|
2018-09-08 15:48:13 +00:00
|
|
|
// many silence samples. These do not need to be timestretched.
|
2018-09-08 15:49:04 +00:00
|
|
|
m_stretch_ratio = std::max(m_stretch_ratio, 0.05);
|
2018-09-08 15:48:13 +00:00
|
|
|
m_sound_touch.setTempo(m_stretch_ratio);
|
|
|
|
|
2018-09-21 03:33:54 +00:00
|
|
|
LOG_TRACE(Audio, "{:5}/{:5} ratio:{:0.6f} backlog:{:0.6f}", num_in, num_out, m_stretch_ratio,
|
2018-09-08 15:49:04 +00:00
|
|
|
backlog_fullness);
|
2018-09-08 15:48:13 +00:00
|
|
|
|
2018-09-13 23:48:36 +00:00
|
|
|
m_sound_touch.putSamples(in, static_cast<u32>(num_in));
|
|
|
|
return m_sound_touch.receiveSamples(out, static_cast<u32>(num_out));
|
2018-09-08 15:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|