pub struct AudioStreamDecoder { /* private fields */ }Expand description
Reads a stream that arrives in pieces, in whatever of the carried formats its leading bytes turn out to name.
§How it buffers
The first Container::PROBE_BYTES bytes are held rather than delegated,
because there is no reader to delegate them to until they have all arrived.
On the push that completes them the format is identified, the matching
streaming reader is constructed, and the held bytes go into it in front
of everything that follows, so the inner reader sees the stream from its
first byte and not from byte thirteen. Whatever is left of that same push
goes straight on to the inner reader, so a caller that hands over the whole
file at once pays nothing for the indirection.
The buffer is twelve bytes and never grows: it is look-ahead, not a staging area, and every byte past identification is the inner reader’s own business.
spec is None until the inner reader knows it,
which is strictly later than identification: knowing a stream is a WAV is
not knowing its rate.
§Example
One byte at a time, which is the case that proves the buffering is real: identification spans twelve separate pushes and the audio still arrives.
use decibri_decode::{
AudioSpec, AudioStreamDecoder, Container, StreamSource, WavCodec, WavWriter,
};
let file = WavWriter::new(AudioSpec::mono(8_000), WavCodec::PcmI16)
.to_bytes(&[0.0, 0.5, -0.5])?;
let mut stream = AudioStreamDecoder::new();
let mut samples = Vec::new();
for byte in &file {
let mut offset = 0;
while offset < 1 {
offset += stream.push(&[*byte][offset..])?;
while stream.pull(&mut samples, usize::MAX)? > 0 {}
}
}
stream.finish(&mut samples)?;
assert_eq!(stream.container(), Some(Container::Wav));
assert_eq!(samples, [0.0, 0.5, -0.5]);Implementations§
Source§impl AudioStreamDecoder
impl AudioStreamDecoder
Sourcepub const fn container(&self) -> Option<Container>
pub const fn container(&self) -> Option<Container>
What the leading bytes turned out to be, once enough of them have arrived.
None before Container::PROBE_BYTES bytes have been pushed.
Sourcepub const fn wav_format(&self) -> Option<WavFormat>
pub const fn wav_format(&self) -> Option<WavFormat>
What the WAV fmt chunk declared, once it has arrived.
This is WavStreamDecoder::format on the reader the probe chose, so
a caller reaching the front door gets the same detail as one that
picked the reader by hand.
None in three cases: before the stream has been identified at all,
for a stream identified as any container other than
Container::Wav, and for a WAV whose fmt chunk has not yet
arrived. Identification is not the header: knowing a stream is a WAV
happens twelve bytes in and the fmt chunk is later.
Sourcepub const fn aiff_format(&self) -> Option<AiffFormat>
pub const fn aiff_format(&self) -> Option<AiffFormat>
What the AIFF COMM chunk declared, once it has arrived.
This is AiffStreamDecoder::format on the reader the probe chose.
None before the stream has been identified, for a stream identified
as any container other than Container::Aiff, and for an AIFF whose
COMM chunk has not yet arrived.
Sourcepub fn flac_stream_info(&self) -> Option<FlacStreamInfo>
pub fn flac_stream_info(&self) -> Option<FlacStreamInfo>
What the FLAC streaminfo metadata block declared, once it has arrived.
This is FlacStreamDecoder::stream_info on the reader the probe
chose.
None before the stream has been identified, for a stream identified
as any container other than Container::Flac, and for a FLAC stream
whose streaminfo block has not yet arrived. The probe never reaches a
bare frame stream, so the value here is always a block read from the
stream rather than one derived from a first frame or supplied by a
caller.
Sourcepub fn flac_md5_check(&self) -> Option<Md5Check>
pub fn flac_md5_check(&self) -> Option<Md5Check>
What the end of a FLAC stream did about the streaminfo MD5.
This is FlacStreamDecoder::md5_check on the reader the probe chose.
None before the stream has been identified, for a stream identified
as any container other than Container::Flac, and for a FLAC stream
on which finish has not run. None is not a
verdict: the check happens at the end of the stream, and a decode that
has not finished has nothing to report.
Trait Implementations§
Source§impl Debug for AudioStreamDecoder
impl Debug for AudioStreamDecoder
Source§impl Default for AudioStreamDecoder
impl Default for AudioStreamDecoder
Source§impl StreamSource for AudioStreamDecoder
impl StreamSource for AudioStreamDecoder
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