2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Merge pull request #9969 from bylaws/master

Audio synchronisation improvements
This commit is contained in:
bunnei 2023-04-01 14:34:59 -07:00 committed by GitHub
commit 6775a6ee02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 79 deletions

View file

@ -121,8 +121,7 @@ u64 DeviceSession::GetPlayedSampleCount() const {
} }
std::optional<std::chrono::nanoseconds> DeviceSession::ThreadFunc() { std::optional<std::chrono::nanoseconds> DeviceSession::ThreadFunc() {
// Add 5ms of samples at a 48K sample rate. played_sample_count = stream->GetExpectedPlayedSampleCount();
played_sample_count += 48'000 * INCREMENT_TIME / 1s;
if (type == Sink::StreamType::Out) { if (type == Sink::StreamType::Out) {
system.AudioCore().GetAudioManager().SetEvent(Event::Type::AudioOutManager, true); system.AudioCore().GetAudioManager().SetEvent(Event::Type::AudioOutManager, true);
} else { } else {

View file

@ -189,6 +189,8 @@ void AudioRenderer::ThreadFunc() {
max_time = std::min(command_buffer.time_limit, max_time); max_time = std::min(command_buffer.time_limit, max_time);
command_list_processor.SetProcessTimeMax(max_time); command_list_processor.SetProcessTimeMax(max_time);
streams[index]->WaitFreeSpace();
// Process the command list // Process the command list
{ {
MICROPROFILE_SCOPE(Audio_Renderer); MICROPROFILE_SCOPE(Audio_Renderer);

View file

@ -10,6 +10,7 @@
#include "audio_core/renderer/adsp/command_buffer.h" #include "audio_core/renderer/adsp/command_buffer.h"
#include "audio_core/renderer/adsp/command_list_processor.h" #include "audio_core/renderer/adsp/command_list_processor.h"
#include "common/common_types.h" #include "common/common_types.h"
#include "common/polyfill_thread.h"
#include "common/reader_writer_queue.h" #include "common/reader_writer_queue.h"
#include "common/thread.h" #include "common/thread.h"

View file

@ -15,14 +15,9 @@ MICROPROFILE_DEFINE(Audio_RenderSystemManager, "Audio", "Render System Manager",
MP_RGB(60, 19, 97)); MP_RGB(60, 19, 97));
namespace AudioCore::AudioRenderer { namespace AudioCore::AudioRenderer {
constexpr std::chrono::nanoseconds RENDER_TIME{5'000'000UL};
SystemManager::SystemManager(Core::System& core_) SystemManager::SystemManager(Core::System& core_)
: core{core_}, adsp{core.AudioCore().GetADSP()}, mailbox{adsp.GetRenderMailbox()}, : core{core_}, adsp{core.AudioCore().GetADSP()}, mailbox{adsp.GetRenderMailbox()} {}
thread_event{Core::Timing::CreateEvent(
"AudioRendererSystemManager", [this](std::uintptr_t, s64 time, std::chrono::nanoseconds) {
return ThreadFunc2(time);
})} {}
SystemManager::~SystemManager() { SystemManager::~SystemManager() {
Stop(); Stop();
@ -33,8 +28,6 @@ bool SystemManager::InitializeUnsafe() {
if (adsp.Start()) { if (adsp.Start()) {
active = true; active = true;
thread = std::jthread([this](std::stop_token stop_token) { ThreadFunc(); }); thread = std::jthread([this](std::stop_token stop_token) { ThreadFunc(); });
core.CoreTiming().ScheduleLoopingEvent(std::chrono::nanoseconds(0), RENDER_TIME,
thread_event);
} }
} }
@ -45,7 +38,6 @@ void SystemManager::Stop() {
if (!active) { if (!active) {
return; return;
} }
core.CoreTiming().UnscheduleEvent(thread_event, {});
active = false; active = false;
update.store(true); update.store(true);
update.notify_all(); update.notify_all();
@ -111,16 +103,7 @@ void SystemManager::ThreadFunc() {
adsp.Signal(); adsp.Signal();
adsp.Wait(); adsp.Wait();
update.wait(false);
update.store(false);
} }
} }
std::optional<std::chrono::nanoseconds> SystemManager::ThreadFunc2(s64 time) {
update.store(true);
update.notify_all();
return std::nullopt;
}
} // namespace AudioCore::AudioRenderer } // namespace AudioCore::AudioRenderer

View file

@ -68,11 +68,6 @@ private:
*/ */
void ThreadFunc(); void ThreadFunc();
/**
* Signalling core timing thread to run ThreadFunc.
*/
std::optional<std::chrono::nanoseconds> ThreadFunc2(s64 time);
enum class StreamState { enum class StreamState {
Filling, Filling,
Steady, Steady,
@ -95,8 +90,6 @@ private:
ADSP::ADSP& adsp; ADSP::ADSP& adsp;
/// AudioRenderer mailbox for communication /// AudioRenderer mailbox for communication
ADSP::AudioRenderer_Mailbox* mailbox{}; ADSP::AudioRenderer_Mailbox* mailbox{};
/// Core timing event to signal main thread
std::shared_ptr<Core::Timing::EventType> thread_event;
/// Atomic for main thread to wait on /// Atomic for main thread to wait on
std::atomic<bool> update{}; std::atomic<bool> update{};
}; };

View file

@ -101,8 +101,6 @@ public:
~CubebSinkStream() override { ~CubebSinkStream() override {
LOG_DEBUG(Service_Audio, "Destructing cubeb stream {}", name); LOG_DEBUG(Service_Audio, "Destructing cubeb stream {}", name);
Unstall();
if (!ctx) { if (!ctx) {
return; return;
} }
@ -143,8 +141,6 @@ public:
* Stop the sink stream. * Stop the sink stream.
*/ */
void Stop() override { void Stop() override {
Unstall();
if (!ctx || paused) { if (!ctx || paused) {
return; return;
} }

View file

@ -88,7 +88,6 @@ public:
* Finalize the sink stream. * Finalize the sink stream.
*/ */
void Finalize() override { void Finalize() override {
Unstall();
if (device == 0) { if (device == 0) {
return; return;
} }
@ -116,7 +115,6 @@ public:
* Stop the sink stream. * Stop the sink stream.
*/ */
void Stop() override { void Stop() override {
Unstall();
if (device == 0 || paused) { if (device == 0 || paused) {
return; return;
} }

View file

@ -14,6 +14,8 @@
#include "common/fixed_point.h" #include "common/fixed_point.h"
#include "common/settings.h" #include "common/settings.h"
#include "core/core.h" #include "core/core.h"
#include "core/core_timing.h"
#include "core/core_timing_util.h"
namespace AudioCore::Sink { namespace AudioCore::Sink {
@ -149,10 +151,6 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
return; return;
} }
if (queued_buffers > max_queue_size) {
Stall();
}
while (frames_written < num_frames) { while (frames_written < num_frames) {
// If the playing buffer has been consumed or has no frames, we need a new one // If the playing buffer has been consumed or has no frames, we need a new one
if (playing_buffer.consumed || playing_buffer.frames == 0) { if (playing_buffer.consumed || playing_buffer.frames == 0) {
@ -187,10 +185,6 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
} }
std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes); 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<s16> output_buffer, std::size_t num_frames) { void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames) {
@ -198,10 +192,15 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
const std::size_t frame_size = num_channels; const std::size_t frame_size = num_channels;
const std::size_t frame_size_bytes = frame_size * sizeof(s16); const std::size_t frame_size_bytes = frame_size * sizeof(s16);
size_t frames_written{0}; 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 // 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. // paused and we'll desync, so just play silence.
if (system.IsPaused() || system.IsShuttingDown()) { if (system.IsPaused() || system.IsShuttingDown()) {
if (system.IsShuttingDown()) {
release_cv.notify_one();
}
static constexpr std::array<s16, 6> silence{}; static constexpr std::array<s16, 6> silence{};
for (size_t i = frames_written; i < num_frames; i++) { for (size_t i = frames_written; i < num_frames; i++) {
std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes); std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
@ -209,20 +208,6 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
return; 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) { while (frames_written < num_frames) {
// If the playing buffer has been consumed or has no frames, we need a new one // If the playing buffer has been consumed or has no frames, we need a new one
if (playing_buffer.consumed || playing_buffer.frames == 0) { if (playing_buffer.consumed || playing_buffer.frames == 0) {
@ -237,6 +222,10 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
} }
// Successfully dequeued a new buffer. // Successfully dequeued a new buffer.
queued_buffers--; queued_buffers--;
{ std::unique_lock lk{release_mutex}; }
release_cv.notify_one();
} }
// Get the minimum frames available between the currently playing buffer, and the // Get the minimum frames available between the currently playing buffer, and the
@ -248,6 +237,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
frames_available * frame_size); frames_available * frame_size);
frames_written += frames_available; frames_written += frames_available;
actual_frames_written += frames_available;
playing_buffer.frames_played += 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 // If that's all the frames in the current buffer, add its samples and mark it as
@ -260,26 +250,29 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
std::memcpy(&last_frame[0], &output_buffer[(frames_written - 1) * frame_size], std::memcpy(&last_frame[0], &output_buffer[(frames_written - 1) * frame_size],
frame_size_bytes); frame_size_bytes);
if (system.IsMulticore() && queued_buffers <= max_queue_size) { {
Unstall(); 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;
} }
} }
void SinkStream::Stall() { u64 SinkStream::GetExpectedPlayedSampleCount() {
std::scoped_lock lk{stall_guard}; std::scoped_lock lk{sample_count_lock};
if (stalled_lock) { auto cur_time{Core::Timing::CyclesToUs(system.CoreTiming().GetClockTicks())};
return; auto time_delta{cur_time - last_sample_count_update_time};
} auto exp_played_sample_count{min_played_sample_count +
stalled_lock = system.StallApplication(); (TargetSampleRate * time_delta) / std::chrono::seconds{1}};
return std::min<u64>(exp_played_sample_count, max_played_sample_count);
} }
void SinkStream::Unstall() { void SinkStream::WaitFreeSpace() {
std::scoped_lock lk{stall_guard}; std::unique_lock lk{release_mutex};
if (!stalled_lock) { release_cv.wait(
return; lk, [this]() { return queued_buffers < max_queue_size || system.IsShuttingDown(); });
}
system.UnstallApplication();
stalled_lock.unlock();
} }
} // namespace AudioCore::Sink } // namespace AudioCore::Sink

View file

@ -5,6 +5,7 @@
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <chrono>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <span> #include <span>
@ -14,6 +15,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/reader_writer_queue.h" #include "common/reader_writer_queue.h"
#include "common/ring_buffer.h" #include "common/ring_buffer.h"
#include "common/thread.h"
namespace Core { namespace Core {
class System; class System;
@ -53,9 +55,7 @@ struct SinkBuffer {
class SinkStream { class SinkStream {
public: public:
explicit SinkStream(Core::System& system_, StreamType type_) : system{system_}, type{type_} {} explicit SinkStream(Core::System& system_, StreamType type_) : system{system_}, type{type_} {}
virtual ~SinkStream() { virtual ~SinkStream() {}
Unstall();
}
/** /**
* Finalize the sink stream. * Finalize the sink stream.
@ -201,14 +201,16 @@ public:
void ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames); void ProcessAudioOutAndRender(std::span<s16> output_buffer, std::size_t num_frames);
/** /**
* Stall core processes if the audio thread falls too far behind. * Get the total number of samples expected to have been played by this stream.
*
* @return The number of samples.
*/ */
void Stall(); u64 GetExpectedPlayedSampleCount();
/** /**
* Unstall core processes. * Waits for free space in the sample ring buffer
*/ */
void Unstall(); void WaitFreeSpace();
protected: protected:
/// Core system /// Core system
@ -237,12 +239,21 @@ private:
std::atomic<u32> queued_buffers{}; std::atomic<u32> queued_buffers{};
/// The ring size for audio out buffers (usually 4, rarely 2 or 8) /// The ring size for audio out buffers (usually 4, rarely 2 or 8)
u32 max_queue_size{}; u32 max_queue_size{};
/// Locks access to sample count tracking info
std::mutex sample_count_lock;
/// Minimum number of total samples that have been played since the last callback
u64 min_played_sample_count{};
/// Maximum number of total samples that can be played since the last callback
u64 max_played_sample_count{};
/// The time the two above tracking variables were last written to
std::chrono::microseconds last_sample_count_update_time{};
/// Set by the audio render/in/out system which uses this stream /// Set by the audio render/in/out system which uses this stream
f32 system_volume{1.0f}; f32 system_volume{1.0f};
/// Set via IAudioDevice service calls /// Set via IAudioDevice service calls
f32 device_volume{1.0f}; f32 device_volume{1.0f};
std::mutex stall_guard; /// Signalled when ring buffer entries are consumed
std::unique_lock<std::mutex> stalled_lock; std::condition_variable release_cv;
std::mutex release_mutex;
}; };
using SinkStreamPtr = std::unique_ptr<SinkStream>; using SinkStreamPtr = std::unique_ptr<SinkStream>;