Skip to main content

libflo_audio/dsp/backends/
rustfft.rs

1//! rustfft backend
2
3use crate::dsp::{Complex32, Fft, FftDirection, FftPlanner};
4use alloc::boxed::Box;
5use alloc::sync::Arc;
6
7/// Planner
8pub struct RustFftPlanner;
9
10impl FftPlanner for RustFftPlanner {
11    fn new() -> Self {
12        Self
13    }
14
15    fn plan_fft(&mut self, len: usize, direction: FftDirection) -> Box<dyn Fft> {
16        let mut planner = rustfft::FftPlanner::new();
17        let fft = match direction {
18            FftDirection::Forward => planner.plan_fft_forward(len),
19            FftDirection::Inverse => planner.plan_fft_inverse(len),
20        };
21        Box::new(RustFft(fft))
22    }
23
24    /// rustfft supports arbitrary lengths; cap at 65536 as a defensive thing
25    /// so callers cannot accidentally plan an OOM-sized transform (silly of them)
26    fn max_supported_len(&self) -> usize {
27        1 << 16
28    }
29}
30
31/// A `rustfft` plan behind the [`Fft`] trait.
32pub struct RustFft(Arc<dyn rustfft::Fft<f32>>);
33
34impl Fft for RustFft {
35    fn process(&self, buffer: &mut [Complex32]) {
36        self.0.process(buffer);
37    }
38}