Kitedesk fork of github.com/rustdesk-org/cpal (Apache-2.0, upstream osx-screencapturekit 96d4da12). Branch kd/b283-sck-audio: ScreenCaptureKit audio only on macOS 13+, without the capturing process's own audio (B283).
Find a file
2023-10-17 12:13:30 +02:00
.github/workflows Fix compile issues in wasm-beep example (#723) 2022-11-06 04:34:08 +01:00
asio-sys Automate asio sys build (#804) 2023-10-17 12:13:30 +02:00
examples Fix synth_tones frequency (#792) 2023-08-03 03:42:34 +02:00
src Replace assert in WASAPI check with bubble'd error (#797) 2023-09-01 01:57:39 +02:00
.gitignore Ignore RLS log files 2019-12-31 15:42:35 +01:00
build.rs Re-run build script if CPAL_ASIO_DIR changes or the folder it points to does (#759) 2023-03-10 12:43:43 +01:00
Cargo.toml Update windows-rs dependency (#791) 2023-07-25 11:35:54 +02:00
CHANGELOG.md Update windows-rs dependency (#791) 2023-07-25 11:35:54 +02:00
Cross.toml Check and test for armv7, 32bit 2020-11-09 20:52:00 +01:00
Dockerfile Check and test for armv7, 32bit 2020-11-09 20:52:00 +01:00
LICENSE Create LICENSE 2019-08-11 20:20:43 +09:00
README.md Automate asio sys build (#804) 2023-10-17 12:13:30 +02:00

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. The script will try to find vcvarsall.bat and execute it with the right 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
    
  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.