summaryrefslogtreecommitdiff
path: root/src/audio_core/sink
diff options
context:
space:
mode:
authorGravatar Kelebek12022-08-01 02:58:13 +0100
committerGravatar Kelebek12022-09-02 04:43:04 +0100
commitea9ff71725113b8dbb159917c57aa536bba0cb53 (patch)
tree512cce0fea5eb511aa7803bc67f741815885bfcb /src/audio_core/sink
parentMerge pull request #8752 from vonchenplus/rectangle_texture (diff)
downloadyuzu-ea9ff71725113b8dbb159917c57aa536bba0cb53.tar.gz
yuzu-ea9ff71725113b8dbb159917c57aa536bba0cb53.tar.xz
yuzu-ea9ff71725113b8dbb159917c57aa536bba0cb53.zip
Rework audio output, connecting AudioOut into coretiming to fix desync during heavy loads.
Diffstat (limited to 'src/audio_core/sink')
-rw-r--r--src/audio_core/sink/cubeb_sink.cpp349
-rw-r--r--src/audio_core/sink/cubeb_sink.h2
-rw-r--r--src/audio_core/sink/null_sink.h47
-rw-r--r--src/audio_core/sink/sdl2_sink.cpp350
-rw-r--r--src/audio_core/sink/sdl2_sink.h2
-rw-r--r--src/audio_core/sink/sink.h2
-rw-r--r--src/audio_core/sink/sink_details.cpp6
-rw-r--r--src/audio_core/sink/sink_stream.cpp259
-rw-r--r--src/audio_core/sink/sink_stream.h171
9 files changed, 440 insertions, 748 deletions
diff --git a/src/audio_core/sink/cubeb_sink.cpp b/src/audio_core/sink/cubeb_sink.cpp
index 90d049e8e..9ae043611 100644
--- a/src/audio_core/sink/cubeb_sink.cpp
+++ b/src/audio_core/sink/cubeb_sink.cpp
@@ -1,21 +1,13 @@
1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include <algorithm>
5#include <atomic>
6#include <span> 4#include <span>
5#include <vector>
7 6
8#include "audio_core/audio_core.h" 7#include "audio_core/common/common.h"
9#include "audio_core/audio_event.h"
10#include "audio_core/audio_manager.h"
11#include "audio_core/sink/cubeb_sink.h" 8#include "audio_core/sink/cubeb_sink.h"
12#include "audio_core/sink/sink_stream.h" 9#include "audio_core/sink/sink_stream.h"
13#include "common/assert.h"
14#include "common/fixed_point.h"
15#include "common/logging/log.h" 10#include "common/logging/log.h"
16#include "common/reader_writer_queue.h"
17#include "common/ring_buffer.h"
18#include "common/settings.h"
19#include "core/core.h" 11#include "core/core.h"
20 12
21#ifdef _WIN32 13#ifdef _WIN32
@@ -42,10 +34,10 @@ public:
42 * @param system_ - Core system. 34 * @param system_ - Core system.
43 * @param event - Event used only for audio renderer, signalled on buffer consume. 35 * @param event - Event used only for audio renderer, signalled on buffer consume.
44 */ 36 */
45 CubebSinkStream(cubeb* ctx_, const u32 device_channels_, const u32 system_channels_, 37 CubebSinkStream(cubeb* ctx_, u32 device_channels_, u32 system_channels_,
46 cubeb_devid output_device, cubeb_devid input_device, const std::string& name_, 38 cubeb_devid output_device, cubeb_devid input_device, const std::string& name_,
47 const StreamType type_, Core::System& system_) 39 StreamType type_, Core::System& system_)
48 : ctx{ctx_}, type{type_}, system{system_} { 40 : SinkStream(system_, type_), ctx{ctx_} {
49#ifdef _WIN32 41#ifdef _WIN32
50 CoInitializeEx(nullptr, COINIT_MULTITHREADED); 42 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
51#endif 43#endif
@@ -79,12 +71,10 @@ public:
79 71
80 minimum_latency = std::max(minimum_latency, 256u); 72 minimum_latency = std::max(minimum_latency, 256u);
81 73
82 playing_buffer.consumed = true; 74 LOG_INFO(Service_Audio,
83 75 "Opening cubeb stream {} type {} with: rate {} channels {} (system channels {}) "
84 LOG_DEBUG(Service_Audio, 76 "latency {}",
85 "Opening cubeb stream {} type {} with: rate {} channels {} (system channels {}) " 77 name, type, params.rate, params.channels, system_channels, minimum_latency);
86 "latency {}",
87 name, type, params.rate, params.channels, system_channels, minimum_latency);
88 78
89 auto init_error{0}; 79 auto init_error{0};
90 if (type == StreamType::In) { 80 if (type == StreamType::In) {
@@ -111,6 +101,8 @@ public:
111 ~CubebSinkStream() override { 101 ~CubebSinkStream() override {
112 LOG_DEBUG(Service_Audio, "Destructing cubeb stream {}", name); 102 LOG_DEBUG(Service_Audio, "Destructing cubeb stream {}", name);
113 103
104 Unstall();
105
114 if (!ctx) { 106 if (!ctx) {
115 return; 107 return;
116 } 108 }
@@ -136,7 +128,7 @@ public:
136 * @param resume - Set to true if this is resuming the stream a previously-active stream. 128 * @param resume - Set to true if this is resuming the stream a previously-active stream.
137 * Default false. 129 * Default false.
138 */ 130 */
139 void Start(const bool resume = false) override { 131 void Start(bool resume = false) override {
140 if (!ctx) { 132 if (!ctx) {
141 return; 133 return;
142 } 134 }
@@ -158,6 +150,7 @@ public:
158 * Stop the sink stream. 150 * Stop the sink stream.
159 */ 151 */
160 void Stop() override { 152 void Stop() override {
153 Unstall();
161 if (!ctx) { 154 if (!ctx) {
162 return; 155 return;
163 } 156 }
@@ -170,195 +163,8 @@ public:
170 paused = true; 163 paused = true;
171 } 164 }
172 165
173 /**
174 * Append a new buffer and its samples to a waiting queue to play.
175 *
176 * @param buffer - Audio buffer information to be queued.
177 * @param samples - The s16 samples to be queue for playback.
178 */
179 void AppendBuffer(::AudioCore::Sink::SinkBuffer& buffer, std::vector<s16>& samples) override {
180 if (type == StreamType::In) {
181 queue.enqueue(buffer);
182 queued_buffers++;
183 } else {
184 constexpr s32 min{std::numeric_limits<s16>::min()};
185 constexpr s32 max{std::numeric_limits<s16>::max()};
186
187 auto yuzu_volume{Settings::Volume()};
188 if (yuzu_volume > 1.0f) {
189 yuzu_volume = 0.6f + 20 * std::log10(yuzu_volume);
190 }
191 auto volume{system_volume * device_volume * yuzu_volume};
192
193 if (system_channels == 6 && device_channels == 2) {
194 // We're given 6 channels, but our device only outputs 2, so downmix.
195 constexpr std::array<f32, 4> down_mix_coeff{1.0f, 0.707f, 0.251f, 0.707f};
196
197 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
198 read_index += system_channels, write_index += device_channels) {
199 const auto left_sample{
200 ((Common::FixedPoint<49, 15>(
201 samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
202 down_mix_coeff[0] +
203 samples[read_index + static_cast<u32>(Channels::Center)] *
204 down_mix_coeff[1] +
205 samples[read_index + static_cast<u32>(Channels::LFE)] *
206 down_mix_coeff[2] +
207 samples[read_index + static_cast<u32>(Channels::BackLeft)] *
208 down_mix_coeff[3]) *
209 volume)
210 .to_int()};
211
212 const auto right_sample{
213 ((Common::FixedPoint<49, 15>(
214 samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
215 down_mix_coeff[0] +
216 samples[read_index + static_cast<u32>(Channels::Center)] *
217 down_mix_coeff[1] +
218 samples[read_index + static_cast<u32>(Channels::LFE)] *
219 down_mix_coeff[2] +
220 samples[read_index + static_cast<u32>(Channels::BackRight)] *
221 down_mix_coeff[3]) *
222 volume)
223 .to_int()};
224
225 samples[write_index + static_cast<u32>(Channels::FrontLeft)] =
226 static_cast<s16>(std::clamp(left_sample, min, max));
227 samples[write_index + static_cast<u32>(Channels::FrontRight)] =
228 static_cast<s16>(std::clamp(right_sample, min, max));
229 }
230
231 samples.resize(samples.size() / system_channels * device_channels);
232
233 } else if (system_channels == 2 && device_channels == 6) {
234 // We need moar samples! Not all games will provide 6 channel audio.
235 // TODO: Implement some upmixing here. Currently just passthrough, with other
236 // channels left as silence.
237 std::vector<s16> new_samples(samples.size() / system_channels * device_channels, 0);
238
239 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
240 read_index += system_channels, write_index += device_channels) {
241 const auto left_sample{static_cast<s16>(std::clamp(
242 static_cast<s32>(
243 static_cast<f32>(
244 samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
245 volume),
246 min, max))};
247
248 new_samples[write_index + static_cast<u32>(Channels::FrontLeft)] = left_sample;
249
250 const auto right_sample{static_cast<s16>(std::clamp(
251 static_cast<s32>(
252 static_cast<f32>(
253 samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
254 volume),
255 min, max))};
256
257 new_samples[write_index + static_cast<u32>(Channels::FrontRight)] =
258 right_sample;
259 }
260 samples = std::move(new_samples);
261
262 } else if (volume != 1.0f) {
263 for (u32 i = 0; i < samples.size(); i++) {
264 samples[i] = static_cast<s16>(std::clamp(
265 static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
266 }
267 }
268
269 samples_buffer.Push(samples);
270 queue.enqueue(buffer);
271 queued_buffers++;
272 }
273 }
274
275 /**
276 * Release a buffer. Audio In only, will fill a buffer with recorded samples.
277 *
278 * @param num_samples - Maximum number of samples to receive.
279 * @return Vector of recorded samples. May have fewer than num_samples.
280 */
281 std::vector<s16> ReleaseBuffer(const u64 num_samples) override {
282 static constexpr s32 min = std::numeric_limits<s16>::min();
283 static constexpr s32 max = std::numeric_limits<s16>::max();
284
285 auto samples{samples_buffer.Pop(num_samples)};
286
287 // TODO: Up-mix to 6 channels if the game expects it.
288 // For audio input this is unlikely to ever be the case though.
289
290 // Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
291 // TODO: Play with this and find something that works better.
292 auto volume{system_volume * device_volume * 8};
293 for (u32 i = 0; i < samples.size(); i++) {
294 samples[i] = static_cast<s16>(
295 std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
296 }
297
298 if (samples.size() < num_samples) {
299 samples.resize(num_samples, 0);
300 }
301 return samples;
302 }
303
304 /**
305 * Check if a certain buffer has been consumed (fully played).
306 *
307 * @param tag - Unique tag of a buffer to check for.
308 * @return True if the buffer has been played, otherwise false.
309 */
310 bool IsBufferConsumed(const u64 tag) override {
311 if (released_buffer.tag == 0) {
312 if (!released_buffers.try_dequeue(released_buffer)) {
313 return false;
314 }
315 }
316
317 if (released_buffer.tag == tag) {
318 released_buffer.tag = 0;
319 return true;
320 }
321 return false;
322 }
323
324 /**
325 * Empty out the buffer queue.
326 */
327 void ClearQueue() override {
328 samples_buffer.Pop();
329 while (queue.pop()) {
330 }
331 while (released_buffers.pop()) {
332 }
333 queued_buffers = 0;
334 released_buffer = {};
335 playing_buffer = {};
336 playing_buffer.consumed = true;
337 }
338
339private: 166private:
340 /** 167 /**
341 * Signal events back to the audio system that a buffer was played/can be filled.
342 *
343 * @param buffer - Consumed audio buffer to be released.
344 */
345 void SignalEvent(const ::AudioCore::Sink::SinkBuffer& buffer) {
346 auto& manager{system.AudioCore().GetAudioManager()};
347 switch (type) {
348 case StreamType::Out:
349 released_buffers.enqueue(buffer);
350 manager.SetEvent(Event::Type::AudioOutManager, true);
351 break;
352 case StreamType::In:
353 released_buffers.enqueue(buffer);
354 manager.SetEvent(Event::Type::AudioInManager, true);
355 break;
356 case StreamType::Render:
357 break;
358 }
359 }
360
361 /**
362 * Main callback from Cubeb. Either expects samples from us (audio render/audio out), or will 168 * Main callback from Cubeb. Either expects samples from us (audio render/audio out), or will
363 * provide samples to be copied (audio in). 169 * provide samples to be copied (audio in).
364 * 170 *
@@ -378,106 +184,15 @@ private:
378 184
379 const std::size_t num_channels = impl->GetDeviceChannels(); 185 const std::size_t num_channels = impl->GetDeviceChannels();
380 const std::size_t frame_size = num_channels; 186 const std::size_t frame_size = num_channels;
381 const std::size_t frame_size_bytes = frame_size * sizeof(s16);
382 const std::size_t num_frames{static_cast<size_t>(num_frames_)}; 187 const std::size_t num_frames{static_cast<size_t>(num_frames_)};
383 size_t frames_written{0};
384 [[maybe_unused]] bool underrun{false};
385 188
386 if (impl->type == StreamType::In) { 189 if (impl->type == StreamType::In) {
387 // INPUT
388 std::span<const s16> input_buffer{reinterpret_cast<const s16*>(in_buff), 190 std::span<const s16> input_buffer{reinterpret_cast<const s16*>(in_buff),
389 num_frames * frame_size}; 191 num_frames * frame_size};
390 192 impl->ProcessAudioIn(input_buffer, num_frames);
391 while (frames_written < num_frames) {
392 auto& playing_buffer{impl->playing_buffer};
393
394 // If the playing buffer has been consumed or has no frames, we need a new one
395 if (playing_buffer.consumed || playing_buffer.frames == 0) {
396 if (!impl->queue.try_dequeue(impl->playing_buffer)) {
397 // If no buffer was available we've underrun, just push the samples and
398 // continue.
399 underrun = true;
400 impl->samples_buffer.Push(&input_buffer[frames_written * frame_size],
401 (num_frames - frames_written) * frame_size);
402 frames_written = num_frames;
403 continue;
404 } else {
405 // Successfully got a new buffer, mark the old one as consumed and signal.
406 impl->queued_buffers--;
407 impl->SignalEvent(impl->playing_buffer);
408 }
409 }
410
411 // Get the minimum frames available between the currently playing buffer, and the
412 // amount we have left to fill
413 size_t frames_available{
414 std::min(playing_buffer.frames - playing_buffer.frames_played,
415 num_frames - frames_written)};
416
417 impl->samples_buffer.Push(&input_buffer[frames_written * frame_size],
418 frames_available * frame_size);
419
420 frames_written += frames_available;
421 playing_buffer.frames_played += frames_available;
422
423 // If that's all the frames in the current buffer, add its samples and mark it as
424 // consumed
425 if (playing_buffer.frames_played >= playing_buffer.frames) {
426 impl->AddPlayedSampleCount(playing_buffer.frames_played * num_channels);
427 impl->playing_buffer.consumed = true;
428 }
429 }
430
431 std::memcpy(&impl->last_frame[0], &input_buffer[(frames_written - 1) * frame_size],
432 frame_size_bytes);
433 } else { 193 } else {
434 // OUTPUT
435 std::span<s16> output_buffer{reinterpret_cast<s16*>(out_buff), num_frames * frame_size}; 194 std::span<s16> output_buffer{reinterpret_cast<s16*>(out_buff), num_frames * frame_size};
436 195 impl->ProcessAudioOutAndRender(output_buffer, num_frames);
437 while (frames_written < num_frames) {
438 auto& playing_buffer{impl->playing_buffer};
439
440 // If the playing buffer has been consumed or has no frames, we need a new one
441 if (playing_buffer.consumed || playing_buffer.frames == 0) {
442 if (!impl->queue.try_dequeue(impl->playing_buffer)) {
443 // If no buffer was available we've underrun, fill the remaining buffer with
444 // the last written frame and continue.
445 underrun = true;
446 for (size_t i = frames_written; i < num_frames; i++) {
447 std::memcpy(&output_buffer[i * frame_size], &impl->last_frame[0],
448 frame_size_bytes);
449 }
450 frames_written = num_frames;
451 continue;
452 } else {
453 // Successfully got a new buffer, mark the old one as consumed and signal.
454 impl->queued_buffers--;
455 impl->SignalEvent(impl->playing_buffer);
456 }
457 }
458
459 // Get the minimum frames available between the currently playing buffer, and the
460 // amount we have left to fill
461 size_t frames_available{
462 std::min(playing_buffer.frames - playing_buffer.frames_played,
463 num_frames - frames_written)};
464
465 impl->samples_buffer.Pop(&output_buffer[frames_written * frame_size],
466 frames_available * frame_size);
467
468 frames_written += frames_available;
469 playing_buffer.frames_played += frames_available;
470
471 // If that's all the frames in the current buffer, add its samples and mark it as
472 // consumed
473 if (playing_buffer.frames_played >= playing_buffer.frames) {
474 impl->AddPlayedSampleCount(playing_buffer.frames_played * num_channels);
475 impl->playing_buffer.consumed = true;
476 }
477 }
478
479 std::memcpy(&impl->last_frame[0], &output_buffer[(frames_written - 1) * frame_size],
480 frame_size_bytes);
481 } 196 }
482 197
483 return num_frames_; 198 return num_frames_;
@@ -490,32 +205,12 @@ private:
490 * @param user_data - Custom data pointer passed along, points to a CubebSinkStream. 205 * @param user_data - Custom data pointer passed along, points to a CubebSinkStream.
491 * @param state - New state of the device. 206 * @param state - New state of the device.
492 */ 207 */
493 static void StateCallback([[maybe_unused]] cubeb_stream* stream, 208 static void StateCallback(cubeb_stream*, void*, cubeb_state) {}
494 [[maybe_unused]] void* user_data,
495 [[maybe_unused]] cubeb_state state) {}
496 209
497 /// Main Cubeb context 210 /// Main Cubeb context
498 cubeb* ctx{}; 211 cubeb* ctx{};
499 /// Cubeb stream backend 212 /// Cubeb stream backend
500 cubeb_stream* stream_backend{}; 213 cubeb_stream* stream_backend{};
501 /// Name of this stream
502 std::string name{};
503 /// Type of this stream
504 StreamType type;
505 /// Core system
506 Core::System& system;
507 /// Ring buffer of the samples waiting to be played or consumed
508 Common::RingBuffer<s16, 0x10000> samples_buffer;
509 /// Audio buffers queued and waiting to play
510 Common::ReaderWriterQueue<::AudioCore::Sink::SinkBuffer> queue;
511 /// The currently-playing audio buffer
512 ::AudioCore::Sink::SinkBuffer playing_buffer{};
513 /// Audio buffers which have been played and are in queue to be released by the audio system
514 Common::ReaderWriterQueue<::AudioCore::Sink::SinkBuffer> released_buffers{};
515 /// Currently released buffer waiting to be taken by the audio system
516 ::AudioCore::Sink::SinkBuffer released_buffer{};
517 /// The last played (or received) frame of audio, used when the callback underruns
518 std::array<s16, MaxChannels> last_frame{};
519}; 214};
520 215
521CubebSink::CubebSink(std::string_view target_device_name) { 216CubebSink::CubebSink(std::string_view target_device_name) {
@@ -569,15 +264,15 @@ CubebSink::~CubebSink() {
569#endif 264#endif
570} 265}
571 266
572SinkStream* CubebSink::AcquireSinkStream(Core::System& system, const u32 system_channels, 267SinkStream* CubebSink::AcquireSinkStream(Core::System& system, u32 system_channels,
573 const std::string& name, const StreamType type) { 268 const std::string& name, StreamType type) {
574 SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<CubebSinkStream>( 269 SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<CubebSinkStream>(
575 ctx, device_channels, system_channels, output_device, input_device, name, type, system)); 270 ctx, device_channels, system_channels, output_device, input_device, name, type, system));
576 271
577 return stream.get(); 272 return stream.get();
578} 273}
579 274
580void CubebSink::CloseStream(const SinkStream* stream) { 275void CubebSink::CloseStream(SinkStream* stream) {
581 for (size_t i = 0; i < sink_streams.size(); i++) { 276 for (size_t i = 0; i < sink_streams.size(); i++) {
582 if (sink_streams[i].get() == stream) { 277 if (sink_streams[i].get() == stream) {
583 sink_streams[i].reset(); 278 sink_streams[i].reset();
@@ -611,19 +306,19 @@ f32 CubebSink::GetDeviceVolume() const {
611 return sink_streams[0]->GetDeviceVolume(); 306 return sink_streams[0]->GetDeviceVolume();
612} 307}
613 308
614void CubebSink::SetDeviceVolume(const f32 volume) { 309void CubebSink::SetDeviceVolume(f32 volume) {
615 for (auto& stream : sink_streams) { 310 for (auto& stream : sink_streams) {
616 stream->SetDeviceVolume(volume); 311 stream->SetDeviceVolume(volume);
617 } 312 }
618} 313}
619 314
620void CubebSink::SetSystemVolume(const f32 volume) { 315void CubebSink::SetSystemVolume(f32 volume) {
621 for (auto& stream : sink_streams) { 316 for (auto& stream : sink_streams) {
622 stream->SetSystemVolume(volume); 317 stream->SetSystemVolume(volume);
623 } 318 }
624} 319}
625 320
626std::vector<std::string> ListCubebSinkDevices(const bool capture) { 321std::vector<std::string> ListCubebSinkDevices(bool capture) {
627 std::vector<std::string> device_list; 322 std::vector<std::string> device_list;
628 cubeb* ctx; 323 cubeb* ctx;
629 324
diff --git a/src/audio_core/sink/cubeb_sink.h b/src/audio_core/sink/cubeb_sink.h
index f0f43dfa1..91a6480fa 100644
--- a/src/audio_core/sink/cubeb_sink.h
+++ b/src/audio_core/sink/cubeb_sink.h
@@ -46,7 +46,7 @@ public:
46 * 46 *
47 * @param stream - The stream to close. 47 * @param stream - The stream to close.
48 */ 48 */
49 void CloseStream(const SinkStream* stream) override; 49 void CloseStream(SinkStream* stream) override;
50 50
51 /** 51 /**
52 * Close all streams. 52 * Close all streams.
diff --git a/src/audio_core/sink/null_sink.h b/src/audio_core/sink/null_sink.h
index 47a342171..eab9c3a0c 100644
--- a/src/audio_core/sink/null_sink.h
+++ b/src/audio_core/sink/null_sink.h
@@ -3,10 +3,29 @@
3 3
4#pragma once 4#pragma once
5 5
6#include <string>
7#include <string_view>
8#include <vector>
9
6#include "audio_core/sink/sink.h" 10#include "audio_core/sink/sink.h"
7#include "audio_core/sink/sink_stream.h" 11#include "audio_core/sink/sink_stream.h"
8 12
13namespace Core {
14class System;
15} // namespace Core
16
9namespace AudioCore::Sink { 17namespace AudioCore::Sink {
18class NullSinkStreamImpl final : public SinkStream {
19public:
20 explicit NullSinkStreamImpl(Core::System& system_, StreamType type_)
21 : SinkStream{system_, type_} {}
22 ~NullSinkStreamImpl() override {}
23 void AppendBuffer(SinkBuffer&, std::vector<s16>&) override {}
24 std::vector<s16> ReleaseBuffer(u64) override {
25 return {};
26 }
27};
28
10/** 29/**
11 * A no-op sink for when no audio out is wanted. 30 * A no-op sink for when no audio out is wanted.
12 */ 31 */
@@ -15,14 +34,15 @@ public:
15 explicit NullSink(std::string_view) {} 34 explicit NullSink(std::string_view) {}
16 ~NullSink() override = default; 35 ~NullSink() override = default;
17 36
18 SinkStream* AcquireSinkStream([[maybe_unused]] Core::System& system, 37 SinkStream* AcquireSinkStream(Core::System& system, u32, const std::string&,
19 [[maybe_unused]] u32 system_channels, 38 StreamType type) override {
20 [[maybe_unused]] const std::string& name, 39 if (null_sink == nullptr) {
21 [[maybe_unused]] StreamType type) override { 40 null_sink = std::make_unique<NullSinkStreamImpl>(system, type);
22 return &null_sink_stream; 41 }
42 return null_sink.get();
23 } 43 }
24 44
25 void CloseStream([[maybe_unused]] const SinkStream* stream) override {} 45 void CloseStream(SinkStream*) override {}
26 void CloseStreams() override {} 46 void CloseStreams() override {}
27 void PauseStreams() override {} 47 void PauseStreams() override {}
28 void UnpauseStreams() override {} 48 void UnpauseStreams() override {}
@@ -33,20 +53,7 @@ public:
33 void SetSystemVolume(f32 volume) override {} 53 void SetSystemVolume(f32 volume) override {}
34 54
35private: 55private:
36 struct NullSinkStreamImpl final : SinkStream { 56 SinkStreamPtr null_sink{};
37 void Finalize() override {}
38 void Start(bool resume = false) override {}
39 void Stop() override {}
40 void AppendBuffer([[maybe_unused]] ::AudioCore::Sink::SinkBuffer& buffer,
41 [[maybe_unused]] std::vector<s16>& samples) override {}
42 std::vector<s16> ReleaseBuffer([[maybe_unused]] u64 num_samples) override {
43 return {};
44 }
45 bool IsBufferConsumed([[maybe_unused]] const u64 tag) {
46 return true;
47 }
48 void ClearQueue() override {}
49 } null_sink_stream;
50}; 57};
51 58
52} // namespace AudioCore::Sink 59} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/sdl2_sink.cpp b/src/audio_core/sink/sdl2_sink.cpp
index d6c9ec90d..7ee1dd7cd 100644
--- a/src/audio_core/sink/sdl2_sink.cpp
+++ b/src/audio_core/sink/sdl2_sink.cpp
@@ -1,20 +1,13 @@
1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project 1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later 2// SPDX-License-Identifier: GPL-2.0-or-later
3 3
4#include <algorithm> 4#include <span>
5#include <atomic> 5#include <vector>
6 6
7#include "audio_core/audio_core.h" 7#include "audio_core/common/common.h"
8#include "audio_core/audio_event.h"
9#include "audio_core/audio_manager.h"
10#include "audio_core/sink/sdl2_sink.h" 8#include "audio_core/sink/sdl2_sink.h"
11#include "audio_core/sink/sink_stream.h" 9#include "audio_core/sink/sink_stream.h"
12#include "common/assert.h"
13#include "common/fixed_point.h"
14#include "common/logging/log.h" 10#include "common/logging/log.h"
15#include "common/reader_writer_queue.h"
16#include "common/ring_buffer.h"
17#include "common/settings.h"
18#include "core/core.h" 11#include "core/core.h"
19 12
20// Ignore -Wimplicit-fallthrough due to https://github.com/libsdl-org/SDL/issues/4307 13// Ignore -Wimplicit-fallthrough due to https://github.com/libsdl-org/SDL/issues/4307
@@ -44,10 +37,9 @@ public:
44 * @param system_ - Core system. 37 * @param system_ - Core system.
45 * @param event - Event used only for audio renderer, signalled on buffer consume. 38 * @param event - Event used only for audio renderer, signalled on buffer consume.
46 */ 39 */
47 SDLSinkStream(u32 device_channels_, const u32 system_channels_, 40 SDLSinkStream(u32 device_channels_, u32 system_channels_, const std::string& output_device,
48 const std::string& output_device, const std::string& input_device, 41 const std::string& input_device, StreamType type_, Core::System& system_)
49 const StreamType type_, Core::System& system_) 42 : SinkStream{system_, type_} {
50 : type{type_}, system{system_} {
51 system_channels = system_channels_; 43 system_channels = system_channels_;
52 device_channels = device_channels_; 44 device_channels = device_channels_;
53 45
@@ -63,8 +55,6 @@ public:
63 spec.callback = &SDLSinkStream::DataCallback; 55 spec.callback = &SDLSinkStream::DataCallback;
64 spec.userdata = this; 56 spec.userdata = this;
65 57
66 playing_buffer.consumed = true;
67
68 std::string device_name{output_device}; 58 std::string device_name{output_device};
69 bool capture{false}; 59 bool capture{false};
70 if (type == StreamType::In) { 60 if (type == StreamType::In) {
@@ -84,31 +74,30 @@ public:
84 return; 74 return;
85 } 75 }
86 76
87 LOG_DEBUG(Service_Audio, 77 LOG_INFO(Service_Audio,
88 "Opening sdl stream {} with: rate {} channels {} (system channels {}) " 78 "Opening SDL stream {} with: rate {} channels {} (system channels {}) "
89 " samples {}", 79 " samples {}",
90 device, obtained.freq, obtained.channels, system_channels, obtained.samples); 80 device, obtained.freq, obtained.channels, system_channels, obtained.samples);
91 } 81 }
92 82
93 /** 83 /**
94 * Destroy the sink stream. 84 * Destroy the sink stream.
95 */ 85 */
96 ~SDLSinkStream() override { 86 ~SDLSinkStream() override {
97 if (device == 0) { 87 LOG_DEBUG(Service_Audio, "Destructing SDL stream {}", name);
98 return; 88 Finalize();
99 }
100
101 SDL_CloseAudioDevice(device);
102 } 89 }
103 90
104 /** 91 /**
105 * Finalize the sink stream. 92 * Finalize the sink stream.
106 */ 93 */
107 void Finalize() override { 94 void Finalize() override {
95 Unstall();
108 if (device == 0) { 96 if (device == 0) {
109 return; 97 return;
110 } 98 }
111 99
100 Stop();
112 SDL_CloseAudioDevice(device); 101 SDL_CloseAudioDevice(device);
113 } 102 }
114 103
@@ -118,7 +107,7 @@ public:
118 * @param resume - Set to true if this is resuming the stream a previously-active stream. 107 * @param resume - Set to true if this is resuming the stream a previously-active stream.
119 * Default false. 108 * Default false.
120 */ 109 */
121 void Start(const bool resume = false) override { 110 void Start(bool resume = false) override {
122 if (device == 0) { 111 if (device == 0) {
123 return; 112 return;
124 } 113 }
@@ -135,7 +124,8 @@ public:
135 /** 124 /**
136 * Stop the sink stream. 125 * Stop the sink stream.
137 */ 126 */
138 void Stop() { 127 void Stop() override {
128 Unstall();
139 if (device == 0) { 129 if (device == 0) {
140 return; 130 return;
141 } 131 }
@@ -143,192 +133,8 @@ public:
143 paused = true; 133 paused = true;
144 } 134 }
145 135
146 /**
147 * Append a new buffer and its samples to a waiting queue to play.
148 *
149 * @param buffer - Audio buffer information to be queued.
150 * @param samples - The s16 samples to be queue for playback.
151 */
152 void AppendBuffer(::AudioCore::Sink::SinkBuffer& buffer, std::vector<s16>& samples) override {
153 if (type == StreamType::In) {
154 queue.enqueue(buffer);
155 queued_buffers++;
156 } else {
157 constexpr s32 min = std::numeric_limits<s16>::min();
158 constexpr s32 max = std::numeric_limits<s16>::max();
159
160 auto yuzu_volume{Settings::Volume()};
161 auto volume{system_volume * device_volume * yuzu_volume};
162
163 if (system_channels == 6 && device_channels == 2) {
164 // We're given 6 channels, but our device only outputs 2, so downmix.
165 constexpr std::array<f32, 4> down_mix_coeff{1.0f, 0.707f, 0.251f, 0.707f};
166
167 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
168 read_index += system_channels, write_index += device_channels) {
169 const auto left_sample{
170 ((Common::FixedPoint<49, 15>(
171 samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
172 down_mix_coeff[0] +
173 samples[read_index + static_cast<u32>(Channels::Center)] *
174 down_mix_coeff[1] +
175 samples[read_index + static_cast<u32>(Channels::LFE)] *
176 down_mix_coeff[2] +
177 samples[read_index + static_cast<u32>(Channels::BackLeft)] *
178 down_mix_coeff[3]) *
179 volume)
180 .to_int()};
181
182 const auto right_sample{
183 ((Common::FixedPoint<49, 15>(
184 samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
185 down_mix_coeff[0] +
186 samples[read_index + static_cast<u32>(Channels::Center)] *
187 down_mix_coeff[1] +
188 samples[read_index + static_cast<u32>(Channels::LFE)] *
189 down_mix_coeff[2] +
190 samples[read_index + static_cast<u32>(Channels::BackRight)] *
191 down_mix_coeff[3]) *
192 volume)
193 .to_int()};
194
195 samples[write_index + static_cast<u32>(Channels::FrontLeft)] =
196 static_cast<s16>(std::clamp(left_sample, min, max));
197 samples[write_index + static_cast<u32>(Channels::FrontRight)] =
198 static_cast<s16>(std::clamp(right_sample, min, max));
199 }
200
201 samples.resize(samples.size() / system_channels * device_channels);
202
203 } else if (system_channels == 2 && device_channels == 6) {
204 // We need moar samples! Not all games will provide 6 channel audio.
205 // TODO: Implement some upmixing here. Currently just passthrough, with other
206 // channels left as silence.
207 std::vector<s16> new_samples(samples.size() / system_channels * device_channels, 0);
208
209 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
210 read_index += system_channels, write_index += device_channels) {
211 const auto left_sample{static_cast<s16>(std::clamp(
212 static_cast<s32>(
213 static_cast<f32>(
214 samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
215 volume),
216 min, max))};
217
218 new_samples[write_index + static_cast<u32>(Channels::FrontLeft)] = left_sample;
219
220 const auto right_sample{static_cast<s16>(std::clamp(
221 static_cast<s32>(
222 static_cast<f32>(
223 samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
224 volume),
225 min, max))};
226
227 new_samples[write_index + static_cast<u32>(Channels::FrontRight)] =
228 right_sample;
229 }
230 samples = std::move(new_samples);
231
232 } else if (volume != 1.0f) {
233 for (u32 i = 0; i < samples.size(); i++) {
234 samples[i] = static_cast<s16>(std::clamp(
235 static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
236 }
237 }
238
239 samples_buffer.Push(samples);
240 queue.enqueue(buffer);
241 queued_buffers++;
242 }
243 }
244
245 /**
246 * Release a buffer. Audio In only, will fill a buffer with recorded samples.
247 *
248 * @param num_samples - Maximum number of samples to receive.
249 * @return Vector of recorded samples. May have fewer than num_samples.
250 */
251 std::vector<s16> ReleaseBuffer(const u64 num_samples) override {
252 static constexpr s32 min = std::numeric_limits<s16>::min();
253 static constexpr s32 max = std::numeric_limits<s16>::max();
254
255 auto samples{samples_buffer.Pop(num_samples)};
256
257 // TODO: Up-mix to 6 channels if the game expects it.
258 // For audio input this is unlikely to ever be the case though.
259
260 // Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
261 // TODO: Play with this and find something that works better.
262 auto volume{system_volume * device_volume * 8};
263 for (u32 i = 0; i < samples.size(); i++) {
264 samples[i] = static_cast<s16>(
265 std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
266 }
267
268 if (samples.size() < num_samples) {
269 samples.resize(num_samples, 0);
270 }
271 return samples;
272 }
273
274 /**
275 * Check if a certain buffer has been consumed (fully played).
276 *
277 * @param tag - Unique tag of a buffer to check for.
278 * @return True if the buffer has been played, otherwise false.
279 */
280 bool IsBufferConsumed(const u64 tag) override {
281 if (released_buffer.tag == 0) {
282 if (!released_buffers.try_dequeue(released_buffer)) {
283 return false;
284 }
285 }
286
287 if (released_buffer.tag == tag) {
288 released_buffer.tag = 0;
289 return true;
290 }
291 return false;
292 }
293
294 /**
295 * Empty out the buffer queue.
296 */
297 void ClearQueue() override {
298 samples_buffer.Pop();
299 while (queue.pop()) {
300 }
301 while (released_buffers.pop()) {
302 }
303 released_buffer = {};
304 playing_buffer = {};
305 playing_buffer.consumed = true;
306 queued_buffers = 0;
307 }
308
309private: 136private:
310 /** 137 /**
311 * Signal events back to the audio system that a buffer was played/can be filled.
312 *
313 * @param buffer - Consumed audio buffer to be released.
314 */
315 void SignalEvent(const ::AudioCore::Sink::SinkBuffer& buffer) {
316 auto& manager{system.AudioCore().GetAudioManager()};
317 switch (type) {
318 case StreamType::Out:
319 released_buffers.enqueue(buffer);
320 manager.SetEvent(Event::Type::AudioOutManager, true);
321 break;
322 case StreamType::In:
323 released_buffers.enqueue(buffer);
324 manager.SetEvent(Event::Type::AudioInManager, true);
325 break;
326 case StreamType::Render:
327 break;
328 }
329 }
330
331 /**
332 * Main callback from SDL. Either expects samples from us (audio render/audio out), or will 138 * Main callback from SDL. Either expects samples from us (audio render/audio out), or will
333 * provide samples to be copied (audio in). 139 * provide samples to be copied (audio in).
334 * 140 *
@@ -345,122 +151,20 @@ private:
345 151
346 const std::size_t num_channels = impl->GetDeviceChannels(); 152 const std::size_t num_channels = impl->GetDeviceChannels();
347 const std::size_t frame_size = num_channels; 153 const std::size_t frame_size = num_channels;
348 const std::size_t frame_size_bytes = frame_size * sizeof(s16);
349 const std::size_t num_frames{len / num_channels / sizeof(s16)}; 154 const std::size_t num_frames{len / num_channels / sizeof(s16)};
350 size_t frames_written{0};
351 [[maybe_unused]] bool underrun{false};
352 155
353 if (impl->type == StreamType::In) { 156 if (impl->type == StreamType::In) {
354 std::span<s16> input_buffer{reinterpret_cast<s16*>(stream), num_frames * frame_size}; 157 std::span<const s16> input_buffer{reinterpret_cast<const s16*>(stream),
355 158 num_frames * frame_size};
356 while (frames_written < num_frames) { 159 impl->ProcessAudioIn(input_buffer, num_frames);
357 auto& playing_buffer{impl->playing_buffer};
358
359 // If the playing buffer has been consumed or has no frames, we need a new one
360 if (playing_buffer.consumed || playing_buffer.frames == 0) {
361 if (!impl->queue.try_dequeue(impl->playing_buffer)) {
362 // If no buffer was available we've underrun, just push the samples and
363 // continue.
364 underrun = true;
365 impl->samples_buffer.Push(&input_buffer[frames_written * frame_size],
366 (num_frames - frames_written) * frame_size);
367 frames_written = num_frames;
368 continue;
369 } else {
370 impl->queued_buffers--;
371 impl->SignalEvent(impl->playing_buffer);
372 }
373 }
374
375 // Get the minimum frames available between the currently playing buffer, and the
376 // amount we have left to fill
377 size_t frames_available{
378 std::min(playing_buffer.frames - playing_buffer.frames_played,
379 num_frames - frames_written)};
380
381 impl->samples_buffer.Push(&input_buffer[frames_written * frame_size],
382 frames_available * frame_size);
383
384 frames_written += frames_available;
385 playing_buffer.frames_played += frames_available;
386
387 // If that's all the frames in the current buffer, add its samples and mark it as
388 // consumed
389 if (playing_buffer.frames_played >= playing_buffer.frames) {
390 impl->AddPlayedSampleCount(playing_buffer.frames_played * num_channels);
391 impl->playing_buffer.consumed = true;
392 }
393 }
394
395 std::memcpy(&impl->last_frame[0], &input_buffer[(frames_written - 1) * frame_size],
396 frame_size_bytes);
397 } else { 160 } else {
398 std::span<s16> output_buffer{reinterpret_cast<s16*>(stream), num_frames * frame_size}; 161 std::span<s16> output_buffer{reinterpret_cast<s16*>(stream), num_frames * frame_size};
399 162 impl->ProcessAudioOutAndRender(output_buffer, num_frames);
400 while (frames_written < num_frames) {
401 auto& playing_buffer{impl->playing_buffer};
402
403 // If the playing buffer has been consumed or has no frames, we need a new one
404 if (playing_buffer.consumed || playing_buffer.frames == 0) {
405 if (!impl->queue.try_dequeue(impl->playing_buffer)) {
406 // If no buffer was available we've underrun, fill the remaining buffer with
407 // the last written frame and continue.
408 underrun = true;
409 for (size_t i = frames_written; i < num_frames; i++) {
410 std::memcpy(&output_buffer[i * frame_size], &impl->last_frame[0],
411 frame_size_bytes);
412 }
413 frames_written = num_frames;
414 continue;
415 } else {
416 impl->queued_buffers--;
417 impl->SignalEvent(impl->playing_buffer);
418 }
419 }
420
421 // Get the minimum frames available between the currently playing buffer, and the
422 // amount we have left to fill
423 size_t frames_available{
424 std::min(playing_buffer.frames - playing_buffer.frames_played,
425 num_frames - frames_written)};
426
427 impl->samples_buffer.Pop(&output_buffer[frames_written * frame_size],
428 frames_available * frame_size);
429
430 frames_written += frames_available;
431 playing_buffer.frames_played += frames_available;
432
433 // If that's all the frames in the current buffer, add its samples and mark it as
434 // consumed
435 if (playing_buffer.frames_played >= playing_buffer.frames) {
436 impl->AddPlayedSampleCount(playing_buffer.frames_played * num_channels);
437 impl->playing_buffer.consumed = true;
438 }
439 }
440
441 std::memcpy(&impl->last_frame[0], &output_buffer[(frames_written - 1) * frame_size],
442 frame_size_bytes);
443 } 163 }
444 } 164 }
445 165
446 /// SDL device id of the opened input/output device 166 /// SDL device id of the opened input/output device
447 SDL_AudioDeviceID device{}; 167 SDL_AudioDeviceID device{};
448 /// Type of this stream
449 StreamType type;
450 /// Core system
451 Core::System& system;
452 /// Ring buffer of the samples waiting to be played or consumed
453 Common::RingBuffer<s16, 0x10000> samples_buffer;
454 /// Audio buffers queued and waiting to play
455 Common::ReaderWriterQueue<::AudioCore::Sink::SinkBuffer> queue;
456 /// The currently-playing audio buffer
457 ::AudioCore::Sink::SinkBuffer playing_buffer{};
458 /// Audio buffers which have been played and are in queue to be released by the audio system
459 Common::ReaderWriterQueue<::AudioCore::Sink::SinkBuffer> released_buffers{};
460 /// Currently released buffer waiting to be taken by the audio system
461 ::AudioCore::Sink::SinkBuffer released_buffer{};
462 /// The last played (or received) frame of audio, used when the callback underruns
463 std::array<s16, MaxChannels> last_frame{};
464}; 168};
465 169
466SDLSink::SDLSink(std::string_view target_device_name) { 170SDLSink::SDLSink(std::string_view target_device_name) {
@@ -482,14 +186,14 @@ SDLSink::SDLSink(std::string_view target_device_name) {
482 186
483SDLSink::~SDLSink() = default; 187SDLSink::~SDLSink() = default;
484 188
485SinkStream* SDLSink::AcquireSinkStream(Core::System& system, const u32 system_channels, 189SinkStream* SDLSink::AcquireSinkStream(Core::System& system, u32 system_channels,
486 const std::string&, const StreamType type) { 190 const std::string&, StreamType type) {
487 SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<SDLSinkStream>( 191 SinkStreamPtr& stream = sink_streams.emplace_back(std::make_unique<SDLSinkStream>(
488 device_channels, system_channels, output_device, input_device, type, system)); 192 device_channels, system_channels, output_device, input_device, type, system));
489 return stream.get(); 193 return stream.get();
490} 194}
491 195
492void SDLSink::CloseStream(const SinkStream* stream) { 196void SDLSink::CloseStream(SinkStream* stream) {
493 for (size_t i = 0; i < sink_streams.size(); i++) { 197 for (size_t i = 0; i < sink_streams.size(); i++) {
494 if (sink_streams[i].get() == stream) { 198 if (sink_streams[i].get() == stream) {
495 sink_streams[i].reset(); 199 sink_streams[i].reset();
@@ -523,19 +227,19 @@ f32 SDLSink::GetDeviceVolume() const {
523 return sink_streams[0]->GetDeviceVolume(); 227 return sink_streams[0]->GetDeviceVolume();
524} 228}
525 229
526void SDLSink::SetDeviceVolume(const f32 volume) { 230void SDLSink::SetDeviceVolume(f32 volume) {
527 for (auto& stream : sink_streams) { 231 for (auto& stream : sink_streams) {
528 stream->SetDeviceVolume(volume); 232 stream->SetDeviceVolume(volume);
529 } 233 }
530} 234}
531 235
532void SDLSink::SetSystemVolume(const f32 volume) { 236void SDLSink::SetSystemVolume(f32 volume) {
533 for (auto& stream : sink_streams) { 237 for (auto& stream : sink_streams) {
534 stream->SetSystemVolume(volume); 238 stream->SetSystemVolume(volume);
535 } 239 }
536} 240}
537 241
538std::vector<std::string> ListSDLSinkDevices(const bool capture) { 242std::vector<std::string> ListSDLSinkDevices(bool capture) {
539 std::vector<std::string> device_list; 243 std::vector<std::string> device_list;
540 244
541 if (!SDL_WasInit(SDL_INIT_AUDIO)) { 245 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
diff --git a/src/audio_core/sink/sdl2_sink.h b/src/audio_core/sink/sdl2_sink.h
index 186bc2fa3..57de9b6c2 100644
--- a/src/audio_core/sink/sdl2_sink.h
+++ b/src/audio_core/sink/sdl2_sink.h
@@ -44,7 +44,7 @@ public:
44 * 44 *
45 * @param stream - The stream to close. 45 * @param stream - The stream to close.
46 */ 46 */
47 void CloseStream(const SinkStream* stream) override; 47 void CloseStream(SinkStream* stream) override;
48 48
49 /** 49 /**
50 * Close all streams. 50 * Close all streams.
diff --git a/src/audio_core/sink/sink.h b/src/audio_core/sink/sink.h
index 91fe455e4..43d99b62e 100644
--- a/src/audio_core/sink/sink.h
+++ b/src/audio_core/sink/sink.h
@@ -32,7 +32,7 @@ public:
32 * 32 *
33 * @param stream - The stream to close. 33 * @param stream - The stream to close.
34 */ 34 */
35 virtual void CloseStream(const SinkStream* stream) = 0; 35 virtual void CloseStream(SinkStream* stream) = 0;
36 36
37 /** 37 /**
38 * Close all streams. 38 * Close all streams.
diff --git a/src/audio_core/sink/sink_details.cpp b/src/audio_core/sink/sink_details.cpp
index 253c0fd1e..67bdab779 100644
--- a/src/audio_core/sink/sink_details.cpp
+++ b/src/audio_core/sink/sink_details.cpp
@@ -5,7 +5,7 @@
5#include <memory> 5#include <memory>
6#include <string> 6#include <string>
7#include <vector> 7#include <vector>
8#include "audio_core/sink/null_sink.h" 8
9#include "audio_core/sink/sink_details.h" 9#include "audio_core/sink/sink_details.h"
10#ifdef HAVE_CUBEB 10#ifdef HAVE_CUBEB
11#include "audio_core/sink/cubeb_sink.h" 11#include "audio_core/sink/cubeb_sink.h"
@@ -13,6 +13,7 @@
13#ifdef HAVE_SDL2 13#ifdef HAVE_SDL2
14#include "audio_core/sink/sdl2_sink.h" 14#include "audio_core/sink/sdl2_sink.h"
15#endif 15#endif
16#include "audio_core/sink/null_sink.h"
16#include "common/logging/log.h" 17#include "common/logging/log.h"
17 18
18namespace AudioCore::Sink { 19namespace AudioCore::Sink {
@@ -59,8 +60,7 @@ const SinkDetails& GetOutputSinkDetails(std::string_view sink_id) {
59 60
60 if (sink_id == "auto" || iter == std::end(sink_details)) { 61 if (sink_id == "auto" || iter == std::end(sink_details)) {
61 if (sink_id != "auto") { 62 if (sink_id != "auto") {
62 LOG_ERROR(Audio, "AudioCore::Sink::GetOutputSinkDetails given invalid sink_id {}", 63 LOG_ERROR(Audio, "Invalid sink_id {}", sink_id);
63 sink_id);
64 } 64 }
65 // Auto-select. 65 // Auto-select.
66 // sink_details is ordered in terms of desirability, with the best choice at the front. 66 // sink_details is ordered in terms of desirability, with the best choice at the front.
diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp
new file mode 100644
index 000000000..3770c515d
--- /dev/null
+++ b/src/audio_core/sink/sink_stream.cpp
@@ -0,0 +1,259 @@
1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <array>
7#include <atomic>
8#include <memory>
9#include <span>
10#include <vector>
11
12#include "audio_core/common/common.h"
13#include "audio_core/sink/sink_stream.h"
14#include "common/common_types.h"
15#include "common/fixed_point.h"
16#include "common/settings.h"
17#include "core/core.h"
18
19namespace AudioCore::Sink {
20
21void SinkStream::AppendBuffer(SinkBuffer& buffer, std::vector<s16>& samples) {
22 if (type == StreamType::In) {
23 queue.enqueue(buffer);
24 queued_buffers++;
25 return;
26 }
27
28 constexpr s32 min{std::numeric_limits<s16>::min()};
29 constexpr s32 max{std::numeric_limits<s16>::max()};
30
31 auto yuzu_volume{Settings::Volume()};
32 if (yuzu_volume > 1.0f) {
33 yuzu_volume = 0.6f + 20 * std::log10(yuzu_volume);
34 }
35 auto volume{system_volume * device_volume * yuzu_volume};
36
37 if (system_channels == 6 && device_channels == 2) {
38 // We're given 6 channels, but our device only outputs 2, so downmix.
39 constexpr std::array<f32, 4> down_mix_coeff{1.0f, 0.707f, 0.251f, 0.707f};
40
41 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
42 read_index += system_channels, write_index += device_channels) {
43 const auto left_sample{
44 ((Common::FixedPoint<49, 15>(
45 samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
46 down_mix_coeff[0] +
47 samples[read_index + static_cast<u32>(Channels::Center)] * down_mix_coeff[1] +
48 samples[read_index + static_cast<u32>(Channels::LFE)] * down_mix_coeff[2] +
49 samples[read_index + static_cast<u32>(Channels::BackLeft)] * down_mix_coeff[3]) *
50 volume)
51 .to_int()};
52
53 const auto right_sample{
54 ((Common::FixedPoint<49, 15>(
55 samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
56 down_mix_coeff[0] +
57 samples[read_index + static_cast<u32>(Channels::Center)] * down_mix_coeff[1] +
58 samples[read_index + static_cast<u32>(Channels::LFE)] * down_mix_coeff[2] +
59 samples[read_index + static_cast<u32>(Channels::BackRight)] * down_mix_coeff[3]) *
60 volume)
61 .to_int()};
62
63 samples[write_index + static_cast<u32>(Channels::FrontLeft)] =
64 static_cast<s16>(std::clamp(left_sample, min, max));
65 samples[write_index + static_cast<u32>(Channels::FrontRight)] =
66 static_cast<s16>(std::clamp(right_sample, min, max));
67 }
68
69 samples.resize(samples.size() / system_channels * device_channels);
70
71 } else if (system_channels == 2 && device_channels == 6) {
72 // We need moar samples! Not all games will provide 6 channel audio.
73 // TODO: Implement some upmixing here. Currently just passthrough, with other
74 // channels left as silence.
75 std::vector<s16> new_samples(samples.size() / system_channels * device_channels, 0);
76
77 for (u32 read_index = 0, write_index = 0; read_index < samples.size();
78 read_index += system_channels, write_index += device_channels) {
79 const auto left_sample{static_cast<s16>(std::clamp(
80 static_cast<s32>(
81 static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
82 volume),
83 min, max))};
84
85 new_samples[write_index + static_cast<u32>(Channels::FrontLeft)] = left_sample;
86
87 const auto right_sample{static_cast<s16>(std::clamp(
88 static_cast<s32>(
89 static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
90 volume),
91 min, max))};
92
93 new_samples[write_index + static_cast<u32>(Channels::FrontRight)] = right_sample;
94 }
95 samples = std::move(new_samples);
96
97 } else if (volume != 1.0f) {
98 for (u32 i = 0; i < samples.size(); i++) {
99 samples[i] = static_cast<s16>(
100 std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
101 }
102 }
103
104 samples_buffer.Push(samples);
105 queue.enqueue(buffer);
106 queued_buffers++;
107}
108
109std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
110 constexpr s32 min = std::numeric_limits<s16>::min();
111 constexpr s32 max = std::numeric_limits<s16>::max();
112
113 auto samples{samples_buffer.Pop(num_samples)};
114
115 // TODO: Up-mix to 6 channels if the game expects it.
116 // For audio input this is unlikely to ever be the case though.
117
118 // Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
119 // TODO: Play with this and find something that works better.
120 auto volume{system_volume * device_volume * 8};
121 for (u32 i = 0; i < samples.size(); i++) {
122 samples[i] = static_cast<s16>(
123 std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
124 }
125
126 if (samples.size() < num_samples) {
127 samples.resize(num_samples, 0);
128 }
129 return samples;
130}
131
132void SinkStream::ClearQueue() {
133 samples_buffer.Pop();
134 while (queue.pop()) {
135 }
136 queued_buffers = 0;
137 playing_buffer = {};
138 playing_buffer.consumed = true;
139}
140
141void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t num_frames) {
142 const std::size_t num_channels = GetDeviceChannels();
143 const std::size_t frame_size = num_channels;
144 const std::size_t frame_size_bytes = frame_size * sizeof(s16);
145 size_t frames_written{0};
146
147 if (queued_buffers > max_queue_size) {
148 Stall();
149 }
150
151 while (frames_written < num_frames) {
152 // If the playing buffer has been consumed or has no frames, we need a new one
153 if (playing_buffer.consumed || playing_buffer.frames == 0) {
154 if (!queue.try_dequeue(playing_buffer)) {
155 // If no buffer was available we've underrun, just push the samples and
156 // continue.
157 samples_buffer.Push(&input_buffer[frames_written * frame_size],
158 (num_frames - frames_written) * frame_size);
159 frames_written = num_frames;
160 continue;
161 }
162 // Successfully dequeued a new buffer.
163 queued_buffers--;
164 }
165
166 // Get the minimum frames available between the currently playing buffer, and the
167 // amount we have left to fill
168 size_t frames_available{std::min(playing_buffer.frames - playing_buffer.frames_played,
169 num_frames - frames_written)};
170
171 samples_buffer.Push(&input_buffer[frames_written * frame_size],
172 frames_available * frame_size);
173
174 frames_written += frames_available;
175 playing_buffer.frames_played += frames_available;
176
177 // If that's all the frames in the current buffer, add its samples and mark it as
178 // consumed
179 if (playing_buffer.frames_played >= playing_buffer.frames) {
180 playing_buffer.consumed = true;
181 }
182 }
183
184 std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes);
185
186 if (queued_buffers <= max_queue_size) {
187 Unstall();
188 }
189}
190
191void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames) {
192 const std::size_t num_channels = GetDeviceChannels();
193 const std::size_t frame_size = num_channels;
194 const std::size_t frame_size_bytes = frame_size * sizeof(s16);
195 size_t frames_written{0};
196
197 if (queued_buffers > max_queue_size) {
198 Stall();
199 }
200
201 while (frames_written < num_frames) {
202 // If the playing buffer has been consumed or has no frames, we need a new one
203 if (playing_buffer.consumed || playing_buffer.frames == 0) {
204 if (!queue.try_dequeue(playing_buffer)) {
205 // If no buffer was available we've underrun, fill the remaining buffer with
206 // the last written frame and continue.
207 for (size_t i = frames_written; i < num_frames; i++) {
208 std::memcpy(&output_buffer[i * frame_size], &last_frame[0], frame_size_bytes);
209 }
210 frames_written = num_frames;
211 continue;
212 }
213 // Successfully dequeued a new buffer.
214 queued_buffers--;
215 }
216
217 // Get the minimum frames available between the currently playing buffer, and the
218 // amount we have left to fill
219 size_t frames_available{std::min(playing_buffer.frames - playing_buffer.frames_played,
220 num_frames - frames_written)};
221
222 samples_buffer.Pop(&output_buffer[frames_written * frame_size],
223 frames_available * frame_size);
224
225 frames_written += frames_available;
226 playing_buffer.frames_played += frames_available;
227
228 // If that's all the frames in the current buffer, add its samples and mark it as
229 // consumed
230 if (playing_buffer.frames_played >= playing_buffer.frames) {
231 playing_buffer.consumed = true;
232 }
233 }
234
235 std::memcpy(&last_frame[0], &output_buffer[(frames_written - 1) * frame_size],
236 frame_size_bytes);
237
238 if (stalled && queued_buffers <= max_queue_size) {
239 Unstall();
240 }
241}
242
243void SinkStream::Stall() {
244 if (stalled) {
245 return;
246 }
247 stalled = true;
248 system.StallProcesses();
249}
250
251void SinkStream::Unstall() {
252 if (!stalled) {
253 return;
254 }
255 system.UnstallProcesses();
256 stalled = false;
257}
258
259} // namespace AudioCore::Sink
diff --git a/src/audio_core/sink/sink_stream.h b/src/audio_core/sink/sink_stream.h
index 17ed6593f..db7cff45e 100644
--- a/src/audio_core/sink/sink_stream.h
+++ b/src/audio_core/sink/sink_stream.h
@@ -3,12 +3,20 @@
3 3
4#pragma once 4#pragma once
5 5
6#include <array>
6#include <atomic> 7#include <atomic>
7#include <memory> 8#include <memory>
9#include <span>
8#include <vector> 10#include <vector>
9 11
10#include "audio_core/common/common.h" 12#include "audio_core/common/common.h"
11#include "common/common_types.h" 13#include "common/common_types.h"
14#include "common/reader_writer_queue.h"
15#include "common/ring_buffer.h"
16
17namespace Core {
18class System;
19} // namespace Core
12 20
13namespace AudioCore::Sink { 21namespace AudioCore::Sink {
14 22
@@ -34,20 +42,24 @@ struct SinkBuffer {
34 * You should regularly call IsBufferConsumed with the unique SinkBuffer tag to check if the buffer 42 * You should regularly call IsBufferConsumed with the unique SinkBuffer tag to check if the buffer
35 * has been consumed. 43 * has been consumed.
36 * 44 *
37 * Since these are a FIFO queue, always check IsBufferConsumed in the same order you appended the 45 * Since these are a FIFO queue, IsBufferConsumed must be checked in the same order buffers were
38 * buffers, skipping a buffer will result in all following buffers to never release. 46 * appended, skipping a buffer will result in the queue getting stuck, and all following buffers to
47 * never release.
39 * 48 *
40 * If the buffers appear to be stuck, you can stop and re-open an IAudioIn/IAudioOut service (this 49 * If the buffers appear to be stuck, you can stop and re-open an IAudioIn/IAudioOut service (this
41 * is what games do), or call ClearQueue to flush all of the buffers without a full restart. 50 * is what games do), or call ClearQueue to flush all of the buffers without a full restart.
42 */ 51 */
43class SinkStream { 52class SinkStream {
44public: 53public:
45 virtual ~SinkStream() = default; 54 explicit SinkStream(Core::System& system_, StreamType type_) : system{system_}, type{type_} {}
55 virtual ~SinkStream() {
56 Unstall();
57 }
46 58
47 /** 59 /**
48 * Finalize the sink stream. 60 * Finalize the sink stream.
49 */ 61 */
50 virtual void Finalize() = 0; 62 virtual void Finalize() {}
51 63
52 /** 64 /**
53 * Start the sink stream. 65 * Start the sink stream.
@@ -55,48 +67,19 @@ public:
55 * @param resume - Set to true if this is resuming the stream a previously-active stream. 67 * @param resume - Set to true if this is resuming the stream a previously-active stream.
56 * Default false. 68 * Default false.
57 */ 69 */
58 virtual void Start(bool resume = false) = 0; 70 virtual void Start(bool resume = false) {}
59 71
60 /** 72 /**
61 * Stop the sink stream. 73 * Stop the sink stream.
62 */ 74 */
63 virtual void Stop() = 0; 75 virtual void Stop() {}
64
65 /**
66 * Append a new buffer and its samples to a waiting queue to play.
67 *
68 * @param buffer - Audio buffer information to be queued.
69 * @param samples - The s16 samples to be queue for playback.
70 */
71 virtual void AppendBuffer(SinkBuffer& buffer, std::vector<s16>& samples) = 0;
72
73 /**
74 * Release a buffer. Audio In only, will fill a buffer with recorded samples.
75 *
76 * @param num_samples - Maximum number of samples to receive.
77 * @return Vector of recorded samples. May have fewer than num_samples.
78 */
79 virtual std::vector<s16> ReleaseBuffer(u64 num_samples) = 0;
80
81 /**
82 * Check if a certain buffer has been consumed (fully played).
83 *
84 * @param tag - Unique tag of a buffer to check for.
85 * @return True if the buffer has been played, otherwise false.
86 */
87 virtual bool IsBufferConsumed(u64 tag) = 0;
88
89 /**
90 * Empty out the buffer queue.
91 */
92 virtual void ClearQueue() = 0;
93 76
94 /** 77 /**
95 * Check if the stream is paused. 78 * Check if the stream is paused.
96 * 79 *
97 * @return True if paused, otherwise false. 80 * @return True if paused, otherwise false.
98 */ 81 */
99 bool IsPaused() { 82 bool IsPaused() const {
100 return paused; 83 return paused;
101 } 84 }
102 85
@@ -128,34 +111,6 @@ public:
128 } 111 }
129 112
130 /** 113 /**
131 * Get the total number of samples played by this stream.
132 *
133 * @return Number of samples played.
134 */
135 u64 GetPlayedSampleCount() const {
136 return played_sample_count;
137 }
138
139 /**
140 * Set the number of samples played.
141 * This is started and stopped on system start/stop.
142 *
143 * @param played_sample_count_ - Number of samples to set.
144 */
145 void SetPlayedSampleCount(u64 played_sample_count_) {
146 played_sample_count = played_sample_count_;
147 }
148
149 /**
150 * Add to the played sample count.
151 *
152 * @param num_samples - Number of samples to add.
153 */
154 void AddPlayedSampleCount(u64 num_samples) {
155 played_sample_count += num_samples;
156 }
157
158 /**
159 * Get the system volume. 114 * Get the system volume.
160 * 115 *
161 * @return The current system volume. 116 * @return The current system volume.
@@ -200,15 +155,65 @@ public:
200 return queued_buffers.load(); 155 return queued_buffers.load();
201 } 156 }
202 157
158 /**
159 * Set the maximum buffer queue size.
160 */
161 void SetRingSize(u32 ring_size) {
162 max_queue_size = ring_size;
163 }
164
165 /**
166 * Append a new buffer and its samples to a waiting queue to play.
167 *
168 * @param buffer - Audio buffer information to be queued.
169 * @param samples - The s16 samples to be queue for playback.
170 */
171 virtual void AppendBuffer(SinkBuffer& buffer, std::vector<s16>& samples);
172
173 /**
174 * Release a buffer. Audio In only, will fill a buffer with recorded samples.
175 *
176 * @param num_samples - Maximum number of samples to receive.
177 * @return Vector of recorded samples. May have fewer than num_samples.
178 */
179 virtual std::vector<s16> ReleaseBuffer(u64 num_samples);
180
181 /**
182 * Empty out the buffer queue.
183 */
184 void ClearQueue();
185
186 /**
187 * Callback for AudioIn.
188 *
189 * @param input_buffer - Input buffer to be filled with samples.
190 * @param num_frames - Number of frames to be filled.
191 */
192 void ProcessAudioIn(std::span<const s16> input_buffer, std::size_t num_frames);
193
194 /**
195 * Callback for AudioOut and AudioRenderer.
196 *
197 * @param output_buffer - Output buffer to be filled with samples.
198 * @param num_frames - Number of frames to be filled.
199 */
200 void ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames);
201
202 /**
203 * Stall core processes if the audio thread falls too far behind.
204 */
205 void Stall();
206
207 /**
208 * Unstall core processes.
209 */
210 void Unstall();
211
203protected: 212protected:
204 /// Number of buffers waiting to be played 213 /// Core system
205 std::atomic<u32> queued_buffers{}; 214 Core::System& system;
206 /// Total samples played by this stream 215 /// Type of this stream
207 std::atomic<u64> played_sample_count{}; 216 StreamType type;
208 /// Set by the audio render/in/out system which uses this stream
209 f32 system_volume{1.0f};
210 /// Set via IAudioDevice service calls
211 f32 device_volume{1.0f};
212 /// Set by the audio render/in/out systen which uses this stream 217 /// Set by the audio render/in/out systen which uses this stream
213 u32 system_channels{2}; 218 u32 system_channels{2};
214 /// Channels supported by hardware 219 /// Channels supported by hardware
@@ -217,6 +222,28 @@ protected:
217 std::atomic<bool> paused{true}; 222 std::atomic<bool> paused{true};
218 /// Was this stream previously playing? 223 /// Was this stream previously playing?
219 std::atomic<bool> was_playing{false}; 224 std::atomic<bool> was_playing{false};
225 /// Name of this stream
226 std::string name{};
227
228private:
229 /// Ring buffer of the samples waiting to be played or consumed
230 Common::RingBuffer<s16, 0x10000> samples_buffer;
231 /// Audio buffers queued and waiting to play
232 Common::ReaderWriterQueue<SinkBuffer> queue;
233 /// The currently-playing audio buffer
234 SinkBuffer playing_buffer{};
235 /// The last played (or received) frame of audio, used when the callback underruns
236 std::array<s16, MaxChannels> last_frame{};
237 /// Number of buffers waiting to be played
238 std::atomic<u32> queued_buffers{};
239 /// The ring size for audio out buffers (usually 4, rarely 2 or 8)
240 u32 max_queue_size{};
241 /// Set by the audio render/in/out system which uses this stream
242 f32 system_volume{1.0f};
243 /// Set via IAudioDevice service calls
244 f32 device_volume{1.0f};
245 /// True if coretiming has been stalled
246 bool stalled{false};
220}; 247};
221 248
222using SinkStreamPtr = std::unique_ptr<SinkStream>; 249using SinkStreamPtr = std::unique_ptr<SinkStream>;