Expand description
A pure Rust MPEG audio (MP3, MP2, MP1) decoder: a safe, bit-exact port of
minimp3 and its minimp3_ex helpers.
Reader(over anyRead + Seek) andSliceReader(over bytes in memory) are the easy way in: they skip tags, trim the encoder delay and padding for gapless playback, know the stream length, and seek to exact samples.decode_alldecodes a whole buffer into aVec.frames,VbrTag,detectand the tag functions work without allocating.Decoder(fromnanomp3-core) decodes one frame at a time with no allocation or buffering at all.
let file = std::fs::File::open("song.mp3")?;
let mut reader = nanomp3::Reader::<_, f32>::new(std::io::BufReader::new(file))?;
println!("{:?} samples per channel at {} Hz", reader.total_samples(), reader.sample_rate());
reader.seek(44_100)?; // one second in, at 44.1 kHz
let mut pcm = vec![0f32; 4096];
while let n @ 1.. = reader.read(&mut pcm)? {
let interleaved = &pcm[..n];
// ...
}§Features
std(default):Readerand the other*_readerfunctions. Impliesalloc.alloc:SliceReaderanddecode_all.wideandlayer12(default): seenanomp3-core.
§minimp3 compatibility
Output matches minimp3 bit for bit, except where minimp3 has an obvious
bug: nanomp3 fixes those by default, and Options::minimp3_compat (or
Decoder::new_minimp3_compat) restores minimp3’s behavior exactly. The
fixed bugs are:
i16output rounds samples in (−1.5, −0.5] to 0 instead of −1.- APEv2 tags without a header are over-trimmed by 32 bytes of audio.
- With I/O readers, tags at the end of the stream are stripped again every time the end is reached, which can cut audio that happens to end like a tag.
- With I/O readers, junk longer than the read buffer (128 KiB) ends the scan early, so the length and seek index stop short.
- With I/O readers, the decoder is told there are more bytes available than there are, and can read stale data (in C, past the buffer). This can make the reported length come out short.
- With I/O readers, a seek fails with an I/O error if a frame it needs to re-read is cut short by the end of the file.
By default, the I/O readers behave exactly like the in-memory ones, and those behave exactly like minimp3 (except for the tag fix above).
Structs§
- Decoded
- A decoded stream, from
decode_all. - Decoder
- The core MP3 decoder, with no internal buffering.
- Frame
- An MPEG audio frame found by
frames. - Frame
Info - Information about a frame found by
Decoder::decode. - Frames
- Iterator returned by
frames. - Options
- Options for
SliceReaderandReader. - Reader
- A seekable reader over MP3 data from any
Read + Seeksource, such as aFile. See the crate documentation. - Slice
Reader - A seekable reader over MP3 data in memory. See the crate documentation.
- VbrTag
- A Xing or Info tag: the first frame of most encoded MP3 files carries one instead of audio, recording the stream length and, with the LAME/Lavc extension, the encoder delay and padding needed for gapless playback.
Enums§
- Channels
- The channel formats that may be encoded in an MP3 frame.
- Decode
Error - Why
Decoder::decodeproduced no audio. - Error
- Errors from the readers and
*_readerfunctions.
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.
Functions§
- decode_
all - Decodes all of
data(mp3dec_load_buf). Tags are skipped, and the encoder delay and padding are trimmed when the stream has a LAME/Lavc tag. - decode_
all_ reader - Decodes all of
iointo memory, likedecode_all(mp3dec_load_cb). - decode_
all_ reader_ with decode_all_readerwith options.- decode_
all_ with decode_allwith options.Options::skip_scanhas no effect.- detect
- Guesses whether
buf(the start of a file) is MPEG audio: it starts with an ID3v2 tag, or valid frames follow each other within the first 16 KiB (mp3dec_detect_buf). - detect_
reader - Like
detect, reading the start ofio(mp3dec_detect_cb). - for_
each_ frame_ reader - Calls
ffor each frame read fromio, starting at its current position, likeframes(mp3dec_iterate_cb). Frame offsets are relative to the starting position. - frames
- Iterates over the frames in
buf, skipping tags and junk between frames. - id3v2_
len - Returns the length of the ID3v2 tag at the start of
buf(header, body and footer), or 0 if there isn’t one. Only the first 10 bytes are examined, so the length can exceedbuf.len(). - strip_
tags - Removes an ID3v2 tag from the start of
bufand ID3v1/APEv2 tags from its end. - strip_
trailing_ tags - Removes ID3v1 (including the extended “TAG+” block) and APEv2 tags from
the end of
buf.