Crate simplemad

Source
Expand description

This crate provides an interface to libmad, the MPEG audio decoding library.

To begin, create a Decoder from a byte-oriented source using Decoder::decode or Decoder::decode_interval. Fetch results using get_frame or the Iterator interface. MP3 files often begin or end with metadata, which will cause libmad to produce errors. It is safe to ignore these errors until libmad reaches the start of the audio data or the end of the file.

§Examples

#![allow(unused_variables)]
use simplemad::{Decoder, Frame};
use std::time::Duration;
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 decoder = Decoder::decode(file).unwrap();

for decoding_result in decoder {
    match decoding_result {
        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]);
        },
    }
}

// Decode the interval from 1s to 2s (to the nearest frame),
let file_b = File::open(&path).unwrap();
let partial_decoder = Decoder::decode_interval(file_b,
                                               Duration::from_secs(1),
                                               Duration::from_secs(2));
let frames: Vec<Frame> = partial_decoder.unwrap()
                                        .filter_map(|r| match r {
                                            Ok(f) => Some(f),
                                            Err(_) => None})
                                        .collect();

// Decode only the headers to quickly calculate the file's length
let file_c = File::open(&path).unwrap();
let headers = Decoder::decode_headers(file_c).unwrap();
let duration = headers.filter_map(|r| {
                          match r {
                              Ok(f) => Some(f.duration),
                              Err(_) => None,
                          }
                      }).fold(Duration::new(0, 0), |acc, dtn| acc + dtn);

Structs§

Decoder
An interface for the decoding operation
Frame
A decoded frame
MadFixed32
libmad’s native fixed-point sample format

Enums§

SimplemadError
An error encountered during the decoding process