audio API

← audio · version 0.2.8

23 public items, in source order. Generated from the registry index, which re-derives them from the package source at publish time — so a signature here is the one that version actually ships.

pub fn init(rate: integer = 44100)

Set the output rate (frames per second, 44100 unless set) and stop every voice; the loaded clips stay. A platform's output layer calls this with the rate its device runs at.

pub fn rate() -> integer

The output rate the mix renders at.

pub fn has_vorbis() -> boolean

Does this build decode OGG Vorbis? Every target but the freestanding wasm build (the Rust writer's page) does.

pub fn load(path: text) -> integer?

Load a clip from a WAV (PCM 8/16/24/32-bit or float, mono or stereo) or OGG Vorbis file. Null when the file cannot be read or decoded.

pub fn load_bytes(bytes: const vector<u8>) -> integer?

Load a clip from a file's bytes already in memory.

pub fn load_samples(samples: const vector<single>, sample_rate: integer) -> integer?

A mono clip from samples in [-1, 1] at sample_rate — a sound made in loft, kept to be played again.

pub fn unload(clip: integer)

Unload a clip; every voice playing it stops.

pub fn clip_frames(clip: integer) -> integer

A clip's length in frames, its rate and its channel count (1 or 2); 0 for no clip.

pub fn clip_rate(clip: integer) -> integer
pub fn clip_channels(clip: integer) -> integer
pub fn play(clip: integer, volume: float, looping: boolean = false, pan: float = 0.0, start: float = 0.0) -> integer?

Play a clip: volume 0.0-1.0, looping repeats it until it is stopped, pan places it from -1 (left) through 0 (centre) to 1 (right), start begins that many seconds in. Answers the voice, or null when the clip is unknown or every voice is busy (64 play at once).

pub fn play_raw(samples: const vector<single>, sample_rate: integer, volume: float) -> integer?

Play mono samples in [-1, 1] at sample_rate once; the samples are released when it ends.

pub fn stop(voice: integer)

Stop a voice. A voice that already ended is left alone, as is whatever plays in its place.

pub fn stop_all()

Stop every voice — a pause menu, a scene change, a game over.

pub fn playing(voice: integer) -> boolean

Is this voice still playing?

pub fn set_volume(voice: integer, volume: float)

Set a playing voice's volume, 0.0-1.0.

pub fn set_pan(voice: integer, pan: float)

Move a playing voice across the stereo field, -1 (left) to 1 (right).

pub fn seek(voice: integer, seconds: float) -> boolean

Jump a playing voice to seconds into its clip. False for a voice that ended, and for a position past the end of a clip that does not loop (a looping clip wraps).

pub fn render(frames: integer) -> vector<i32>

The next frames frames of the mix: 2 * frames samples, left and right interleaved, each a 16-bit value (-32767 to 32767). Advances every voice; a voice that reaches the end of a clip it does not loop ends.

pub fn start(silent: boolean = false) -> boolean

Start the output, so every voice is heard: the system's sound device on a desktop, the board's I2S stream on a Tanmatsu. silent plays at real-time pace into no device at all, for a test or a server. Answers whether it runs: false where this machine has no output (no sound device, or a browser build), and the engine then only renders. Starting twice is starting once. Set the rate with init before this.

pub fn stop_output()

Stop the output; the voices keep their place and play on from it after the next start.

pub fn output_running() -> boolean

Is the output running?

pub fn output_frames() -> integer

How many stereo frames the output has taken since it started — the clock of what was heard.