#mpeg #mp3 #libmad

simplemad

An interface for libmad, the MPEG audio decoding library

19 releases

Uses old Rust 2015

0.9.0 Mar 11, 2018
0.8.1 Nov 18, 2015
0.7.1 Oct 28, 2015
0.2.2 Apr 23, 2015

#514 in Audio

Download history 38/week @ 2024-03-15 36/week @ 2024-03-22 70/week @ 2024-03-29 41/week @ 2024-04-05 45/week @ 2024-04-12 53/week @ 2024-04-19 41/week @ 2024-04-26 40/week @ 2024-05-03 37/week @ 2024-05-10 42/week @ 2024-05-17 53/week @ 2024-05-24 50/week @ 2024-05-31 28/week @ 2024-06-07 33/week @ 2024-06-14 36/week @ 2024-06-21 14/week @ 2024-06-28

119 downloads per month
Used in mp3-metadata

MIT license

2.5MB
23K SLoC

Shell 7.5K SLoC // 0.2% comments M4 7K SLoC // 0.2% comments C 6.5K SLoC // 0.2% comments Rust 1K SLoC // 0.1% comments GNU Style Assembly 690 SLoC // 0.1% comments Automake 61 SLoC // 0.6% comments

Build Status

simplemad is a simple interface for libmad, the MPEG audio decoding library.

Use and examples

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.

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 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 30 seconds to 60 seconds:

let partial_decoder = Decoder::decode_interval(file,
                                               Duration::from_secs(30),
                                               Duration::from_secs(60)).unwrap();

You can also elect to only decode the header of each frame. This is useful if you want to quickly determine the length of a file.

let headers = Decoder::decode_headers(file).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);

Documentation

http://bendykst.github.io/doc/simplemad/index.html

License

MIT

Dependencies