Skip to content

Commit b0932d0

Browse files
committed
Place helpers in their own module
1 parent 078a4df commit b0932d0

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

examples/feedback_interleaved.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::sync::{Arc, Mutex};
77

88
use coreaudio::audio_unit::audio_format::LinearPcmFlags;
99
use coreaudio::audio_unit::render_callback::{self, data};
10-
use coreaudio::audio_unit::{audio_unit_from_device_id, get_default_device_id, get_device_name};
11-
use coreaudio::audio_unit::{Element, RateListener, SampleFormat, Scope, StreamFormat};
10+
use coreaudio::audio_unit::macos_helpers::{audio_unit_from_device_id, get_default_device_id, get_device_name, RateListener};
11+
use coreaudio::audio_unit::{Element, SampleFormat, Scope, StreamFormat};
1212
use coreaudio::sys::*;
1313

1414
const SAMPLE_RATE: f64 = 44100.0;

examples/sine_advanced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate coreaudio;
44

55
use coreaudio::audio_unit::audio_format::LinearPcmFlags;
66
use coreaudio::audio_unit::render_callback::{self, data};
7-
use coreaudio::audio_unit::{
7+
use coreaudio::audio_unit::macos_helpers::{
88
audio_unit_from_device_id, find_matching_physical_format, get_default_device_id,
99
get_supported_physical_stream_formats, set_device_physical_stream_format,
1010
set_device_sample_rate, AliveListener, RateListener,

src/audio_unit/macos_helpers.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// This is a collection of helper functions for performing common tasks on macOS.
2+
/// These functions are only implemented for macOS, not iOS.
23
use crate::error::Error;
34
use std::collections::VecDeque;
45
use std::ffi::CStr;
@@ -32,8 +33,7 @@ use crate::audio_unit::sample_format::SampleFormat;
3233
use crate::audio_unit::stream_format::StreamFormat;
3334
use crate::audio_unit::{AudioUnit, Element, IOType, Scope};
3435

35-
/// Helper function to get the device id of the default input or output device
36-
/// Only implemented for macOS, not iOS.
36+
/// Helper function to get the device id of the default input or output device.
3737
pub fn get_default_device_id(input: bool) -> Option<AudioDeviceID> {
3838
let selector = if input {
3939
kAudioHardwarePropertyDefaultInputDevice
@@ -65,8 +65,7 @@ pub fn get_default_device_id(input: bool) -> Option<AudioDeviceID> {
6565
Some(audio_device_id)
6666
}
6767

68-
/// Helper function to get the device id of from a device name.
69-
/// Only implemented for macOS, not iOS.
68+
/// Find the device id for a device name.
7069
pub fn get_device_id_from_name(name: &str) -> Option<AudioDeviceID> {
7170
if let Ok(all_ids) = get_audio_device_ids() {
7271
return all_ids
@@ -78,7 +77,6 @@ pub fn get_device_id_from_name(name: &str) -> Option<AudioDeviceID> {
7877
}
7978

8079
/// Create an AudioUnit instance from a device id.
81-
/// Only implemented for macOS, not iOS.
8280
pub fn audio_unit_from_device_id(
8381
device_id: AudioDeviceID,
8482
input: bool,
@@ -115,8 +113,7 @@ pub fn audio_unit_from_device_id(
115113
Ok(audio_unit)
116114
}
117115

118-
/// Helper to list all audio device ids on the system.
119-
/// Only implemented for macOS, not iOS.
116+
/// List all audio device ids on the system.
120117
pub fn get_audio_device_ids() -> Result<Vec<AudioDeviceID>, Error> {
121118
let property_address = AudioObjectPropertyAddress {
122119
mSelector: kAudioHardwarePropertyDevices,
@@ -163,8 +160,7 @@ pub fn get_audio_device_ids() -> Result<Vec<AudioDeviceID>, Error> {
163160
Ok(audio_devices)
164161
}
165162

166-
/// Get the device name for the device id.
167-
/// Only implemented for macOS, not iOS.
163+
/// Get the device name for a device id.
168164
pub fn get_device_name(device_id: AudioDeviceID) -> Result<String, Error> {
169165
let property_address = AudioObjectPropertyAddress {
170166
mSelector: kAudioDevicePropertyDeviceNameCFString,
@@ -224,7 +220,6 @@ pub fn get_device_name(device_id: AudioDeviceID) -> Result<String, Error> {
224220

225221
/// Change the sample rate of a device.
226222
/// Adapted from CPAL.
227-
/// Only implemented for macOS, not iOS.
228223
pub fn set_device_sample_rate(device_id: AudioDeviceID, new_rate: f64) -> Result<(), Error> {
229224
// Check whether or not we need to change the device sample rate to suit the one specified for the stream.
230225
unsafe {
@@ -323,7 +318,7 @@ pub fn set_device_sample_rate(device_id: AudioDeviceID, new_rate: f64) -> Result
323318
}
324319

325320
/// Find the closest match of the physical formats to the provided `StreamFormat`.
326-
/// It will pick the first format it finds that supports the provided sample format, rate and number of channels.
321+
/// This function will pick the first format it finds that supports the provided sample format, rate and number of channels.
327322
/// The provided format flags in the `StreamFormat` are ignored.
328323
pub fn find_matching_physical_format(
329324
device_id: AudioDeviceID,
@@ -377,7 +372,6 @@ pub fn find_matching_physical_format(
377372
}
378373

379374
/// Change the physical stream format (sample rate and format) of a device.
380-
/// Only implemented for macOS, not iOS.
381375
pub fn set_device_physical_stream_format(
382376
device_id: AudioDeviceID,
383377
new_asbd: AudioStreamBasicDescription,
@@ -465,7 +459,6 @@ fn asbds_are_equal(
465459
}
466460

467461
/// Get a vector with all supported physical formats as AudioBasicRangedDescriptions.
468-
/// Only implemented for macOS, not iOS.
469462
pub fn get_supported_physical_stream_formats(
470463
device_id: AudioDeviceID,
471464
) -> Result<Vec<AudioStreamRangedDescription>, Error> {
@@ -506,8 +499,7 @@ pub fn get_supported_physical_stream_formats(
506499
}
507500

508501
/// Changing the sample rate is an asynchonous process.
509-
/// Use a RateListener to get notified when the rate is changed.
510-
/// Only implemented for macOS, not iOS.
502+
/// A RateListener can be used to get notified when the rate is changed.
511503
pub struct RateListener {
512504
pub queue: Mutex<VecDeque<f64>>,
513505
sync_channel: Option<Sender<f64>>,
@@ -526,8 +518,9 @@ impl Drop for RateListener {
526518

527519
impl RateListener {
528520
/// Create a new RateListener for the given AudioDeviceID.
529-
/// If a sync Sender is provided, then events will be pushed to that channel.
530-
/// If not, they will be stored in an internal queue that will need to be polled.
521+
/// If an `std::sync::mpsc::Sender` is provided, then events will be pushed to that channel.
522+
/// If not, they will instead be stored in an internal queue that will need to be polled.
523+
/// The listener must be registered by calling `register()` in order to start receiving notifications.
531524
pub fn new(device_id: AudioDeviceID, sync_channel: Option<Sender<f64>>) -> RateListener {
532525
// Add our sample rate change listener callback.
533526
let property_address = AudioObjectPropertyAddress {
@@ -592,7 +585,7 @@ impl RateListener {
592585
Ok(())
593586
}
594587

595-
/// Unregister this listener to stop receiving notifications
588+
/// Unregister this listener to stop receiving notifications.
596589
pub fn unregister(&mut self) -> Result<(), Error> {
597590
if self.rate_listener.is_some() {
598591
let status = unsafe {
@@ -610,12 +603,14 @@ impl RateListener {
610603
}
611604

612605
/// Get the number of sample rate values received (equals the number of change events).
606+
/// Not used if the RateListener was created with a `std::sync::mpsc::Sender`.
613607
pub fn get_nbr_values(&self) -> usize {
614608
self.queue.lock().unwrap().len()
615609
}
616610

617611
/// Copy all received values to a Vec. The latest value is the last element.
618612
/// The internal buffer is preserved.
613+
/// Not used if the RateListener was created with a `std::sync::mpsc::Sender`.
619614
pub fn copy_values(&self) -> Vec<f64> {
620615
self.queue
621616
.lock()
@@ -627,13 +622,13 @@ impl RateListener {
627622

628623
/// Get all received values as a Vec. The latest value is the last element.
629624
/// This clears the internal buffer.
625+
/// Not used if the RateListener was created with a `std::sync::mpsc::Sender`.
630626
pub fn drain_values(&mut self) -> Vec<f64> {
631627
self.queue.lock().unwrap().drain(..).collect::<Vec<f64>>()
632628
}
633629
}
634630

635631
/// An AliveListener is used to get notified when a device is disconnected.
636-
/// Only implemented for macOS, not iOS.
637632
pub struct AliveListener {
638633
alive: Box<AtomicBool>,
639634
device_id: AudioDeviceID,
@@ -651,6 +646,7 @@ impl Drop for AliveListener {
651646

652647
impl AliveListener {
653648
/// Create a new AliveListener for the given AudioDeviceID.
649+
/// The listener must be registered by calling `register()` in order to start receiving notifications.
654650
pub fn new(device_id: AudioDeviceID) -> AliveListener {
655651
// Add our listener callback.
656652
let property_address = AudioObjectPropertyAddress {

src/audio_unit/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ pub use self::types::{
3333
};
3434

3535
#[cfg(target_os = "macos")]
36-
mod macos_helpers;
37-
#[cfg(target_os = "macos")]
38-
pub use self::macos_helpers::*;
36+
pub mod macos_helpers;
3937

4038
pub mod audio_format;
4139
pub mod render_callback;

0 commit comments

Comments
 (0)