pub struct AiffStreamDecoder { /* private fields */ }Expand description
Reads an AIFF or AIFF-C file that arrives in pieces.
The StreamSource half of this module, with the same shape and the same
bounds as WavStreamDecoder: 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, the
COMM body (capped at the format’s own 278-byte ceiling) and SSND’s
eight-byte prefix are buffered, every other chunk body is discarded as it
flows past, and the payload decodes into a bounded ready buffer.
§COMM has to come first
AiffReader accepts SSND before COMM; this does not, and answers
DecodeError::Malformed when it meets one, for the reason recorded on
the module: decoding a payload that arrives before the header describing
it would mean holding all of it. Every other chunk order, and unknown
chunks in any position, are the same on both paths.
§Example
use decibri_decode::{AiffStreamDecoder, AudioSpec, StreamSource};
// The same minimal file as the `AiffReader` example.
let mut file = Vec::new();
file.extend_from_slice(b"FORM");
file.extend_from_slice(&50u32.to_be_bytes());
file.extend_from_slice(b"AIFF");
file.extend_from_slice(b"COMM");
file.extend_from_slice(&18u32.to_be_bytes());
file.extend_from_slice(&1u16.to_be_bytes());
file.extend_from_slice(&3u32.to_be_bytes());
file.extend_from_slice(&8u16.to_be_bytes());
file.extend_from_slice(&[0x40, 0x0B, 0xFA, 0, 0, 0, 0, 0, 0, 0]);
file.extend_from_slice(b"SSND");
file.extend_from_slice(&11u32.to_be_bytes());
file.extend_from_slice(&[0; 8]);
file.extend_from_slice(&[0x00, 0x40, 0x80]);
file.push(0);
let mut stream = AiffStreamDecoder::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(8_000)));
assert_eq!(samples, [0.0, 0.5, -1.0]);Implementations§
Source§impl AiffStreamDecoder
impl AiffStreamDecoder
Sourcepub const fn format(&self) -> Option<AiffFormat>
pub const fn format(&self) -> Option<AiffFormat>
What the COMM 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 AiffStreamDecoder
impl Debug for AiffStreamDecoder
Source§impl Default for AiffStreamDecoder
impl Default for AiffStreamDecoder
Source§impl StreamSource for AiffStreamDecoder
impl StreamSource for AiffStreamDecoder
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