Skip to content

Commit 6e9e791

Browse files
committed
iOS default sample rate pulled from audio unit
1 parent 83ef760 commit 6e9e791

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed

examples/ios-feedback/src/feedback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ pub fn run_example() -> Result<(), anyhow::Error> {
8282
"Attempting to build both streams with f32 samples and `{:?}`.",
8383
config
8484
);
85-
println!("setup is");
85+
println!("Setup input stream");
8686
let input_stream = input_device.build_input_stream(&config, input_data_fn, err_fn)?;
87-
println!("setup os");
87+
println!("Setup output stream");
8888
let output_stream = output_device.build_output_stream(&config, output_data_fn, err_fn)?;
8989
println!("Successfully built streams.");
9090

src/host/coreaudio/ios/mod.rs

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
//! the AVAudioSession objc API which doesn't exist on macOS.
44
//!
55
//! TODO:
6-
//! - Use AVAudioSession to enumerate (and set) buffer size / sample rate / number of channels
6+
//! - Use AVAudioSession to enumerate buffer size / sample rate / number of channels and set
7+
//! buffer size.
78
//!
89
910
extern crate core_foundation_sys;
@@ -36,8 +37,6 @@ use std::slice;
3637

3738
pub mod enumerate;
3839

39-
const DEFAULT_SAMPLE_RATE: SampleRate = SampleRate(44_100);
40-
4140
// These days the default of iOS is now F32 and no longer I16
4241
const SUPPORTED_SAMPLE_FORMAT: SampleFormat = SampleFormat::F32;
4342

