Skip to content
3 changes: 0 additions & 3 deletions src/host/coreaudio/macos/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl Iterator for Devices {
fn next(&mut self) -> Option<Device> {
self.0.next().map(|id| Device {
audio_device_id: id,
is_default: false,
})
}
}
Expand Down Expand Up @@ -111,7 +110,6 @@ pub fn default_input_device() -> Option<Device> {

let device = Device {
audio_device_id,
is_default: true,
};
Some(device)
}
Expand Down Expand Up @@ -141,7 +139,6 @@ pub fn default_output_device() -> Option<Device> {

let device = Device {
audio_device_id,
is_default: true,
};
Some(device)
}
Expand Down
28 changes: 18 additions & 10 deletions src/host/coreaudio/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use objc2_core_audio::{
kAudioDevicePropertyNominalSampleRate, kAudioDevicePropertyStreamConfiguration,
kAudioDevicePropertyStreamFormat, kAudioObjectPropertyElementMaster,
kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeInput,
kAudioObjectPropertyScopeOutput, AudioDeviceID, AudioObjectGetPropertyData,
AudioObjectGetPropertyDataSize, AudioObjectID, AudioObjectPropertyAddress,
AudioObjectPropertyScope, AudioObjectSetPropertyData,
kAudioObjectPropertyScopeOutput, AudioDeviceID,
AudioObjectGetPropertyData, AudioObjectGetPropertyDataSize, AudioObjectID,
AudioObjectPropertyAddress, AudioObjectPropertyScope, AudioObjectSetPropertyData,
};
use objc2_core_audio_types::{
AudioBuffer, AudioBufferList, AudioStreamBasicDescription, AudioValueRange,
Expand Down Expand Up @@ -159,17 +159,25 @@ impl DeviceTrait for Device {
#[derive(Clone, PartialEq, Eq)]
pub struct Device {
pub(crate) audio_device_id: AudioDeviceID,
is_default: bool,
}

fn is_default_device(device: Device) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take a &Device here so we don't need to clone in audio_unit_from_device?

Copy link

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function should accept &Device instead of taking ownership of Device to avoid unnecessary clones at call sites.

Suggested change
fn is_default_device(device: Device) -> bool {
fn is_default_device(device: &Device) -> bool {

Copilot uses AI. Check for mistakes.
default_input_device()
.map(|d| d.audio_device_id == device.audio_device_id)
.unwrap_or(false)
||
default_output_device()
.map(|d| d.audio_device_id == device.audio_device_id)
.unwrap_or(false)
}


impl Device {
/// Construct a new device given its ID.
/// Useful for constructing hidden devices.
pub fn new(audio_device_id: AudioDeviceID) -> Self {
Device {
Self {
audio_device_id,
// TODO: This could be made to detect the default device properly.
is_default: false,
}
}

Expand Down Expand Up @@ -523,7 +531,7 @@ where
}

fn audio_unit_from_device(device: &Device, input: bool) -> Result<AudioUnit, coreaudio::Error> {
let output_type = if device.is_default && !input {
let output_type = if is_default_device(device.clone()) && !input {
Copy link

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary clone operation. Consider passing a reference to is_default_device() instead of cloning the device, or modify the function to accept &Device.

Copilot uses AI. Check for mistakes.
coreaudio::audio_unit::IOType::DefaultOutput
} else {
coreaudio::audio_unit::IOType::HalOutput
Expand Down Expand Up @@ -665,7 +673,7 @@ impl Device {

// If we didn't request the default device, stop the stream if the
// device disconnects.
if !self.is_default {
if !is_default_device(self.clone()) {
Copy link

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary clone operation. Consider passing a reference to is_default_device() instead of cloning the device, or modify the function to accept &Device.

Copilot uses AI. Check for mistakes.
add_disconnect_listener(&stream, error_callback_disconnect)?;
}

Expand Down Expand Up @@ -770,7 +778,7 @@ impl Device {

// If we didn't request the default device, stop the stream if the
// device disconnects.
if !self.is_default {
if !is_default_device(self.clone()) {
Copy link

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary clone operation. Consider passing a reference to is_default_device() instead of cloning the device, or modify the function to accept &Device.

Suggested change
if !is_default_device(self.clone()) {
if !is_default_device(&self) {

Copilot uses AI. Check for mistakes.
add_disconnect_listener(&stream, error_callback_disconnect)?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/host/coreaudio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub use self::macos::{
Device, Host, Stream,
};

// Common helper methods used by both macOS and iOS

// Common helper methods used by both macOS and iOS
fn check_os_status(os_status: OSStatus) -> Result<(), BackendSpecificError> {
match coreaudio::Error::from_os_status(os_status) {
Ok(()) => Ok(()),
Expand Down
2 changes: 0 additions & 2 deletions src/samples_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ pub enum SampleFormat {

/// `U24` with a valid range of '0..16777216' with `1 << 23 == 8388608` being the origin
// U24,

/// `u32` with a valid range of `u32::MIN..=u32::MAX` with `1 << 31` being the origin.
U32,

/// `U48` with a valid range of '0..(1 << 48)' with `1 << 47` being the origin
// U48,

/// `u64` with a valid range of `u64::MIN..=u64::MAX` with `1 << 63` being the origin.
U64,

Expand Down
Loading