Skip to main content

WavStreamDecoder

Struct WavStreamDecoder 

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

Reads a WAV file that arrives in pieces.

The StreamSource half of this module. Bytes are pushed in whatever sizes they turn up in, samples are pulled out, and nothing is buffered in proportion to a size the file declared: chunk headers and the fmt and ds64 bodies are buffered, every other chunk body is discarded as it flows past, and the payload is decoded straight into a bounded ready buffer.

§fmt has to come first

WavReader accepts data before fmt ; this does not, and answers DecodeError::Malformed when it meets one. Decoding a payload that arrives before the header describing it would mean holding all of it, and an unbounded buffer is exactly what this type is arranged to avoid. Every other chunk order, and unknown chunks in any position, are the same on both paths.

§Example

use decibri_decode::{AudioSpec, StreamSource, WavCodec, WavStreamDecoder, WavWriter};

let file = WavWriter::new(AudioSpec::mono(16_000), WavCodec::PcmI16)
    .to_bytes(&[0.0, 0.5, -0.5])?;

let mut stream = WavStreamDecoder::new();
let mut samples = Vec::new();
for piece in file.chunks(7) {
    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(), Some(AudioSpec::mono(16_000)));
assert_eq!(samples, [0.0, 0.5, -0.5]);

Implementations§

Source§

impl WavStreamDecoder

Source

pub fn new() -> Self

A reader waiting for the first byte of a file.

Source

pub const fn format(&self) -> Option<WavFormat>

What the fmt chunk declared, once it has arrived.

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 WavStreamDecoder

Source§

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

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

impl Default for WavStreamDecoder

Source§

fn default() -> Self

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

impl StreamSource for WavStreamDecoder

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.