diff --git a/ferretro_components/src/provided/sdl2/audio_thread.rs b/ferretro_components/src/provided/sdl2/audio_thread.rs index 139eef7..e059483 100644 --- a/ferretro_components/src/provided/sdl2/audio_thread.rs +++ b/ferretro_components/src/provided/sdl2/audio_thread.rs @@ -1,6 +1,7 @@ use crate::prelude::*; use std::path::Path; +use std::time::Duration; use sdl2::Sdl; use sdl2::audio::{AudioCallback, AudioDevice, AudioSpecDesired}; @@ -16,6 +17,7 @@ pub struct SimpleSdl2AudioThreadComponent { struct MySdlAudio { must_resample: bool, reader: Consumer, + timing: SystemTiming, } impl AudioCallback for MySdlAudio { @@ -27,8 +29,17 @@ impl AudioCallback for MySdlAudio { } let buf_len = data.len(); let mut remaining = buf_len; + let mut dead_frames = 0; while remaining > 0 { - remaining -= self.reader.pop_slice(&mut data[buf_len - remaining..]); + let read = self.reader.pop_slice(&mut data[buf_len - remaining..]); + remaining -= read; + if read == 0 { + std::thread::sleep(Duration::from_secs_f64(1.0 / self.timing.fps)); + dead_frames += 1; + if dead_frames > self.timing.fps as usize / 2 { + break; + } + } } } } @@ -88,6 +99,7 @@ impl SimpleSdl2AudioThreadComponent { MySdlAudio { must_resample, // todo: include freqs from & to reader, + timing: timing.clone(), } })?; device.resume();