Skip to content

Commit

Permalink
continue on core audio
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jan 16, 2025
1 parent 076f244 commit 8a0c8b8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
72 changes: 49 additions & 23 deletions cidre/src/core_audio/hardware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,13 @@ impl std::ops::DerefMut for Device {
}

impl Device {
pub fn with_uid(uid: arc::R<cf::String>) -> os::Result<Self> {
System::OBJ.prop_with_qualifier(
&PropSelector::HARDWARE_TRANSLATE_UID_TO_DEVICE.global_addr(),
&uid,
)
}

pub fn uid(&self) -> os::Result<arc::R<cf::String>> {
self.cf_prop(&PropSelector::DEVICE_UID.global_addr())
}
Expand All @@ -516,17 +523,34 @@ impl Device {
&val,
)
}
///
/// A f64 that indicates the current actual sample rate of the AudioDevice as measured by its time stamps.
#[doc(alias = "kAudioDevicePropertyActualSampleRate")]
pub fn actual_sample_rate(&self) -> os::Result<f64> {
self.prop(&PropSelector::DEVICE_ACTUAL_SAMPLE_RATE.global_addr())
}

pub fn asbd(&self, scope: PropScope) -> os::Result<AudioBasicStreamDesc> {
// NOTE: this is depricated property for device, but it is working well
self.prop(&PropSelector::STREAM_VIRTUAL_FORMAT.addr(scope, PropElement::MAIN))
}

#[inline]
pub fn input_asbd(&self) -> os::Result<AudioBasicStreamDesc> {
self.asbd(PropScope::INPUT)
}

#[inline]
pub fn output_asbd(&self) -> os::Result<AudioBasicStreamDesc> {
self.asbd(PropScope::OUTPUT)
}

pub fn available_nominal_sample_rates(&self) -> os::Result<Vec<ValueRange>> {
self.prop_vec(&PropSelector::DEVICE_AVAILABLE_NOMINAL_SAMPLE_RATES.global_addr())
}

pub fn stream_cfg(&self, scope: PropScope) -> os::Result<AudioBufListN> {
let addr = PropAddr {
selector: PropSelector::DEVICE_STREAM_CFG,
scope,
element: PropElement::WILDCARD,
};
let addr = PropSelector::DEVICE_STREAM_CFG.addr(scope, PropElement::MAIN);
let mut size = self.prop_size(&addr)?;
let mut res = AudioBufListN::new(size as _);
unsafe {
Expand Down Expand Up @@ -559,13 +583,6 @@ impl Device {
self.prop(&PropSelector::DEVICE_TRANSPORT_TYPE.global_addr())
}

/// A f64 that indicates the current actual sample rate of the AudioDevice
/// as measured by its time stamps.
#[doc(alias = "kAudioDevicePropertyActualSampleRate")]
pub fn actual_sample_rate(&self) -> os::Result<f64> {
self.prop(&PropSelector::DEVICE_ACTUAL_SAMPLE_RATE.global_addr())
}

/// A cf::String that contains the UID for the AudioClockDevice that is currently
/// serving as the main time base of the device.
#[doc(alias = "kAudioDevicePropertyClockDevice")]
Expand All @@ -578,13 +595,7 @@ impl Device {
/// Note that this property does not apply to aggregate devices, just real, physical devices.
#[doc(alias = "kAudioDevicePropertyProcessMute")]
pub fn is_process_muted(&self, scope: PropScope) -> os::Result<bool> {
let addr = PropAddr {
selector: PropSelector::DEVICE_PROCESS_MUTE,
scope,
element: PropElement::WILDCARD,
};

self.bool_prop(&addr)
self.bool_prop(&PropSelector::DEVICE_PROCESS_MUTE.addr(scope, PropElement::WILDCARD))
}

pub fn is_process_input_muted(&self) -> os::Result<bool> {
Expand Down Expand Up @@ -1260,7 +1271,7 @@ impl Stream {
/// the AudioStream. The virtual format refers to the data format in which all
/// IOProcs for the owning AudioDevice will perform IO transactions.
#[doc(alias = "kAudioStreamPropertyVirtualFormat")]
pub fn virtual_format(&self) -> os::Result<AudioBasicStreamDesc> {
pub fn virtual_format(&self) -> os::Result<cat::AudioBasicStreamDesc> {
self.prop(&PropSelector::STREAM_VIRTUAL_FORMAT.global_addr())
}

Expand All @@ -1273,7 +1284,7 @@ impl Stream {
/// the AudioStream. The physical format refers to the data format in which the
/// hardware for the owning AudioDevice performs its IO transactions.
#[doc(alias = "kAudioStreamPropertyPhysicalFormat")]
pub fn physical_format(&self) -> os::Result<AudioBasicStreamDesc> {
pub fn physical_format(&self) -> os::Result<cat::AudioBasicStreamDesc> {
self.prop(&PropSelector::STREAM_PHYSICAL_FORMAT.global_addr())
}

Expand Down Expand Up @@ -1659,6 +1670,17 @@ mod tests {
ns, os,
};

#[test]
fn device() {
let uid = cf::str!(c"BuiltInSpeakerDevice");
let device = Device::with_uid(uid.retained()).unwrap();
let uid_from_device = device.uid().unwrap();
assert_eq!(uid, uid_from_device.as_ref());
unsafe { assert_ne!(uid.as_type_ptr(), uid_from_device.as_type_ptr()) };
let uid2 = uid.retained();
unsafe { assert_eq!(uid.as_type_ptr(), uid2.as_type_ptr()) };
}

#[test]
fn list_devices() {
let addr = PropSelector::HARDWARE_DEFAULT_INPUT_DEVICE.global_addr();
Expand All @@ -1684,8 +1706,9 @@ mod tests {

#[test]
fn aggregate_device() {
let output_device = System::default_output_device().unwrap();
let output_uid = output_device.uid().unwrap();
// let output_device = System::default_output_device().unwrap();
let output_uid = cf::str!(c"BuiltInSpeakerDevice"); // output_device.uid().unwrap();
println!("device_uid {output_uid:?}");
let uuid = cf::Uuid::new().to_cf_string();
let dict = cf::DictionaryOf::with_keys_values(
&[
Expand Down Expand Up @@ -1761,6 +1784,9 @@ mod tests {

let _proc_id = agg_device.create_io_proc_id(proc, None).unwrap();

let input = agg_device.output_asbd().unwrap();
println!("input {input:?}");

let streams = agg_device.streams().unwrap();
println!("streams {streams:?}");
let asbd = streams[0].virtual_format().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions cidre/src/core_audio/hardware_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl std::fmt::Debug for Class {
}

#[doc(alias = "AudioObjectPropertySelector")]
#[derive(Eq, PartialEq, Copy, Clone)]
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
#[repr(transparent)]
pub struct PropSelector(pub u32);

Expand Down Expand Up @@ -59,7 +59,7 @@ impl std::fmt::Debug for PropSelector {
}

#[doc(alias = "AudioObjectPropertyScope")]
#[derive(Eq, PartialEq, Copy, Clone)]
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
#[repr(transparent)]
pub struct PropScope(pub u32);

Expand All @@ -74,12 +74,12 @@ impl std::fmt::Debug for PropScope {
}

#[doc(alias = "AudioObjectPropertyElement")]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
#[repr(transparent)]
pub struct PropElement(pub u32);

#[doc(alias = "AudioObjectPropertyAddress")]
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct PropAddr {
pub selector: PropSelector,
Expand Down

0 comments on commit 8a0c8b8

Please # to comment.