audiooutput: specify number of frames of audio to buffer

This commit is contained in:
Alex Janka 2023-03-09 10:25:29 +11:00
parent 59d85ab371
commit 89dc1160ed
3 changed files with 15 additions and 6 deletions

View file

@ -132,7 +132,7 @@ fn create_audio_output() -> (AudioOutput, Stream) {
let sample_rate = config.sample_rate().0; let sample_rate = config.sample_rate().0;
let (output, mut rx) = AudioOutput::new_unfilled(sample_rate as f32, true); let (output, mut rx) = AudioOutput::new_unfilled(sample_rate as f32, true, 1);
let stream = device let stream = device
.build_output_stream( .build_output_stream(

View file

@ -32,6 +32,8 @@ pub struct GameboyEmu {
type FrameReceiver = Mutex<Option<Receiver<Vec<[u8; 4]>>>>; type FrameReceiver = Mutex<Option<Receiver<Vec<[u8; 4]>>>>;
type JoypadSender = Mutex<Option<Sender<(JoypadButtons, bool)>>>; type JoypadSender = Mutex<Option<Sender<(JoypadButtons, bool)>>>;
const FRAMES_TO_BUFFER: usize = 3;
impl Plugin for GameboyEmu { impl Plugin for GameboyEmu {
const NAME: &'static str = "Gameboy"; const NAME: &'static str = "Gameboy";
@ -118,14 +120,16 @@ impl Plugin for GameboyEmu {
}; };
if let Some(ref mut vars) = self.vars { if let Some(ref mut vars) = self.vars {
let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); let (output, rx) =
AudioOutput::new_unfilled(buffer_config.sample_rate, false, FRAMES_TO_BUFFER);
vars.emulator_core.replace_output(output); vars.emulator_core.replace_output(output);
vars.rx = rx; vars.rx = rx;
} else { } else {
let (sender, receiver) = channel::<EmulatorMessage>(); let (sender, receiver) = channel::<EmulatorMessage>();
let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); let (output, rx) =
AudioOutput::new_unfilled(buffer_config.sample_rate, false, FRAMES_TO_BUFFER);
let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new(); let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new();

View file

@ -46,8 +46,12 @@ pub struct AudioOutput {
} }
impl AudioOutput { impl AudioOutput {
pub fn new(sample_rate: f32, wait_for_output: bool) -> (Self, AsyncHeapConsumer<[f32; 2]>) { pub fn new(
let (mut output, rx) = Self::new_unfilled(sample_rate, wait_for_output); sample_rate: f32,
wait_for_output: bool,
frames_to_buffer: usize,
) -> (Self, AsyncHeapConsumer<[f32; 2]>) {
let (mut output, rx) = Self::new_unfilled(sample_rate, wait_for_output, frames_to_buffer);
executor::block_on( executor::block_on(
output output
@ -62,8 +66,9 @@ impl AudioOutput {
pub fn new_unfilled( pub fn new_unfilled(
sample_rate: f32, sample_rate: f32,
wait_for_output: bool, wait_for_output: bool,
frames_to_buffer: usize,
) -> (Self, AsyncHeapConsumer<[f32; 2]>) { ) -> (Self, AsyncHeapConsumer<[f32; 2]>) {
let rb_len = sample_rate as usize / 60; let rb_len = (sample_rate as usize / 60) * frames_to_buffer;
let rb = AsyncHeapRb::<[f32; 2]>::new(rb_len); let rb = AsyncHeapRb::<[f32; 2]>::new(rb_len);
let (send_rb, rx) = rb.split(); let (send_rb, rx) = rb.split();