Skip to main content

Crate nanomp3

Crate nanomp3 

Source
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 any Read + Seek) and SliceReader (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_all decodes a whole buffer into a Vec.
  • frames, VbrTag, detect and the tag functions work without allocating.
  • Decoder (from nanomp3-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): Reader and the other *_reader functions. Implies alloc.
  • alloc: SliceReader and decode_all.
  • wide and layer12 (default): see nanomp3-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:

  • i16 output 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.
FrameInfo
Information about a frame found by Decoder::decode.
Frames
Iterator returned by frames.
Options
Options for SliceReader and Reader.
Reader
A seekable reader over MP3 data from any Read + Seek source, such as a File. See the crate documentation.
SliceReader
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.
DecodeError
Why Decoder::decode produced no audio.
Error
Errors from the readers and *_reader functions.

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, i32 or u32.

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 io into memory, like decode_all (mp3dec_load_cb).
decode_all_reader_with
decode_all_reader with options.
decode_all_with
decode_all with options. Options::skip_scan has 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 of io (mp3dec_detect_cb).
for_each_frame_reader
Calls f for each frame read from io, starting at its current position, like frames (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 exceed buf.len().
strip_tags
Removes an ID3v2 tag from the start of buf and ID3v1/APEv2 tags from its end.
strip_trailing_tags
Removes ID3v1 (including the extended “TAG+” block) and APEv2 tags from the end of buf.