Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

Audio encoder over codec-sized interleaved f32 PCM.

Build one with Encoder::new, feed full PCM frames via encode, then pass the trailing partial frame to finish. Publish every packet either call returns, in order, and apply the terminal Finish::discard_padding when the container supports it.

Every frame becomes exactly one packet, in order, though a codec that pipelines returns it from a later call, so the nth packet always starts n frames into the stream.

Implementations§

Source§

impl Encoder

Source

pub fn new(config: &Config) -> Result<Self, Error>

Open an encoder for config, refusing a codec no backend on this host encodes.

Source

pub fn name(&self) -> &str

The encoder backend name in use, e.g. "libopus".

Source

pub fn config(&self) -> &Config

The encoder config, including the latest accepted runtime bitrate.

Source

pub fn codec(&self) -> Codec

The codec this encoder emits. A Producer must be built for the same codec to publish its packets.

Source

pub fn codec_rate(&self) -> u32

Sample rate the codec actually runs at, which is Config::sample_rate.

Source

pub fn codec_channels(&self) -> u32

Channel count the codec actually runs at, which is Config::layout’s channel count.

Source

pub fn frame_size(&self) -> usize

Number of samples per channel the codec consumes per call to encode.

Source

pub fn bitrate(&self) -> Rate

Current target bitrate.

Source

pub fn set_bitrate(&mut self, bitrate: Rate) -> Result<(), Error>

Retune the live encoder to bitrate.

§Errors

Returns Error::Unsupported when the codec’s rate is fixed (PCM) or the backend can’t change it mid-stream. The encoder keeps running at its opening rate, so a caller driving a control loop should stop adapting rather than stop encoding.

Source

pub fn encode(&mut self, pcm: &[f32]) -> Result<(), Error>

Submit one frame of interleaved f32 PCM at codec_rate, whose packet receive returns.

A codec that pipelines holds the packet past later frames; libopus and PCM ready it at once.

pcm.len() must equal frame_size() * codec_channels(). The Producer handles format conversion and resampling before calling this; for direct use, the caller does the same.

Source

pub fn receive(&mut self) -> Result<Option<Encoded>, Error>

Pop the oldest ready packet, or None while the codec holds the rest.

Call until None after each encode or flush.

Source

pub fn flush(&mut self) -> Result<(), Error>

Mark the end of input, so receive returns every packet the codec still holds. Frames encoded afterwards start new input.

Pads nothing, so the codec’s lookahead stays inside it: reach for this at a boundary the packets have to respect, and for finish at the end of the stream.

Source

pub fn finish(self, pcm: &[f32]) -> Result<Finish, Error>

Finish encoding, zero-padding pcm as the final partial frame and returning every packet needed to drain codec lookahead.

pcm is interleaved at codec_rate, may be empty, and must contain at most one frame. The packets start with any not yet received from earlier encode calls. Silence added here only drains audio already supplied; Finish::discard_padding reports how much of the decoded tail is artificial and must not count as source duration. Consuming the encoder prevents encoding across the artificial terminal padding.

Source

pub fn catalog(&self) -> AudioConfig

hang catalog entry describing this encoder’s output stream.

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<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more