Skip to main content

Normalizer

Struct Normalizer 

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

Stateful two-stage converter that normalizes input device frames to the internal canonical form (48k/stereo/960 frames), then converts them to one or more output taps (primary and optional secondary).

Stage 1 (internal normalization) runs once and feeds the resulting canonical form to stage 2 of both taps. An optional InnerProcessor is applied once to the canonical form before it is split for stage 2, so both taps receive the same processed audio.

Accumulate interleaved samples with push, then retrieve completed output chunks one at a time with pop_chunk (primary) and pop_secondary (secondary).

Implementations§

Source§

impl Normalizer

Source

pub fn new( in_sample_rate: u32, in_channels: u16, output: OutputFormat, ) -> Result<Self>

Create a normalizer for the input sample rate, input channel count, and output format.

Stage 1 normalizes input to 48k/stereo (in_sample_rate == 48000 bypasses sample-rate conversion; in_channels of 1 duplicates mono to stereo, 2 is unchanged, and 3 or more uses the front two channels). Stage 2 converts the canonical form to output (passthrough when output == {48000, 2}).

Expects the caller to have run OutputFormat::validate on output (this method does not clamp it to a valid range).

rubato resampler construction can fail, for example with extreme rate ratios. Panicking would silently stop the non-RT capture thread, so failures return Error::Backend and propagate to the caller.

Source

pub fn with_secondary(self, secondary: OutputFormat) -> Result<Self>

Add a secondary output tap (expects OutputFormat::validate to have been called).

The internal canonical form (48k/stereo) is generated once and fed to stage 2 of both taps. The secondary tap has independent stage 2 and PTS state, and emits its own fixed 20ms chunks. rubato construction failures return Error::Backend.

Source

pub fn with_inner_processor(self, processor: Box<dyn InnerProcessor>) -> Self

Inject a processor to apply once to the internal canonical form before the stage 2 split.

Source

pub fn in_sample_rate(&self) -> u32

Input sample rate (Hz).

Source

pub fn output(&self) -> OutputFormat

Primary output format.

Source

pub fn secondary_output(&self) -> Option<OutputFormat>

Secondary output format (only when a secondary tap is configured).

Source

pub fn has_secondary(&self) -> bool

Whether a secondary tap is enabled.

Source

pub fn is_passthrough(&self) -> bool

Whether stage 1 sample-rate conversion is bypassed (in == 48000).

Source

pub fn is_output_passthrough(&self) -> bool

Whether stage 2 for the primary output is fully bypassed (output == {48000, 2}).

Source

pub fn push(&mut self, interleaved: &[f32], device_pts_ns: i64) -> Result<()>

Accumulate interleaved input samples.

interleaved must have a length divisible by in_channels. device_pts_ns is the device-provided PTS for the first frame in this push.

Returns Error::Backend if rubato’s process fails. This avoids silently stopping the capture thread with a panic and lets the caller stop the stream explicitly.

Source

pub fn pop_chunk(&mut self) -> Option<(Vec<f32>, i64)>

Retrieve one completed primary output chunk.

Returns (out_chunk_frames interleaved frames at output.channels, PTS in ns for the first sample). Returns None if a full chunk is not buffered.

Source

pub fn pop_secondary(&mut self) -> Option<(Vec<f32>, i64)>

Retrieve one completed secondary output chunk (None if no secondary tap is configured).

Source

pub fn flush(&mut self)

Flush on stop. Feed the processor’s trailing tail (e.g. denoise), drain each tap’s stage 2 resampler remainder, and pad a final partial chunk with silence to align it to the fixed 20ms boundary so it can be retrieved with pop_chunk / pop_secondary.

Continue on a best-effort basis if resampler flushing fails, so the stop path does not fail (at most the final few milliseconds are lost).

Source

pub fn buffered_out_frames(&self) -> usize

Number of output frames buffered but not yet retrieved from out_buf (primary tap).

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 = !

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.