AudioSamples
Typed, invariant-preserving audio processing for Rust
Overview
Most audio libraries hand you a bare buffer. In Python it is a NumPy array
whose dtype is explicit but whose meaning is not: the sample rate, amplitude
range, channel layout, and interleaving live elsewhere, if they are tracked at
all. Rust libraries give you fast, safe primitives but still leave you holding
the raw buffer, writing your own conversions, and carrying those conventions in
your head. Mismatches between them, such as a stale sample rate after
resampling or a buffer that was interleaved when the next stage expected planar,
are a recurring source of bugs.
audio_samples puts that metadata in the type. [AudioSamples<T>] pairs the
PCM data with its sample rate, channel count, and memory layout, and every
operation either preserves those invariants or updates them explicitly. The
sample type T (u8, i16, [I24], i32, f32, f64) is part of the type,
so format and bit-depth conversions are checked and scaled correctly rather than
left to ad hoc casts.
Installation
The default feature set (bare-bones) is the core types and traits with no
optional dependencies. Add features for the operations you need; see
Features.
Upgrading from 1.x? See the migration guide.
Quick Start
Creating audio
Build AudioSamples from an ndarray, or generate a signal. Construction is
fallible because the data, channel count, and sample rate must agree.
use ;
use array;
use Duration;
Generators cover the classic oscillators plus logarithmic chirps
(exponential_chirp), band-limited square/sawtooth/triangle waves, FM
(fm_signal), and white/pink/brown noise.
A processing pipeline
Operations carry the sample rate and channel layout with the data and chain
through the borrowing API. Here two tones are mixed, the low one is filtered
out, the result is peak-normalized, and its level is read back. Enable the
processing feature.
use ;
use Duration;
Type conversions
Conversions are audio-aware: integer PCM is scaled to and from the float
[-1.0, 1.0] range rather than cast blindly.
use ;
use Duration;
let pcm = ; // i16 PCM
let as_float = pcm.as_f32; // scaled into [-1.0, 1.0]
let _back = as_float.as_i16; // and back to i16
Spectral analysis
Enable the transforms feature for FFT/STFT and the spectral feature suite.
use ;
use ;
use Duration;
The full transform set (FFT, MFCC, chromagram, CQT, PSD, inverse STFT) and the rest of the spectral features are listed under Features.
Features
The default feature is bare-bones: the core types and traits, no optional
dependencies. Enable the rest as needed.
Core operations
| Feature | Description |
|---|---|
statistics |
Peak, RMS, mean, variance, zero-crossings; the spectral feature suite (centroid, rolloff, bandwidth, flatness, contrast, slope, crest) when transforms is also enabled |
processing |
Normalization, scaling, clipping, DC-offset removal (requires statistics) |
editing |
Trim, pad, reverse, fade, concatenate, perturb (requires statistics, random-generation) |
channels |
Interleave/deinterleave, mono/stereo conversion, channel extraction |
iir-filtering |
Butterworth, Chebyshev I and II, Elliptic (Cauer), and Bessel filters; low-, high-, band-pass and band-stop responses; zero-phase filtfilt; design-once streaming SosFilter |
parametric-eq |
Parametric EQ bands and ThreeBandEqConfig (requires iir-filtering) |
dynamic-range |
Compression, limiting, gating, expansion via config structs, with side-chain support |
envelopes |
Amplitude, RMS, and attack-decay envelope followers |
vad |
Voice activity detection |
Spectral and analysis
| Feature | Description |
|---|---|
transforms |
FFT, STFT and inverse STFT, MFCC, chromagram, CQT, power spectral density |
psychoacoustic |
Bark/Mel band layouts, absolute threshold of hearing, masking thresholds, SMR (requires transforms) |
pitch-analysis |
YIN and autocorrelation pitch detection and tracking (requires transforms) |
onset-detection |
Onset detection (requires transforms, peak-picking, processing) |
beat-tracking |
Beat tracking and tempo estimation (estimate_tempo) |
peak-picking |
Peak picking on onset-strength envelopes |
decomposition |
Harmonic/percussive source separation (requires onset-detection) |
Utility
| Feature | Description |
|---|---|
resampling |
Sample-rate conversion via rubato |
random-generation |
White, pink, and brown noise generators |
fixed-size-audio |
Stack-allocated fixed-size buffers |
plotting |
Interactive HTML plots (waveform, spectrum, phase, spectrogram, Lissajous) via plotly |
static-plots |
PNG/SVG export (requires plotting; see PLOTTING.md) |
simd |
SIMD-accelerated sample conversions via the wide crate (stable; results are bit-identical to the scalar path) |
Bundles
| Feature | Description |
|---|---|
full |
Everything below |
full_no_plotting |
Everything except plotting |
Documentation
Full API documentation: https://docs.rs/audio_samples. Architecture notes are in documentation/ARCHITECTURE.md.
Examples
The repository includes runnable examples in examples/, each annotated with
its required feature flags. Run one with, for example:
A larger demo lives in a separate repository:
Companion Crates
audio_samples_io: audio file decoding and encoding- `audio_samples_streaming: streaming functionality
- `audio_samples_ml: audio machine learning such as STT and TTS.
- `audio_samples_qoe: audio Quality of Experience (qoe) metrics.
audio_samples_python: Python bindingsspectrograms: spectrograms and time-frequency transforms (used by thetransformsfeature)i24: 24-bit signed integer type for Rustdtmf_tones:no_stdDTMF keypad frequencies
License
MIT.
Citing
If you use AudioSamples in research, please cite:
Contributing
Contributions are welcome. Please open an issue or pull request, and see CONTRIBUTING.md for guidance.