diff --git a/gb-emu/src/audio.rs b/gb-emu/src/audio.rs index 0cef9c5..53f9ba8 100644 --- a/gb-emu/src/audio.rs +++ b/gb-emu/src/audio.rs @@ -6,6 +6,7 @@ use futures::executor; use gb_emu_lib::connect::{AudioOutput, DownsampleType}; const BUFFERS_PER_FRAME: usize = 5; +const DEFAULT_BUFFER_SIZE: u32 = 64; const DOWNSAMPLE_TYPE: DownsampleType = DownsampleType::ZeroOrderHold; pub fn create_output(muted: bool) -> (AudioOutput, Stream) { @@ -21,12 +22,20 @@ pub fn create_output(muted: bool) -> (AudioOutput, Stream) { let sample_rate = config.sample_rate().0; + let mut stream_config = config.config(); + + if let cpal::SupportedBufferSize::Range { min, max } = config.buffer_size() { + stream_config.buffer_size = + cpal::BufferSize::Fixed(DEFAULT_BUFFER_SIZE.min(*max).max(*min)); + } + println!("got buffer size {:#?}", stream_config.buffer_size); + let (output, mut rx) = AudioOutput::new(sample_rate as f32, BUFFERS_PER_FRAME, DOWNSAMPLE_TYPE); let stream = if muted { device .build_output_stream( - &config.config(), + &stream_config, move |data: &mut [f32], _info: &cpal::OutputCallbackInfo| { for _ in data.chunks_exact_mut(2) { match executor::block_on(rx.pop()) { @@ -60,6 +69,7 @@ pub fn create_output(muted: bool) -> (AudioOutput, Stream) { ) .unwrap() }; + stream.play().unwrap(); (output, stream)