From 083a11274e7d694e50d307a988d568a042450468 Mon Sep 17 00:00:00 2001 From: Liam Warfield Date: Fri, 6 Feb 2026 17:47:55 -0700 Subject: [PATCH] Fix buffer bug in native audio. Setting this buffer to 2400 was only enough to store 50m of sound at 48,000 samples/sec. I've also done a small refactor here to make this buffer scale with sample rate. --- gui/src/imp/native_audio.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/gui/src/imp/native_audio.rs b/gui/src/imp/native_audio.rs index 3867bd6..3778aac 100644 --- a/gui/src/imp/native_audio.rs +++ b/gui/src/imp/native_audio.rs @@ -22,6 +22,8 @@ pub struct AudioSystem { const SAMPLE_RATE: u32 = 48_000; const PACKET_SAMPLES: u32 = 960; +// Divide by 1000 to get samples per ms, then multiply by 60ms for max Opus frame size. +const MAX_DECODE_SAMPLES: usize = SAMPLE_RATE as usize / 1000 * 60; fn encode_and_send( state: TransmitState, @@ -184,7 +186,7 @@ impl AudioSystem { decoder, stream, buffer, - tmp: vec![0; 2400], + tmp: vec![0; MAX_DECODE_SAMPLES], }) } } @@ -198,13 +200,11 @@ pub struct AudioPlayer { impl AudioPlayer { pub fn play_opus(&mut self, payload: &[u8]) { - let len = loop { - match self.decoder.decode(payload, &mut self.tmp, false) { - Ok(l) => break l, - Err(e) => { - error!("opus decode error {e:?}"); - return; - } + let len = match self.decoder.decode(payload, &mut self.tmp, false) { + Ok(l) => l, + Err(e) => { + error!("opus decode error {e:?}"); + return; } };