From 80c3fb8d5132290251a433433d48e8c5aa984f2d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 28 Mar 2022 17:05:28 +0200 Subject: [PATCH] Swap fftw in the stft example out for realfft The FFTW bindings can't statically link on Windows. --- Cargo.lock | 65 ++++++++++++++++++++++-- plugins/examples/stft/Cargo.toml | 2 +- plugins/examples/stft/src/lib.rs | 87 +++++++++++++++----------------- 3 files changed, 102 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 519568e4..5b514bc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1002,7 +1002,7 @@ dependencies = [ "fftw-sys", "lazy_static 1.4.0", "ndarray", - "num-complex", + "num-complex 0.3.1", "num-traits", "thiserror", ] @@ -1028,7 +1028,7 @@ checksum = "e8e3951d695cc2f17610cd041e87ebc15078d1af5eb8c6be77921381fc98b3fd" dependencies = [ "fftw-src", "libc", - "num-complex", + "num-complex 0.3.1", ] [[package]] @@ -2138,7 +2138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c0d5c9540a691d153064dc47a4db2504587a75eae07bf1d73f7a596ebc73c04" dependencies = [ "matrixmultiply", - "num-complex", + "num-complex 0.3.1", "num-integer", "num-traits", "rawpointer", @@ -2276,6 +2276,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-complex" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26873667bbbb7c5182d4a37c1add32cdf09f841af72da53318fdb81543c15085" +dependencies = [ + "num-traits", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -2658,6 +2667,15 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" +[[package]] +name = "primal-check" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01419cee72c1a1ca944554e23d83e483e1bccf378753344e881de28b5487511d" +dependencies = [ + "num-integer", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -2871,6 +2889,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9" +[[package]] +name = "realfft" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a83b876fe55da7e1bf5deeacb93d6411edf81eba0e1a497e79c067734729053a" +dependencies = [ + "rustfft", +] + [[package]] name = "redox_syscall" version = "0.2.12" @@ -2991,6 +3018,20 @@ dependencies = [ "semver", ] +[[package]] +name = "rustfft" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d089e5c57521629a59f5f39bca7434849ff89bd6873b521afe389c1c602543" +dependencies = [ + "num-complex 0.4.0", + "num-integer", + "num-traits", + "primal-check", + "strength_reduce", + "transpose", +] + [[package]] name = "rustybuzz" version = "0.4.0" @@ -3308,10 +3349,16 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" name = "stft" version = "0.1.0" dependencies = [ - "fftw", "nih_plug", + "realfft", ] +[[package]] +name = "strength_reduce" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254" + [[package]] name = "svg_fmt" version = "0.4.1" @@ -3445,6 +3492,16 @@ dependencies = [ "serde", ] +[[package]] +name = "transpose" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95f9c900aa98b6ea43aee227fd680550cdec726526aab8ac801549eadb25e39f" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "triple_buffer" version = "6.0.0" diff --git a/plugins/examples/stft/Cargo.toml b/plugins/examples/stft/Cargo.toml index 15cbfb72..9c7404f3 100644 --- a/plugins/examples/stft/Cargo.toml +++ b/plugins/examples/stft/Cargo.toml @@ -11,4 +11,4 @@ crate-type = ["cdylib"] [dependencies] nih_plug = { path = "../../../", features = ["assert_process_allocs"] } -fftw = "0.7.0" +realfft = "3.0" diff --git a/plugins/examples/stft/src/lib.rs b/plugins/examples/stft/src/lib.rs index f3d9e677..6adc31aa 100644 --- a/plugins/examples/stft/src/lib.rs +++ b/plugins/examples/stft/src/lib.rs @@ -1,9 +1,9 @@ -use fftw::array::AlignedVec; -use fftw::plan::{C2RPlan, C2RPlan32, R2CPlan, R2CPlan32}; -use fftw::types::{c32, Flag}; use nih_plug::prelude::*; +use realfft::num_complex::Complex32; +use realfft::{ComplexToReal, RealFftPlanner, RealToComplex}; use std::f32; use std::pin::Pin; +use std::sync::Arc; const WINDOW_SIZE: usize = 2048; const OVERLAP_TIMES: usize = 4; @@ -17,51 +17,47 @@ struct Stft { window_function: Vec, /// The FFT of a simple low-pass FIR filter. - lp_filter_kernel: Vec, + lp_filter_kernel: Vec, - /// The algorithms for the FFT and IFFT operations. - plan: Plan, - /// Scratch buffers for computing our FFT. The [`StftHelper`] already contains a buffer for the - /// real values. - complex_fft_scratch_buffer: AlignedVec, + /// The algorithm for the FFT operation. + r2c_plan: Arc>, + /// The algorithm for the IFFT operation. + c2r_plan: Arc>, + /// The output of our real->complex FFT. + complex_fft_buffer: Vec, + /// Scratch buffers for computing our FFT. RustFFT requires a separate scratch buffer. The + /// [`StftHelper`] already contains a buffer for the real values. + fft_scratch_buffer: Vec, } -/// FFTW uses raw pointers which aren't Send+Sync, so we'll wrap this in a separate struct. -struct Plan { - r2c_plan: R2CPlan32, - c2r_plan: C2RPlan32, -} - -unsafe impl Send for Plan {} -unsafe impl Sync for Plan {} - #[derive(Params)] struct StftParams {} impl Default for Stft { fn default() -> Self { - let mut r2c_plan: R2CPlan32 = - R2CPlan32::aligned(&[WINDOW_SIZE], Flag::MEASURE | Flag::DESTROYINPUT).unwrap(); - let c2r_plan: C2RPlan32 = - C2RPlan32::aligned(&[WINDOW_SIZE], Flag::MEASURE | Flag::DESTROYINPUT).unwrap(); - let mut real_fft_scratch_buffer: AlignedVec = AlignedVec::new(WINDOW_SIZE); - let mut complex_fft_scratch_buffer: AlignedVec = AlignedVec::new(WINDOW_SIZE / 2 + 1); + let mut planner = RealFftPlanner::new(); + let r2c_plan = planner.plan_fft_forward(WINDOW_SIZE); + let c2r_plan = planner.plan_fft_inverse(WINDOW_SIZE); + let mut real_fft_buffer = r2c_plan.make_input_vec(); + let mut complex_fft_buffer = r2c_plan.make_output_vec(); + let mut fft_scratch_buffer = r2c_plan.make_scratch_vec(); // Build a super simple low-pass filter from one of the built in window function const FILTER_WINDOW_SIZE: usize = 33; let filter_window = util::window::hann(FILTER_WINDOW_SIZE); - real_fft_scratch_buffer[0..FILTER_WINDOW_SIZE].copy_from_slice(&filter_window); + real_fft_buffer[0..FILTER_WINDOW_SIZE].copy_from_slice(&filter_window); // And make sure to normalize this so convolution sums to 1 - let filter_normalization_factor = real_fft_scratch_buffer.iter().sum::().recip(); - for sample in real_fft_scratch_buffer.as_slice_mut() { + let filter_normalization_factor = real_fft_buffer.iter().sum::().recip(); + for sample in &mut real_fft_buffer { *sample *= filter_normalization_factor; } r2c_plan - .r2c( - &mut real_fft_scratch_buffer, - &mut complex_fft_scratch_buffer, + .process_with_scratch( + &mut real_fft_buffer, + &mut complex_fft_buffer, + &mut fft_scratch_buffer, ) .unwrap(); @@ -71,14 +67,12 @@ impl Default for Stft { stft: util::StftHelper::new(2, WINDOW_SIZE), window_function: util::window::hann(WINDOW_SIZE), - lp_filter_kernel: complex_fft_scratch_buffer - .iter() - .take(WINDOW_SIZE) - .copied() - .collect(), + lp_filter_kernel: complex_fft_buffer.clone(), - plan: Plan { r2c_plan, c2r_plan }, - complex_fft_scratch_buffer, + r2c_plan, + c2r_plan, + complex_fft_buffer, + fft_scratch_buffer, } } } @@ -146,19 +140,18 @@ impl Plugin for Stft { OVERLAP_TIMES, |_channel_idx, real_fft_scratch_buffer| { // Forward FFT, the helper has already applied window function - self.plan - .r2c_plan - .r2c( + self.r2c_plan + .process_with_scratch( real_fft_scratch_buffer, - &mut self.complex_fft_scratch_buffer, + &mut self.complex_fft_buffer, + &mut self.fft_scratch_buffer, ) .unwrap(); // As per the convolution theorem we can simply multiply these two buffers. We'll // also apply the gain compensation at this point. for (fft_bin, kernel_bin) in self - .complex_fft_scratch_buffer - .as_slice_mut() + .complex_fft_buffer .iter_mut() .zip(&self.lp_filter_kernel) { @@ -167,11 +160,11 @@ impl Plugin for Stft { // Inverse FFT back into the scratch buffer. This will be added to a ring buffer // which gets written back to the host at a one block delay. - self.plan - .c2r_plan - .c2r( - &mut self.complex_fft_scratch_buffer, + self.c2r_plan + .process_with_scratch( + &mut self.complex_fft_buffer, real_fft_scratch_buffer, + &mut self.fft_scratch_buffer, ) .unwrap(); },