• Stars
    star
    2,614
  • Rank 17,511 (Top 0.4 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform audio I/O library in pure Rust

CPAL - Cross-Platform Audio Library

Actions 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 or JACK)
  • Windows (via WASAPI by default, see ASIO instructions below)
  • macOS (via CoreAudio)
  • iOS (via CoreAudio)
  • Android (via Oboe)
  • 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.

Compiling for Web Assembly

If you are interested in using CPAL with WASM, please see this guide in our Wiki which walks through setting up a new project from scratch.

Feature flags for audio backends

Some audio backends are optional and will only be compiled with a feature flag.

  • JACK (on Linux): jack
  • ASIO (on Windows): asio

Oboe can either use a shared or static runtime. The static runtime is used by default, but activating the oboe-shared-stdcxx feature makes it use the shared runtime, which requires libc++_shared.so from the Android NDK to be present during execution.

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.

Locating the ASIO SDK

The location of ASIO SDK is exposed to CPAL by setting the CPAL_ASIO_DIR environment variable.

The build script will try to find the ASIO SDK by following these steps in order:

  1. Check if CPAL_ASIO_DIR is set and if so use the path to point to the SDK.
  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").
  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").

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

Preparing the build environment

  1. 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 17.0.1.

  2. 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"
    
  3. 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.

  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 and execute it with the right host and target machine architecture regardless of the Microsoft Visual Studio version. If there are any errors encountered in this process which is unlikely, you may find the vcvarsall.bat manually and execute it with your machine architecture as an argument. The script will detect this and skip the step.

    A manually executed command example for 64 bit machines:

    "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
    

    For more information please refer to the documentation of `vcvarsall.bat``.

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

  6. 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"] }

Updated as of ASIO version 2.3.3.

Cross compilation

When Windows is the host and the target OS, the build script of asio-sys supports all cross compilation targets which are supported by the MSVC compiler. An exhaustive list of combinations could be found here with the addition of undocumented arm64, arm64_x86, arm64_amd64 and arm64_arm targets. (5.11.2023)

It is also possible to compile Windows applications with ASIO support on Linux and macOS.

For both platforms the common way to do this is to use the MinGW-w64 toolchain.

Make sure that you have included the MinGW-w64 include directory in your CPLUS_INCLUDE_PATH environment variable. Make sure that LLVM is installed and include directory is also included in your CPLUS_INCLUDE_PATH environment variable.

Example for macOS for the target of x86_64-pc-windows-gnu where mingw-w64 is installed via brew:

export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/opt/homebrew/Cellar/mingw-w64/11.0.1/toolchain-x86_64/x86_64-w64-mingw32/include"

More Repositories

1

rodio

Rust audio playback library
Rust
1,690
star
2

vst-rs

VST 2.4 API implementation in rust. Create plugins or hosts. Previously rust-vst on the RustDSP group.
Rust
973
star
3

dasp

The fundamentals for Digital Audio Signal Processing. Formerly `sample`.
Rust
720
star
4

rust-portaudio

PortAudio bindings and wrappers for Rust.
Rust
373
star
5

deepspeech-rs

Rust bindings for the deepspeech library
Rust
295
star
6

dsp-chain

A library for chaining together multiple audio dsp processors/generators, written in Rust!
Rust
287
star
7

lewton

Rust vorbis decoder
Rust
250
star
8

vst3-sys

Raw Bindings to the VST3 API
Rust
210
star
9

rust-jack

Decent jack bindings for rust
Rust
204
star
10

baseview

low-level window system interface for audio plugin UIs
Rust
184
star
11

synth

A polyphonic Synth type whose multiple oscillators generate sound via amplitude and frequency envelopes, implemented in Rust. DEPRECATED: This is a very old crate with very old design patterns and is no longer maintained. You might be interested in checking out `dasp` for composing your own synth instead.
Rust
172
star
12

coreaudio-rs

A friendly rust interface to Apple's Core Audio API.
Rust
169
star
13

rust-lv2

A safe, fast, and modular framework to create LV2 plugins, written in Rust
Rust
150
star
14

audrey

A crate to simplify reading, writing and converting between a variety of audio formats.
Rust
114
star
15

ogg

Ogg container decoder and encoder written in pure Rust
Rust
97
star
16

rimd

Library for handling Midi and Standard Midi Files in Rust
Rust
77
star
17

wmidi

Rust midi encoding and decoding library.
Rust
63
star
18

pitch_calc

A library for musical pitch conversions! Provides functions and methods for converting between frequency (hz), midi-step, letter-octave and mels.
Rust
60
star
19

coreaudio-sys

Raw bindings to the OSX CoreAudio framework generated by bindgen (see coreaudio-rs for a more rust-esque wrapper).
Rust
59
star
20

sound_stream

A Rust audio IO stream in the style of an "Event Iterator" driven by PortAudio.
Rust
57
star
21

time_calc

A library for music/DSP time conversions! Provides functions and methods for converting between ticks, ms, samples, bars, beats and measures.
Rust
49
star
22

musical_keyboard

A small lib for converting keyboard input into musical notes.
Rust
26
star
23

mp3

Rust MP3 decoder project
Rust
23
star
24

sampler

A polyphonic sampler instrument that supports unique sample mappings across both frequency and velocity ranges. DEPRECATED: This is a very old crate with very old design patterns and is no longer maintained. You might be interested in checking out `dasp` for composing your own sampler instead.
Rust
23
star
25

areweaudioyet

Are We Audio Yet?
CSS
18
star
26

simplemad

A Rust interface for the MPEG audio (MP1, MP2, MP3) decoding library libmad
Shell
17
star
27

caf

Rust decoder for Apple's Core Audio Format (CAF)
Rust
14
star
28

envelope

An interpolatable Envelope type along with a generic 2D Point, useful for controlling parameters over time.
Rust
13
star
29

website

Website for all things audio in the Rust programming language
HTML
10
star
30

vst2-sys

Bindings for the VST 2.4 API.
Rust
9
star
31

instrument

Converts discrete note events to a continuous signal of amplitude and frequency frames over N number of voices.
Rust
8
star
32

rnnoise-c

Rust bindings to Xiph's rnnoise denoising library
Rust
7
star
33

rms

A simple type for calculating and storing the RMS given some buffer of interleaved audio samples.
Rust
7
star
34

volume

A simple dsp-chain node for multiplying the amplitude of the output buffer by some volume.
Rust
6
star
35

panning

A variety of utilities related to audio panning in Rust.
Rust
5
star
36

lyra

Feature extraction POC
Rust
4
star