From 1873288db0a9bda8173429b4807fda8f1e3722a4 Mon Sep 17 00:00:00 2001 From: James Bruska Date: Thu, 23 Mar 2023 13:44:53 -0400 Subject: [PATCH] Fixed odd value length bug in convert_stereo_to_mono_audio functions --- src/utilities.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utilities.rs b/src/utilities.rs index 7fdc057..2da86ed 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -54,6 +54,7 @@ pub fn convert_integer_to_float_audio_simd(samples: &[i16]) -> Vec { /// Convert 32 bit floating point stereo PCM audio to 32 bit floating point mono PCM audio. /// +/// If there are an odd number of samples, the last sample is dropped. /// This variant does not use SIMD instructions. /// /// # Arguments @@ -62,15 +63,12 @@ pub fn convert_integer_to_float_audio_simd(samples: &[i16]) -> Vec { /// # Returns /// A vector of 32 bit floating point mono PCM audio samples. pub fn convert_stereo_to_mono_audio(samples: &[f32]) -> Vec { - let mut mono = Vec::with_capacity(samples.len() / 2); - for i in (0..samples.len()).step_by(2) { - mono.push((samples[i] + samples[i + 1]) / 2.0); - } - mono + samples.chunks_exact(2).map(|x| (x[0] + x[1]) / 2.0).collect() } /// Convert 32 bit floating point stereo PCM audio to 32 bit floating point mono PCM audio. /// +/// If there are an odd number of samples, the last sample is dropped. /// This variant uses SIMD instructions, and as such is only available on /// nightly Rust. /// @@ -104,9 +102,7 @@ pub fn convert_stereo_to_mono_audio_simd(samples: &[f32]) -> Vec { // Handle the remainder. // do this normally because it's only a few samples and the overhead of // converting to SIMD is not worth it. - for i in (0..remainder.len()).step_by(2) { - mono.push((remainder[i] + remainder[i + 1]) / 2.0); - } + mono.extend(convert_stereo_to_mono_audio(remainder)); mono }