Struct rmp3::Decoder[][src]

pub struct Decoder<'src> { /* fields omitted */ }

High-level streaming iterator for parsing or decoding MPEG Audio data.

If the decoder should own the data, use a DecoderOwned.

Examples

Example that decodes every frame in an MP3 file:

use rmp3::{Decoder, Frame};

let mp3 = std::fs::read("test.mp3")?;
let mut decoder = Decoder::new(&mp3);

// step through with `next` which decodes each frame
while let Some(frame) = decoder.next() {
    if let Frame::Audio(audio) = frame {
        // process audio frame here!
    }
}

Another example that steps through every frame with peek (does not decode) and calculates the length:

// step through with `peek` which does not do decoding
while let Some(frame) = decoder.peek() {
    if let Frame::Audio(audio) = frame {
        length += audio.sample_count() as f64 / audio.sample_rate() as f64;
        // important: `peek` does *not* move to the next frame on its own
        decoder.skip();
    }
}

println!("Length: {:02}:{:05.2}", length as u64 / 60, length % 60.0);

Implementations

impl<'src> Decoder<'src>[src]

pub fn new(source: &'src [u8]) -> Self[src]

Constructs a new Decoder for processing MPEG Audio.

pub fn next<'pcm>(&'pcm mut self) -> Option<Frame<'src, 'pcm>>[src]

Reads the next frame, skipping over potential garbage data.

pub fn peek(&mut self) -> Option<Frame<'src, 'static>>[src]

Reads the next frame without decoding it, or advancing the decoder. Use skip to advance.

This means that the samples will always be empty in Audio, and sample_count should be used to inspect the length.

pub fn position(&self) -> usize[src]

Gets the current position in the input data, starting from 0.

pub fn set_position(&mut self, position: usize)[src]

Sets the current position in the input data.

If position is out of bounds, it's set to the end of the data instead.

pub fn skip(&mut self) -> Option<()>[src]

Skips the current frame the decoder is over, if any.

Auto Trait Implementations

impl<'src> RefUnwindSafe for Decoder<'src>

impl<'src> Send for Decoder<'src>

impl<'src> Sync for Decoder<'src>

impl<'src> Unpin for Decoder<'src>

impl<'src> UnwindSafe for Decoder<'src>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.