@@ -84,26 +83,15 @@ impl Device {
8483
&self,
8584
) -> Result<SupportedInputConfigs, SupportedStreamConfigsError> {
8685
// TODO: query AVAudioSession for parameters, some values like sample rate and buffer size
87-
// probably need to be tested but channels can be enumerated.
88-
89-
// setup an audio unit for recording, and then pull some default parameters off it
90-
91-
let mut audio_unit = create_audio_unit()?;
92-
audio_unit.uninitialize()?;
93-
configure_for_recording(&mut audio_unit)?;
94-
audio_unit.initialize()?;
95-
96-
let id = kAudioUnitProperty_StreamFormat;
97-
let asbd: AudioStreamBasicDescription =
98-
audio_unit.get_property(id, Scope::Input, Element::Input)?;
99-
100-
let buffer_size = SupportedBufferSize::Range { min: 0, max: 0 };
86+
// probably need to actually be set to see if it works, but channels can be enumerated.
10187

88+
let asbd: AudioStreamBasicDescription = default_input_asbd()?;
89+
let stream_config = stream_config_from_asbd(asbd);
10290
Ok(vec![SupportedStreamConfigRange {
103-
channels: asbd.mChannelsPerFrame as u16,
104-
min_sample_rate: SampleRate(asbd.mSampleRate as u32),
105-
max_sample_rate: SampleRate(asbd.mSampleRate as u32),
106-
buffer_size: buffer_size.clone(),
91+
channels: stream_config.channels,
92+
min_sample_rate: stream_config.sample_rate,
93+
max_sample_rate: stream_config.sample_rate,
94+
buffer_size: stream_config.buffer_size.clone(),
10795
sample_format: SUPPORTED_SAMPLE_FORMAT,
10896
}]
10997
.into_iter())
@@ -114,22 +102,17 @@ impl Device {
114102
&self,
115103
) -> Result<SupportedOutputConfigs, SupportedStreamConfigsError> {
116104
// TODO: query AVAudioSession for parameters, some values like sample rate and buffer size
117-
// probably need to be tested but channels can be enumerated.
105+
// probably need to actually be set to see if it works, but channels can be enumerated.
118106

119-
// setup an audio unit, and then pull some default parameters off it
107+
let asbd: AudioStreamBasicDescription = default_output_asbd()?;
108+
let stream_config = stream_config_from_asbd(asbd);
120109

121-
let audio_unit = create_audio_unit()?;
122-
let id = kAudioUnitProperty_StreamFormat;
123-
let asbd: AudioStreamBasicDescription =
124-
audio_unit.get_property(id, Scope::Output, Element::Output)?;
125-
126-
let buffer_size = SupportedBufferSize::Range { min: 0, max: 0 };
127110
let configs: Vec<_> = (1..=asbd.mChannelsPerFrame as u16)
128111
.map(|channels| SupportedStreamConfigRange {
129112
channels,
130-
min_sample_rate: SampleRate(asbd.mSampleRate as u32),
131-
max_sample_rate: SampleRate(asbd.mSampleRate as u32),
132-
buffer_size: buffer_size.clone(),
113+
min_sample_rate: stream_config.sample_rate,
114+
max_sample_rate: stream_config.sample_rate,
115+
buffer_size: stream_config.buffer_size.clone(),
133116
sample_format: SUPPORTED_SAMPLE_FORMAT,
134117
})
135118
.collect();
@@ -138,28 +121,16 @@ impl Device {
138121

139122
#[inline]
140123
fn default_input_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> {
141-
const EXPECT: &str = "expected at least one valid coreaudio stream config";
142-
let config = self
143-
.supported_input_configs()
144-
.expect(EXPECT)
145-
.max_by(|a, b| a.cmp_default_heuristics(b))
146-
.unwrap()
147-
.with_sample_rate(DEFAULT_SAMPLE_RATE);
148-
149-
Ok(config)
124+
let asbd: AudioStreamBasicDescription = default_input_asbd()?;
125+
let stream_config = stream_config_from_asbd(asbd);
126+
Ok(stream_config)
150127
}
151128

152129
#[inline]
153130
fn default_output_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> {
154-
const EXPECT: &str = "expected at least one valid coreaudio stream config";
155-
let config = self
156-
.supported_output_configs()
157-
.expect(EXPECT)
158-
.max_by(|a, b| a.cmp_default_heuristics(b))
159-
.unwrap()
160-
.with_sample_rate(DEFAULT_SAMPLE_RATE);
161-
162-
Ok(config)
131+
let asbd: AudioStreamBasicDescription = default_output_asbd()?;
132+
let stream_config = stream_config_from_asbd(asbd);
133+
Ok(stream_config)
163134
}
164135
}
165136

@@ -428,3 +399,33 @@ fn configure_for_recording(audio_unit: &mut AudioUnit) -> Result<(), coreaudio::
428399

429400
Ok(())
430401
}
402+
403+
fn default_output_asbd() -> Result<AudioStreamBasicDescription, coreaudio::Error> {
404+
let audio_unit = create_audio_unit()?;
405+
let id = kAudioUnitProperty_StreamFormat;
406+
let asbd: AudioStreamBasicDescription =
407+
audio_unit.get_property(id, Scope::Output, Element::Output)?;
408+
Ok(asbd)
409+
}
410+
411+
fn default_input_asbd() -> Result<AudioStreamBasicDescription, coreaudio::Error> {
412+
let mut audio_unit = create_audio_unit()?;
413+
audio_unit.uninitialize()?;
414+
configure_for_recording(&mut audio_unit)?;
415+
audio_unit.initialize()?;
416+
417+
let id = kAudioUnitProperty_StreamFormat;
418+
let asbd: AudioStreamBasicDescription =
419+
audio_unit.get_property(id, Scope::Input, Element::Input)?;
420+
Ok(asbd)
421+
}
422+
423+
fn stream_config_from_asbd(asbd: AudioStreamBasicDescription) -> SupportedStreamConfig {
424+
let buffer_size = SupportedBufferSize::Range { min: 0, max: 0 };
425+
SupportedStreamConfig {
426+
channels: asbd.mChannelsPerFrame as u16,
427+
sample_rate: SampleRate(asbd.mSampleRate as u32),
428+
buffer_size: buffer_size.clone(),
429+
sample_format: SUPPORTED_SAMPLE_FORMAT,
430+
}
431+
}

0 commit comments

Comments
 (0)