From 741099dfc648108e4b29063949a5f95c16b7aa0d Mon Sep 17 00:00:00 2001 From: Michael Hills Date: Sun, 25 Oct 2020 00:58:02 +1100 Subject: [PATCH] Tweak iOS example to use sample rate from the output device (old devices use 44.1 new devices use 48) --- examples/ios/src/feedback.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/ios/src/feedback.rs b/examples/ios/src/feedback.rs index cd3fcbc44..d0bca8e47 100644 --- a/examples/ios/src/feedback.rs +++ b/examples/ios/src/feedback.rs @@ -10,7 +10,6 @@ use coreaudio::audio_unit::audio_format::LinearPcmFlags; use coreaudio::audio_unit::render_callback::{self, data}; use coreaudio::sys::*; -const SAMPLE_RATE: f64 = 44100.0; type S = f32; const SAMPLE_FORMAT: SampleFormat = SampleFormat::F32; // type S = i32; const SAMPLE_FORMAT: SampleFormat = SampleFormat::I32; @@ -21,6 +20,12 @@ pub fn run_example() -> Result<(), coreaudio::Error> { let mut input_audio_unit = AudioUnit::new(coreaudio::audio_unit::IOType::RemoteIO)?; let mut output_audio_unit = AudioUnit::new(coreaudio::audio_unit::IOType::RemoteIO)?; + // Read device sample rate off the output stream + let id = kAudioUnitProperty_StreamFormat; + let asbd: AudioStreamBasicDescription = + output_audio_unit.get_property(id, Scope::Output, Element::Output)?; + let sample_rate = asbd.mSampleRate; + // iOS doesn't let you reconfigure an "initialized" audio unit, so uninitialize them input_audio_unit.uninitialize()?; output_audio_unit.uninitialize()?; @@ -34,7 +39,7 @@ pub fn run_example() -> Result<(), coreaudio::Error> { // Using IS_NON_INTERLEAVED everywhere because data::Interleaved is commented out / not implemented let in_stream_format = StreamFormat { - sample_rate: SAMPLE_RATE, + sample_rate: sample_rate, sample_format: SAMPLE_FORMAT, flags: format_flag | LinearPcmFlags::IS_PACKED | LinearPcmFlags::IS_NON_INTERLEAVED, // audio_unit.set_input_callback is hardcoded to 1 buffer, and when using non_interleaved @@ -43,7 +48,7 @@ pub fn run_example() -> Result<(), coreaudio::Error> { }; let out_stream_format = StreamFormat { - sample_rate: SAMPLE_RATE, + sample_rate: sample_rate, sample_format: SAMPLE_FORMAT, flags: format_flag | LinearPcmFlags::IS_PACKED | LinearPcmFlags::IS_NON_INTERLEAVED, // you can change this to 1