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
impl PcmDecoder
Sourcepub const fn new(format: SampleFormat, spec: AudioSpec) -> Self
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.
Sourcepub const fn format(&self) -> SampleFormat
pub const fn format(&self) -> SampleFormat
The format this decoder reads.
Sourcepub fn buffered_bytes(&self) -> usize
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
impl Debug for PcmDecoder
Source§impl Decoder for PcmDecoder
impl Decoder for PcmDecoder
Source§fn output_spec(&self) -> AudioSpec
fn output_spec(&self) -> AudioSpec
Source§fn feed(&mut self, input: &[u8]) -> Result<usize, DecodeError>
fn feed(&mut self, input: &[u8]) -> Result<usize, DecodeError>
input to the decoder and returns how many of its bytes were
taken. Read more