Crate simplemad [] [src]

This crate provides an interface to libmad, allowing the decoding of MPEG audio files, including MP3s.

To begin, create a new Decoder from a byte-oriented source. Decoder implements the Iterator interface, allowing convenient sequential access to the output of libmad. Decoder yields type Result<Frame, MadError>. Frame and MadError correspond to libmad's struct types mad_pcm and mad_error, respectively. Samples are signed 32 bit integers and are organized into channels. For stereo, the left channel is channel 0.

MP3 files often begin with metadata, which will cause libmad to complain. It is safe to ignore errors until libmad reaches audio data and starts producing frames.

Examples

use simplemad::Decoder;
use std::fs::File;
use std::path::Path;

let path = Path::new("sample_mp3s/constant_stereo_128.mp3");
let file = File::open(&path).unwrap();
let mut decoder = Decoder::new(file);

// Take frames one at a time
let first_decode_result = decoder.next();
let second_decode_result = decoder.next();

// Read the rest of the frames using a loop
for item in decoder {
    match item {
        Err(e) => println!("Error: {:?}", e),
        Ok(frame) => {
          println!("Frame sample rate: {}", frame.sample_rate);
          println!("First audio sample (left channel): {}", frame.samples[0][0]);
          println!("First audio sample (right channel): {}", frame.samples[1][0]);
        }
    }
}

Structs

Decoder
Frame

A decoded frame

Enums

MadError

Errors generated by libmad