Cross-platform audio in Rust
Go to file
mitchmindtree 391b32f5b2 Update CPAL to version 0.10.0.
This is quite a significant update for CPAL, including a number of
breaking changes. Here is a list of the breaking changes along with
links to where you can find more information:

- A `Host` API has been introduced in #289 along with a follow-up
  refactor in #295. Please see the examples for a demonstration of how
  to update your code. The necessary changes should hopefully be
  minimal. If this has caused you any major difficulty please let us
  know in an issue!
- An ASIO host has been introduced in #292. This adds support for
  Steinberg's ASIO audio driver API on Windows. Please see the ASIO
  section of the README for more information on how to setup CPAL with
  ASIO support for your project.
- The user callback API has been overhauled to emit `StreamEvent`s
  rather than buffers in order to support handling stream errors. #288.
- Error handling in general was overhauled in #286. This meant taking
  advantage of the `failure` crate and adding support for
  backend-specific errors with custom messages. Many unnecessary
  `panic!`s have been removed, but a few remain that would indicate bugs
  in CPAL.

In general, checking out the updated examples will be the easiest way to
get a quick overview on how you can update your own code for these
changes.

The CHANGELOG.md has been updated to include these changes.
2019-07-05 19:37:17 +02:00
.circleci Add testing on circle-ci (#171) 2017-10-21 15:08:34 +02:00
alsa-sys Bump alsa-sys version number 2017-03-06 15:57:18 +00:00
asio-sys Add missing Cargo.toml entries to asio-sys 2019-07-05 19:20:30 +02:00
examples Refactor `Host` and related traits into a new `traits` module 2019-06-29 14:45:15 +02:00
src Add missing `Hash` constraint to `StreamId` 2019-07-05 19:28:34 +02:00
.gitignore git ignore, temp files, fetch_add, os cfg 2019-07-05 05:51:26 +10:00
.rustfmt.toml Run rustfmt on the code (#162) 2017-10-11 13:24:49 +02:00
.travis.yml Stop publishing on gh-pages 2017-02-10 09:45:41 +01:00
CHANGELOG.md Update CPAL to version 0.10.0. 2019-07-05 19:37:17 +02:00
Cargo.toml Update CPAL to version 0.10.0. 2019-07-05 19:37:17 +02:00
README.md Address README suggestions by @ishitatsuyuki 2019-07-05 05:51:31 +10:00
appveyor.yml Remove gnu target to avoid need to install gcc for cc crate 2019-06-21 16:17:58 +02:00
build.rs opt in and bug fix 2019-07-05 05:51:26 +10:00

README.md

CPAL - Cross-Platform Audio Library

Build Status Crates.io docs.rs

Low-level library for audio input and output in pure Rust.

This library currently supports the following:

  • Enumerate supported audio hosts.
  • 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.

Currently supported hosts include:

  • Linux (via ALSA)
  • Windows (via WASAPI by default, see ASIO instructions below)
  • macOS (via CoreAudio)
  • iOS (via CoreAudio)
  • Emscripten

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.

ASIO on Windows

ASIO 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.

CPAL allows for using the ASIO SDK as the audio host on Windows instead of WASAPI. To do so, follow these steps:

  1. Download the ASIO SDK .zip from this link. The version as of writing this is 2.3.1.

  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, 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 here 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"
    
  6. If you don't have any ASIO devices or drivers available, you can download and install ASIO4ALL. Be sure to enable the "offline" feature during installation despite what the installer says about it being useless.

  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:

    "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
    

    On 32-bit machines run:

    "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86
    

    Note that, depending on your version of Visual Studio, this script might be in a slightly different location.

  8. Select the ASIO host at the start of our program with the following code:

    let host;
    #[cfg(target_os = "windows")]
    {
        host = cpal::host_from_id(cpal::HostId::Asio).expect("failed to initialise ASIO host");
    }
    

    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.

  9. Make sure to enable the asio feature when building CPAL:

    cargo build --features "asio"
    

    or if you are using CPAL as a dependency in a downstream project, enable the feature like this:

    cpal = { version = "*", features = ["asio"] }
    

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.

Updated as of ASIO version 2.3.3.