feat: add get_available_sample_rates and get_device_transport_type helpers#149
Conversation
…lpers Add two new helper functions to macos_helpers: - get_available_sample_rates(device_id): queries kAudioDevicePropertyAvailableNominalSampleRates and returns the supported sample rate ranges as Vec<AudioValueRange> - get_device_transport_type(device_id): queries kAudioDevicePropertyTransportType and returns the transport type constant (USB, built-in, Bluetooth, etc.) These are commonly needed when building audio applications that need to probe device capabilities, for example to implement sample rate switching or to distinguish USB DACs from built-in speakers.
simlay
left a comment
There was a problem hiding this comment.
Could you minimize how much is in the unsafe blocks and throw in a // SAFETY comment on them? I'm sure a lot of this repo doesn't have great examples of this in the past - I think this repo predates folks throwing in safety comments.
Add coreaudio_direct module using coreaudio-rs safe wrappers for: - Device capability probing (supported sample rates, transport type) - Automatic sample rate switching per-track - Device lookup by name for CoreAudio↔CPAL bridging Uses a fork branch of coreaudio-rs that adds get_available_sample_rates and get_device_transport_type helpers (PR RustAudio/coreaudio-rs#149).
|
@simlay Fair, just refactored 👍 |
simlay
left a comment
There was a problem hiding this comment.
LGTM. I checked out your branch, and noticed all the other unsafe blocks in the file that are quite similar to your original. Thanks for upping the quality of comments!
There was a minor nit I had so I pushed a change to your branch. Weirdly enough, that change also fixed clippy and I'm confused why.
Anyway, want me to publish a 0.14.1 release so you can use these features from crate.io?
|
Thanks for the quick review @simlay! I think that moving the ? outside the unsafe block is cleaner and it likely fixed clippy because the early-return path from ? isn’t itself unsafe, so having it inside the block was the trigger for the lint about unnecessary unsafe scope. And yes, a 0.14.1 release would be great, would love to use these from crates.io! 🙏 |
|
Released via #150. |
Summary
Adds two new helper functions to
macos_helpers:get_available_sample_rates(device_id)— querieskAudioDevicePropertyAvailableNominalSampleRatesand returns the supported sample rate ranges asVec<AudioValueRange>. For devices with discrete rates,mMinimum == mMaximum; for continuous ranges, they differ.get_device_transport_type(device_id)— querieskAudioDevicePropertyTransportTypeand returns the transport type constant (kAudioDeviceTransportTypeUSB,kAudioDeviceTransportTypeBuiltIn, etc.).Motivation
These are commonly needed when building audio applications that probe device capabilities — for example:
The sample rate ranges are already queried internally by
set_device_sample_rate, but aren't exposed as a standalone function. The transport type property has no existing wrapper.Both functions follow the same patterns as the existing helpers (
get_device_name,get_hogging_pid, etc.).Test plan
test_get_available_sample_rates— verifies the default output device returns at least one rate range with valid min/max valuestest_get_device_transport_type— verifies the default output device returns a transport type without error