Browse Source

Fix a little regression

pull/2903/head
MaranBr 4 months ago
parent
commit
605f3e0be2
  1. 45
      src/audio_core/sink/sink_stream.cpp

45
src/audio_core/sink/sink_stream.cpp

@ -28,15 +28,13 @@ void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span<s16> samples) {
constexpr s32 min = (std::numeric_limits<s16>::min)(); constexpr s32 min = (std::numeric_limits<s16>::min)();
constexpr s32 max = (std::numeric_limits<s16>::max)(); constexpr s32 max = (std::numeric_limits<s16>::max)();
auto yuzu_volume = Settings::Volume(); auto yuzu_volume = Settings::Volume();
if (yuzu_volume > 1.0f) if (yuzu_volume > 1.0f)
yuzu_volume = 0.6f + 20.0f * std::log10(yuzu_volume); yuzu_volume = 0.6f + 20.0f * std::log10(yuzu_volume);
yuzu_volume = std::max(yuzu_volume, 0.001f); yuzu_volume = std::max(yuzu_volume, 0.001f);
auto const volume = system_volume * device_volume * yuzu_volume; auto const volume = system_volume * device_volume * yuzu_volume;
{
std::scoped_lock lk{release_mutex};
if (system_channels > device_channels) { if (system_channels > device_channels) {
static constexpr std::array<f32, 4> tcoeff{1.0f, 0.596f, 0.354f, 0.707f}; static constexpr std::array<f32, 4> tcoeff{1.0f, 0.596f, 0.354f, 0.707f};
for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size();
@ -71,8 +69,7 @@ void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span<s16> samples) {
for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size();
r_offs += system_channels, w_offs += device_channels) r_offs += system_channels, w_offs += device_channels)
for (u32 channel = 0; channel < system_channels; ++channel) 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));
new_samples[w_offs + channel] = s16(std::clamp(s32(f32(samples[r_offs + channel]) * volume), min, max));
queue.EmplaceWait(buffer); queue.EmplaceWait(buffer);
samples_buffer.Push(new_samples); samples_buffer.Push(new_samples);
@ -88,9 +85,6 @@ void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span<s16> samples) {
++queued_buffers; ++queued_buffers;
} }
release_cv.notify_one();
}
std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) { std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
auto samples{samples_buffer.Pop(num_samples)}; auto samples{samples_buffer.Pop(num_samples)};
@ -139,8 +133,7 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
if (!queue.TryPop(playing_buffer)) { if (!queue.TryPop(playing_buffer)) {
// If no buffer was available we've underrun, just push the samples and // If no buffer was available we've underrun, just push the samples and
// continue. // 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; frames_written = num_frames;
continue; continue;
} }
@ -150,11 +143,9 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
// Get the minimum frames available between the currently playing buffer, and the // Get the minimum frames available between the currently playing buffer, and the
// amount we have left to fill // amount we have left to fill
size_t frames_available{std::min<u64>(playing_buffer.frames - playing_buffer.frames_played,
num_frames - frames_written)};
size_t frames_available{std::min<u64>(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; frames_written += frames_available;
playing_buffer.frames_played += frames_available; playing_buffer.frames_played += frames_available;
@ -206,11 +197,9 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
} }
const size_t frames_available = const size_t frames_available =
std::min<u64>(playing_buffer.frames - playing_buffer.frames_played,
num_frames - frames_written);
std::min<u64>(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; frames_written += frames_available;
actual_frames_written += frames_available; actual_frames_written += frames_available;
@ -220,9 +209,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
playing_buffer.consumed = true; 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}; std::scoped_lock lk{sample_count_lock};
@ -236,8 +223,7 @@ u64 SinkStream::GetExpectedPlayedSampleCount() {
std::scoped_lock lk{sample_count_lock}; std::scoped_lock lk{sample_count_lock};
auto cur_time{system.CoreTiming().GetGlobalTimeNs()}; auto cur_time{system.CoreTiming().GetGlobalTimeNs()};
auto time_delta{cur_time - last_sample_count_update_time}; 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 // Add 15ms of latency in sample reporting to allow for some leeway in scheduler timings
return std::min<u64>(exp_played_sample_count, max_played_sample_count) + TargetSampleCount * 3; return std::min<u64>(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) { void SinkStream::WaitFreeSpace(std::stop_token stop_token) {
std::unique_lock lk{release_mutex}; std::unique_lock lk{release_mutex};
release_cv.wait_for(lk, std::chrono::milliseconds(10),
[this]() { return paused || queued_buffers < max_queue_size; });
if (queued_buffers > max_queue_size + 10) {
release_cv.wait(lk, stop_token, [this] {
auto can_continue = [this]() {
return paused || queued_buffers < max_queue_size; 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, can_continue);
} }
} }

Loading…
Cancel
Save