From 605f3e0be2f7a5c05f87c3d805daf13879785a02 Mon Sep 17 00:00:00 2001 From: MaranBr Date: Fri, 31 Oct 2025 10:45:07 -0400 Subject: [PATCH] Fix a little regression --- src/audio_core/sink/sink_stream.cpp | 131 +++++++++++++--------------- 1 file changed, 60 insertions(+), 71 deletions(-) diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp index a157ac2b60..f2d29dc050 100644 --- a/src/audio_core/sink/sink_stream.cpp +++ b/src/audio_core/sink/sink_stream.cpp @@ -28,67 +28,61 @@ void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span samples) { constexpr s32 min = (std::numeric_limits::min)(); constexpr s32 max = (std::numeric_limits::max)(); + auto yuzu_volume = Settings::Volume(); if (yuzu_volume > 1.0f) yuzu_volume = 0.6f + 20.0f * std::log10(yuzu_volume); yuzu_volume = std::max(yuzu_volume, 0.001f); auto const volume = system_volume * device_volume * yuzu_volume; - { - std::scoped_lock lk{release_mutex}; - - if (system_channels > device_channels) { - static constexpr std::array tcoeff{1.0f, 0.596f, 0.354f, 0.707f}; - for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); - r_offs += system_channels, w_offs += device_channels) { - - std::array ccoeff{0.f}; - for (u32 i = 0; i < system_channels; ++i) - ccoeff[i] = f32(samples[r_offs + i]); - - std::array rcoeff{ - ccoeff[u32(Channels::FrontLeft)], - ccoeff[u32(Channels::BackLeft)], - ccoeff[u32(Channels::Center)], - ccoeff[u32(Channels::LFE)], - ccoeff[u32(Channels::BackRight)], - ccoeff[u32(Channels::FrontRight)], - }; - - const f32 left = rcoeff[0] * tcoeff[0] + rcoeff[2] * tcoeff[1] + - rcoeff[3] * tcoeff[2] + rcoeff[1] * tcoeff[3]; - const f32 right = rcoeff[5] * tcoeff[0] + rcoeff[2] * tcoeff[1] + - rcoeff[3] * tcoeff[2] + rcoeff[4] * tcoeff[3]; - - samples[w_offs + 0] = s16(std::clamp(s32(left * volume), min, max)); - samples[w_offs + 1] = s16(std::clamp(s32(right * volume), min, max)); - } - - queue.EmplaceWait(buffer); - samples_buffer.Push(samples.subspan(0, samples.size() / system_channels * device_channels)); - } else if (system_channels < device_channels) { - std::vector new_samples(samples.size() / system_channels * device_channels); - for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); - r_offs += system_channels, w_offs += device_channels) - for (u32 channel = 0; channel < system_channels; ++channel) - new_samples[w_offs + channel] = s16(std::clamp( - s32(f32(samples[r_offs + channel]) * volume), min, max)); - - queue.EmplaceWait(buffer); - samples_buffer.Push(new_samples); - } else { - if (volume != 1.0f) { - for (u32 i = 0; i < samples.size(); ++i) - samples[i] = s16(std::clamp(s32(f32(samples[i]) * volume), min, max)); - } - queue.EmplaceWait(buffer); - samples_buffer.Push(samples); + if (system_channels > device_channels) { + static constexpr std::array tcoeff{1.0f, 0.596f, 0.354f, 0.707f}; + for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); + r_offs += system_channels, w_offs += device_channels) { + + std::array ccoeff{0.f}; + for (u32 i = 0; i < system_channels; ++i) + ccoeff[i] = f32(samples[r_offs + i]); + + std::array rcoeff{ + ccoeff[u32(Channels::FrontLeft)], + ccoeff[u32(Channels::BackLeft)], + ccoeff[u32(Channels::Center)], + ccoeff[u32(Channels::LFE)], + ccoeff[u32(Channels::BackRight)], + ccoeff[u32(Channels::FrontRight)], + }; + + const f32 left = rcoeff[0] * tcoeff[0] + rcoeff[2] * tcoeff[1] + + rcoeff[3] * tcoeff[2] + rcoeff[1] * tcoeff[3]; + const f32 right = rcoeff[5] * tcoeff[0] + rcoeff[2] * tcoeff[1] + + rcoeff[3] * tcoeff[2] + rcoeff[4] * tcoeff[3]; + + samples[w_offs + 0] = s16(std::clamp(s32(left * volume), min, max)); + samples[w_offs + 1] = s16(std::clamp(s32(right * volume), min, max)); } - ++queued_buffers; + queue.EmplaceWait(buffer); + samples_buffer.Push(samples.subspan(0, samples.size() / system_channels * device_channels)); + } else if (system_channels < device_channels) { + std::vector new_samples(samples.size() / system_channels * device_channels); + for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); + r_offs += system_channels, w_offs += device_channels) + for (u32 channel = 0; channel < system_channels; ++channel) + new_samples[w_offs + channel] = s16(std::clamp(s32(f32(samples[r_offs + channel]) * volume), min, max)); + + queue.EmplaceWait(buffer); + samples_buffer.Push(new_samples); + } else { + if (volume != 1.0f) { + for (u32 i = 0; i < samples.size(); ++i) + samples[i] = s16(std::clamp(s32(f32(samples[i]) * volume), min, max)); + } + queue.EmplaceWait(buffer); + samples_buffer.Push(samples); } - release_cv.notify_one(); + ++queued_buffers; } std::vector SinkStream::ReleaseBuffer(u64 num_samples) { @@ -139,8 +133,7 @@ void SinkStream::ProcessAudioIn(std::span input_buffer, std::size_t n if (!queue.TryPop(playing_buffer)) { // If no buffer was available we've underrun, just push the samples and // continue. - samples_buffer.Push(&input_buffer[frames_written * frame_size], - (num_frames - frames_written) * frame_size); + samples_buffer.Push(&input_buffer[frames_written * frame_size], (num_frames - frames_written) * frame_size); frames_written = num_frames; continue; } @@ -150,11 +143,9 @@ void SinkStream::ProcessAudioIn(std::span input_buffer, std::size_t n // Get the minimum frames available between the currently playing buffer, and the // amount we have left to fill - size_t frames_available{std::min(playing_buffer.frames - playing_buffer.frames_played, - num_frames - frames_written)}; + size_t frames_available{std::min(playing_buffer.frames - playing_buffer.frames_played, num_frames - frames_written)}; - samples_buffer.Push(&input_buffer[frames_written * frame_size], - frames_available * frame_size); + samples_buffer.Push(&input_buffer[frames_written * frame_size], frames_available * frame_size); frames_written += frames_available; playing_buffer.frames_played += frames_available; @@ -206,11 +197,9 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz } const size_t frames_available = - std::min(playing_buffer.frames - playing_buffer.frames_played, - num_frames - frames_written); + std::min(playing_buffer.frames - playing_buffer.frames_played, num_frames - frames_written); - samples_buffer.Pop(&output_buffer[frames_written * frame_size], - frames_available * frame_size); + samples_buffer.Pop(&output_buffer[frames_written * frame_size], frames_available * frame_size); frames_written += frames_available; actual_frames_written += frames_available; @@ -220,9 +209,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span output_buffer, std::siz playing_buffer.consumed = true; } - std::memcpy(last_frame.data(), - &output_buffer[(frames_written - 1) * frame_size], - frame_size_bytes); + std::memcpy(last_frame.data(), &output_buffer[(frames_written - 1) * frame_size], frame_size_bytes); { std::scoped_lock lk{sample_count_lock}; @@ -236,8 +223,7 @@ u64 SinkStream::GetExpectedPlayedSampleCount() { std::scoped_lock lk{sample_count_lock}; auto cur_time{system.CoreTiming().GetGlobalTimeNs()}; 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}}; + auto exp_played_sample_count{min_played_sample_count + (TargetSampleRate * time_delta) / std::chrono::seconds{1}}; // Add 15ms of latency in sample reporting to allow for some leeway in scheduler timings return std::min(exp_played_sample_count, max_played_sample_count) + TargetSampleCount * 3; @@ -245,12 +231,15 @@ u64 SinkStream::GetExpectedPlayedSampleCount() { void SinkStream::WaitFreeSpace(std::stop_token stop_token) { std::unique_lock lk{release_mutex}; - release_cv.wait_for(lk, std::chrono::milliseconds(10), - [this]() { return paused || queued_buffers < max_queue_size; }); + + auto can_continue = [this]() { + return paused || queued_buffers < max_queue_size; + }; + + release_cv.wait_for(lk, std::chrono::milliseconds(10), can_continue); + if (queued_buffers > max_queue_size + 10) { - release_cv.wait(lk, stop_token, [this] { - return paused || queued_buffers < max_queue_size; - }); + release_cv.wait(lk, stop_token, can_continue); } }