From 86995be111d7c5b6c80ed2a15c30c926a754ad99 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sat, 24 Dec 2022 22:40:19 -0700 Subject: [PATCH] LibAudio: Tolerate a file sample rate lower than the AudioServer's Previously, trying to load a wav file in aplay or SoundPlayer with a sample rate less than 44100 would crash in an assertion failure trying to unchecked_append to a vector that was not resized properly. --- Userland/Libraries/LibAudio/Resampler.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibAudio/Resampler.h b/Userland/Libraries/LibAudio/Resampler.h index 86799d9197d..c4c2e11af21 100644 --- a/Userland/Libraries/LibAudio/Resampler.h +++ b/Userland/Libraries/LibAudio/Resampler.h @@ -62,7 +62,8 @@ public: template Samples, size_t vector_inline_capacity = 0> ErrorOr try_resample_into_end(Vector& destination, Samples&& to_resample) { - TRY(destination.try_ensure_capacity(destination.size() + to_resample.size() * ceil_div(m_source, m_target))); + float ratio = (m_source > m_target) ? static_cast(m_source) / m_target : static_cast(m_target) / m_source; + TRY(destination.try_ensure_capacity(destination.size() + to_resample.size() * ratio)); for (auto sample : to_resample) { process_sample(sample, sample);