ResamplerFir

Struct ResamplerFir 

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

High-quality polyphase FIR audio resampler supporting multi-channel audio with streaming API.

ResamplerFir uses a configurable polyphase FIR filter (32, 64, or 128 taps) decomposed into 1024 branches for high-quality audio resampling with configurable latency. The const generic parameter CHANNEL specifies the number of audio channels.

Unlike the FFT-based resampler, this implementation supports streaming with arbitrary input buffer sizes, making it ideal for real-time applications. The latency can be configured at construction time using the Latency enum to balance quality versus delay.

The stopband attenuation can also be configured via the Attenuation enum.

Implementations§

Source§

impl ResamplerFir

Source

pub fn new( channels: usize, input_rate: SampleRate, output_rate: SampleRate, latency: Latency, attenuation: Attenuation, ) -> Self

Create a new ResamplerFir.

Parameters:

  • channels: The channel count.
  • input_rate: Input sample rate.
  • output_rate: Output sample rate.
  • latency: Latency configuration determining filter length (32, 64, or 128 taps).
  • attenuation: Desired stopband attenuation controlling filter quality.

The resampler will generate polyphase filter coefficients optimized for the given sample rate pair, using a Kaiser window with beta value determined by the attenuation setting. Higher tap counts provide better frequency response at the cost of increased latency. Higher attenuation provides better stopband rejection but slightly wider transition bands.

§Example
use resampler::{Attenuation, Latency, ResamplerFir, SampleRate};

// Create with default latency (128 taps, 64 samples delay) and 90 dB attenuation
let resampler = ResamplerFir::new(
    2,
    SampleRate::Hz48000,
    SampleRate::Hz44100,
    Latency::default(),
    Attenuation::default(),
);

// Create with low latency (32 taps, 16 samples delay) and 60 dB attenuation
let resampler_low_latency = ResamplerFir::new(
    2,
    SampleRate::Hz48000,
    SampleRate::Hz44100,
    Latency::Sample16,
    Attenuation::Db60,
);
Source

pub fn buffer_size_output(&self) -> usize

Calculate the maximum output buffer size that needs to be allocated.

Source

pub fn resample( &mut self, input: &[f32], output: &mut [f32], ) -> Result<(usize, usize), ResampleError>

Process audio samples, resampling from input to output sample rate.

This is a streaming API that accepts arbitrary input buffer sizes and produces as many output samples as possible given the available input.

Input and output must be interleaved f32 slices with all channels interleaved. For stereo audio, the format is [L0, R0, L1, R1, ...]. For mono, it’s [S0, S1, S2, ...].

§Parameters
  • input: Interleaved input samples. Length must be a multiple of CHANNEL.
  • output: Interleaved output buffer. Length must be a multiple of CHANNEL.
§Returns

Ok((consumed, produced)) where:

  • consumed: Number of input samples consumed (in total f32 values, including all channels).
  • produced: Number of output samples produced (in total f32 values, including all channels).
§Example
use resampler::{Attenuation, Latency, ResamplerFir, SampleRate};

let mut resampler = ResamplerFir::new(
    1,
    SampleRate::Hz48000,
    SampleRate::Hz44100,
    Latency::default(),
    Attenuation::default(),
);
let buffer_size_output = resampler.buffer_size_output();
let input = vec![0.0f32; 256];
let mut output = vec![0.0f32; buffer_size_output];

match resampler.resample(&input, &mut output) {
    Ok((consumed, produced)) => {
        println!("Processed {consumed} input samples into {produced} output samples");
    }
    Err(error) => eprintln!("Resampling error: {error:?}"),
}
Source

pub fn delay(&self) -> usize

Returns the algorithmic delay (latency) of the resampler in input samples.

For the polyphase FIR resampler, this equals half the filter length due to the symmetric FIR filter design:

  • Latency::_16: 16 samples (32 taps / 2)
  • Latency::_32: 32 samples (64 taps / 2)
  • Latency::_64: 64 samples (128 taps / 2)
Source

pub fn reset(&mut self)

Resets the resampler state, clearing all internal buffers.

Call this when starting to process a new audio stream to avoid discontinuities from previous audio data.

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.