Skip to main content

libflo_audio/dsp/
fft.rs

1//! FFT abstraction
2
3use alloc::boxed::Box;
4
5/// A complex sample type shared by all FFT backends.
6pub use num_complex::Complex32;
7
8/// Transform direction for a planned FFT.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum FftDirection {
11    /// Forward transform.
12    Forward,
13    /// Inverse transform.
14    Inverse,
15}
16
17/// An in-place FFT plan for a fixed length.
18///
19/// Implementations may cache precomputed twiddle factors and plans.
20pub trait Fft {
21    /// Execute the transform in-place over `buffer`.
22    ///
23    /// `buffer.len()` must equal the size this plan was created for.
24    fn process(&self, buffer: &mut [Complex32]);
25}
26
27/// Factory for [`Fft`] plans.
28pub trait FftPlanner {
29    /// Create a new planner.
30    fn new() -> Self;
31    /// Plan an FFT of `len` complex samples in the given direction.
32    fn plan_fft(&mut self, len: usize, direction: FftDirection) -> Box<dyn Fft>;
33    /// Largest FFT length this backend can plan.
34    fn max_supported_len(&self) -> usize;
35    /// Plan a forward FFT of `len` complex samples.
36    fn plan_fft_forward(&mut self, len: usize) -> Box<dyn Fft> {
37        self.plan_fft(len, FftDirection::Forward)
38    }
39    /// Plan an inverse FFT of `len` complex samples.
40    fn plan_fft_inverse(&mut self, len: usize) -> Box<dyn Fft> {
41        self.plan_fft(len, FftDirection::Inverse)
42    }
43}