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
| Area | Items | Feature |
|---|---|---|
| Decoding and encoding | decode, encode, read_audio_file, write_audio_file for WAV, AIFF, CAF, AU, FLAC and MP3 | always |
| Editing and DSP | Audio: cut, concat, mix, fades, filters, resampling, limiter, tempo and pitch | always |
| Analysis | Audio::loudness, peaks, RMS, analyze::spectrum, waveforms | always |
| Streaming | stream::Decoder for WAV, FLAC and MP3 a chunk at a time with seeking; raw PCM and resampling across chunks | always |
| Disk streaming | stream::FileStream: a native reader thread and bounded PCM/encoded buffers | file-stream |
| Synthesizer | synth: text scores rendered in an 8-bit style | synth |
| Sound effects | sfx: parameters, text form and seeded presets | sfx |
| Game mixer | mixer: voices, buses, fades, loops and an allocation-free render | mixer |
| Sound assets | assets: background loading and a budgeted decoded-audio cache | assets |
| Adaptive music | music: beat/bar transitions, crossfades, cues and intensity layers | music |
| Sound events | events: named text events with seeded choices, variation and limits | events |
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§
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
mixerfeature. - 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
synthmodule. Requires thesfxfeature, which enablessynth. - stream
- Incremental decoding and encoding.
- synth
- An 8-bit style synthesizer that renders text scores. Requires the
synthfeature.
Structs§
- Audio
- Planar floating-point audio. All channels have the same length. Samples may exceed ±1 to retain headroom; encoders clamp at quantization.
- Decode
Info - Decode
Options - Encode
Options - Error
- An error with a stable category and human-readable context.
- Filter
Options - Limiter
Options - Limits
- Allocation and duration limits checked before allocating decoded PCM.
- Normalize
Options - Normalize
Report - Read
Audio Options - Limits for the whole-file convenience reader. Encoded bytes and decoded PCM have separate budgets; codec-specific limits still apply during decoding.
- Silence
Options - Silent
Range - True
Peak Limiter Options - Opt-in offline true-peak limiter. The existing
crate::LimiterOptionsandAudio::limitkeep their sample-peak behavior.
Enums§
- Audio
Format - Dither
- Optional noise at final linear integer PCM quantization.
- Endian
- Error
Code - Stable error categories. Match on the code rather than the message.
- Fade
Curve - Filter
Type - Normalize
Unit - Sample
Format - Time
Position - 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. Seeread_audio_file_with_optionsfor 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