2018-02-12 13:10:24 +00:00
# CPAL - Cross-Platform Audio Library
2014-12-11 13:22:55 +00:00
2019-10-03 11:01:35 +00:00
[![Actions Status ](https://github.com/RustAudio/cpal/workflows/cpal/badge.svg )](https://github.com/RustAudio/cpal/actions)
[![Crates.io ](https://img.shields.io/crates/v/cpal.svg )](https://crates.io/crates/cpal) [![docs.rs ](https://docs.rs/cpal/badge.svg )](https://docs.rs/cpal/)
2014-12-17 09:03:20 +00:00
2018-02-12 13:10:24 +00:00
Low-level library for audio input and output in pure Rust.
2016-08-12 15:51:15 +00:00
2018-02-12 13:10:24 +00:00
This library currently supports the following:
2019-06-24 21:27:46 +00:00
- Enumerate supported audio hosts.
2018-02-12 13:10:24 +00:00
- Enumerate all available audio devices.
- Get the current default input and output devices.
- Enumerate known supported input and output stream formats for a device.
- Get the current default input and output stream formats for a device.
- Build and run input and output PCM streams on a chosen device with a given stream format.
2019-06-28 19:48:18 +00:00
Currently supported hosts include:
2018-02-12 13:10:24 +00:00
- Linux (via ALSA)
2018-11-07 08:02:35 +00:00
- Windows (via WASAPI by default, see ASIO instructions below)
2018-02-12 13:10:24 +00:00
- macOS (via CoreAudio)
- iOS (via CoreAudio)
- Emscripten
2018-09-17 13:57:51 +00:00
Note that on Linux, the ALSA development files are required. These are provided
as part of the `libasound2-dev` package on Debian and Ubuntu distributions and
`alsa-lib-devel` on Fedora.
2018-11-07 08:02:35 +00:00
2020-07-15 10:55:33 +00:00
## Compiling for Web Assembly
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.
2018-11-07 08:02:35 +00:00
## ASIO on Windows
[ASIO ](https://en.wikipedia.org/wiki/Audio_Stream_Input/Output ) is an audio
driver protocol by Steinberg. While it is available on multiple operating
systems, it is most commonly used on Windows to work around limitations of
WASAPI including access to large numbers of channels and lower-latency audio
processing.
2019-06-28 16:58:48 +00:00
CPAL allows for using the ASIO SDK as the audio host on Windows instead of
2018-11-07 08:02:35 +00:00
WASAPI. To do so, follow these steps:
1. **Download the ASIO SDK** `.zip` from [this
link](https://www.steinberg.net/en/company/developers.html). The version as
of writing this is 2.3.1.
2019-06-28 19:48:18 +00:00
2. Extract the files and place the directory somewhere you are happy for it to stay
(e.g. `~/.asio` ).
3. Assign the full path of the directory (that contains the `readme` , `changes` ,
2019-07-01 13:23:45 +00:00
`ASIO SDK 2.3` pdf, etc) to the `CPAL_ASIO_DIR` environment variable. This is
necessary for the `asio-sys` build script to build and bind to the SDK.
4. `bindgen` , the library used to generate bindings to the C++ SDK, requires
clang. **Download and install LLVM** from
2018-11-07 08:02:35 +00:00
[here ](http://releases.llvm.org/download.html ) under the "Pre-Built Binaries"
section. The version as of writing this is 7.0.0.
5. 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:
```
setx LIBCLANG_PATH "C:\Program Files\LLVM\bin"
```
2019-06-28 16:58:48 +00:00
6. If you don't have any ASIO devices or drivers available, you can [**download
2018-11-07 08:02:35 +00:00
and install ASIO4ALL**](http://www.asio4all.org/). Be sure to enable the
"offline" feature during installation despite what the installer says about
it being useless.
2018-11-07 08:34:13 +00:00
7. **Loading VCVARS** . `rust-bindgen` uses the C++ tool-chain when generating
bindings to the ASIO SDK. As a result, it is necessary to load some
environment variables in the command prompt that we use to build our project.
On 64-bit machines run:
```
2019-06-28 16:58:48 +00:00
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
2018-11-07 08:34:13 +00:00
```
On 32-bit machines run:
```
2019-06-28 16:58:48 +00:00
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
2018-11-07 08:34:13 +00:00
```
2019-07-01 13:23:45 +00:00
Note that, depending on your version of Visual Studio, this script might be
in a slightly different location.
2019-06-28 16:58:48 +00:00
8. Select the ASIO host at the start of our program with the following code:
2018-11-07 08:02:35 +00:00
```rust
2019-06-28 16:58:48 +00:00
let host;
2018-11-07 08:02:35 +00:00
#[cfg(target_os = "windows")]
{
2019-06-28 16:58:48 +00:00
host = cpal::host_from_id(cpal::HostId::Asio).expect("failed to initialise ASIO host");
2018-11-07 08:02:35 +00:00
}
```
2019-07-01 13:23:45 +00:00
If you run into compilations errors produced by `asio-sys` or `bindgen` , make
sure that `CPAL_ASIO_DIR` is set correctly and try `cargo clean` .
2019-06-28 20:08:35 +00:00
9. Make sure to enable the `asio` feature when building CPAL:
2018-11-07 08:02:35 +00:00
2019-06-28 20:08:35 +00:00
```
cargo build --features "asio"
```
or if you are using CPAL as a dependency in a downstream project, enable the
feature like this:
```toml
cpal = { version = "*", features = ["asio"] }
```
2019-06-28 19:48:18 +00:00
2018-11-07 08:02:35 +00:00
In the future we would like to work on automating this process to make it
easier, but we are not familiar enough with the ASIO license to do so yet.
2019-06-28 19:48:18 +00:00
*Updated as of ASIO version 2.3.3.*