mirror of https://git.suyu.dev/suyu/suyu
Merge pull request #1163 from FearlessTobi/add-audio-stretching
audio_core: Add audio stretching supportmerge-requests/60/head
commit
926dd41587
@ -0,0 +1 @@
|
||||
Subproject commit 060181eaf273180d3a7e87349895bd0cb6ccbf4a
|
@ -0,0 +1,68 @@
|
||||
// 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 {
|
||||
|
||||
TimeStretcher::TimeStretcher(u32 sample_rate, u32 channel_count)
|
||||
: m_sample_rate(sample_rate), m_channel_count(channel_count) {
|
||||
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();
|
||||
}
|
||||
|
||||
void TimeStretcher::Flush() {
|
||||
m_sound_touch.flush();
|
||||
}
|
||||
|
||||
size_t TimeStretcher::Process(const s16* in, size_t num_in, s16* out, size_t num_out) {
|
||||
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);
|
||||
|
||||
const double max_latency = 1.0; // seconds
|
||||
const double max_backlog = m_sample_rate * max_latency;
|
||||
const double backlog_fullness = m_sound_touch.numSamples() / max_backlog;
|
||||
if (backlog_fullness > 5.0) {
|
||||
// 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.
|
||||
constexpr double lpf_time_scale = 2.0; // seconds
|
||||
const double lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale);
|
||||
m_stretch_ratio += lpf_gain * (current_ratio - m_stretch_ratio);
|
||||
|
||||
// Place a lower limit of 5% speed. When a game boots up, there will be
|
||||
// many silence samples. These do not need to be timestretched.
|
||||
m_stretch_ratio = std::max(m_stretch_ratio, 0.05);
|
||||
m_sound_touch.setTempo(m_stretch_ratio);
|
||||
|
||||
LOG_DEBUG(Audio, "{:5}/{:5} ratio:{:0.6f} backlog:{:0.6f}", num_in, num_out, m_stretch_ratio,
|
||||
backlog_fullness);
|
||||
|
||||
m_sound_touch.putSamples(in, num_in);
|
||||
return m_sound_touch.receiveSamples(out, num_out);
|
||||
}
|
||||
|
||||
} // namespace AudioCore
|
@ -0,0 +1,36 @@
|
||||
// Copyright 2018 yuzu Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <SoundTouch.h>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace AudioCore {
|
||||
|
||||
class TimeStretcher {
|
||||
public:
|
||||
TimeStretcher(u32 sample_rate, u32 channel_count);
|
||||
|
||||
/// @param in Input sample buffer
|
||||
/// @param num_in Number of input frames in `in`
|
||||
/// @param out Output sample buffer
|
||||
/// @param num_out Desired number of output frames in `out`
|
||||
/// @returns Actual number of frames written to `out`
|
||||
size_t Process(const s16* in, size_t num_in, s16* out, size_t num_out);
|
||||
|
||||
void Clear();
|
||||
|
||||
void Flush();
|
||||
|
||||
private:
|
||||
u32 m_sample_rate;
|
||||
u32 m_channel_count;
|
||||
soundtouch::SoundTouch m_sound_touch;
|
||||
double m_stretch_ratio = 1.0;
|
||||
};
|
||||
|
||||
} // namespace AudioCore
|
@ -0,0 +1,111 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
/// SPSC ring buffer
|
||||
/// @tparam T Element type
|
||||
/// @tparam capacity Number of slots in ring buffer
|
||||
/// @tparam granularity Slot size in terms of number of elements
|
||||
template <typename T, size_t capacity, size_t granularity = 1>
|
||||
class RingBuffer {
|
||||
/// A "slot" is made of `granularity` elements of `T`.
|
||||
static constexpr size_t slot_size = granularity * sizeof(T);
|
||||
// T must be safely memcpy-able and have a trivial default constructor.
|
||||
static_assert(std::is_trivial_v<T>);
|
||||
// Ensure capacity is sensible.
|
||||
static_assert(capacity < std::numeric_limits<size_t>::max() / 2 / granularity);
|
||||
static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");
|
||||
// Ensure lock-free.
|
||||
static_assert(std::atomic<size_t>::is_always_lock_free);
|
||||
|
||||
public:
|
||||
/// Pushes slots into the ring buffer
|
||||
/// @param new_slots Pointer to the slots to push
|
||||
/// @param slot_count Number of slots to push
|
||||
/// @returns The number of slots actually pushed
|
||||
size_t Push(const void* new_slots, size_t slot_count) {
|
||||
const size_t write_index = m_write_index.load();
|
||||
const size_t slots_free = capacity + m_read_index.load() - write_index;
|
||||
const size_t push_count = std::min(slot_count, slots_free);
|
||||
|
||||
const size_t pos = write_index % capacity;
|
||||
const size_t first_copy = std::min(capacity - pos, push_count);
|
||||
const size_t second_copy = push_count - first_copy;
|
||||
|
||||
const char* in = static_cast<const char*>(new_slots);
|
||||
std::memcpy(m_data.data() + pos * granularity, in, first_copy * slot_size);
|
||||
in += first_copy * slot_size;
|
||||
std::memcpy(m_data.data(), in, second_copy * slot_size);
|
||||
|
||||
m_write_index.store(write_index + push_count);
|
||||
|
||||
return push_count;
|
||||
}
|
||||
|
||||
size_t Push(const std::vector<T>& input) {
|
||||
return Push(input.data(), input.size());
|
||||
}
|
||||
|
||||
/// Pops slots from the ring buffer
|
||||
/// @param output Where to store the popped slots
|
||||
/// @param max_slots Maximum number of slots to pop
|
||||
/// @returns The number of slots actually popped
|
||||
size_t Pop(void* output, size_t max_slots = ~size_t(0)) {
|
||||
const size_t read_index = m_read_index.load();
|
||||
const size_t slots_filled = m_write_index.load() - read_index;
|
||||
const size_t pop_count = std::min(slots_filled, max_slots);
|
||||
|
||||
const size_t pos = read_index % capacity;
|
||||
const size_t first_copy = std::min(capacity - pos, pop_count);
|
||||
const size_t second_copy = pop_count - first_copy;
|
||||
|
||||
char* out = static_cast<char*>(output);
|
||||
std::memcpy(out, m_data.data() + pos * granularity, first_copy * slot_size);
|
||||
out += first_copy * slot_size;
|
||||
std::memcpy(out, m_data.data(), second_copy * slot_size);
|
||||
|
||||
m_read_index.store(read_index + pop_count);
|
||||
|
||||
return pop_count;
|
||||
}
|
||||
|
||||
std::vector<T> Pop(size_t max_slots = ~size_t(0)) {
|
||||
std::vector<T> out(std::min(max_slots, capacity) * granularity);
|
||||
const size_t count = Pop(out.data(), out.size() / granularity);
|
||||
out.resize(count * granularity);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// @returns Number of slots used
|
||||
size_t Size() const {
|
||||
return m_write_index.load() - m_read_index.load();
|
||||
}
|
||||
|
||||
/// @returns Maximum size of ring buffer
|
||||
constexpr size_t Capacity() const {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
private:
|
||||
// It is important to align the below variables for performance reasons:
|
||||
// Having them on the same cache-line would result in false-sharing between them.
|
||||
alignas(128) std::atomic<size_t> m_read_index{0};
|
||||
alignas(128) std::atomic<size_t> m_write_index{0};
|
||||
|
||||
std::array<T, granularity * capacity> m_data;
|
||||
};
|
||||
|
||||
} // namespace Common
|
@ -0,0 +1,130 @@
|
||||
// Copyright 2018 yuzu emulator team
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <numeric>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <catch2/catch.hpp>
|
||||
#include "common/ring_buffer.h"
|
||||
|
||||
namespace Common {
|
||||
|
||||
TEST_CASE("RingBuffer: Basic Tests", "[common]") {
|
||||
RingBuffer<char, 4, 1> buf;
|
||||
|
||||
// Pushing values into a ring buffer with space should succeed.
|
||||
for (size_t i = 0; i < 4; i++) {
|
||||
const char elem = static_cast<char>(i);
|
||||
const size_t count = buf.Push(&elem, 1);
|
||||
REQUIRE(count == 1);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 4);
|
||||
|
||||
// Pushing values into a full ring buffer should fail.
|
||||
{
|
||||
const char elem = static_cast<char>(42);
|
||||
const size_t count = buf.Push(&elem, 1);
|
||||
REQUIRE(count == 0);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 4);
|
||||
|
||||
// Popping multiple values from a ring buffer with values should succeed.
|
||||
{
|
||||
const std::vector<char> popped = buf.Pop(2);
|
||||
REQUIRE(popped.size() == 2);
|
||||
REQUIRE(popped[0] == 0);
|
||||
REQUIRE(popped[1] == 1);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 2);
|
||||
|
||||
// Popping a single value from a ring buffer with values should succeed.
|
||||
{
|
||||
const std::vector<char> popped = buf.Pop(1);
|
||||
REQUIRE(popped.size() == 1);
|
||||
REQUIRE(popped[0] == 2);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 1);
|
||||
|
||||
// Pushing more values than space available should partially suceed.
|
||||
{
|
||||
std::vector<char> to_push(6);
|
||||
std::iota(to_push.begin(), to_push.end(), 88);
|
||||
const size_t count = buf.Push(to_push);
|
||||
REQUIRE(count == 3);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 4);
|
||||
|
||||
// Doing an unlimited pop should pop all values.
|
||||
{
|
||||
const std::vector<char> popped = buf.Pop();
|
||||
REQUIRE(popped.size() == 4);
|
||||
REQUIRE(popped[0] == 3);
|
||||
REQUIRE(popped[1] == 88);
|
||||
REQUIRE(popped[2] == 89);
|
||||
REQUIRE(popped[3] == 90);
|
||||
}
|
||||
|
||||
REQUIRE(buf.Size() == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("RingBuffer: Threaded Test", "[common]") {
|
||||
RingBuffer<char, 4, 2> buf;
|
||||
const char seed = 42;
|
||||
const size_t count = 1000000;
|
||||
size_t full = 0;
|
||||
size_t empty = 0;
|
||||
|
||||
const auto next_value = [](std::array<char, 2>& value) {
|
||||
value[0] += 1;
|
||||
value[1] += 2;
|
||||
};
|
||||
|
||||
std::thread producer{[&] {
|
||||
std::array<char, 2> value = {seed, seed};
|
||||
size_t i = 0;
|
||||
while (i < count) {
|
||||
if (const size_t c = buf.Push(&value[0], 1); c > 0) {
|
||||
REQUIRE(c == 1);
|
||||
i++;
|
||||
next_value(value);
|
||||
} else {
|
||||
full++;
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
std::thread consumer{[&] {
|
||||
std::array<char, 2> value = {seed, seed};
|
||||
size_t i = 0;
|
||||
while (i < count) {
|
||||
if (const std::vector<char> v = buf.Pop(1); v.size() > 0) {
|
||||
REQUIRE(v.size() == 2);
|
||||
REQUIRE(v[0] == value[0]);
|
||||
REQUIRE(v[1] == value[1]);
|
||||
i++;
|
||||
next_value(value);
|
||||
} else {
|
||||
empty++;
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
producer.join();
|
||||
consumer.join();
|
||||
|
||||
REQUIRE(buf.Size() == 0);
|
||||
printf("RingBuffer: Threaded Test: full: %zu, empty: %zu\n", full, empty);
|
||||
}
|
||||
|
||||
} // namespace Common
|
Loading…
Reference in New Issue