gitmyhub

cpal

Rust ★ 3.9k updated 3d ago

Low-level cross-platform audio I/O library in Rust

CPAL is a Rust library for playing and recording audio across all major platforms, Windows, macOS, Linux, Android, iOS, and browsers, using a single unified API that abstracts each platform's native audio system.

RustWASAPICoreAudioALSAWebAssemblyASIOsetup: moderatecomplexity 3/5

CPAL is a low-level library for playing and recording audio, written in the Rust programming language. It is designed to work across many different operating systems without requiring separate code for each one. If you are building software in Rust that needs to produce or capture sound, CPAL gives you a single interface to work with regardless of whether you are targeting Windows, macOS, Linux, Android, iOS, or even a web browser.

The library works by talking to whatever audio system is native to the platform it runs on. On Windows it defaults to WASAPI, on macOS and iOS it uses CoreAudio, and on Linux it uses ALSA by default with options to switch to PipeWire, PulseAudio, or JACK. For web-based projects compiled to WebAssembly, it uses the browser's Web Audio API. Each of these audio systems is handled transparently, so the developer writes the same Rust code and the library figures out how to make it work on the underlying platform.

The library lets you list available audio devices on a system, choose between input (microphone) and output (speakers), and open a stream for recording or playback. A stream is a continuous flow of audio data, and CPAL gives you control over starting and pausing it, along with information about the buffer size and timing.

There are optional features available for use cases that need lower latency or specific routing. On Windows, ASIO support can be enabled for professional-grade audio with very tight timing. On Linux, real-time scheduling can be enabled so the audio thread gets priority over other system processes. These are opt-in and require additional setup, which the README documents in detail.

CPAL is a building block, not an end-user application. Developers use it as a dependency when writing audio tools, games, or other programs that need to work with sound directly.

Where it fits