Crate resampler

Crate resampler 

Source
Expand description

§Audio resampling library

Resampler is a small, zero-dependency crate for high-quality audio resampling between common sample rates. It provides both FFT-based and FIR-based resamplers optimized for different use cases.

§Usage Examples

§FFT-Based Resampler (Highest Quality)

use resampler::{ResamplerFft, SampleRate};

// Create a stereo resampler (2 channels) from 44.1 kHz to 48 kHz.
let mut resampler = ResamplerFft::<2>::new(SampleRate::Hz44100, SampleRate::Hz48000);

// Get required buffer sizes (already includes all channels).
let input_size = resampler.chunk_size_input();
let output_size = resampler.chunk_size_output();

// Create input and output buffers (interleaved format: [L0, R0, L1, R1, ...]).
let input = vec![0.0f32; input_size];
let mut output = vec![0.0f32; output_size];

resampler.resample(&input, &mut output).unwrap();

§FIR-Based Resampler (Low Latency, Streaming)

use resampler::{Latency, ResamplerFir, SampleRate};

// Create a stereo resampler with configurable latency (16, 32, or 64 samples).
let mut resampler =
    ResamplerFir::<2>::new(SampleRate::Hz48000, SampleRate::Hz44100, Latency::_32);

// Streaming API - accepts arbitrary input buffer sizes.
let input = vec![0.0f32; 512];
let mut output = vec![0.0f32; resampler.buffer_size_output()];

let (consumed, produced) = resampler.resample(&input, &mut output).unwrap();
println!("Consumed {consumed} samples, produced {produced} samples");

§Choosing a Resampler

Both resamplers provide good quality, but are optimized for different use cases:

FeatureResamplerFftResamplerFir
QualityVery good (sharp rolloff)Good (slow rolloff)
PerformanceVery fastFast (configurable)
Latency~256 samples16-64 samples (configurable)
APIFixed chunk sizeFlexible streaming
Best forNon-latency sensitive processingLow-latency processing

Use ResamplerFft when:

  • You need the absolute highest quality
  • Latency is not a concern
  • Processing pre-recorded audio files

Use ResamplerFir when:

  • You need low latency (real-time audio)
  • You can live with a slower rolloff
  • Working with streaming data

§FFT-Based Implementation

The resampler uses an FFT-based overlap-add algorithm with Kaiser windowing for high-quality audio resampling. Key technical details:

  • Custom mixed-radix FFT with the standard Cooley-Tukey algorithm.
  • SIMD optimizations: All butterflies have SSE, AVX, and ARM NEON implementations with compile time CPU feature detection.
  • Real-valued FFT: Exploits conjugate symmetry for 2x performance.
  • Kaiser window: Beta parameter of 10.0 provides excellent stopband attenuation of -100 dB while maintaining good time-domain localization.
  • Optimal configurations: Pre-computed FFT sizes and factorizations for all supported sample rate pairs, with throughput scaling to ensure a latency around 256 samples.

§FIR-Based Implementation

The FIR resampler uses a polyphase filter with linear interpolation for high-quality audio resampling with low latency. Key technical details:

  • Polyphase decomposition: 1024 phases with linear interpolation between phases.
  • SIMD optimizations: Convolution kernels optimized with SSE, AVX, and ARM NEON.
  • Configurable filter length: 32, 64, or 128 taps (16, 32, or 64 samples latency).
  • Kaiser windowing: Beta parameter of 10.0 provides -90 dB stopband attenuation.
  • Streaming API: Accepts arbitrary input buffer sizes for flexible real-time processing.

§Performance

Both resamplers include SIMD optimizations for maximum performance:

  • SSE on x86_64 and NEON on aarch64 are enabled by default.
  • For best performance on x86_64, enable AVX (+avx) and FMA (+fma) as target features at compile time.
  • FFT butterflies and FIR convolution kernels are both fully optimized with SIMD instructions.

§no-std Compatibility

The library supports no-std environments with alloc. To use the library in a no-std environment, enable the no_std feature:

[dependencies]
resampler = { version = "0.2", features = ["no_std"] }

§Behavior Differences

When the no_std feature is enabled:

  • Caching: The library will not cache FFT and FIR objects globally to shorten resampler creation time and lower overall memory consumption for multiple resamplers.

The default build (without no_std feature) has zero dependencies and uses the standard library for optimal performance and memory efficiency through global caching.

§Alternatives

Other high-quality audio resampling libraries in Rust are:

  • Rubato: The overlap-add resampling approach used in this library is based on Rubato’s implementation.

§License

Licensed under either of

  • Apache License, Version 2.0
  • MIT license

at your option.

Structs§

ResamplerFft
High-quality and high-performance FFT-based audio resampler supporting multi-channel audio.
ResamplerFir
High-quality polyphase FIR audio resampler supporting multi-channel audio with streaming API.

Enums§

Latency
Latency configuration for the FIR resampler.
ResampleError
Errors the process function can throw.
SampleRate
All sample rates the resampler can operate on.