Skip to content

feat: add get_available_sample_rates and get_device_transport_type helpers#149

Merged
simlay merged 4 commits into
RustAudio:masterfrom
afonsojramos:feat/device-helpers
Apr 3, 2026
Merged

feat: add get_available_sample_rates and get_device_transport_type helpers#149
simlay merged 4 commits into
RustAudio:masterfrom
afonsojramos:feat/device-helpers

Conversation

@afonsojramos

Copy link
Copy Markdown
Contributor

Summary

Adds 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>. For devices with discrete rates, mMinimum == mMaximum; for continuous ranges, they differ.

  • get_device_transport_type(device_id) — queries kAudioDevicePropertyTransportType and returns the transport type constant (kAudioDeviceTransportTypeUSB, kAudioDeviceTransportTypeBuiltIn, etc.).

Motivation

These are commonly needed when building audio applications that probe device capabilities — for example:

  • Implementing automatic sample rate switching (matching track sample rate to device)
  • Distinguishing USB DACs from built-in speakers or Bluetooth devices (to conditionally enable exclusive/hog mode)
  • Displaying device capabilities in a settings UI

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 values
  • test_get_device_transport_type — verifies the default output device returns a transport type without error
  • All existing tests continue to pass

…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 simlay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

afonsojramos added a commit to afonsojramos/qbz that referenced this pull request Mar 31, 2026
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).
@afonsojramos

Copy link
Copy Markdown
Contributor Author

@simlay Fair, just refactored 👍

@simlay simlay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@afonsojramos

afonsojramos commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

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! 🙏

@simlay simlay merged commit 5684013 into RustAudio:master Apr 3, 2026
10 checks passed
@simlay simlay mentioned this pull request Apr 3, 2026
@simlay

simlay commented Apr 3, 2026

Copy link
Copy Markdown
Member

Released via #150.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants