tests: Make the audio backend tick every frame in the runner

This commit is contained in:
TÖRÖK Attila 2023-05-19 01:44:03 +02:00 committed by Nathan Adams
parent 54cf9065fb
commit e54f3fef8a
2 changed files with 19 additions and 17 deletions

View File

@ -1,5 +1,4 @@
num_frames = 240 num_frames = 240
sleep_to_meet_frame_rate = true
[player_options] [player_options]
with_audio = true with_audio = true

View File

@ -22,26 +22,18 @@ use std::time::Duration;
pub struct TestAudioBackend { pub struct TestAudioBackend {
mixer: AudioMixer, mixer: AudioMixer,
buffer: Vec<f32>,
} }
impl TestAudioBackend { impl TestAudioBackend {
const NUM_CHANNELS: u8 = 2;
const SAMPLE_RATE: u32 = 44100;
pub fn new() -> Self { pub fn new() -> Self {
const NUM_CHANNELS: u8 = 2; Self {
const SAMPLE_RATE: u32 = 44100; mixer: AudioMixer::new(Self::NUM_CHANNELS, Self::SAMPLE_RATE),
const BUFFER_SIZE: usize = 1024; buffer: vec![],
}
let mixer = AudioMixer::new(NUM_CHANNELS, SAMPLE_RATE);
let mixer_proxy = mixer.proxy();
std::thread::spawn(move || loop {
let mut buffer = [0f32; NUM_CHANNELS as usize * BUFFER_SIZE];
mixer_proxy.mix::<f32>(&mut buffer);
std::thread::sleep(std::time::Duration::from_secs_f32(
BUFFER_SIZE as f32 / SAMPLE_RATE as f32,
));
});
Self { mixer }
} }
} }
@ -49,6 +41,16 @@ impl AudioBackend for TestAudioBackend {
impl_audio_mixer_backend!(mixer); impl_audio_mixer_backend!(mixer);
fn play(&mut self) {} fn play(&mut self) {}
fn pause(&mut self) {} fn pause(&mut self) {}
fn set_frame_rate(&mut self, frame_rate: f64) {
let new_buffer_size =
((Self::NUM_CHANNELS as u32 * Self::SAMPLE_RATE) as f64 / frame_rate).round() as usize;
self.buffer.resize(new_buffer_size, 0.0);
}
fn tick(&mut self) {
debug_assert!(!self.buffer.is_empty());
self.mixer.mix::<f32>(self.buffer.as_mut());
}
} }
#[derive(Clone)] #[derive(Clone)]
@ -142,6 +144,7 @@ pub fn run_swf(
player.lock().unwrap().run_frame(); player.lock().unwrap().run_frame();
player.lock().unwrap().update_timers(frame_time); player.lock().unwrap().update_timers(frame_time);
player.lock().unwrap().audio_mut().tick();
executor.run(); executor.run();
injector.next(|evt, _btns_down| { injector.next(|evt, _btns_down| {