summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--.gitmodules3
-rw-r--r--externals/CMakeLists.txt3
m---------externals/soundtouch0
-rw-r--r--src/audio_core/CMakeLists.txt3
-rw-r--r--src/audio_core/cubeb_sink.cpp114
-rw-r--r--src/audio_core/null_sink.h6
-rw-r--r--src/audio_core/sink_stream.h4
-rw-r--r--src/audio_core/stream.cpp3
-rw-r--r--src/audio_core/time_stretch.cpp68
-rw-r--r--src/audio_core/time_stretch.h36
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/ring_buffer.h111
-rw-r--r--src/core/settings.h1
-rw-r--r--src/core/telemetry_session.cpp3
-rw-r--r--src/tests/CMakeLists.txt1
-rw-r--r--src/tests/common/ring_buffer.cpp130
-rw-r--r--src/yuzu/configuration/config.cpp3
-rw-r--r--src/yuzu/configuration/configure_audio.cpp3
-rw-r--r--src/yuzu/configuration/configure_audio.ui10
-rw-r--r--src/yuzu_cmd/config.cpp2
-rw-r--r--src/yuzu_cmd/default_ini.h6
21 files changed, 462 insertions, 49 deletions
diff --git a/.gitmodules b/.gitmodules
index 4f4e8690b..e73ca99e3 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -31,3 +31,6 @@
31[submodule "opus"] 31[submodule "opus"]
32 path = externals/opus 32 path = externals/opus
33 url = https://github.com/ogniK5377/opus.git 33 url = https://github.com/ogniK5377/opus.git
34[submodule "soundtouch"]
35 path = externals/soundtouch
36 url = https://github.com/citra-emu/ext-soundtouch.git
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index 3d8e10c2b..53dcf1f1a 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -50,6 +50,9 @@ add_subdirectory(open_source_archives EXCLUDE_FROM_ALL)
50add_library(unicorn-headers INTERFACE) 50add_library(unicorn-headers INTERFACE)
51target_include_directories(unicorn-headers INTERFACE ./unicorn/include) 51target_include_directories(unicorn-headers INTERFACE ./unicorn/include)
52 52
53# SoundTouch
54add_subdirectory(soundtouch)
55
53# Xbyak 56# Xbyak
54if (ARCHITECTURE_x86_64) 57if (ARCHITECTURE_x86_64)
55 # Defined before "dynarmic" above 58 # Defined before "dynarmic" above
diff --git a/externals/soundtouch b/externals/soundtouch
new file mode 160000
Subproject 060181eaf273180d3a7e87349895bd0cb6ccbf4
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index 82e4850f7..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)
@@ -24,6 +26,7 @@ add_library(audio_core STATIC
24create_target_directory_groups(audio_core) 26create_target_directory_groups(audio_core)
25 27
26target_link_libraries(audio_core PUBLIC common core) 28target_link_libraries(audio_core PUBLIC common core)
29target_link_libraries(audio_core PRIVATE SoundTouch)
27 30
28if(ENABLE_CUBEB) 31if(ENABLE_CUBEB)
29 target_link_libraries(audio_core PRIVATE cubeb) 32 target_link_libraries(audio_core PRIVATE cubeb)
diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp
index 5a1177d0c..79155a7a0 100644
--- a/src/audio_core/cubeb_sink.cpp
+++ b/src/audio_core/cubeb_sink.cpp
@@ -3,27 +3,23 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm> 5#include <algorithm>
6#include <atomic>
6#include <cstring> 7#include <cstring>
7#include <mutex>
8
9#include "audio_core/cubeb_sink.h" 8#include "audio_core/cubeb_sink.h"
10#include "audio_core/stream.h" 9#include "audio_core/stream.h"
10#include "audio_core/time_stretch.h"
11#include "common/logging/log.h" 11#include "common/logging/log.h"
12#include "common/ring_buffer.h"
13#include "core/settings.h"
12 14
13namespace AudioCore { 15namespace AudioCore {
14 16
15class SinkStreamImpl final : public SinkStream { 17class CubebSinkStream final : public SinkStream {
16public: 18public:
17 SinkStreamImpl(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, 19 CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device,
18 const std::string& name) 20 const std::string& name)
19 : ctx{ctx}, num_channels{num_channels_} { 21 : ctx{ctx}, num_channels{std::min(num_channels_, 2u)}, time_stretch{sample_rate,
20 22 num_channels} {
21 if (num_channels == 6) {
22 // 6-channel audio does not seem to work with cubeb + SDL, so we downsample this to 2
23 // channel for now
24 is_6_channel = true;
25 num_channels = 2;
26 }
27 23
28 cubeb_stream_params params{}; 24 cubeb_stream_params params{};
29 params.rate = sample_rate; 25 params.rate = sample_rate;
@@ -38,7 +34,7 @@ public:
38 34
39 if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device, 35 if (cubeb_stream_init(ctx, &stream_backend, name.c_str(), nullptr, nullptr, output_device,
40 &params, std::max(512u, minimum_latency), 36 &params, std::max(512u, minimum_latency),
41 &SinkStreamImpl::DataCallback, &SinkStreamImpl::StateCallback, 37 &CubebSinkStream::DataCallback, &CubebSinkStream::StateCallback,
42 this) != CUBEB_OK) { 38 this) != CUBEB_OK) {
43 LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream"); 39 LOG_CRITICAL(Audio_Sink, "Error initializing cubeb stream");
44 return; 40 return;
@@ -50,7 +46,7 @@ public:
50 } 46 }
51 } 47 }
52 48
53 ~SinkStreamImpl() { 49 ~CubebSinkStream() {
54 if (!ctx) { 50 if (!ctx) {
55 return; 51 return;
56 } 52 }
@@ -62,27 +58,32 @@ public:
62 cubeb_stream_destroy(stream_backend); 58 cubeb_stream_destroy(stream_backend);
63 } 59 }
64 60
65 void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) override { 61 void EnqueueSamples(u32 source_num_channels, const std::vector<s16>& samples) override {
66 if (!ctx) { 62 if (source_num_channels > num_channels) {
63 // Downsample 6 channels to 2
64 std::vector<s16> buf;
65 buf.reserve(samples.size() * num_channels / source_num_channels);
66 for (size_t i = 0; i < samples.size(); i += source_num_channels) {
67 for (size_t ch = 0; ch < num_channels; ch++) {
68 buf.push_back(samples[i + ch]);
69 }
70 }
71 queue.Push(buf);
67 return; 72 return;
68 } 73 }
69 74
70 std::lock_guard lock{queue_mutex}; 75 queue.Push(samples);
76 }
71 77
72 queue.reserve(queue.size() + samples.size() * GetNumChannels()); 78 size_t SamplesInQueue(u32 num_channels) const override {
79 if (!ctx)
80 return 0;
73 81
74 if (is_6_channel) { 82 return queue.Size() / num_channels;
75 // Downsample 6 channels to 2 83 }
76 const size_t sample_count_copy_size = samples.size() * 2; 84
77 queue.reserve(sample_count_copy_size); 85 void Flush() override {
78 for (size_t i = 0; i < samples.size(); i += num_channels) { 86 should_flush = true;
79 queue.push_back(samples[i]);
80 queue.push_back(samples[i + 1]);
81 }
82 } else {
83 // Copy as-is
84 std::copy(samples.begin(), samples.end(), std::back_inserter(queue));
85 }
86 } 87 }
87 88
88 u32 GetNumChannels() const { 89 u32 GetNumChannels() const {
@@ -95,10 +96,11 @@ private:
95 cubeb* ctx{}; 96 cubeb* ctx{};
96 cubeb_stream* stream_backend{}; 97 cubeb_stream* stream_backend{};
97 u32 num_channels{}; 98 u32 num_channels{};
98 bool is_6_channel{};
99 99
100 std::mutex queue_mutex; 100 Common::RingBuffer<s16, 0x10000> queue;
101 std::vector<s16> queue; 101 std::array<s16, 2> last_frame;
102 std::atomic<bool> should_flush{};
103 TimeStretcher time_stretch;
102 104
103 static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, 105 static long DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
104 void* output_buffer, long num_frames); 106 void* output_buffer, long num_frames);
@@ -144,38 +146,52 @@ CubebSink::~CubebSink() {
144SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, 146SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels,
145 const std::string& name) { 147 const std::string& name) {
146 sink_streams.push_back( 148 sink_streams.push_back(
147 std::make_unique<SinkStreamImpl>(ctx, sample_rate, num_channels, output_device, name)); 149 std::make_unique<CubebSinkStream>(ctx, sample_rate, num_channels, output_device, name));
148 return *sink_streams.back(); 150 return *sink_streams.back();
149} 151}
150 152
151long SinkStreamImpl::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, 153long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer,
152 void* output_buffer, long num_frames) { 154 void* output_buffer, long num_frames) {
153 SinkStreamImpl* impl = static_cast<SinkStreamImpl*>(user_data); 155 CubebSinkStream* impl = static_cast<CubebSinkStream*>(user_data);
154 u8* buffer = reinterpret_cast<u8*>(output_buffer); 156 u8* buffer = reinterpret_cast<u8*>(output_buffer);
155 157
156 if (!impl) { 158 if (!impl) {
157 return {}; 159 return {};
158 } 160 }
159 161
160 std::lock_guard lock{impl->queue_mutex}; 162 const size_t num_channels = impl->GetNumChannels();
163 const size_t samples_to_write = num_channels * num_frames;
164 size_t samples_written;
165
166 if (Settings::values.enable_audio_stretching) {
167 const std::vector<s16> in{impl->queue.Pop()};
168 const size_t num_in{in.size() / num_channels};
169 s16* const out{reinterpret_cast<s16*>(buffer)};
170 const size_t out_frames = impl->time_stretch.Process(in.data(), num_in, out, num_frames);
171 samples_written = out_frames * num_channels;
161 172
162 const size_t frames_to_write{ 173 if (impl->should_flush) {
163 std::min(impl->queue.size() / impl->GetNumChannels(), static_cast<size_t>(num_frames))}; 174 impl->time_stretch.Flush();
175 impl->should_flush = false;
176 }
177 } else {
178 samples_written = impl->queue.Pop(buffer, samples_to_write);
179 }
164 180
165 memcpy(buffer, impl->queue.data(), frames_to_write * sizeof(s16) * impl->GetNumChannels()); 181 if (samples_written >= num_channels) {
166 impl->queue.erase(impl->queue.begin(), 182 std::memcpy(&impl->last_frame[0], buffer + (samples_written - num_channels) * sizeof(s16),
167 impl->queue.begin() + frames_to_write * impl->GetNumChannels()); 183 num_channels * sizeof(s16));
184 }
168 185
169 if (frames_to_write < num_frames) { 186 // Fill the rest of the frames with last_frame
170 // Fill the rest of the frames with silence 187 for (size_t i = samples_written; i < samples_to_write; i += num_channels) {
171 memset(buffer + frames_to_write * sizeof(s16) * impl->GetNumChannels(), 0, 188 std::memcpy(buffer + i * sizeof(s16), &impl->last_frame[0], num_channels * sizeof(s16));
172 (num_frames - frames_to_write) * sizeof(s16) * impl->GetNumChannels());
173 } 189 }
174 190
175 return num_frames; 191 return num_frames;
176} 192}
177 193
178void SinkStreamImpl::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} 194void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {}
179 195
180std::vector<std::string> ListCubebSinkDevices() { 196std::vector<std::string> ListCubebSinkDevices() {
181 std::vector<std::string> device_list; 197 std::vector<std::string> device_list;
diff --git a/src/audio_core/null_sink.h b/src/audio_core/null_sink.h
index f235d93e5..2ed0c83b6 100644
--- a/src/audio_core/null_sink.h
+++ b/src/audio_core/null_sink.h
@@ -21,6 +21,12 @@ public:
21private: 21private:
22 struct NullSinkStreamImpl final : SinkStream { 22 struct NullSinkStreamImpl final : SinkStream {
23 void EnqueueSamples(u32 /*num_channels*/, const std::vector<s16>& /*samples*/) override {} 23 void EnqueueSamples(u32 /*num_channels*/, const std::vector<s16>& /*samples*/) override {}
24
25 size_t SamplesInQueue(u32 /*num_channels*/) const override {
26 return 0;
27 }
28
29 void Flush() override {}
24 } null_sink_stream; 30 } null_sink_stream;
25}; 31};
26 32
diff --git a/src/audio_core/sink_stream.h b/src/audio_core/sink_stream.h
index 41b6736d8..4309ad094 100644
--- a/src/audio_core/sink_stream.h
+++ b/src/audio_core/sink_stream.h
@@ -25,6 +25,10 @@ public:
25 * @param samples Samples in interleaved stereo PCM16 format. 25 * @param samples Samples in interleaved stereo PCM16 format.
26 */ 26 */
27 virtual void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) = 0; 27 virtual void EnqueueSamples(u32 num_channels, const std::vector<s16>& samples) = 0;
28
29 virtual std::size_t SamplesInQueue(u32 num_channels) const = 0;
30
31 virtual void Flush() = 0;
28}; 32};
29 33
30using SinkStreamPtr = std::unique_ptr<SinkStream>; 34using SinkStreamPtr = std::unique_ptr<SinkStream>;
diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp
index dbae75d8c..84dcdd98d 100644
--- a/src/audio_core/stream.cpp
+++ b/src/audio_core/stream.cpp
@@ -73,6 +73,7 @@ static void VolumeAdjustSamples(std::vector<s16>& samples) {
73void Stream::PlayNextBuffer() { 73void Stream::PlayNextBuffer() {
74 if (!IsPlaying()) { 74 if (!IsPlaying()) {
75 // Ensure we are in playing state before playing the next buffer 75 // Ensure we are in playing state before playing the next buffer
76 sink_stream.Flush();
76 return; 77 return;
77 } 78 }
78 79
@@ -83,6 +84,7 @@ void Stream::PlayNextBuffer() {
83 84
84 if (queued_buffers.empty()) { 85 if (queued_buffers.empty()) {
85 // No queued buffers - we are effectively paused 86 // No queued buffers - we are effectively paused
87 sink_stream.Flush();
86 return; 88 return;
87 } 89 }
88 90
@@ -90,6 +92,7 @@ void Stream::PlayNextBuffer() {
90 queued_buffers.pop(); 92 queued_buffers.pop();
91 93
92 VolumeAdjustSamples(active_buffer->Samples()); 94 VolumeAdjustSamples(active_buffer->Samples());
95
93 sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples()); 96 sink_stream.EnqueueSamples(GetNumChannels(), active_buffer->GetSamples());
94 97
95 CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {}); 98 CoreTiming::ScheduleEventThreadsafe(GetBufferReleaseCycles(*active_buffer), release_event, {});
diff --git a/src/audio_core/time_stretch.cpp b/src/audio_core/time_stretch.cpp
new file mode 100644
index 000000000..da094c46b
--- /dev/null
+++ b/src/audio_core/time_stretch.cpp
@@ -0,0 +1,68 @@
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
25void TimeStretcher::Flush() {
26 m_sound_touch.flush();
27}
28
29size_t TimeStretcher::Process(const s16* in, size_t num_in, s16* out, size_t num_out) {
30 const double time_delta = static_cast<double>(num_out) / m_sample_rate; // seconds
31
32 // We were given actual_samples number of samples, and num_samples were requested from us.
33 double current_ratio = static_cast<double>(num_in) / static_cast<double>(num_out);
34
35 const double max_latency = 1.0; // seconds
36 const double max_backlog = m_sample_rate * max_latency;
37 const double backlog_fullness = m_sound_touch.numSamples() / max_backlog;
38 if (backlog_fullness > 5.0) {
39 // Too many samples in backlog: Don't push anymore on
40 num_in = 0;
41 }
42
43 // We ideally want the backlog to be about 50% full.
44 // This gives some headroom both ways to prevent underflow and overflow.
45 // We tweak current_ratio to encourage this.
46 constexpr double tweak_time_scale = 0.05; // seconds
47 const double tweak_correction = (backlog_fullness - 0.5) * (time_delta / tweak_time_scale);
48 current_ratio *= std::pow(1.0 + 2.0 * tweak_correction, tweak_correction < 0 ? 3.0 : 1.0);
49
50 // This low-pass filter smoothes out variance in the calculated stretch ratio.
51 // The time-scale determines how responsive this filter is.
52 constexpr double lpf_time_scale = 2.0; // seconds
53 const double lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale);
54 m_stretch_ratio += lpf_gain * (current_ratio - m_stretch_ratio);
55
56 // Place a lower limit of 5% speed. When a game boots up, there will be
57 // many silence samples. These do not need to be timestretched.
58 m_stretch_ratio = std::max(m_stretch_ratio, 0.05);
59 m_sound_touch.setTempo(m_stretch_ratio);
60
61 LOG_DEBUG(Audio, "{:5}/{:5} ratio:{:0.6f} backlog:{:0.6f}", num_in, num_out, m_stretch_ratio,
62 backlog_fullness);
63
64 m_sound_touch.putSamples(in, num_in);
65 return m_sound_touch.receiveSamples(out, num_out);
66}
67
68} // namespace AudioCore
diff --git a/src/audio_core/time_stretch.h b/src/audio_core/time_stretch.h
new file mode 100644
index 000000000..7e39e695e
--- /dev/null
+++ b/src/audio_core/time_stretch.h
@@ -0,0 +1,36 @@
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
12namespace AudioCore {
13
14class TimeStretcher {
15public:
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 void Flush();
28
29private:
30 u32 m_sample_rate;
31 u32 m_channel_count;
32 soundtouch::SoundTouch m_sound_touch;
33 double m_stretch_ratio = 1.0;
34};
35
36} // namespace AudioCore
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index f41946cc6..6a3f1fe08 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -71,6 +71,7 @@ add_library(common STATIC
71 param_package.cpp 71 param_package.cpp
72 param_package.h 72 param_package.h
73 quaternion.h 73 quaternion.h
74 ring_buffer.h
74 scm_rev.cpp 75 scm_rev.cpp
75 scm_rev.h 76 scm_rev.h
76 scope_exit.h 77 scope_exit.h
diff --git a/src/common/ring_buffer.h b/src/common/ring_buffer.h
new file mode 100644
index 000000000..30d934a38
--- /dev/null
+++ b/src/common/ring_buffer.h
@@ -0,0 +1,111 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include <atomic>
10#include <cstddef>
11#include <cstring>
12#include <type_traits>
13#include <vector>
14#include "common/common_types.h"
15
16namespace Common {
17
18/// SPSC ring buffer
19/// @tparam T Element type
20/// @tparam capacity Number of slots in ring buffer
21/// @tparam granularity Slot size in terms of number of elements
22template <typename T, size_t capacity, size_t granularity = 1>
23class RingBuffer {
24 /// A "slot" is made of `granularity` elements of `T`.
25 static constexpr size_t slot_size = granularity * sizeof(T);
26 // T must be safely memcpy-able and have a trivial default constructor.
27 static_assert(std::is_trivial_v<T>);
28 // Ensure capacity is sensible.
29 static_assert(capacity < std::numeric_limits<size_t>::max() / 2 / granularity);
30 static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");
31 // Ensure lock-free.
32 static_assert(std::atomic<size_t>::is_always_lock_free);
33
34public:
35 /// Pushes slots into the ring buffer
36 /// @param new_slots Pointer to the slots to push
37 /// @param slot_count Number of slots to push
38 /// @returns The number of slots actually pushed
39 size_t Push(const void* new_slots, size_t slot_count) {
40 const size_t write_index = m_write_index.load();
41 const size_t slots_free = capacity + m_read_index.load() - write_index;
42 const size_t push_count = std::min(slot_count, slots_free);
43
44 const size_t pos = write_index % capacity;
45 const size_t first_copy = std::min(capacity - pos, push_count);
46 const size_t second_copy = push_count - first_copy;
47
48 const char* in = static_cast<const char*>(new_slots);
49 std::memcpy(m_data.data() + pos * granularity, in, first_copy * slot_size);
50 in += first_copy * slot_size;
51 std::memcpy(m_data.data(), in, second_copy * slot_size);
52
53 m_write_index.store(write_index + push_count);
54
55 return push_count;
56 }
57
58 size_t Push(const std::vector<T>& input) {
59 return Push(input.data(), input.size());
60 }
61
62 /// Pops slots from the ring buffer
63 /// @param output Where to store the popped slots
64 /// @param max_slots Maximum number of slots to pop
65 /// @returns The number of slots actually popped
66 size_t Pop(void* output, size_t max_slots = ~size_t(0)) {
67 const size_t read_index = m_read_index.load();
68 const size_t slots_filled = m_write_index.load() - read_index;
69 const size_t pop_count = std::min(slots_filled, max_slots);
70
71 const size_t pos = read_index % capacity;
72 const size_t first_copy = std::min(capacity - pos, pop_count);
73 const size_t second_copy = pop_count - first_copy;
74
75 char* out = static_cast<char*>(output);
76 std::memcpy(out, m_data.data() + pos * granularity, first_copy * slot_size);
77 out += first_copy * slot_size;
78 std::memcpy(out, m_data.data(), second_copy * slot_size);
79
80 m_read_index.store(read_index + pop_count);
81
82 return pop_count;
83 }
84
85 std::vector<T> Pop(size_t max_slots = ~size_t(0)) {
86 std::vector<T> out(std::min(max_slots, capacity) * granularity);
87 const size_t count = Pop(out.data(), out.size() / granularity);
88 out.resize(count * granularity);
89 return out;
90 }
91
92 /// @returns Number of slots used
93 size_t Size() const {
94 return m_write_index.load() - m_read_index.load();
95 }
96
97 /// @returns Maximum size of ring buffer
98 constexpr size_t Capacity() const {
99 return capacity;
100 }
101
102private:
103 // It is important to align the below variables for performance reasons:
104 // Having them on the same cache-line would result in false-sharing between them.
105 alignas(128) std::atomic<size_t> m_read_index{0};
106 alignas(128) std::atomic<size_t> m_write_index{0};
107
108 std::array<T, granularity * capacity> m_data;
109};
110
111} // namespace Common
diff --git a/src/core/settings.h b/src/core/settings.h
index 08a16ef2c..0318d019c 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -148,6 +148,7 @@ struct Values {
148 148
149 // Audio 149 // Audio
150 std::string sink_id; 150 std::string sink_id;
151 bool enable_audio_stretching;
151 std::string audio_device_id; 152 std::string audio_device_id;
152 float volume; 153 float volume;
153 154
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp
index 3730e85b8..b0df154ca 100644
--- a/src/core/telemetry_session.cpp
+++ b/src/core/telemetry_session.cpp
@@ -120,6 +120,9 @@ TelemetrySession::TelemetrySession() {
120 Telemetry::AppendOSInfo(field_collection); 120 Telemetry::AppendOSInfo(field_collection);
121 121
122 // Log user configuration information 122 // Log user configuration information
123 AddField(Telemetry::FieldType::UserConfig, "Audio_SinkId", Settings::values.sink_id);
124 AddField(Telemetry::FieldType::UserConfig, "Audio_EnableAudioStretching",
125 Settings::values.enable_audio_stretching);
123 AddField(Telemetry::FieldType::UserConfig, "Core_UseCpuJit", Settings::values.use_cpu_jit); 126 AddField(Telemetry::FieldType::UserConfig, "Core_UseCpuJit", Settings::values.use_cpu_jit);
124 AddField(Telemetry::FieldType::UserConfig, "Core_UseMultiCore", 127 AddField(Telemetry::FieldType::UserConfig, "Core_UseMultiCore",
125 Settings::values.use_multi_core); 128 Settings::values.use_multi_core);
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index 4d74bb395..4e75a72ec 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,5 +1,6 @@
1add_executable(tests 1add_executable(tests
2 common/param_package.cpp 2 common/param_package.cpp
3 common/ring_buffer.cpp
3 core/arm/arm_test_common.cpp 4 core/arm/arm_test_common.cpp
4 core/arm/arm_test_common.h 5 core/arm/arm_test_common.h
5 core/core_timing.cpp 6 core/core_timing.cpp
diff --git a/src/tests/common/ring_buffer.cpp b/src/tests/common/ring_buffer.cpp
new file mode 100644
index 000000000..f3fe57839
--- /dev/null
+++ b/src/tests/common/ring_buffer.cpp
@@ -0,0 +1,130 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <array>
7#include <cstddef>
8#include <numeric>
9#include <thread>
10#include <vector>
11#include <catch2/catch.hpp>
12#include "common/ring_buffer.h"
13
14namespace Common {
15
16TEST_CASE("RingBuffer: Basic Tests", "[common]") {
17 RingBuffer<char, 4, 1> buf;
18
19 // Pushing values into a ring buffer with space should succeed.
20 for (size_t i = 0; i < 4; i++) {
21 const char elem = static_cast<char>(i);
22 const size_t count = buf.Push(&elem, 1);
23 REQUIRE(count == 1);
24 }
25
26 REQUIRE(buf.Size() == 4);
27
28 // Pushing values into a full ring buffer should fail.
29 {
30 const char elem = static_cast<char>(42);
31 const size_t count = buf.Push(&elem, 1);
32 REQUIRE(count == 0);
33 }
34
35 REQUIRE(buf.Size() == 4);
36
37 // Popping multiple values from a ring buffer with values should succeed.
38 {
39 const std::vector<char> popped = buf.Pop(2);
40 REQUIRE(popped.size() == 2);
41 REQUIRE(popped[0] == 0);
42 REQUIRE(popped[1] == 1);
43 }
44
45 REQUIRE(buf.Size() == 2);
46
47 // Popping a single value from a ring buffer with values should succeed.
48 {
49 const std::vector<char> popped = buf.Pop(1);
50 REQUIRE(popped.size() == 1);
51 REQUIRE(popped[0] == 2);
52 }
53
54 REQUIRE(buf.Size() == 1);
55
56 // Pushing more values than space available should partially suceed.
57 {
58 std::vector<char> to_push(6);
59 std::iota(to_push.begin(), to_push.end(), 88);
60 const size_t count = buf.Push(to_push);
61 REQUIRE(count == 3);
62 }
63
64 REQUIRE(buf.Size() == 4);
65
66 // Doing an unlimited pop should pop all values.
67 {
68 const std::vector<char> popped = buf.Pop();
69 REQUIRE(popped.size() == 4);
70 REQUIRE(popped[0] == 3);
71 REQUIRE(popped[1] == 88);
72 REQUIRE(popped[2] == 89);
73 REQUIRE(popped[3] == 90);
74 }
75
76 REQUIRE(buf.Size() == 0);
77}
78
79TEST_CASE("RingBuffer: Threaded Test", "[common]") {
80 RingBuffer<char, 4, 2> buf;
81 const char seed = 42;
82 const size_t count = 1000000;
83 size_t full = 0;
84 size_t empty = 0;
85
86 const auto next_value = [](std::array<char, 2>& value) {
87 value[0] += 1;
88 value[1] += 2;
89 };
90
91 std::thread producer{[&] {
92 std::array<char, 2> value = {seed, seed};
93 size_t i = 0;
94 while (i < count) {
95 if (const size_t c = buf.Push(&value[0], 1); c > 0) {
96 REQUIRE(c == 1);
97 i++;
98 next_value(value);
99 } else {
100 full++;
101 std::this_thread::yield();
102 }
103 }
104 }};
105
106 std::thread consumer{[&] {
107 std::array<char, 2> value = {seed, seed};
108 size_t i = 0;
109 while (i < count) {
110 if (const std::vector<char> v = buf.Pop(1); v.size() > 0) {
111 REQUIRE(v.size() == 2);
112 REQUIRE(v[0] == value[0]);
113 REQUIRE(v[1] == value[1]);
114 i++;
115 next_value(value);
116 } else {
117 empty++;
118 std::this_thread::yield();
119 }
120 }
121 }};
122
123 producer.join();
124 consumer.join();
125
126 REQUIRE(buf.Size() == 0);
127 printf("RingBuffer: Threaded Test: full: %zu, empty: %zu\n", full, empty);
128}
129
130} // namespace Common
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index c43e79e78..d229225b4 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -95,6 +95,8 @@ void Config::ReadValues() {
95 95
96 qt_config->beginGroup("Audio"); 96 qt_config->beginGroup("Audio");
97 Settings::values.sink_id = qt_config->value("output_engine", "auto").toString().toStdString(); 97 Settings::values.sink_id = qt_config->value("output_engine", "auto").toString().toStdString();
98 Settings::values.enable_audio_stretching =
99 qt_config->value("enable_audio_stretching", true).toBool();
98 Settings::values.audio_device_id = 100 Settings::values.audio_device_id =
99 qt_config->value("output_device", "auto").toString().toStdString(); 101 qt_config->value("output_device", "auto").toString().toStdString();
100 Settings::values.volume = qt_config->value("volume", 1).toFloat(); 102 Settings::values.volume = qt_config->value("volume", 1).toFloat();
@@ -230,6 +232,7 @@ void Config::SaveValues() {
230 232
231 qt_config->beginGroup("Audio"); 233 qt_config->beginGroup("Audio");
232 qt_config->setValue("output_engine", QString::fromStdString(Settings::values.sink_id)); 234 qt_config->setValue("output_engine", QString::fromStdString(Settings::values.sink_id));
235 qt_config->setValue("enable_audio_stretching", Settings::values.enable_audio_stretching);
233 qt_config->setValue("output_device", QString::fromStdString(Settings::values.audio_device_id)); 236 qt_config->setValue("output_device", QString::fromStdString(Settings::values.audio_device_id));
234 qt_config->setValue("volume", Settings::values.volume); 237 qt_config->setValue("volume", Settings::values.volume);
235 qt_config->endGroup(); 238 qt_config->endGroup();
diff --git a/src/yuzu/configuration/configure_audio.cpp b/src/yuzu/configuration/configure_audio.cpp
index fbb813f6c..6ea59f2a3 100644
--- a/src/yuzu/configuration/configure_audio.cpp
+++ b/src/yuzu/configuration/configure_audio.cpp
@@ -46,6 +46,8 @@ void ConfigureAudio::setConfiguration() {
46 } 46 }
47 ui->output_sink_combo_box->setCurrentIndex(new_sink_index); 47 ui->output_sink_combo_box->setCurrentIndex(new_sink_index);
48 48
49 ui->toggle_audio_stretching->setChecked(Settings::values.enable_audio_stretching);
50
49 // The device list cannot be pre-populated (nor listed) until the output sink is known. 51 // The device list cannot be pre-populated (nor listed) until the output sink is known.
50 updateAudioDevices(new_sink_index); 52 updateAudioDevices(new_sink_index);
51 53
@@ -67,6 +69,7 @@ void ConfigureAudio::applyConfiguration() {
67 Settings::values.sink_id = 69 Settings::values.sink_id =
68 ui->output_sink_combo_box->itemText(ui->output_sink_combo_box->currentIndex()) 70 ui->output_sink_combo_box->itemText(ui->output_sink_combo_box->currentIndex())
69 .toStdString(); 71 .toStdString();
72 Settings::values.enable_audio_stretching = ui->toggle_audio_stretching->isChecked();
70 Settings::values.audio_device_id = 73 Settings::values.audio_device_id =
71 ui->audio_device_combo_box->itemText(ui->audio_device_combo_box->currentIndex()) 74 ui->audio_device_combo_box->itemText(ui->audio_device_combo_box->currentIndex())
72 .toStdString(); 75 .toStdString();
diff --git a/src/yuzu/configuration/configure_audio.ui b/src/yuzu/configuration/configure_audio.ui
index ef67890dc..a29a0e265 100644
--- a/src/yuzu/configuration/configure_audio.ui
+++ b/src/yuzu/configuration/configure_audio.ui
@@ -31,6 +31,16 @@
31 </item> 31 </item>
32 </layout> 32 </layout>
33 </item> 33 </item>
34 <item>
35 <widget class="QCheckBox" name="toggle_audio_stretching">
36 <property name="toolTip">
37 <string>This post-processing effect adjusts audio speed to match emulation speed and helps prevent audio stutter. This however increases audio latency.</string>
38 </property>
39 <property name="text">
40 <string>Enable audio stretching</string>
41 </property>
42 </widget>
43 </item>
34 <item> 44 <item>
35 <layout class="QHBoxLayout"> 45 <layout class="QHBoxLayout">
36 <item> 46 <item>
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index f00b5a66b..991abda2e 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -108,6 +108,8 @@ void Config::ReadValues() {
108 108
109 // Audio 109 // Audio
110 Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto"); 110 Settings::values.sink_id = sdl2_config->Get("Audio", "output_engine", "auto");
111 Settings::values.enable_audio_stretching =
112 sdl2_config->GetBoolean("Audio", "enable_audio_stretching", true);
111 Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto"); 113 Settings::values.audio_device_id = sdl2_config->Get("Audio", "output_device", "auto");
112 Settings::values.volume = sdl2_config->GetReal("Audio", "volume", 1); 114 Settings::values.volume = sdl2_config->GetReal("Audio", "volume", 1);
113 115
diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h
index 6ed9e7962..002a4ec15 100644
--- a/src/yuzu_cmd/default_ini.h
+++ b/src/yuzu_cmd/default_ini.h
@@ -150,6 +150,12 @@ swap_screen =
150# auto (default): Auto-select, null: No audio output, cubeb: Cubeb audio engine (if available) 150# auto (default): Auto-select, null: No audio output, cubeb: Cubeb audio engine (if available)
151output_engine = 151output_engine =
152 152
153# Whether or not to enable the audio-stretching post-processing effect.
154# This effect adjusts audio speed to match emulation speed and helps prevent audio stutter,
155# at the cost of increasing audio latency.
156# 0: No, 1 (default): Yes
157enable_audio_stretching =
158
153# Which audio device to use. 159# Which audio device to use.
154# auto (default): Auto-select 160# auto (default): Auto-select
155output_device = 161output_device =