Skip to content

Commit 8dbe730

Browse files
committed
Simplify feedback example, clean up comments
1 parent d5bff40 commit 8dbe730

File tree

3 files changed

+14
-98
lines changed

3 files changed

+14
-98
lines changed

examples/feedback.rs

Lines changed: 12 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
extern crate coreaudio;
44

55
use std::collections::VecDeque;
6-
use std::mem;
7-
use std::ptr::null;
86
use std::sync::{Arc, Mutex};
97

108
use coreaudio::audio_unit::audio_format::LinearPcmFlags;
119
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};
1312
use coreaudio::sys::*;
1413

1514
const SAMPLE_RATE: f64 = 44100.0;
@@ -21,8 +20,8 @@ const SAMPLE_FORMAT: SampleFormat = SampleFormat::F32;
2120
// type S = i8; const SAMPLE_FORMAT: SampleFormat = SampleFormat::I8;
2221

2322
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)?;
2625

2726
let format_flag = match SAMPLE_FORMAT {
2827
SampleFormat::F32 => LinearPcmFlags::IS_FLOAT,
@@ -87,6 +86,10 @@ fn main() -> Result<(), coreaudio::Error> {
8786
mut data,
8887
..
8988
} = 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.
9093
println!("input cb {} frames", num_frames);
9194
let buffer_left = producer_left.lock().unwrap();
9295
let buffer_right = producer_right.lock().unwrap();
@@ -107,6 +110,10 @@ fn main() -> Result<(), coreaudio::Error> {
107110
mut data,
108111
..
109112
} = 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.
110117
println!("output cb {} frames", num_frames);
111118
let buffer_left = consumer_left.lock().unwrap();
112119
let buffer_right = consumer_right.lock().unwrap();
@@ -128,94 +135,3 @@ fn main() -> Result<(), coreaudio::Error> {
128135

129136
Ok(())
130137
}
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-
}

examples/feedback_interleaved.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn main() -> Result<(), coreaudio::Error> {
9999
num_frames, data, ..
100100
} = args;
101101
// Print the number of frames the callback requests.
102-
// Included to aid understanding, don't use println and other things
102+
// Included to aid understanding, don't use println and other things
103103
// that may block for an unknown amount of time inside the callback
104104
// of a real application.
105105
println!("input cb {} frames", num_frames);

examples/sine_advanced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn main() -> Result<(), coreaudio::Error> {
137137
num_frames, data, ..
138138
} = args;
139139
// Print the number of frames the callback requests.
140-
// Included to aid understanding, don't use println and other things
140+
// Included to aid understanding, don't use println and other things
141141
// that may block for an unknown amount of time inside the callback
142142
// of a real application.
143143
println!("frames: {}", num_frames);

0 commit comments

Comments
 (0)