pub struct ResamplerFft { /* private fields */ }Expand description
High-quality and high-performance FFT-based audio resampler supporting multi-channel audio.
ResamplerFft uses the overlap-add FFT method with Kaiser windowing to convert audio
between different sample rates. The field channels specifies the
number of audio channels (e.g., 1 for mono, 2 for stereo).
Implementations§
Source§impl ResamplerFft
impl ResamplerFft
Sourcepub fn new(
channels: usize,
sample_rate_input: SampleRate,
sample_rate_output: SampleRate,
) -> Self
pub fn new( channels: usize, sample_rate_input: SampleRate, sample_rate_output: SampleRate, ) -> Self
Create a new ResamplerFft.
Parameters are:
channels: The channel count.sample_rate_input: Input sample rate.sample_rate_output: Output sample rate.
Sourcepub fn chunk_size_input(&self) -> usize
pub fn chunk_size_input(&self) -> usize
Returns the required input buffer size in total f32 values (including all channels).
For example, with a stereo resampler (CHANNEL=2), this returns the total number of f32 values needed in the interleaved input buffer [L0, R0, L1, R1, …].
Sourcepub fn chunk_size_output(&self) -> usize
pub fn chunk_size_output(&self) -> usize
Returns the required output buffer size in total f32 values (including all channels).
For example, with a stereo resampler (CHANNEL=2), this returns the total number of f32 values needed in the interleaved output buffer [L0, R0, L1, R1, …].
Sourcepub fn delay(&self) -> usize
pub fn delay(&self) -> usize
Returns the algorithmic delay (latency) of the resampler in input samples.
This delay is inherent to the FFT-based overlap-add process and equals half the FFT input size due to the windowing operation.
Sourcepub fn resample(
&mut self,
input: &[f32],
output: &mut [f32],
) -> Result<(), ResampleError>
pub fn resample( &mut self, input: &[f32], output: &mut [f32], ) -> Result<(), ResampleError>
Processes one chunk of audio, resampling from input to output sample rate.
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. Must contain at leastchunk_size_input()values.output: Interleaved output buffer. Must have capacity for at leastchunk_size_output()values.
§Example
use resampler::{ResamplerFft, SampleRate};
let mut resampler = ResamplerFft::new(1, SampleRate::Hz48000, SampleRate::Hz44100);
let input = vec![0.0f32; resampler.chunk_size_input()];
let mut output = vec![0.0f32; resampler.chunk_size_output()];
match resampler.resample(&input, &mut output) {
Ok(()) => {
println!("Resample successfully");
}
Err(error) => eprintln!("Resampling error: {error:?}"),
}