Skip to main content

LiveEncoder

Struct LiveEncoder 

Source
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

Source

pub fn new(rate: Rate) -> Self

Open a new live encoder at the given rate, all state cold.

Source

pub fn vocoder(&self) -> &Vocoder

Read-only access to the underlying Vocoder (for stats / rate / disposition queries).

Source

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.

Source

pub fn pending_samples(&self) -> usize

Number of samples currently buffered (between 0 and frame_samples()-1 after every push returns).

Source

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.

Source

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.

Source

pub fn reset(&mut self)

Reset all state — both the inner Vocoder (predictor / look-ahead / synth substates) and the residual sample buffer.

Source

pub fn rate(&self) -> Rate

Configured rate.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.