Skip to main content

FlacStreamDecoder

Struct FlacStreamDecoder 

Source
pub struct FlacStreamDecoder { /* private fields */ }
Expand description

Reads a FLAC stream that arrives in pieces.

The StreamSource half of this module, with the same shape and the same bounds as WavStreamDecoder and AiffStreamDecoder. Nothing is buffered in proportion to a size the file declared: the signature and block headers are buffered, the streaminfo body is buffered at its fixed 34 bytes, every other metadata body is discarded as it flows past, and one frame at a time is held while it is assembled, capped at 2,101,216 bytes, which is the largest frame the format can express plus room for its headers, and is a figure stated in this crate rather than read from a file.

The MD5 is computed as frames are decoded and verified by finish, so a streaming decode has the same end-to-end check a whole-file decode has.

§Example

use decibri_decode::{FlacStreamDecoder, StreamSource};

// `file` is RFC 9639 appendix D.3's worked example.
let mut stream = FlacStreamDecoder::new();
let mut samples = Vec::new();
for piece in file.chunks(5) {
    let mut offset = 0;
    while offset < piece.len() {
        offset += stream.push(&piece[offset..])?;
        while stream.pull(&mut samples, usize::MAX)? > 0 {}
    }
}
stream.finish(&mut samples)?;

assert_eq!(stream.spec().map(|spec| spec.sample_rate), Some(32_000));
assert_eq!(samples.len(), 24);

Implementations§

Source§

impl FlacStreamDecoder

Source

pub fn new() -> Self

A reader waiting for the first byte of a stream.

Source

pub fn frames() -> Self

A reader for a bare frame stream: no fLaC signature and no metadata, the first byte pushed is the first byte of a frame header.

The streaming half of FlacFrameReader, with the same properties and the same two escape-code rejections. spec and stream_info answer None until the first frame header has arrived, which is what they already mean on this type and why a bare stream needs no separate way of saying it.

Source

pub fn frames_with_stream_info(info: FlacStreamInfo) -> Self

A reader for a bare frame stream described by a streaminfo block the caller obtained elsewhere.

This is the Ogg and Matroska shape: streaminfo in the container’s codec-private header, bare frames in the packets. The block’s bytes become an info through FlacStreamInfo::from_block. It resolves the two escape codes and restores the MD5 check to a path that otherwise has none. See md5_check, which is how a caller finds out whether that check actually ran.

Source

pub const fn stream_info(&self) -> Option<FlacStreamInfo>

What the streaminfo metadata block declared, once it has arrived.

For a bare frame stream this is the description derived from the first frame, or the one the caller supplied.

Source

pub const fn md5_check(&self) -> Option<Md5Check>

What finish did about the streaminfo MD5, or None before it has run.

None is not a verdict. The check happens at the end of the stream and this reports what happened, never what is expected to happen, so a caller cannot read a guarantee off a decode that has not finished.

Source

pub fn ready_samples(&self) -> usize

How many interleaved samples are decoded and waiting to be pulled: frames times channels, which is the length a caller’s output vector grows by when they are.

Interleaved samples, not the frames pull counts; the two figures differ by the channel count.

Trait Implementations§

Source§

impl Debug for FlacStreamDecoder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FlacStreamDecoder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl StreamSource for FlacStreamDecoder

Source§

fn push(&mut self, bytes: &[u8]) -> Result<usize, DecodeError>

Hands bytes to the stream and returns how many were taken. Read more
Source§

fn pull( &mut self, output: &mut Vec<f32>, max_frames: usize, ) -> Result<usize, DecodeError>

Appends up to max_frames frames of decoded samples to output, and returns how many frames it appended. Read more
Source§

fn spec(&self) -> Option<AudioSpec>

The rate and layout of what pull produces, once enough of the header has arrived to know. Read more
Source§

fn buffered_bytes(&self) -> usize

How many bytes are held awaiting the rest of their frame. Read more
Source§

fn finish(&mut self, output: &mut Vec<f32>) -> Result<usize, DecodeError>

Declares that no more bytes are coming, appends whatever remains and returns how many frames it appended. Read more
Source§

fn reset(&mut self)

Returns the stream to its just-constructed state, dropping buffered bytes, undelivered samples and anything learned from the header.

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.