Swap fftw in the stft example out for realfft
The FFTW bindings can't statically link on Windows.
This commit is contained in:
parent
b56c9892c0
commit
80c3fb8d51
65
Cargo.lock
generated
65
Cargo.lock
generated
|
@ -1002,7 +1002,7 @@ dependencies = [
|
||||||
"fftw-sys",
|
"fftw-sys",
|
||||||
"lazy_static 1.4.0",
|
"lazy_static 1.4.0",
|
||||||
"ndarray",
|
"ndarray",
|
||||||
"num-complex",
|
"num-complex 0.3.1",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
@ -1028,7 +1028,7 @@ checksum = "e8e3951d695cc2f17610cd041e87ebc15078d1af5eb8c6be77921381fc98b3fd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fftw-src",
|
"fftw-src",
|
||||||
"libc",
|
"libc",
|
||||||
"num-complex",
|
"num-complex 0.3.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2138,7 +2138,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6c0d5c9540a691d153064dc47a4db2504587a75eae07bf1d73f7a596ebc73c04"
|
checksum = "6c0d5c9540a691d153064dc47a4db2504587a75eae07bf1d73f7a596ebc73c04"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"matrixmultiply",
|
"matrixmultiply",
|
||||||
"num-complex",
|
"num-complex 0.3.1",
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"rawpointer",
|
"rawpointer",
|
||||||
|
@ -2276,6 +2276,15 @@ dependencies = [
|
||||||
"num-traits",
|
"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]]
|
[[package]]
|
||||||
name = "num-integer"
|
name = "num-integer"
|
||||||
version = "0.1.44"
|
version = "0.1.44"
|
||||||
|
@ -2658,6 +2667,15 @@ version = "0.2.16"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
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]]
|
[[package]]
|
||||||
name = "proc-macro-error"
|
name = "proc-macro-error"
|
||||||
version = "1.0.4"
|
version = "1.0.4"
|
||||||
|
@ -2871,6 +2889,15 @@ version = "0.4.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9"
|
checksum = "9ae028b272a6e99d9f8260ceefa3caa09300a8d6c8d2b2001316474bc52122e9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "realfft"
|
||||||
|
version = "3.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a83b876fe55da7e1bf5deeacb93d6411edf81eba0e1a497e79c067734729053a"
|
||||||
|
dependencies = [
|
||||||
|
"rustfft",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.2.12"
|
version = "0.2.12"
|
||||||
|
@ -2991,6 +3018,20 @@ dependencies = [
|
||||||
"semver",
|
"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]]
|
[[package]]
|
||||||
name = "rustybuzz"
|
name = "rustybuzz"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -3308,10 +3349,16 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||||
name = "stft"
|
name = "stft"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fftw",
|
|
||||||
"nih_plug",
|
"nih_plug",
|
||||||
|
"realfft",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strength_reduce"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a3ff2f71c82567c565ba4b3009a9350a96a7269eaa4001ebedae926230bc2254"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "svg_fmt"
|
name = "svg_fmt"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
@ -3445,6 +3492,16 @@ dependencies = [
|
||||||
"serde",
|
"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]]
|
[[package]]
|
||||||
name = "triple_buffer"
|
name = "triple_buffer"
|
||||||
version = "6.0.0"
|
version = "6.0.0"
|
||||||
|
|
|
@ -11,4 +11,4 @@ crate-type = ["cdylib"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
nih_plug = { path = "../../../", features = ["assert_process_allocs"] }
|
nih_plug = { path = "../../../", features = ["assert_process_allocs"] }
|
||||||
|
|
||||||
fftw = "0.7.0"
|
realfft = "3.0"
|
||||||
|
|
|
@ -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 nih_plug::prelude::*;
|
||||||
|
use realfft::num_complex::Complex32;
|
||||||
|
use realfft::{ComplexToReal, RealFftPlanner, RealToComplex};
|
||||||
use std::f32;
|
use std::f32;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
const WINDOW_SIZE: usize = 2048;
|
const WINDOW_SIZE: usize = 2048;
|
||||||
const OVERLAP_TIMES: usize = 4;
|
const OVERLAP_TIMES: usize = 4;
|
||||||
|
@ -17,51 +17,47 @@ struct Stft {
|
||||||
window_function: Vec<f32>,
|
window_function: Vec<f32>,
|
||||||
|
|
||||||
/// The FFT of a simple low-pass FIR filter.
|
/// The FFT of a simple low-pass FIR filter.
|
||||||
lp_filter_kernel: Vec<c32>,
|
lp_filter_kernel: Vec<Complex32>,
|
||||||
|
|
||||||
/// The algorithms for the FFT and IFFT operations.
|
/// The algorithm for the FFT operation.
|
||||||
plan: Plan,
|
r2c_plan: Arc<dyn RealToComplex<f32>>,
|
||||||
/// Scratch buffers for computing our FFT. The [`StftHelper`] already contains a buffer for the
|
/// The algorithm for the IFFT operation.
|
||||||
/// real values.
|
c2r_plan: Arc<dyn ComplexToReal<f32>>,
|
||||||
complex_fft_scratch_buffer: AlignedVec<c32>,
|
/// The output of our real->complex FFT.
|
||||||
|
complex_fft_buffer: Vec<Complex32>,
|
||||||
|
/// 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<Complex32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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)]
|
#[derive(Params)]
|
||||||
struct StftParams {}
|
struct StftParams {}
|
||||||
|
|
||||||
impl Default for Stft {
|
impl Default for Stft {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let mut r2c_plan: R2CPlan32 =
|
let mut planner = RealFftPlanner::new();
|
||||||
R2CPlan32::aligned(&[WINDOW_SIZE], Flag::MEASURE | Flag::DESTROYINPUT).unwrap();
|
let r2c_plan = planner.plan_fft_forward(WINDOW_SIZE);
|
||||||
let c2r_plan: C2RPlan32 =
|
let c2r_plan = planner.plan_fft_inverse(WINDOW_SIZE);
|
||||||
C2RPlan32::aligned(&[WINDOW_SIZE], Flag::MEASURE | Flag::DESTROYINPUT).unwrap();
|
let mut real_fft_buffer = r2c_plan.make_input_vec();
|
||||||
let mut real_fft_scratch_buffer: AlignedVec<f32> = AlignedVec::new(WINDOW_SIZE);
|
let mut complex_fft_buffer = r2c_plan.make_output_vec();
|
||||||
let mut complex_fft_scratch_buffer: AlignedVec<c32> = AlignedVec::new(WINDOW_SIZE / 2 + 1);
|
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
|
// Build a super simple low-pass filter from one of the built in window function
|
||||||
const FILTER_WINDOW_SIZE: usize = 33;
|
const FILTER_WINDOW_SIZE: usize = 33;
|
||||||
let filter_window = util::window::hann(FILTER_WINDOW_SIZE);
|
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
|
// And make sure to normalize this so convolution sums to 1
|
||||||
let filter_normalization_factor = real_fft_scratch_buffer.iter().sum::<f32>().recip();
|
let filter_normalization_factor = real_fft_buffer.iter().sum::<f32>().recip();
|
||||||
for sample in real_fft_scratch_buffer.as_slice_mut() {
|
for sample in &mut real_fft_buffer {
|
||||||
*sample *= filter_normalization_factor;
|
*sample *= filter_normalization_factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
r2c_plan
|
r2c_plan
|
||||||
.r2c(
|
.process_with_scratch(
|
||||||
&mut real_fft_scratch_buffer,
|
&mut real_fft_buffer,
|
||||||
&mut complex_fft_scratch_buffer,
|
&mut complex_fft_buffer,
|
||||||
|
&mut fft_scratch_buffer,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -71,14 +67,12 @@ impl Default for Stft {
|
||||||
stft: util::StftHelper::new(2, WINDOW_SIZE),
|
stft: util::StftHelper::new(2, WINDOW_SIZE),
|
||||||
window_function: util::window::hann(WINDOW_SIZE),
|
window_function: util::window::hann(WINDOW_SIZE),
|
||||||
|
|
||||||
lp_filter_kernel: complex_fft_scratch_buffer
|
lp_filter_kernel: complex_fft_buffer.clone(),
|
||||||
.iter()
|
|
||||||
.take(WINDOW_SIZE)
|
|
||||||
.copied()
|
|
||||||
.collect(),
|
|
||||||
|
|
||||||
plan: Plan { r2c_plan, c2r_plan },
|
r2c_plan,
|
||||||
complex_fft_scratch_buffer,
|
c2r_plan,
|
||||||
|
complex_fft_buffer,
|
||||||
|
fft_scratch_buffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,19 +140,18 @@ impl Plugin for Stft {
|
||||||
OVERLAP_TIMES,
|
OVERLAP_TIMES,
|
||||||
|_channel_idx, real_fft_scratch_buffer| {
|
|_channel_idx, real_fft_scratch_buffer| {
|
||||||
// Forward FFT, the helper has already applied window function
|
// Forward FFT, the helper has already applied window function
|
||||||
self.plan
|
self.r2c_plan
|
||||||
.r2c_plan
|
.process_with_scratch(
|
||||||
.r2c(
|
|
||||||
real_fft_scratch_buffer,
|
real_fft_scratch_buffer,
|
||||||
&mut self.complex_fft_scratch_buffer,
|
&mut self.complex_fft_buffer,
|
||||||
|
&mut self.fft_scratch_buffer,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// As per the convolution theorem we can simply multiply these two buffers. We'll
|
// As per the convolution theorem we can simply multiply these two buffers. We'll
|
||||||
// also apply the gain compensation at this point.
|
// also apply the gain compensation at this point.
|
||||||
for (fft_bin, kernel_bin) in self
|
for (fft_bin, kernel_bin) in self
|
||||||
.complex_fft_scratch_buffer
|
.complex_fft_buffer
|
||||||
.as_slice_mut()
|
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.zip(&self.lp_filter_kernel)
|
.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
|
// 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.
|
// which gets written back to the host at a one block delay.
|
||||||
self.plan
|
self.c2r_plan
|
||||||
.c2r_plan
|
.process_with_scratch(
|
||||||
.c2r(
|
&mut self.complex_fft_buffer,
|
||||||
&mut self.complex_fft_scratch_buffer,
|
|
||||||
real_fft_scratch_buffer,
|
real_fft_scratch_buffer,
|
||||||
|
&mut self.fft_scratch_buffer,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue