3
3
//! the AVAudioSession objc API which doesn't exist on macOS.
4
4
//!
5
5
//! 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.
7
8
//!
8
9
9
10
extern crate core_foundation_sys;
@@ -36,8 +37,6 @@ use std::slice;
36
37
37
38
pub mod enumerate;
38
39
39
- const DEFAULT_SAMPLE_RATE : SampleRate = SampleRate ( 44_100 ) ;
40
-
41
40
// These days the default of iOS is now F32 and no longer I16
42
41
const SUPPORTED_SAMPLE_FORMAT : SampleFormat = SampleFormat :: F32 ;
43
42
@@ -84,26 +83,15 @@ impl Device {
84
83
& self ,
85
84
) -> Result < SupportedInputConfigs , SupportedStreamConfigsError > {
86
85
// 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.
101
87
88
+ let asbd: AudioStreamBasicDescription = default_input_asbd ( ) ?;
89
+ let stream_config = stream_config_from_asbd ( asbd) ;
102
90
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( ) ,
107
95
sample_format: SUPPORTED_SAMPLE_FORMAT ,
108
96
} ]
109
97
. into_iter ( ) )
@@ -114,22 +102,17 @@ impl Device {
114
102
& self ,
115
103
) -> Result < SupportedOutputConfigs , SupportedStreamConfigsError > {
116
104
// 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.
118
106
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) ;
120
109
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 } ;
127
110
let configs: Vec < _ > = ( 1 ..=asbd. mChannelsPerFrame as u16 )
128
111
. map ( |channels| SupportedStreamConfigRange {
129
112
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 ( ) ,
133
116
sample_format : SUPPORTED_SAMPLE_FORMAT ,
134
117
} )
135
118
. collect ( ) ;
@@ -138,28 +121,16 @@ impl Device {
138
121
139
122
#[ inline]
140
123
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)
150
127
}
151
128
152
129
#[ inline]
153
130
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)
163
134
}
164
135
}
165
136
@@ -428,3 +399,33 @@ fn configure_for_recording(audio_unit: &mut AudioUnit) -> Result<(), coreaudio::
428
399
429
400
Ok ( ( ) )
430
401
}
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