docs.rs failed to build coreaudio-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
coreaudio
A safe, idiomatic Rust wrapper around the macOS CoreAudio Hardware Abstraction Layer (HAL).
This crate provides typed access to audio devices, streams, and system-level audio objects, with compile-time guarantees around property access permissions and listener support.
Features
- Type-safe object model —
AudioObject<System>,AudioObject<Device>, andAudioObject<Stream>expose only the operations valid for each object type. - Compile-time property safety — Properties carry phantom types encoding their value type, owning object, read/write access, and listenability. Attempting to write a read-only property or listen to a non-listenable one is a compile error.
- Property listeners — Subscribe to property changes with
add_listener, then poll withlatest(), collect withall_since_last_check(), or block withblock_until_change(). - IO Procs — Register audio render callbacks on devices with
add_io_procand control playback withplay()/pause(). - Structured error handling — All CoreAudio
OSStatuscodes are mapped to a typedErrorKindenum with human-readable four-character-code formatting. - Format support — Rich enums for audio format IDs (Linear PCM, AAC variants, ALAC, AC3, Opus, MP3, etc.), format flags, sample formats, and sample resampling utilities.
Requirements
- macOS (the crate is gated with
#[cfg(target_os = "macos")]) coreaudio-sysfor raw FFI bindingscore-foundationforCFStringhandling
Quick start
use ;
Usage
Querying device properties
use ;
let system = default;
let device = system.current_device?;
let name: String = device.get_property?;
let uid: String = device.get_property?;
let sample_rate: f64 = device.get_property?;
let alive: bool = device.get_property?;
println!;
Setting writable properties
use ;
let system = default;
let device = system.current_device?;
device.set_property?;
device.set_property?;
Listening for property changes
use ;
use Duration;
let system = default;
let device = system.current_device?;
let listener = device.add_listener?;
// Non-blocking check
if let Some = listener.latest
// Blocking with timeout
match listener.block_for_duration
Working with streams
use ;
let system = default;
let device = system.current_device?;
let streams = device.streams_with_scope?;
for stream in &streams
Registering an IO proc (audio callback)
use ;
let system = default;
let device = system.current_device?;
let io_proc = device.add_io_proc?;
io_proc.play?;
// ...
io_proc.pause?;
io_proc.remove;
Available buffer sizes and sample rates
let buffer_range = device.avaliable_buffer_sizes?;
println!;
let sample_rates = device.avaliable_sample_rates?;
for range in &sample_rates
Property reference
Device properties
| Constant | Type | Access | Listenable |
|---|---|---|---|
DEVICE_NAME |
String |
Read | No |
DEVICE_UID |
String |
Read | No |
DEVICE_IS_ALIVE |
bool |
Read | Yes |
DEVICE_IS_RUNNING |
bool |
Read | Yes |
DEVICE_NOMINAL_SAMPLE_RATE |
f64 |
Read/Write | Yes |
DEVICE_BUFFER_FRAME_SIZE |
u32 |
Read/Write | Yes |
DEVICE_INPUT_LATENCY |
u32 |
Read | No |
DEVICE_OUTPUT_LATENCY |
u32 |
Read | No |
DEVICE_HOG_MODE |
i32 |
Read/Write | Yes |
Stream properties
| Constant | Type | Access | Listenable |
|---|---|---|---|
STREAM_NAME |
String |
Read | No |
STREAM_IS_ACTIVE |
bool |
Read | Yes |
STREAM_DIRECTION |
u32 |
Read | No |
STREAM_LATENCY |
u32 |
Read | No |
System properties
| Constant | Type | Access | Listenable |
|---|---|---|---|
SYSTEM_NAME |
String |
Read | No |
SYSTEM_IS_INITING_OR_EXITING |
bool |
Read | No |
SYSTEM_SLEEPING_IS_ALLOWED |
bool |
Read/Write | Yes |
SYSTEM_POWER_HINT |
u32 |
Read/Write | No |
Error handling
All fallible operations return Result<T, CoreAudioError>. The error type wraps a typed ErrorKind enum and the raw OSStatus code. You can match on the kind or inspect the four-character code string:
use ErrorKind;
match device.get_property
Supported audio formats
The FormatId enum covers Linear PCM, AAC (Standard, HE, HEv2, LD, ELD, ELDv2, ELD+SBR, Spatial), Apple Lossless, AC-3, Enhanced AC-3, APAC, AES3, A-Law, AMR, AMR-WB, Opus, and MP3. Unrecognised format IDs are preserved as FormatId::Unknown(u32).
Roadmap
- Sample rate validation —
SampleRateRangecurrently exposes raw min/max values. A future release will add a validation method that checks whether a given sample rate falls within a device's supported ranges and snaps to the nearest valid rate. - Control properties — Expose device-level controls such as volume, mute, stereo pan, and per-channel gain through the existing property system.
- More properties — Expand coverage of the CoreAudio HAL property surface, including transport controls, clock source selection, and safety/alive offsets.
- Semantic newtypes for integer properties — Properties like
SYSTEM_POWER_HINT,STREAM_DIRECTION, andDEVICE_HOG_MODEcurrently return raw integer types. These will be replaced with dedicated enums or wrapper types that give the values meaningful names.
License
See LICENSE file for details.