pub struct LiveEncoder { /* private fields */ }Expand description
Push-driven encoder for live PCM streams that arrive in chunks of arbitrary length (audio device callbacks, file readers, sockets). Holds residual samples internally across calls so the caller can push whatever they have and harvest frames as they become available.
One-frame-at-a-time Vocoder is the right primitive for callers
that already have whole-buffer PCM. LiveEncoder is for callers
that don’t.
let mut enc = LiveEncoder::new(Rate::Imbe7200x4400);
// 256 samples (audio-device callback); not a multiple of 160.
let chunk: [i16; 256] = [0; 256];
let frames = enc.push(&chunk);
assert_eq!(frames.len(), 1); // one full frame produced
assert!(frames[0].is_ok());
// 96 samples residue; next push contributes them to the next frame.
assert_eq!(enc.pending_samples(), 96);Implementations§
Source§impl LiveEncoder
impl LiveEncoder
Sourcepub fn vocoder(&self) -> &Vocoder
pub fn vocoder(&self) -> &Vocoder
Read-only access to the underlying Vocoder (for stats /
rate / disposition queries).
Sourcepub fn push(&mut self, pcm: &[i16]) -> Vec<Result<Vec<u8>, VocoderError>>
pub fn push(&mut self, pcm: &[i16]) -> Vec<Result<Vec<u8>, VocoderError>>
Append PCM samples and emit zero or more FEC frames. Per-frame
errors are surfaced as Err entries in the returned Vec; the
buffer drains regardless so a single bad frame doesn’t stall
the stream.
Sourcepub fn pending_samples(&self) -> usize
pub fn pending_samples(&self) -> usize
Number of samples currently buffered (between 0 and
frame_samples()-1 after every push returns).
Sourcepub fn discard_pending(&mut self)
pub fn discard_pending(&mut self)
Drop any pending samples without encoding them. Useful at stream shutdown when the caller doesn’t want a partial-frame flush.
Sourcepub fn flush(&mut self) -> Result<Option<Vec<u8>>, VocoderError>
pub fn flush(&mut self) -> Result<Option<Vec<u8>>, VocoderError>
Pad pending residue with zeros to a full frame and encode one
final frame. Returns Ok(Some(bits)) if residue existed,
Ok(None) if the buffer was empty. Buffer is drained either
way.
Use this at end-of-stream to avoid abruptly dropping the trailing samples; the cost is at most one extra frame of zero-padding tacked onto the last word of audio.