DecoderStream

Struct DecoderStream 

Source
pub struct DecoderStream<'src> { /* private fields */ }
Expand description

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

Convenience wrapper over Decoder to simplify general use where the entire file is already loaded in memory.

§Examples

These examples are adapted from Decoder to show the conveniences of DecoderStream.

Simple example decoding frames into a big Vec of all the samples:

use empy::{DecoderStream, Frame};

let mut decoder = DecoderStream::new(&[/* your file here */]);
let mut pcm: Vec<f32> = Vec::with_capacity(1024 * 1024 * 32);

while let Some(frame) = decoder.next() {
    match frame {
        Frame::Audio(audio) => {
            // note that you'd want to keep track of bitrate, channels, sample_rate
            // they can differ between adjacent frames (especially bitrate for VBR)
            pcm.extend_from_slice(audio.samples());
        },
        Frame::Other(_) => (/* don't care */),
    }
}

You don’t need to decode the samples if it’s not necessary. Example computing length in minutes and seconds:

use empy::{DecoderStream, Frame};

let mut decoder = DecoderStream::new(&[/* your file here */]);
let mut length = 0.0f64;

while let Some(frame) = decoder.peek() {
    if let Frame::Audio(audio) = frame {
        // note here that sample_count is *per channel* so it works out
        length += f64::from(audio.sample_count()) / f64::from(audio.sample_rate());
    }
    decoder.skip();
}

println!("Length: {:.0}m{:.0}s", length / 60.0, length % 60.0);

Implementations§

Source§

impl<'src> DecoderStream<'src>

Source

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

Initialises a new DecoderStream over src.

Source

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

Decodes the next frame, skipping over potential garbage data.

Source

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

Parses the next frame without decoding any samples or moving forward.

To advance, use the skip function.

Source

pub fn skip(&mut self) -> Option<usize>

Skips the current frame, moving on to the next. Avoids re-parsing after a previous call to peek.

If there was a frame to skip, returns how many bytes forward the DecoderStream advanced.

Source

pub fn offset(&self) -> usize

Returns the offset in the input data from the start (0).

Source

pub fn set_offset(&mut self, offset: usize) -> Result<(), usize>

Sets the offset in the input data from the beginning.

If offset is out of bounds, returns the maximum valid offset.

Auto Trait Implementations§

§

impl<'src> Freeze for DecoderStream<'src>

§

impl<'src> RefUnwindSafe for DecoderStream<'src>

§

impl<'src> Send for DecoderStream<'src>

§

impl<'src> Sync for DecoderStream<'src>

§

impl<'src> Unpin for DecoderStream<'src>

§

impl<'src> UnwindSafe for DecoderStream<'src>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.