fft-convolver
Fast, real-time safe FFT-based convolution for audio processing in Rust.
Port of HiFi-LoFi/FFTConvolver to pure Rust.
Features
- Real-time safe: No allocations, locks, or unpredictable operations during audio processing
- Highly efficient: Partitioned FFT convolution algorithm with uniform block sizes
- Zero latency: Output is sample-aligned with input (excluding processing time)
- Flexible: Handles arbitrary input/output buffer sizes through internal buffering
- Generic: Works with
f32andf64floating-point types
Perfect for real-time audio applications like convolution reverbs, cabinet simulators, and other impulse response-based effects.
How it Works
The convolver uses a partitioned FFT convolution algorithm that divides the impulse response into uniform blocks. This approach provides:
- Consistent processing time regardless of buffer size
- Efficient computation through FFT
- Low latency suitable for real-time audio
All memory allocation happens during initialization (init()), making subsequent processing (process()) completely allocation-free and suitable for real-time audio threads.
Usage
Basic Example
use FFTConvolver;
// Create an impulse response (e.g., a simple delay)
let mut impulse_response = vec!;
impulse_response = 0.8; // Direct sound
impulse_response = 0.3; // Echo
// Initialize the convolver
let mut convolver = default;
convolver.init.unwrap;
// Process audio in any buffer size
let input = vec!;
let mut output = vec!;
convolver.process.unwrap;
Updating the Impulse Response
use FFTConvolver;
let mut convolver = default;
let ir1 = vec!;
convolver.init.unwrap;
// Update to a different impulse response (must be ≤ original length)
let ir2 = vec!;
convolver.set_response.unwrap;
Handling Stream Discontinuities
use FFTConvolver;
let mut convolver = default;
let ir = vec!;
convolver.init.unwrap;
// Process some audio...
let input = vec!;
let mut output = vec!;
convolver.process.unwrap;
// Clear state when seeking or handling playback discontinuities
convolver.reset;
// Continue processing with clean state
convolver.process.unwrap;
Performance Considerations
- Block size: Affects CPU efficiency. Larger blocks are more efficient (better FFT performance) but require more computation per block. Typical values: 64-512 samples.
- Impulse response length: Longer IRs require more computation. The algorithm scales well with IR length.
- Buffer size: Any input/output size is supported efficiently through internal buffering.
Real-Time Safety
The following operations are real-time safe (no allocations):
process()- Audio processingset_response()- Updating impulse responsereset()- Clearing internal state
The following operations are NOT real-time safe (perform allocations):
init()- Initial setup
License
Licensed under the MIT license.