Skip to content

neodsp/audio-host

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

audio-host

A backend-agnostic Rust library for managing audio input and output devices. This crate provides a unified, high-level interface for interacting with various audio backends, allowing you to write audio application code that is independent of the underlying audio driver implementation.

Features

audio-host abstracts over several popular audio backends. You can choose the one that best fits your needs via Cargo features.

Available Backends:

⚠️ Important: Mutual Exclusivity

You must enable exactly one audio backend feature at a time.

These backends are mutually exclusive. Trying to enable more than one (e.g., cpal and juce together) will result in a compile-time error.

Installation

Add audio-host to your Cargo.toml.

To use the default backend (rtaudio):

[dependencies]
audio-host = "0.5.0"

To use a specific backend (e.g., cpal), disable the default features:

[dependencies]
audio-host = { version = "0.5.0", default-features = false, features = ["cpal"] }

Usage

Listing devices

use audio_host::{AudioHost, Error, AudioBackend};

fn main() -> Result<(), Error> {
    let host = AudioHost::new()?;

    println!("API:     {}", host.api());
    println!("APIs:    {:#?}", host.apis());
    println!("Inputs:  {:#?}", host.inputs());
    println!("Outputs: {:#?}", host.outputs());

    Ok(())
}

Selecting devices

Call set_api, set_input, or set_output with a substring of the desired name before starting the stream. Each returns Err(Error::NotFound) if no matching device is found.

use audio_host::{AudioHost, Error, AudioBackend, Config};

fn main() -> Result<(), Error> {
    let mut host = AudioHost::new()?;

    host.set_api("ALSA")?;
    host.set_input("Focusrite")?;
    host.set_output("Focusrite")?;

    // ...
    Ok(())
}

Note: Some backends (e.g. cpal on Linux) expose a virtual "Default Audio Device" as the default that does not appear in the inputs() / outputs() lists. In that case, omit the set_input / set_output calls and rely on the default selected by AudioHost::new().

Starting a stream

use audio_host::{AudioBlockOpsMut, AudioHost, Error, AudioBackend, Config};

fn main() -> Result<(), Error> {
    let mut host = AudioHost::new()?;

    host.start(
        Config {
            num_input_channels: 2,
            num_output_channels: 2,
            sample_rate: 48000,
            num_frames: 1024,
        },
        move |input, mut output| {
            // Simple pass-through: copy input to output
            output.copy_from_block(&input);
        },
    )?;

    std::thread::sleep(std::time::Duration::from_secs(5));

    host.stop()?;

    Ok(())
}

Set num_input_channels or num_output_channels to 0 to open an output-only or input-only stream.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE.Apache
MIT
LICENSE.MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages