From d8fc3f403b62e2b3d67ec08791fdc66847ddb4ac Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sat, 18 Mar 2023 20:52:02 +0000 Subject: audio: Interpolate system manager sample count using host sink sample info This avoids the need to stall if the host sink sporadically misses the deadline, in such a case the previous implementation would report them samples as being played on-time, causing the guest to send more samples and leading to a gradual buildup. --- src/audio_core/sink/sink_stream.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/audio_core/sink/sink_stream.cpp') diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp index 39a21b0f0..1af96f793 100644 --- a/src/audio_core/sink/sink_stream.cpp +++ b/src/audio_core/sink/sink_stream.cpp @@ -14,6 +14,8 @@ #include "common/fixed_point.h" #include "common/settings.h" #include "core/core.h" +#include "core/core_timing.h" +#include "core/core_timing_util.h" namespace AudioCore::Sink { @@ -198,6 +200,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz const std::size_t frame_size = num_channels; const std::size_t frame_size_bytes = frame_size * sizeof(s16); size_t frames_written{0}; + size_t actual_frames_written{0}; // If we're paused or going to shut down, we don't want to consume buffers as coretiming is // paused and we'll desync, so just play silence. @@ -248,6 +251,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz frames_available * frame_size); frames_written += frames_available; + actual_frames_written += frames_available; playing_buffer.frames_played += frames_available; // If that's all the frames in the current buffer, add its samples and mark it as @@ -260,6 +264,13 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz std::memcpy(&last_frame[0], &output_buffer[(frames_written - 1) * frame_size], frame_size_bytes); + { + std::scoped_lock lk{sample_count_lock}; + last_sample_count_update_time = Core::Timing::CyclesToUs(system.CoreTiming().GetClockTicks()); + min_played_sample_count = max_played_sample_count; + max_played_sample_count += actual_frames_written; + } + if (system.IsMulticore() && queued_buffers <= max_queue_size) { Unstall(); } @@ -282,4 +293,14 @@ void SinkStream::Unstall() { stalled_lock.unlock(); } +u64 SinkStream::GetExpectedPlayedSampleCount() { + std::scoped_lock lk{sample_count_lock}; + auto cur_time{Core::Timing::CyclesToUs(system.CoreTiming().GetClockTicks())}; + auto time_delta{cur_time - last_sample_count_update_time}; + auto exp_played_sample_count{min_played_sample_count + + (TargetSampleRate * time_delta) / std::chrono::seconds{1}}; + + return std::min(exp_played_sample_count, max_played_sample_count); +} + } // namespace AudioCore::Sink -- cgit v1.2.3 From ea5dd02db9bdb9759a400907672ec6606bebb96b Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sat, 18 Mar 2023 20:57:00 +0000 Subject: audio: Wait for samples on the emulated DSP side to avoid desyncs Waiting on the host side is inaccurate and leads to desyncs in the event of the sink missing a deadline that require stalls to fix. By waiting for the sink to have space before even starting rendering such desyncs can be avoided. --- src/audio_core/sink/sink_stream.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/audio_core/sink/sink_stream.cpp') diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp index 1af96f793..a54c61845 100644 --- a/src/audio_core/sink/sink_stream.cpp +++ b/src/audio_core/sink/sink_stream.cpp @@ -205,6 +205,10 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz // If we're paused or going to shut down, we don't want to consume buffers as coretiming is // paused and we'll desync, so just play silence. if (system.IsPaused() || system.IsShuttingDown()) { + if (system.IsShuttingDown()) { + release_cv.notify_one(); + } + static constexpr std::array silence{}; for (size_t i = frames_written; i < num_frames; i++) { std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes); @@ -240,6 +244,12 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz } // Successfully dequeued a new buffer. queued_buffers--; + + { + std::unique_lock lk{release_mutex}; + } + + release_cv.notify_one(); } // Get the minimum frames available between the currently playing buffer, and the @@ -303,4 +313,9 @@ u64 SinkStream::GetExpectedPlayedSampleCount() { return std::min(exp_played_sample_count, max_played_sample_count); } +void SinkStream::WaitFreeSpace() { + std::unique_lock lk{release_mutex}; + release_cv.wait(lk, [this]() { return queued_buffers < max_queue_size || system.IsShuttingDown(); }); +} + } // namespace AudioCore::Sink -- cgit v1.2.3 From 237934b73690a56ff756d6e682fa336dee8c95a4 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sun, 26 Mar 2023 20:07:03 +0100 Subject: Run clang-format --- src/audio_core/sink/sink_stream.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/audio_core/sink/sink_stream.cpp') diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp index a54c61845..084ac5edf 100644 --- a/src/audio_core/sink/sink_stream.cpp +++ b/src/audio_core/sink/sink_stream.cpp @@ -245,9 +245,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz // Successfully dequeued a new buffer. queued_buffers--; - { - std::unique_lock lk{release_mutex}; - } + { std::unique_lock lk{release_mutex}; } release_cv.notify_one(); } @@ -276,7 +274,8 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz { std::scoped_lock lk{sample_count_lock}; - last_sample_count_update_time = Core::Timing::CyclesToUs(system.CoreTiming().GetClockTicks()); + last_sample_count_update_time = + Core::Timing::CyclesToUs(system.CoreTiming().GetClockTicks()); min_played_sample_count = max_played_sample_count; max_played_sample_count += actual_frames_written; } @@ -315,7 +314,8 @@ u64 SinkStream::GetExpectedPlayedSampleCount() { void SinkStream::WaitFreeSpace() { std::unique_lock lk{release_mutex}; - release_cv.wait(lk, [this]() { return queued_buffers < max_queue_size || system.IsShuttingDown(); }); + release_cv.wait( + lk, [this]() { return queued_buffers < max_queue_size || system.IsShuttingDown(); }); } } // namespace AudioCore::Sink -- cgit v1.2.3 From 530fe24768357d4151ac6c6aca4a0e122ef8260a Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sun, 26 Mar 2023 20:21:04 +0100 Subject: audio_core: No longer stall when sink queue is full Now the audout and audren update rates are tied to the sink status stalling is no longer necessary. --- src/audio_core/sink/sink_stream.cpp | 43 ------------------------------------- 1 file changed, 43 deletions(-) (limited to 'src/audio_core/sink/sink_stream.cpp') diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp index 084ac5edf..f99dbd8ec 100644 --- a/src/audio_core/sink/sink_stream.cpp +++ b/src/audio_core/sink/sink_stream.cpp @@ -151,10 +151,6 @@ void SinkStream::ProcessAudioIn(std::span input_buffer, std::size_t n return; } - if (queued_buffers > max_queue_size) { - Stall(); - } - while (frames_written < num_frames) { // If the playing buffer has been consumed or has no frames, we need a new one if (playing_buffer.consumed || playing_buffer.frames == 0) { @@ -189,10 +185,6 @@ void SinkStream::ProcessAudioIn(std::span input_buffer, std::size_t n } std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes); - - if (queued_buffers <= max_queue_size) { - Unstall(); - } } void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::size_t num_frames) { @@ -216,20 +208,6 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz return; } - // Due to many frames being queued up with nvdec (5 frames or so?), a lot of buffers also get - // queued up (30+) but not all at once, which causes constant stalling here, so just let the - // video play out without attempting to stall. - // Can hopefully remove this later with a more complete NVDEC implementation. - const auto nvdec_active{system.AudioCore().IsNVDECActive()}; - - // Core timing cannot be paused in single-core mode, so Stall ends up being called over and over - // and never recovers to a normal state, so just skip attempting to sync things on single-core. - if (system.IsMulticore() && !nvdec_active && queued_buffers > max_queue_size) { - Stall(); - } else if (system.IsMulticore() && queued_buffers <= max_queue_size) { - Unstall(); - } - while (frames_written < num_frames) { // If the playing buffer has been consumed or has no frames, we need a new one if (playing_buffer.consumed || playing_buffer.frames == 0) { @@ -279,27 +257,6 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz min_played_sample_count = max_played_sample_count; max_played_sample_count += actual_frames_written; } - - if (system.IsMulticore() && queued_buffers <= max_queue_size) { - Unstall(); - } -} - -void SinkStream::Stall() { - std::scoped_lock lk{stall_guard}; - if (stalled_lock) { - return; - } - stalled_lock = system.StallApplication(); -} - -void SinkStream::Unstall() { - std::scoped_lock lk{stall_guard}; - if (!stalled_lock) { - return; - } - system.UnstallApplication(); - stalled_lock.unlock(); } u64 SinkStream::GetExpectedPlayedSampleCount() { -- cgit v1.2.3