3
3
extern crate coreaudio;
4
4
5
5
use std:: collections:: VecDeque ;
6
- use std:: mem;
7
- use std:: ptr:: null;
8
6
use std:: sync:: { Arc , Mutex } ;
9
7
10
8
use coreaudio:: audio_unit:: audio_format:: LinearPcmFlags ;
11
9
use coreaudio:: audio_unit:: render_callback:: { self , data} ;
12
- use coreaudio:: audio_unit:: { AudioUnit , Element , SampleFormat , Scope , StreamFormat } ;
10
+ use coreaudio:: audio_unit:: { Element , SampleFormat , Scope , StreamFormat } ;
11
+ use coreaudio:: audio_unit:: macos_helpers:: { audio_unit_from_device_id, get_default_device_id} ;
13
12
use coreaudio:: sys:: * ;
14
13
15
14
const SAMPLE_RATE : f64 = 44100.0 ;
@@ -21,8 +20,8 @@ const SAMPLE_FORMAT: SampleFormat = SampleFormat::F32;
21
20
// type S = i8; const SAMPLE_FORMAT: SampleFormat = SampleFormat::I8;
22
21
23
22
fn main ( ) -> Result < ( ) , coreaudio:: Error > {
24
- let mut input_audio_unit = audio_unit_from_device ( default_input_device ( ) . unwrap ( ) , true ) ?;
25
- let mut output_audio_unit = audio_unit_from_device ( default_output_device ( ) . unwrap ( ) , false ) ?;
23
+ let mut input_audio_unit = audio_unit_from_device_id ( get_default_device_id ( true ) . unwrap ( ) , true ) ?;
24
+ let mut output_audio_unit = audio_unit_from_device_id ( get_default_device_id ( false ) . unwrap ( ) , false ) ?;
26
25
27
26
let format_flag = match SAMPLE_FORMAT {
28
27
SampleFormat :: F32 => LinearPcmFlags :: IS_FLOAT ,
@@ -87,6 +86,10 @@ fn main() -> Result<(), coreaudio::Error> {
87
86
mut data,
88
87
..
89
88
} = args;
89
+ // Print the number of frames the callback provides.
90
+ // Included to aid understanding, don't use println and other things
91
+ // that may block for an unknown amount of time inside the callback
92
+ // of a real application.
90
93
println ! ( "input cb {} frames" , num_frames) ;
91
94
let buffer_left = producer_left. lock ( ) . unwrap ( ) ;
92
95
let buffer_right = producer_right. lock ( ) . unwrap ( ) ;
@@ -107,6 +110,10 @@ fn main() -> Result<(), coreaudio::Error> {
107
110
mut data,
108
111
..
109
112
} = args;
113
+ // Print the number of frames the callback requests.
114
+ // Included to aid understanding, don't use println and other things
115
+ // that may block for an unknown amount of time inside the callback
116
+ // of a real application.
110
117
println ! ( "output cb {} frames" , num_frames) ;
111
118
let buffer_left = consumer_left. lock ( ) . unwrap ( ) ;
112
119
let buffer_right = consumer_right. lock ( ) . unwrap ( ) ;
@@ -128,94 +135,3 @@ fn main() -> Result<(), coreaudio::Error> {
128
135
129
136
Ok ( ( ) )
130
137
}
131
-
132
- /// Copied from cpal
133
- pub fn default_input_device ( ) -> Option < AudioDeviceID > {
134
- let property_address = AudioObjectPropertyAddress {
135
- mSelector : kAudioHardwarePropertyDefaultInputDevice,
136
- mScope : kAudioObjectPropertyScopeGlobal,
137
- mElement : kAudioObjectPropertyElementMaster,
138
- } ;
139
-
140
- let audio_device_id: AudioDeviceID = 0 ;
141
- let data_size = mem:: size_of :: < AudioDeviceID > ( ) ;
142
- let status = unsafe {
143
- AudioObjectGetPropertyData (
144
- kAudioObjectSystemObject,
145
- & property_address as * const _ ,
146
- 0 ,
147
- null ( ) ,
148
- & data_size as * const _ as * mut _ ,
149
- & audio_device_id as * const _ as * mut _ ,
150
- )
151
- } ;
152
- if status != kAudioHardwareNoError as i32 {
153
- return None ;
154
- }
155
-
156
- Some ( audio_device_id)
157
- }
158
-
159
- /// Copied from cpal
160
- pub fn default_output_device ( ) -> Option < AudioDeviceID > {
161
- let property_address = AudioObjectPropertyAddress {
162
- mSelector : kAudioHardwarePropertyDefaultOutputDevice,
163
- mScope : kAudioObjectPropertyScopeGlobal,
164
- mElement : kAudioObjectPropertyElementMaster,
165
- } ;
166
-
167
- let audio_device_id: AudioDeviceID = 0 ;
168
- let data_size = mem:: size_of :: < AudioDeviceID > ( ) ;
169
- let status = unsafe {
170
- AudioObjectGetPropertyData (
171
- kAudioObjectSystemObject,
172
- & property_address as * const _ ,
173
- 0 ,
174
- null ( ) ,
175
- & data_size as * const _ as * mut _ ,
176
- & audio_device_id as * const _ as * mut _ ,
177
- )
178
- } ;
179
- if status != kAudioHardwareNoError as i32 {
180
- return None ;
181
- }
182
-
183
- Some ( audio_device_id)
184
- }
185
-
186
- /// Copied from cpal
187
- fn audio_unit_from_device (
188
- device_id : AudioDeviceID ,
189
- input : bool ,
190
- ) -> Result < AudioUnit , coreaudio:: Error > {
191
- let mut audio_unit = AudioUnit :: new ( coreaudio:: audio_unit:: IOType :: HalOutput ) ?;
192
-
193
- if input {
194
- // Enable input processing.
195
- let enable_input = 1u32 ;
196
- audio_unit. set_property (
197
- kAudioOutputUnitProperty_EnableIO,
198
- Scope :: Input ,
199
- Element :: Input ,
200
- Some ( & enable_input) ,
201
- ) ?;
202
-
203
- // Disable output processing.
204
- let disable_output = 0u32 ;
205
- audio_unit. set_property (
206
- kAudioOutputUnitProperty_EnableIO,
207
- Scope :: Output ,
208
- Element :: Output ,
209
- Some ( & disable_output) ,
210
- ) ?;
211
- }
212
-
213
- audio_unit. set_property (
214
- kAudioOutputUnitProperty_CurrentDevice,
215
- Scope :: Global ,
216
- Element :: Output ,
217
- Some ( & device_id) ,
218
- ) ?;
219
-
220
- Ok ( audio_unit)
221
- }
0 commit comments