Expand description
The MPEG audio frame decoder behind nanomp3: a safe Rust port of minimp3 whose output is bit-identical to upstream’s.
It’s no_std, allocates nothing and does no buffering: Decoder::decode
finds and decodes one frame at a time from the bytes you give it. Because a
frame only counts once the frames after it are seen to be consistent, keep
several frames of input available (16 KiB is plenty) and refill as it
drains.
use nanomp3_core::{DecodeError, Decoder, MAX_SAMPLES_PER_FRAME};
let mut decoder = Decoder::new();
let mut pcm = [0f32; MAX_SAMPLES_PER_FRAME]; // or i16, u8, i32, ...
let mut mp3 = data;
while !mp3.is_empty() {
let (consumed, result) = decoder.decode(mp3, &mut pcm);
mp3 = &mp3[consumed..];
match result {
Ok(info) => {
let samples = &pcm[..info.samples_produced * info.channels.num() as usize];
// ... interleaved samples at info.sample_rate
}
// Normal while starting up or after a seek; later frames decode.
Err(DecodeError::ReservoirUnavailable(_)) => {}
Err(e) => eprintln!("skipped: {e}"),
}
}Most users want the nanomp3 crate, which re-exports this one and adds a
seekable reader with its own buffering, gapless playback, and tag handling.
§Features
wide(default): explicit SIMD via thewidecrate. Without it, the same kernels run on plain arrays and rely on LLVM’s auto-vectorizer.layer12(default): decode MPEG Layer I and II frames too. Without it they are reported asDecodeError::UnsupportedLayer, like minimp3 built withMINIMP3_ONLY_MP3.
Structs§
- Decoder
- The core MP3 decoder, with no internal buffering.
- Frame
Info - Information about a frame found by
Decoder::decode.
Enums§
- Channels
- The channel formats that may be encoded in an MP3 frame.
- Decode
Error - Why
Decoder::decodeproduced no audio.
Constants§
- MAX_
SAMPLES_ PER_ FRAME - The minimum length of the PCM output buffer, in samples.
Traits§
- Sample
- A PCM output sample format:
f32,i8,u8,i16,u16,i32oru32.