Skip to content

Commit 5ec0841

Browse files
committed
feat: add StreamConfigBuilder with cross-platform configuration support
- Add ergonomic StreamConfigBuilder API for stream configuration - Add platform-specific configuration types: - AlsaStreamConfig: configure periods and access types (RW/MMap modes) - JackStreamConfig: configure client names and port auto-connection - WasapiStreamConfig: configure exclusive/shared mode - Add device-tied stream building methods on SupportedStreamConfig - Add convenience methods: Device::default_input/output_config() - Add platform builder methods: .on_alsa(), .on_jack(), .on_wasapi() - Add jack feature flag for JACK audio backend support - Add audio_thread_priority feature flag for ALSA/WASAPI real-time scheduling - Update documentation with Quick Start guide and cross-platform examples - Maintain backward compatibility with existing Device::build_*_stream APIs This introduces a major ergonomic improvement for cross-platform audio configuration while preserving the existing low-level APIs.
1 parent ea41597 commit 5ec0841

File tree

16 files changed

+3799
-156
lines changed

16 files changed

+3799
-156
lines changed

CHANGELOG.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
# Unreleased
22

3-
- Added `Sample::bits_per_sample` method.
4-
- ALSA(process_output): Pass `silent=true` to `PCM.try_recover`, so it doesn't write to stderr.
5-
- ALSA: Fix BufferSize::Fixed by selecting the nearest supported frame count.
6-
- ALSA: Change BufferSize::Default to use the device defaults instead of 4 periods of 25 ms.
3+
- Add `audio_thread_priority` feature flag for real-time thread priority on ALSA/WASAPI.
4+
- Add `Sample::bits_per_sample` method.
5+
- Add `StreamConfigBuilder` with platform-specific options.
6+
- Add device-tied stream building methods: `SupportedStreamConfig::build_input_stream()` and `build_output_stream()`.
7+
- `BufferSize` now impls `Default`.
8+
- Remove deprecated `oboe-shared-stdcxx` feature flag.
9+
- ALSA: Add `AlsaStreamConfig` for periods and access types.
10+
- ALSA: Fix `BufferSize::Fixed` by selecting the nearest supported frame count.
11+
- ALSA: Change `BufferSize::Default` to use the device defaults.
712
- ALSA: Change card enumeration to work like `aplay -L` does.
13+
- ALSA(process_output): Pass `silent=true` to `PCM.try_recover`, so it doesn't write to stderr.
814
- ASIO: Fix linker flags for MinGW cross-compilation.
915
- CoreAudio: Change `Device::supported_configs` to return a single element containing the available sample rate range when all elements have the same `mMinimum` and `mMaximum` values.
1016
- CoreAudio: Change default audio device detection to be lazy when building a stream, instead of during device enumeration.
1117
- iOS: Fix example by properly activating audio session.
12-
- WASAPI: Expose IMMDevice from WASAPI host Device.
18+
- JACK: Add `jack` feature flag to enable JACK audio backend support.
19+
- JACK: Add `JackStreamConfig` for client names and port connections.
20+
- WASAPI: Add `WasapiStreamConfig` for exclusive mode support.
21+
- WASAPI: Expose `IMMDevice` from WASAPI host Device.
1322

1423
# Version 0.16.0 (2025-06-07)
1524

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ edition = "2021"
1010
rust-version = "1.70"
1111

1212
[features]
13-
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions.
13+
# ASIO backend (Windows only). See README for setup instructions.
14+
asio = ["dep:asio-sys", "dep:num-traits"]
1415

15-
# Deprecated, the `oboe` backend has been removed
16-
oboe-shared-stdcxx = []
16+
# Real-time thread priority for audio threads on ALSA/WASAPI
17+
audio_thread_priority = ["dep:audio_thread_priority"]
18+
19+
# JACK Audio backend
20+
jack = ["dep:jack"]
1721

1822
[dependencies]
1923
dasp_sample = "0.11"
24+
jack = { version = "0.13.0", optional = true }
2025

2126
[dev-dependencies]
2227
anyhow = "1.0"
@@ -46,7 +51,6 @@ num-traits = { version = "0.2.6", optional = true }
4651
alsa = "0.9"
4752
libc = "0.2"
4853
audio_thread_priority = { version = "0.33.0", optional = true }
49-
jack = { version = "0.13.0", optional = true }
5054

