| Lift freeq's AV media plane out of sleek 90f8b89 nandi 20d ago | 1 | # CPAL - Cross-Platform Audio Library |
| 2 | |
| 3 | [](https://github.com/RustAudio/cpal/actions) |
| 4 | [](https://crates.io/crates/cpal) [](https://docs.rs/cpal/) |
| 5 | |
| 6 | Low-level library for audio input and output in pure Rust. |
| 7 | |
| 8 | ## Minimum Supported Rust Version (MSRV) |
| 9 | |
| 10 | The minimum Rust version required depends on which audio backend and features you're using, as each platform has different dependencies: |
| 11 | |
| 12 | - **AAudio (Android):** Rust **1.85** |
| 13 | - **ALSA (Linux/BSD):** Rust **1.82** |
| 14 | - **CoreAudio (macOS/iOS):** Rust **1.80** |
| 15 | - **JACK (Linux/BSD/macOS/Windows):** Rust **1.82** |
| 16 | - **PipeWire (Linux/BSD):** Rust **1.85** |
| 17 | - **PulseAudio (Linux/BSD):** Rust **1.88** |
| 18 | - **WASAPI/ASIO (Windows):** Rust **1.82** |
| 19 | - **WASM (`wasm32-unknown`):** Rust **1.85** |
| 20 | - **WASM (`wasm32-wasip1`):** Rust **1.78** |
| 21 | - **WASM (`audioworklet`):** Rust **nightly** (requires `-Zbuild-std` for atomics support) |
| 22 | |
| 23 | ## Supported Platforms |
| 24 | |
| 25 | This library currently supports the following: |
| 26 | |
| 27 | - Enumerate supported audio hosts. |
| 28 | - Enumerate all available audio devices. |
| 29 | - Get the current default input and output devices. |
| 30 | - Enumerate known supported input and output stream formats for a device. |
| 31 | - Get the current default input and output stream formats for a device. |
| 32 | - Build and run input and output PCM streams on a chosen device with a given stream format. |
| 33 | |
| 34 | Currently, supported platforms include: |
| 35 | |
| 36 | - Android (via AAudio) |
| 37 | - BSD (via ALSA by default, JACK, PipeWire or PulseAudio optionally) |
| 38 | - Emscripten |
| 39 | - iOS (via CoreAudio) |
| 40 | - Linux (via ALSA by default, JACK, PipeWire or PulseAudio optionally) |
| 41 | - macOS (via CoreAudio by default, JACK optionally) |
| 42 | - WebAssembly (via Web Audio API or Audio Worklet) |
| 43 | - Windows (via WASAPI by default, ASIO or JACK optionally) |
| 44 | |
| 45 | Note that on Linux, the ALSA development files are required for building (even when using JACK, PipeWire or PulseAudio). These are provided as part of the `libasound2-dev` package on Debian and Ubuntu distributions and `alsa-lib-devel` on Fedora. |
| 46 | |
| 47 | ## Compiling for WebAssembly |
| 48 | |
| 49 | If you are interested in using CPAL with WebAssembly, 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. Some of the examples in this repository also provide working configurations that you can use as reference. |
| 50 | |
| 51 | ## Optional Features |
| 52 | |
| 53 | | Feature | Platform | Description | |
| 54 | |---------|----------|-------------| |
| 55 | | `audio_thread_priority` | Linux, BSD, Windows | Raises the audio callback thread to real-time priority for lower latency and fewer glitches. On Linux, requires `rtkit` or appropriate user permissions (`limits.conf` or capabilities). | |
| 56 | | `asio` | Windows | ASIO backend for low-latency audio, bypassing the Windows audio stack. Requires ASIO drivers and LLVM/Clang. See the [ASIO setup guide](#asio-on-windows). | |
| 57 | | `audioworklet` | WebAssembly (`wasm32-unknown-unknown`) | Audio Worklet backend for lower-latency web audio than the default Web Audio API, running audio on a dedicated thread. Requires atomics support (`RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals"`) and `Cross-Origin` headers for `SharedArrayBuffer`. See the `audioworklet-beep` example. | |
| 58 | | `custom` | All | User-defined host implementations for audio systems not natively supported by CPAL. See `examples/custom.rs`. | |
| 59 | | `jack` | Linux, BSD, macOS, Windows | JACK Audio Connection Kit backend for pro-audio routing and inter-application connectivity. Requires `libjack-jackd2-dev` (Debian/Ubuntu) or `jack-devel` (Fedora). | |
| 60 | | `pipewire` | Linux, BSD | PipeWire media server backend. Requires `libpipewire-0.3-dev` (Debian/Ubuntu) or `pipewire-devel` (Fedora). | |
| 61 | | `pulseaudio` | Linux, BSD | PulseAudio sound server backend. Requires `libpulse-dev` (Debian/Ubuntu) or `pulseaudio-libs-devel` (Fedora). | |
| 62 | | `wasm-bindgen` | WebAssembly (`wasm32-unknown-unknown`) | Web Audio API backend for browser-based audio; required for any WebAssembly audio support. See the `wasm-beep` example. | |
| 63 | |
| 64 | See the [beep example](examples/beep.rs) for selecting the host at runtime. |
| 65 | |
| 66 | ## ASIO on Windows |
| 67 | |
| 68 | ### Locating the ASIO SDK |
| 69 | |
| 70 | The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR` environment variable. |
| 71 | |
| 72 | The build script will try to find the ASIO SDK by following these steps in order: |
| 73 | |
| 74 | 1. Check if `CPAL_ASIO_DIR` is set and if so use the path to point to the SDK. |
| 75 | 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")`. |
| 76 | 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")`. |
| 77 | |
| 78 | In an ideal situation you don't need to worry about this step. |
| 79 | |
| 80 | ### Preparing the Build Environment |
| 81 | |
| 82 | 1. **Install LLVM/Clang**: `bindgen`, the library used to generate bindings to the C++ SDK, requires clang. Download and install LLVM from <http://releases.llvm.org/download.html> under the "Pre-Built Binaries" section. |
| 83 | |
| 84 | 2. **Set LIBCLANG_PATH**: Add the LLVM `bin` directory to a `LIBCLANG_PATH` environment variable. If you installed LLVM to the default directory, this should work in the command prompt: |
| 85 | ``` |
| 86 | setx LIBCLANG_PATH "C:\Program Files\LLVM\bin" |
| 87 | ``` |
| 88 | |
| 89 | 3. **Install ASIO Drivers** (optional for testing): If you don't have any ASIO devices or drivers available, you can download and install ASIO4ALL from <http://www.asio4all.org/>. Be sure to enable the "offline" feature during installation. |
| 90 | |
| 91 | 4. **Visual Studio**: The build script assumes Microsoft Visual Studio is installed. It will try to find `vcvarsall.bat` and execute it with the right host and target architecture. If needed, you can manually execute it: |
| 92 | ``` |
| 93 | "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 |
| 94 | ``` |
| 95 | For more information see the [vcvarsall.bat documentation](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line). |
| 96 | |
| 97 | ### Using ASIO in Your Application |
| 98 | |
| 99 | 1. **Enable the feature** in your `Cargo.toml`: |
| 100 | ```toml |
| 101 | cpal = { version = "*", features = ["asio"] } |
| 102 | ``` |
| 103 | |
| 104 | 2. **Select the ASIO host** in your code: |
| 105 | ```rust |
| 106 | let host = cpal::host_from_id(cpal::HostId::Asio) |
| 107 | .expect("failed to initialise ASIO host"); |
| 108 | ``` |
| 109 | |
| 110 | ### Troubleshooting |
| 111 | |
| 112 | If you encounter compilation errors from `asio-sys` or `bindgen`: |
| 113 | - Verify `CPAL_ASIO_DIR` is set correctly |
| 114 | - Try running `cargo clean` |
| 115 | - Ensure LLVM/Clang is properly installed and `LIBCLANG_PATH` is set |
| 116 | |
| 117 | ### Cross-Compilation |
| 118 | |
| 119 | When Windows is the host and target OS, the build script supports all cross-compilation targets supported by the MSVC compiler. |
| 120 | |
| 121 | It is also possible to compile Windows applications with ASIO support on Linux and macOS using the MinGW-w64 toolchain. |
| 122 | |
| 123 | **Requirements:** |
| 124 | - Include the MinGW-w64 include directory in your `CPLUS_INCLUDE_PATH` environment variable |
| 125 | - Include the LLVM include directory in your `CPLUS_INCLUDE_PATH` environment variable |
| 126 | |
| 127 | **Example for macOS** (targeting `x86_64-pc-windows-gnu` with `mingw-w64` installed via brew): |
| 128 | ``` |
| 129 | export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/opt/homebrew/Cellar/mingw-w64/11.0.1/toolchain-x86_64/x86_64-w64-mingw32/include" |
| 130 | ``` |
| 131 | |
| 132 | ## Troubleshooting |
| 133 | |
| 134 | ### No Default Device Available |
| 135 | |
| 136 | If you receive errors about no default input or output device: |
| 137 | |
| 138 | - **Linux/PipeWire:** Check that PipeWire is running: `pw-cli info` |
| 139 | - **Linux/PulseAudio:** Check that PulseAudio is running: `pulseaudio --check` |
| 140 | - **macOS:** Check System Preferences > Sound for available devices |
| 141 | - **Mobile (iOS/Android):** Ensure your app has microphone/audio permissions |
| 142 | - **Windows:** Verify your audio device is enabled in Sound Settings |
| 143 | |
| 144 | ## ALSA, PipeWire, and PulseAudio |
| 145 | |
| 146 | When PipeWire or PulseAudio is running, it holds the ALSA `default` device exclusively. A second stream attempting to open it via the ALSA backend will fail with a `DeviceBusy` error. To route audio through the sound server via ALSA, use the bridge devices `pipewire` or `pulse` instead of `default`. Better yet, use the `pipewire` or `pulseaudio` cpal features for native integration. |
| 147 | |
| 148 | Reserve `hw:` and `plughw:` device names for targets that have no sound server. On those targets, ensure the user is a member of the `audio` group if the system does not grant audio device access automatically via `logind`. |
| 149 | |
| 150 | ### Buffer Size Issues |
| 151 | |
| 152 | `BufferSize::Default` uses the system-configured device default, which on **ALSA** can range from a PipeWire quantum (typically 1024 frames) to `u32::MAX` on misconfigured or exotic hardware. A very deep buffer causes samples to be consumed far faster than audible playback, making audio appear to fast-forward ahead of actual output. |
| 153 | |
| 154 | Configure the system and/or request a fixed size in your application: |
| 155 | |
| 156 | | System | File | Setting | |
| 157 | |--------|------|---------| |
| 158 | | ALSA | `~/.asoundrc` or `/etc/asound.conf` | `buffer_size`, `periods` * `period_size` | |
| 159 | | PipeWire | `~/.config/pipewire/pipewire.conf.d/` | `default.clock.quantum` | |
| 160 | | PulseAudio | `~/.config/pulse/daemon.conf` | `default-fragments` * `default-fragment-size-msec` | |
| 161 | |
| 162 | ```rust |
| 163 | config.buffer_size = cpal::BufferSize::Fixed(1024); |
| 164 | ``` |
| 165 | |
| 166 | Query `device.default_output_config()?.buffer_size()` for valid ranges. Smaller buffers reduce latency but increase CPU load and the risk of glitches. |
| 167 | |
| 168 | ### Build Errors |
| 169 | |
| 170 | If you are unable to build the library: |
| 171 | |
| 172 | - Verify you have installed the required development libraries, as documented above |
| 173 | - **ASIO on Windows:** Verify `LIBCLANG_PATH` is set and LLVM is installed |
| 174 | |
| 175 | ## Examples |
| 176 | |
| 177 | CPAL comes with several examples in `examples/`. |
| 178 | |
| 179 | Run an example with: |
| 180 | ```bash |
| 181 | cargo run --example beep |
| 182 | ``` |
| 183 | |
| 184 | For platform-specific features, enable the relevant features: |
| 185 | ```bash |
| 186 | cargo run --example beep --features asio # Windows ASIO backend |
| 187 | cargo run --example beep --features jack # JACK backend |
| 188 | cargo run --example beep --features pipewire # PipeWire backend |
| 189 | cargo run --example beep --features pulseaudio # PulseAudio backend |
| 190 | ``` |
| 191 | |
| 192 | ## Contributing |
| 193 | |
| 194 | Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. |
| 195 | |
| 196 | ## Resources |
| 197 | |
| 198 | - **Documentation:** [docs.rs/cpal](https://docs.rs/cpal) |
| 199 | - **Examples:** [examples/](examples/) directory in this repository |
| 200 | - **Discord:** Join the [#cpal channel](https://discord.gg/vPmmSgJSPV) for questions and discussion |
| 201 | - **GitHub:** [Report issues](https://github.com/RustAudio/cpal/issues) and [view source code](https://github.com/RustAudio/cpal) |
| 202 | - **RustAudio:** Part of the [RustAudio organization](https://github.com/RustAudio) |
| 203 | |
| 204 | ## License |
| 205 | |
| 206 | Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details. |