From 904b7c2a41384e27c858afcdb526c2ec1a549022 Mon Sep 17 00:00:00 2001 From: lifning <> Date: Thu, 9 Dec 2021 23:40:38 -0800 Subject: [PATCH] fix audio in gpSP (sample rate too high after fixed-point conversion) --- .../src/provided/sdl2/audio.rs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ferretro_components/src/provided/sdl2/audio.rs b/ferretro_components/src/provided/sdl2/audio.rs index 64b38e6..251cefd 100644 --- a/ferretro_components/src/provided/sdl2/audio.rs +++ b/ferretro_components/src/provided/sdl2/audio.rs @@ -69,16 +69,22 @@ impl RetroComponent for SimpleSdl2AudioComponent { } fn post_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow { - if let Ok(converter) = Self::make_converter(self.src_freq, self.queue.spec()) { - if !self.audio_buffer.is_empty() { - let mut samples = std::mem::take(&mut self.audio_buffer); - samples = resample(&converter, samples); - self.audio_buffer = samples; - self.queue.queue(&self.audio_buffer); - self.audio_buffer.clear(); + match Self::make_converter(self.src_freq, self.queue.spec()) { + Ok(converter) => { + if !self.audio_buffer.is_empty() { + let mut samples = std::mem::take(&mut self.audio_buffer); + samples = resample(&converter, samples); + self.audio_buffer = samples; + self.queue.queue(&self.audio_buffer); + self.audio_buffer.clear(); + } + ControlFlow::Continue + } + Err(e) => { + eprintln!("Audio sample rate conversion failed: {:?}", e); + ControlFlow::Break } } - ControlFlow::Continue } } @@ -105,16 +111,16 @@ impl SimpleSdl2AudioComponent { } fn make_converter(src_freq: f64, dest_spec: &AudioSpec) -> Result { - // note on the `* 64`: as long as the ratio between src_rate and dst_rate is right, + // note on the `* 16`: as long as the ratio between src_rate and dst_rate is right, // we should be in the clear -- this is to make up for SDL not giving us floats for // this, we can at least get some quasi-fixed-point precision going on... AudioCVT::new( AudioFormat::s16_sys(), 2, - (src_freq * 64.0).round() as i32, + (src_freq * 16.0).round() as i32, dest_spec.format, dest_spec.channels, - dest_spec.freq * 64, + dest_spec.freq * 16, ) } }