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>
 
impl<'src> DecoderStream<'src>
Sourcepub const fn new(src: &'src [u8]) -> Self
 
pub const fn new(src: &'src [u8]) -> Self
Initialises a new DecoderStream over src.
Sourcepub fn next<'pcm>(&'pcm mut self) -> Option<Frame<'src, 'pcm>>
 
pub fn next<'pcm>(&'pcm mut self) -> Option<Frame<'src, 'pcm>>
Decodes the next frame, skipping over potential garbage data.
Sourcepub fn peek(&mut self) -> Option<Frame<'src, 'static>>
 
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.
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> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more