From 4cd9d0bcec129b8b1a9d7e0c09221405c518f0f9 Mon Sep 17 00:00:00 2001 From: JoshuaBatty Date: Fri, 22 May 2020 14:03:28 +0200 Subject: [PATCH] finished implementing buffersizes for coreaudio --- examples/beep.rs | 9 +++--- src/host/coreaudio/mod.rs | 65 ++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/examples/beep.rs b/examples/beep.rs index 6312d25..0a80a80 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -4,9 +4,9 @@ extern crate cpal; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; fn main() -> Result<(), anyhow::Error> { - //let host = cpal::default_host(); - let host = - cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host"); + let host = cpal::default_host(); + //let host = + // cpal::host_from_id(cpal::platform::HostId::Asio).expect("failed to initialise ASIO host"); let device = host .default_output_device() .expect("failed to find a default output device"); @@ -26,7 +26,7 @@ where T: cpal::Sample, { let mut config: cpal::StreamConfig = config.clone(); - config.buffer_size = cpal::BufferSize::Fixed(256); + config.buffer_size = cpal::BufferSize::Fixed(16); let sample_rate = config.sample_rate.0 as f32; let channels = config.channels as usize; @@ -43,6 +43,7 @@ where let stream = device.build_output_stream( &config, move |data: &mut [T], _: &cpal::OutputCallbackInfo| { + println!("data len = {}", data.len()); write_data(data, channels, &mut next_value) }, err_fn, diff --git a/src/host/coreaudio/mod.rs b/src/host/coreaudio/mod.rs index 5010901..bb1f8ed 100644 --- a/src/host/coreaudio/mod.rs +++ b/src/host/coreaudio/mod.rs @@ -278,11 +278,8 @@ impl Device { let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _; let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges); - let mut audio_unit = audio_unit_from_device(self, true)?; - let buffer_size = match get_io_buffer_frame_size_range(&audio_unit) { - Ok(v) => v.unwrap(), - Err(_) => return SupportedStreamConfigsError::DeviceNotAvailable, - }; + let audio_unit = audio_unit_from_device(self, true)?; + let buffer_size = get_io_buffer_frame_size_range(&audio_unit)?; // Collect the supported formats for the device. let mut fmts = vec![]; @@ -291,7 +288,7 @@ impl Device { channels: n_channels as ChannelCount, min_sample_rate: SampleRate(range.mMinimum as _), max_sample_rate: SampleRate(range.mMaximum as _), - buffer_size, + buffer_size: buffer_size.clone(), sample_format: sample_format, }; fmts.push(fmt); @@ -439,6 +436,24 @@ impl From for BuildStreamError { } } +impl From for SupportedStreamConfigsError { + fn from(err: coreaudio::Error) -> SupportedStreamConfigsError { + let description = format!("{}", err); + let err = BackendSpecificError { description }; + // Check for possible DeviceNotAvailable variant + SupportedStreamConfigsError::BackendSpecific { err } + } +} + +impl From for DefaultStreamConfigError { + fn from(err: coreaudio::Error) -> DefaultStreamConfigError { + let description = format!("{}", err); + let err = BackendSpecificError { description }; + // Check for possible DeviceNotAvailable variant + DefaultStreamConfigError::BackendSpecific { err } + } +} + // Create a coreaudio AudioStreamBasicDescription from a CPAL Format. fn asbd_from_config( config: &StreamConfig, @@ -671,12 +686,19 @@ impl Device { // Set the buffersize match config.buffer_size { - BufferSize::Fixed(v) => audio_unit.set_property( - kAudioDevicePropertyBufferFrameSize, - scope, - element, - Some(&v), - )?, + BufferSize::Fixed(v) => { + let buffer_size_range = get_io_buffer_frame_size_range(&audio_unit)?; + if v >= buffer_size_range.min && v <= buffer_size_range.max { + audio_unit.set_property( + kAudioDevicePropertyBufferFrameSize, + scope, + element, + Some(&v), + )? + } else { + return Err(BuildStreamError::StreamConfigNotSupported); + } + } BufferSize::Default => (), } @@ -753,12 +775,19 @@ impl Device { // Set the buffersize match config.buffer_size { - BufferSize::Fixed(v) => audio_unit.set_property( - kAudioDevicePropertyBufferFrameSize, - scope, - element, - Some(&v), - )?, + BufferSize::Fixed(v) => { + let buffer_size_range = get_io_buffer_frame_size_range(&audio_unit)?; + if v >= buffer_size_range.min && v <= buffer_size_range.max { + audio_unit.set_property( + kAudioDevicePropertyBufferFrameSize, + scope, + element, + Some(&v), + )? + } else { + return Err(BuildStreamError::StreamConfigNotSupported); + } + } BufferSize::Default => (), }