1
1
/// This is a collection of helper functions for performing common tasks on macOS.
2
+ /// These functions are only implemented for macOS, not iOS.
2
3
use crate :: error:: Error ;
3
4
use std:: collections:: VecDeque ;
4
5
use std:: ffi:: CStr ;
@@ -32,8 +33,7 @@ use crate::audio_unit::sample_format::SampleFormat;
32
33
use crate :: audio_unit:: stream_format:: StreamFormat ;
33
34
use crate :: audio_unit:: { AudioUnit , Element , IOType , Scope } ;
34
35
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.
37
37
pub fn get_default_device_id ( input : bool ) -> Option < AudioDeviceID > {
38
38
let selector = if input {
39
39
kAudioHardwarePropertyDefaultInputDevice
@@ -65,8 +65,7 @@ pub fn get_default_device_id(input: bool) -> Option<AudioDeviceID> {
65
65
Some ( audio_device_id)
66
66
}
67
67
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.
70
69
pub fn get_device_id_from_name ( name : & str ) -> Option < AudioDeviceID > {
71
70
if let Ok ( all_ids) = get_audio_device_ids ( ) {
72
71
return all_ids
@@ -78,7 +77,6 @@ pub fn get_device_id_from_name(name: &str) -> Option<AudioDeviceID> {
78
77
}
79
78
80
79
/// Create an AudioUnit instance from a device id.
81
- /// Only implemented for macOS, not iOS.
82
80
pub fn audio_unit_from_device_id (
83
81
device_id : AudioDeviceID ,
84
82
input : bool ,
@@ -115,8 +113,7 @@ pub fn audio_unit_from_device_id(
115
113
Ok ( audio_unit)
116
114
}
117
115
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.
120
117
pub fn get_audio_device_ids ( ) -> Result < Vec < AudioDeviceID > , Error > {
121
118
let property_address = AudioObjectPropertyAddress {
122
119
mSelector : kAudioHardwarePropertyDevices,
@@ -163,8 +160,7 @@ pub fn get_audio_device_ids() -> Result<Vec<AudioDeviceID>, Error> {
163
160
Ok ( audio_devices)
164
161
}
165
162
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.
168
164
pub fn get_device_name ( device_id : AudioDeviceID ) -> Result < String , Error > {
169
165
let property_address = AudioObjectPropertyAddress {
170
166
mSelector : kAudioDevicePropertyDeviceNameCFString,
@@ -224,7 +220,6 @@ pub fn get_device_name(device_id: AudioDeviceID) -> Result<String, Error> {
224
220
225
221
/// Change the sample rate of a device.
226
222
/// Adapted from CPAL.
227
- /// Only implemented for macOS, not iOS.
228
223
pub fn set_device_sample_rate ( device_id : AudioDeviceID , new_rate : f64 ) -> Result < ( ) , Error > {
229
224
// Check whether or not we need to change the device sample rate to suit the one specified for the stream.
230
225
unsafe {
@@ -323,7 +318,7 @@ pub fn set_device_sample_rate(device_id: AudioDeviceID, new_rate: f64) -> Result
323
318
}
324
319
325
320
/// 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.
327
322
/// The provided format flags in the `StreamFormat` are ignored.
328
323
pub fn find_matching_physical_format (
329
324
device_id : AudioDeviceID ,
@@ -377,7 +372,6 @@ pub fn find_matching_physical_format(
377
372
}
378
373
379
374
/// Change the physical stream format (sample rate and format) of a device.
380
- /// Only implemented for macOS, not iOS.
381
375
pub fn set_device_physical_stream_format (
382
376
device_id : AudioDeviceID ,
383
377
new_asbd : AudioStreamBasicDescription ,
@@ -465,7 +459,6 @@ fn asbds_are_equal(
465
459
}
466
460
467
461
/// Get a vector with all supported physical formats as AudioBasicRangedDescriptions.
468
- /// Only implemented for macOS, not iOS.
469
462
pub fn get_supported_physical_stream_formats (
470
463
device_id : AudioDeviceID ,
471
464
) -> Result < Vec < AudioStreamRangedDescription > , Error > {
@@ -506,8 +499,7 @@ pub fn get_supported_physical_stream_formats(
506
499
}
507
500
508
501
/// 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.
511
503
pub struct RateListener {
512
504
pub queue : Mutex < VecDeque < f64 > > ,
513
505
sync_channel : Option < Sender < f64 > > ,
@@ -526,8 +518,9 @@ impl Drop for RateListener {
526
518
527
519
impl RateListener {
528
520
/// 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.
531
524
pub fn new ( device_id : AudioDeviceID , sync_channel : Option < Sender < f64 > > ) -> RateListener {
532
525
// Add our sample rate change listener callback.
533
526
let property_address = AudioObjectPropertyAddress {
@@ -592,7 +585,7 @@ impl RateListener {
592
585
Ok ( ( ) )
593
586
}
594
587
595
- /// Unregister this listener to stop receiving notifications
588
+ /// Unregister this listener to stop receiving notifications.
596
589
pub fn unregister ( & mut self ) -> Result < ( ) , Error > {
597
590
if self . rate_listener . is_some ( ) {
598
591
let status = unsafe {
@@ -610,12 +603,14 @@ impl RateListener {
610
603
}
611
604
612
605
/// 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`.
613
607
pub fn get_nbr_values ( & self ) -> usize {
614
608
self . queue . lock ( ) . unwrap ( ) . len ( )
615
609
}
616
610
617
611
/// Copy all received values to a Vec. The latest value is the last element.
618
612
/// The internal buffer is preserved.
613
+ /// Not used if the RateListener was created with a `std::sync::mpsc::Sender`.
619
614
pub fn copy_values ( & self ) -> Vec < f64 > {
620
615
self . queue
621
616
. lock ( )
@@ -627,13 +622,13 @@ impl RateListener {
627
622
628
623
/// Get all received values as a Vec. The latest value is the last element.
629
624
/// This clears the internal buffer.
625
+ /// Not used if the RateListener was created with a `std::sync::mpsc::Sender`.
630
626
pub fn drain_values ( & mut self ) -> Vec < f64 > {
631
627
self . queue . lock ( ) . unwrap ( ) . drain ( ..) . collect :: < Vec < f64 > > ( )
632
628
}
633
629
}
634
630
635
631
/// An AliveListener is used to get notified when a device is disconnected.
636
- /// Only implemented for macOS, not iOS.
637
632
pub struct AliveListener {
638
633
alive : Box < AtomicBool > ,
639
634
device_id : AudioDeviceID ,
@@ -651,6 +646,7 @@ impl Drop for AliveListener {
651
646
652
647
impl AliveListener {
653
648
/// Create a new AliveListener for the given AudioDeviceID.
649
+ /// The listener must be registered by calling `register()` in order to start receiving notifications.
654
650
pub fn new ( device_id : AudioDeviceID ) -> AliveListener {
655
651
// Add our listener callback.
656
652
let property_address = AudioObjectPropertyAddress {
0 commit comments