Skip to main content

Crate audiobox

Crate audiobox 

Source
Expand description

Audio decoding, editing, analysis and encoding in safe Rust, with zero dependencies, and the audio side of a game: a real-time mixer, an 8-bit synthesizer and parametric sound effects.

The crate uses only the standard library: no C libraries, FFmpeg, build scripts or unsafe. It compiles to WebAssembly, and its synthesizer, effects and mixer give the same samples on every platform.

use audiobox::{AudioFormat, NormalizeOptions, decode, encode};

fn prepare(bytes: &[u8]) -> audiobox::Result<Vec<u8>> {
    let clip = decode(bytes)?
        .cut(1.5, 30.0)?
        .to_mono()
        .resample(16_000)?
        .normalize(NormalizeOptions::default())?;
    encode(&clip, AudioFormat::Flac)
}

§What is included

AreaItemsFeature
Decoding and encodingdecode, encode, read_audio_file, write_audio_file for WAV, AIFF, CAF, AU, FLAC and MP3always
Editing and DSPAudio: cut, concat, mix, fades, filters, resampling, limiter, tempo and pitchalways
AnalysisAudio::loudness, peaks, RMS, analyze::spectrum, waveformsalways
Streamingstream::Decoder for WAV, FLAC and MP3 a chunk at a time with seeking; raw PCM and resampling across chunksalways
Disk streamingstream::FileStream: a native reader thread and bounded PCM/encoded buffersfile-stream
Synthesizersynth: text scores rendered in an 8-bit stylesynth
Sound effectssfx: parameters, text form and seeded presetssfx
Game mixermixer: voices, buses, fades, loops and an allocation-free rendermixer
Sound assetsassets: background loading and a budgeted decoded-audio cacheassets
Adaptive musicmusic: beat/bar transitions, crossfades, cues and intensity layersmusic
Sound eventsevents: named text events with seeded choices, variation and limitsevents

Enable the optional modules you use:

[dependencies]
audiobox = { version = "0.5", features = ["mixer", "synth", "sfx"] }

§Determinism

These give the same samples on every platform, including WebAssembly: the synthesizer, the sound effects and the mixer, and the offline transforms that render audio — Audio::pan, Audio::gain and Audio::gain_db, Audio::fade, Audio::crossfade_to, Audio::filter, Audio::limit, Audio::limit_true_peak, Audio::resample, Audio::tempo, Audio::pitch, Audio::trim_silence, Audio::normalize, and analyze::from_db. They compute with the crate’s own sine and 2^x series instead of the platform’s sin, cos, exp and powf, whose last bits can differ between systems; CI pins their output with checksums on Linux, macOS, Windows and WebAssembly. Analysis that only reports numbers, such as loudness, true peak and spectra, use the platform’s math. Normalization in every unit uses a separate deterministic measurement path and reports its values through Audio::normalize_with_report. Audio::measure_deterministic exposes that path for non-mutating final validation. Both LUFS paths require >= 4000 Hz.

Integer PCM export supports optional seeded Dither::Tpdf noise through EncodeOptions::dither. Its quantization is deterministic, including FLAC and stream::PcmEncoder::with_dither across chunk boundaries. Dither is disabled by default; float, G.711 and MP3 output reject enabled dither.

Errors are Error values with a stable ErrorCode. Decoding is bounded by Limits, checked before allocating. The website has a browser sandbox and a sound-effect and chiptune playground that run this crate through WebAssembly.

Re-exports§

pub use mixer::events;
pub use mixer::music;
pub use metadata::CoverArt;
pub use metadata::Tags;

Modules§

analyze
Waveforms, spectral analysis and K-weighted integrated loudness.
assets
Background loading and a cache of decoded sounds. Requires assets.
metadata
Publishing tags for MP3 (ID3v2.4), FLAC and WAV INFO.
mixer
A real-time mixer for games. Requires the mixer feature.
mp3
MPEG-1/2/2.5 Layer III decoding, probing and MPEG-1 CBR encoding.
pcm
Raw PCM conversion, including G.711 A-law and µ-law.
sfx
A parametric sound-effect generator in the style of the synth module. Requires the sfx feature, which enables synth.
stream
Incremental decoding and encoding.
synth
An 8-bit style synthesizer that renders text scores. Requires the synth feature.

Structs§

Audio
Planar floating-point audio. All channels have the same length. Samples may exceed ±1 to retain headroom; encoders clamp at quantization.
DecodeInfo
DecodeOptions
EncodeOptions
Error
An error with a stable category and human-readable context.
FilterOptions
LimiterOptions
Limits
Allocation and duration limits checked before allocating decoded PCM.
NormalizeOptions
NormalizeReport
ReadAudioOptions
Limits for the whole-file convenience reader. Encoded bytes and decoded PCM have separate budgets; codec-specific limits still apply during decoding.
SilenceOptions
SilentRange
TruePeakLimiterOptions
Opt-in offline true-peak limiter. The existing crate::LimiterOptions and Audio::limit keep their sample-peak behavior.

Enums§

AudioFormat
Dither
Optional noise at final linear integer PCM quantization.
Endian
ErrorCode
Stable error categories. Match on the code rather than the message.
FadeCurve
FilterType
NormalizeUnit
SampleFormat
TimePosition
Seconds, an exact frame position, or a parsed timestamp. Negative positions count from the end.

Functions§

decode
decode_with_options
encode
encode_with_options
read_audio_file
Decode a regular file using a 512 MiB encoded-byte cap and default decoded crate::Limits. See read_audio_file_with_options for path and error policy.
read_audio_file_with_options
Read a bounded regular file, then decode the same buffer without copying it.
sniff
Detect a supported format from its bytes, independent of the extension.
write_audio_file

Type Aliases§

AudioMetadata
Result