5155
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
5256
mach2 = "0.4" # For access to mach_timebase type.

README.md

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ This library currently supports the following:
1717
Currently, supported hosts include:
1818

1919
- Linux (via ALSA or JACK)
20-
- Windows (via WASAPI by default, see ASIO instructions below)
21-
- macOS (via CoreAudio)
20+
- Windows (via WASAPI by default, JACK or ASIO, see instructions below)
21+
- macOS (via CoreAudio or JACK)
2222
- iOS (via CoreAudio)
2323
- Android (via AAudio)
2424
- Emscripten
@@ -27,16 +27,30 @@ Note that on Linux, the ALSA development files are required. These are provided
2727
as part of the `libasound2-dev` package on Debian and Ubuntu distributions and
2828
`alsa-lib-devel` on Fedora.
2929

30+
For JACK support, install the JACK development libraries on Linux
31+
(`libjack-jackd2-dev` on Debian/Ubuntu) or download JACK from
32+
[jackaudio.org](https://jackaudio.org/downloads/) for macOS/Windows.
33+
3034
## Compiling for Web Assembly
3135

32-
If you are interested in using CPAL with WASM, please see [this guide](https://github.com/RustAudio/cpal/wiki/Setting-up-a-new-CPAL-WASM-project) in our Wiki which walks through setting up a new project from scratch.
36+
If you are interested in using CPAL with WASM, please see
37+
[this guide](https://github.com/RustAudio/cpal/wiki/Setting-up-a-new-CPAL-WASM-project)
38+
in our Wiki which walks through setting up a new project from scratch.
3339

3440
## Feature flags for audio backends
3541

36-
Some audio backends are optional and will only be compiled with a [feature flag](https://doc.rust-lang.org/cargo/reference/features.html).
42+
Some audio backends and features are optional and will only be compiled with a
43+
[feature flag](https://doc.rust-lang.org/cargo/reference/features.html).
44+
45+
### Audio Backend Features
46+
47+
- **ASIO**: `asio` - Enable ASIO support (Windows only)
48+
- **JACK**: `jack` - Enable JACK audio support
49+
50+
### Additional Features
3751

38-
- JACK (on Linux): `jack`
39-
- ASIO (on Windows): `asio`
52+
- **Real-time Thread Priority**: `audio_thread_priority` - Enable real-time
53+
thread priority for ALSA/WASAPI audio threads (may require elevated privileges)
4054

4155
## ASIO on Windows
4256

@@ -51,13 +65,20 @@ WASAPI.
5165

5266
### Locating the ASIO SDK
5367

54-
The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR` environment variable.
68+
The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR`
69+
environment variable.
5570

56-
The build script will try to find the ASIO SDK by following these steps in order:
71+
The build script will try to find the ASIO SDK by following these steps in
72+
order:
5773

5874
1. Check if `CPAL_ASIO_DIR` is set and if so use the path to point to the SDK.
59-
2. Check if the ASIO SDK is already installed in the temporary directory, if so use that and set the path of `CPAL_ASIO_DIR` to the output of `std::env::temp_dir().join("asio_sdk")`.
60-
3. If the ASIO SDK is not already installed, download it from <https://www.steinberg.net/asiosdk> and install it in the temporary directory. The path of `CPAL_ASIO_DIR` will be set to the output of `std::env::temp_dir().join("asio_sdk")`.
75+
2. Check if the ASIO SDK is already installed in the temporary directory, if so
76+
use that and set the path of `CPAL_ASIO_DIR` to the output of
77+
`std::env::temp_dir().join("asio_sdk")`.
78+
3. If the ASIO SDK is not already installed, download it from
79+
<https://www.steinberg.net/asiosdk> and install it in the temporary directory.
80+
The path of `CPAL_ASIO_DIR` will be set to the output of
81+
`std::env::temp_dir().join("asio_sdk")`.
6182

6283
In an ideal situation you don't need to worry about this step.
6384

@@ -70,22 +91,25 @@ In an ideal situation you don't need to worry about this step.
7091
2. Add the LLVM `bin` directory to a `LIBCLANG_PATH` environment variable. If
7192
you installed LLVM to the default directory, this should work in the command
7293
prompt:
73-
```
94+
```cmd
7495
setx LIBCLANG_PATH "C:\Program Files\LLVM\bin"
7596
```
7697
3. If you don't have any ASIO devices or drivers available, you can [**download
7798
and install ASIO4ALL**](http://www.asio4all.org/). Be sure to enable the
7899
"offline" feature during installation despite what the installer says about
79100
it being useless.
80-
4. Our build script assumes that Microsoft Visual Studio is installed if the host OS for compilation is Windows. The script will try to find `vcvarsall.bat`
81-
and execute it with the right host and target machine architecture regardless of the Microsoft Visual Studio version.
101+
4. Our build script assumes that Microsoft Visual Studio is installed if the
102+
host OS for compilation is Windows. The script will try to find
103+
`vcvarsall.bat` and execute it with the right host and target machine
104+
architecture regardless of the Microsoft Visual Studio version.
82105
If there are any errors encountered in this process which is unlikely,
83-
you may find the `vcvarsall.bat` manually and execute it with your machine architecture as an argument.
106+
you may find the `vcvarsall.bat` manually and execute it with your machine
107+
architecture as an argument.
84108
The script will detect this and skip the step.
85109

86110
A manually executed command example for 64 bit machines:
87111

88-
```
112+
```cmd
89113
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
90114
```
91115

@@ -106,7 +130,7 @@ In an ideal situation you don't need to worry about this step.
106130

107131
6. Make sure to enable the `asio` feature when building CPAL:
108132

109-
```
133+
```shell
110134
cargo build --features "asio"
111135
```
112136

@@ -121,18 +145,26 @@ _Updated as of ASIO version 2.3.3._
121145

122146
### Cross compilation
123147

124-
When Windows is the host and the target OS, the build script of `asio-sys` supports all cross compilation targets
125-
which are supported by the MSVC compiler. An exhaustive list of combinations could be found [here](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160#vcvarsall-syntax) with the addition of undocumented `arm64`, `arm64_x86`, `arm64_amd64` and `arm64_arm` targets. (5.11.2023)
148+
When Windows is the host and the target OS, the build script of `asio-sys`
149+
supports all cross compilation targets which are supported by the MSVC
150+
compiler. An exhaustive list of combinations could be found [here](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160#vcvarsall-syntax)
151+
with the addition of undocumented `arm64`, `arm64_x86`, `arm64_amd64` and
152+
`arm64_arm` targets. (5.11.2023)
126153

127-
It is also possible to compile Windows applications with ASIO support on Linux and macOS.
154+
It is also possible to compile Windows applications with ASIO support on Linux
155+
and macOS.
128156

129-
For both platforms the common way to do this is to use the [MinGW-w64](https://www.mingw-w64.org/) toolchain.
157+
For both platforms the common way to do this is to use the
158+
[MinGW-w64](https://www.mingw-w64.org/) toolchain.
130159

131-
Make sure that you have included the `MinGW-w64` include directory in your `CPLUS_INCLUDE_PATH` environment variable.
132-
Make sure that LLVM is installed and include directory is also included in your `CPLUS_INCLUDE_PATH` environment variable.
160+
Make sure that you have included the `MinGW-w64` include directory in your
161+
`CPLUS_INCLUDE_PATH` environment variable.
162+
Make sure that LLVM is installed and include directory is also included in your
163+
`CPLUS_INCLUDE_PATH` environment variable.
133164

134-
Example for macOS for the target of `x86_64-pc-windows-gnu` where `mingw-w64` is installed via brew:
165+
Example for macOS for the target of `x86_64-pc-windows-gnu` where `mingw-w64`
166+
is installed via brew:
135167

136-
```
168+
```shell
137169
export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/opt/homebrew/Cellar/mingw-w64/11.0.1/toolchain-x86_64/x86_64-w64-mingw32/include"
138170
```

examples/synth_tones.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn host_device_setup(
125125
let config = device.default_output_config()?;
126126
println!("Default output config : {config:?}");
127127

128-
Ok((host, device, config))
128+
Ok((host, device, config.into()))
129129
}
130130

131131
pub fn make_stream<T>(

0 commit comments

Comments
 (0)