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
impl WavStreamDecoder
Sourcepub const fn format(&self) -> Option<WavFormat>
pub const fn format(&self) -> Option<WavFormat>
What the fmt chunk declared, once it has arrived.
Sourcepub fn ready_samples(&self) -> usize
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
impl Debug for WavStreamDecoder
Source§impl Default for WavStreamDecoder
impl Default for WavStreamDecoder
Source§impl StreamSource for WavStreamDecoder
impl StreamSource for WavStreamDecoder
Source§fn push(&mut self, bytes: &[u8]) -> Result<usize, DecodeError>
fn push(&mut self, bytes: &[u8]) -> Result<usize, DecodeError>
bytes to the stream and returns how many were taken. Read moreSource§fn pull(
&mut self,
output: &mut Vec<f32>,
max_frames: usize,
) -> Result<usize, DecodeError>
fn pull( &mut self, output: &mut Vec<f32>, max_frames: usize, ) -> Result<usize, DecodeError>
max_frames frames of decoded samples to output, and
returns how many frames it appended. Read more