Skip to main content

PcmDecoder

Struct PcmDecoder 

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

Decodes a headerless linear PCM stream: bytes in one of the SampleFormats, samples out at a AudioSpec the caller states.

Headerless PCM is what a microphone produces and what several streaming transports carry, so nothing declares its rate, layout or sample format: they arrive out of band and are given at construction. Both are then fixed for the life of the instance, as Decoder::output_spec requires.

§Partial samples and partial frames

A feed that ends part-way through a sample holds the leftover bytes until the rest arrive. Three bytes of a four-byte f32 are not an error while the stream is open, and the boundary a caller happens to split its buffers on is not something a decoder gets to have an opinion about: feeding a stream in one slice, in 4096-byte slices or one byte at a time produces the same samples in the same order.

flush is where an incomplete item becomes DecodeError::Truncated, and it counts frames, not samples. A stereo stream that ends after an odd number of samples ended mid-frame, and reporting that as a clean end would hand the caller a buffer whose length is not a whole number of frames.

§Example

use decibri_decode::{AudioSpec, Decoder, PcmDecoder, SampleFormat};

let mut decoder = PcmDecoder::new(SampleFormat::I16Le, AudioSpec::mono(16_000));
let mut samples = Vec::new();

// 0x8000 is -1.0; 0x4000 is 0.5. The second sample arrives split in two.
decoder.feed(&[0x00, 0x80, 0x00])?;
decoder.decode(&mut samples)?;
assert_eq!(samples, [-1.0]);

decoder.feed(&[0x40])?;
decoder.decode(&mut samples)?;
decoder.flush(&mut samples)?;
assert_eq!(samples, [-1.0, 0.5]);

Implementations§

Source§

impl PcmDecoder

Source

pub const fn new(format: SampleFormat, spec: AudioSpec) -> Self

A decoder for format bytes producing samples at spec.

Nothing is validated because nothing can be: a headerless stream carries no claim to check the arguments against. spec is the assertion.

Source

pub const fn format(&self) -> SampleFormat

The format this decoder reads.

Source

pub fn buffered_bytes(&self) -> usize

How many bytes are held awaiting the rest of their frame.

0 means the stream is on a frame boundary and could be cut here without loss, the same meaning the figure carries on StreamSource::buffered_bytes, and the same figure flush reports as available when it rejects a truncated stream. It counts the bytes of an incomplete sample and any whole samples sitting past the last frame boundary.

Trait Implementations§

Source§

impl Debug for PcmDecoder

Source§

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

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

impl Decoder for PcmDecoder

Source§

fn output_spec(&self) -> AudioSpec

The rate and layout of the samples this decoder produces. Read more
Source§

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

Hands input to the decoder and returns how many of its bytes were taken. Read more
Source§

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

Appends every sample the decoder can produce from what it has been fed, and returns how many it appended. Read more
Source§

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

Declares end of input, appends any remaining samples and returns how many were appended. Read more
Source§

fn reset(&mut self)

Returns the decoder to its just-constructed state. Read more

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.