Skip to content

Commit

Permalink
Fix audio glitching when audio is late
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Nov 23, 2024
1 parent bc60e0c commit cb4882f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion frontend_tui/src/renderer_sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ impl SDLEventPump {

pub struct SDLAudioSink {
recv: AudioReceiver,
last_sample: u8,
}

impl AudioCallback for SDLAudioSink {
type Channel = u8;

fn callback(&mut self, out: &mut [u8]) {
if let Ok(buffer) = self.recv.try_recv() {
self.last_sample = buffer.last().copied().unwrap();
out.copy_from_slice(&buffer);
} else {
// Audio is late. Continue the last output sample to reduce
// pops and other abrupt noises.
out.fill(self.last_sample);
}
}
}
Expand All @@ -181,7 +187,10 @@ impl SDLAudioSink {
let device = audio_subsystem
.open_playback(None, &spec, |spec| {
debug!("Audio spec: {:?}", spec);
Self { recv: audioch }
Self {
recv: audioch,
last_sample: 0,
}
})
.map_err(|e| anyhow!(e))?;
device.resume();
Expand Down

0 comments on commit cb4882f

Please sign in to comment.