#audio-devices #audio-samples #low-level #operating-system #linux-macos #ouput

tinyaudio

TinyAudio is a cross-platform, easy-to-use, low-level, audio output library

4 releases

0.1.3 Mar 4, 2024
0.1.2 Jul 18, 2023
0.1.1 Apr 17, 2023
0.1.0 Mar 14, 2023

#64 in Audio

Download history 186/week @ 2024-02-14 140/week @ 2024-02-21 350/week @ 2024-02-28 203/week @ 2024-03-06 155/week @ 2024-03-13 226/week @ 2024-03-20 160/week @ 2024-03-27 137/week @ 2024-04-03 287/week @ 2024-04-10 155/week @ 2024-04-17 217/week @ 2024-04-24 171/week @ 2024-05-01 123/week @ 2024-05-08 152/week @ 2024-05-15 239/week @ 2024-05-22 220/week @ 2024-05-29

748 downloads per month
Used in 7 crates (2 directly)

MIT license

46KB
873 lines

TinyAudio

TinyAudio is a cross-platform audio output library. Its main goal to provide unified access to a default sound output device of your operating system as easy as possible, covering as many platforms such as PC (Windows, Linux, macOS), Mobile Devices (Android, iOS), and WebAssembly.

What this crate can do

The crate just takes the data you've prepared and sends it to a default operating system's sound output device. It uses floating-point audio samples and converts them to the closest supported platform-dependent format automatically. The crate guarantees, that the intermediate data buffer will always be of requested size. Use this crate, if you need to play your audio samples as easy as possible.

What this crate cannot do

It does not load any sound formats, it doesn't apply any digital signal processing (DSP) techniques, it doesn't do audio spatialization and so on. Also, the crate does not support device enumeration, device selection, querying of supported formats, input capturing (i.e. from microphone).

Supported platforms

Windows Linux macOS WebAssembly Android iOS

How it works

The crate internally creates an audio output context and uses a user-defined callback to supply the device with samples to play. The callback will be called periodically to generate new data; it will be called util the device instance is "alive". In other words this crate performs the simplest audio streaming.

Android details

This crate uses AAudio for audio output on Android platform. AAudio is quite new API, which was added in ~2017 (in Android 8.1 Oreo). This means that you have to use API Level 26+ to get the crate up and running. Also, you must initialize an audio device only after your application has gained focus (GainedFocus event in android-activity crate), otherwise device creation will fail. See android-examples directory for examples.

WebAssembly details

Most of the web browsers nowadays requires a "confirmation" action from a user (usually a button click or something similar) to allow a web page to play an audio. This means that you must initialize an audio device only after some action on a web page that runs your WebAssembly package. In the simplest scenario it could be a simple button with a callback that initializes an audio device. See wasm-examples directory for examples.

Examples

The crate is very easy to use, here's a few examples that will help you to start using it right away.

Initialization

The simplest possible example that shows how to initialize an output device.

use tinyaudio::prelude::*;

let _device = run_output_device(
    OutputDeviceParameters {
        channels_count: 2,
        sample_rate: 44100,
        channel_sample_count: 4410,
    },
    move |_| {
        // Output silence
    },
)
.unwrap();

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

Playing a sine wave

A simple example that plays a sine wave of 440 Hz looks like so:

# use tinyaudio::prelude::*;
let params = OutputDeviceParameters {
    channels_count: 2,
    sample_rate: 44100,
    channel_sample_count: 4410,
};

let _device = run_output_device(params, {
    let mut clock = 0f32;
    move |data| {
        for samples in data.chunks_mut(params.channels_count) {
            clock = (clock + 1.0) % params.sample_rate as f32;
            let value =
                (clock * 440.0 * 2.0 * std::f32::consts::PI / params.sample_rate as f32).sin();
            for sample in samples {
                *sample = value;
            }
        }
    }
})
.unwrap();

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

Dependencies

~0–3MB
~46K SLoC