again with timeout

This commit is contained in:
lifning 2021-12-17 23:52:15 -08:00
parent 085445b35d
commit 08e69a3062
1 changed files with 13 additions and 1 deletions

View File

@ -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<i16>,
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();