Skip to content

Commit dacf71e

Browse files
committed
Add helper to get device id from name
1 parent 979951f commit dacf71e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

examples/feedback_interleaved.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex};
88
use coreaudio::audio_unit::audio_format::LinearPcmFlags;
99
use coreaudio::audio_unit::render_callback::{self, data};
1010
use coreaudio::audio_unit::{audio_unit_from_device_id, get_default_device_id, get_device_name};
11-
use coreaudio::audio_unit::{Element, SampleFormat, Scope, StreamFormat, RateListener};
11+
use coreaudio::audio_unit::{Element, RateListener, SampleFormat, Scope, StreamFormat};
1212
use coreaudio::sys::*;
1313

1414
const SAMPLE_RATE: f64 = 44100.0;
@@ -76,7 +76,7 @@ fn main() -> Result<(), coreaudio::Error> {
7676
// Register a rate listener for playback
7777
let mut listener_pb = RateListener::new(output_device_id, None)?;
7878
listener_pb.register()?;
79-
79+
8080
// Register a rate listener for capture
8181
let mut listener_cap = RateListener::new(input_device_id, None)?;
8282
listener_cap.register()?;

src/audio_unit/macos_helpers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ pub fn get_default_device_id(input: bool) -> Option<AudioDeviceID> {
6161
Some(audio_device_id)
6262
}
6363

64+
/// Helper function to get the device id of from a device name.
65+
/// Only implemented for macOS, not iOS.
66+
pub fn get_device_id_from_name(name: &str) -> Option<AudioDeviceID> {
67+
if let Ok(all_ids) = get_audio_device_ids() {
68+
return all_ids
69+
.iter()
70+
.find(|id| get_device_name(**id).unwrap_or("".to_string()) == name)
71+
.map(|id| *id);
72+
} else {
73+
return None;
74+
}
75+
}
76+
6477
/// Create an AudioUnit instance from a device id.
6578
/// Only implemented for macOS, not iOS.
6679
pub fn audio_unit_from_device_id(

0 commit comments

Comments
 (0)