1
0
Fork 0

Get rid of unnecessary RustFFT scratch buffer

This commit is contained in:
Robbert van der Helm 2022-03-28 17:45:46 +02:00
parent 80c3fb8d51
commit b4ff09ca33

View file

@ -25,9 +25,6 @@ struct Stft {
c2r_plan: Arc<dyn ComplexToReal<f32>>, c2r_plan: Arc<dyn ComplexToReal<f32>>,
/// The output of our real->complex FFT. /// The output of our real->complex FFT.
complex_fft_buffer: Vec<Complex32>, 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>,
} }
#[derive(Params)] #[derive(Params)]
@ -40,7 +37,6 @@ impl Default for Stft {
let c2r_plan = planner.plan_fft_inverse(WINDOW_SIZE); let c2r_plan = planner.plan_fft_inverse(WINDOW_SIZE);
let mut real_fft_buffer = r2c_plan.make_input_vec(); let mut real_fft_buffer = r2c_plan.make_input_vec();
let mut complex_fft_buffer = r2c_plan.make_output_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 // 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;
@ -53,12 +49,10 @@ impl Default for Stft {
*sample *= filter_normalization_factor; *sample *= filter_normalization_factor;
} }
// RustFFT doesn't actually need a scratch buffer here, so we'll pass an empty buffer
// instead
r2c_plan r2c_plan
.process_with_scratch( .process_with_scratch(&mut real_fft_buffer, &mut complex_fft_buffer, &mut [])
&mut real_fft_buffer,
&mut complex_fft_buffer,
&mut fft_scratch_buffer,
)
.unwrap(); .unwrap();
Self { Self {
@ -72,7 +66,6 @@ impl Default for Stft {
r2c_plan, r2c_plan,
c2r_plan, c2r_plan,
complex_fft_buffer, complex_fft_buffer,
fft_scratch_buffer,
} }
} }
} }
@ -138,14 +131,10 @@ impl Plugin for Stft {
buffer, buffer,
&self.window_function, &self.window_function,
OVERLAP_TIMES, OVERLAP_TIMES,
|_channel_idx, real_fft_scratch_buffer| { |_channel_idx, real_fft_buffer| {
// Forward FFT, the helper has already applied window function // Forward FFT, the helper has already applied window function
self.r2c_plan self.r2c_plan
.process_with_scratch( .process_with_scratch(real_fft_buffer, &mut self.complex_fft_buffer, &mut [])
real_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
@ -161,11 +150,7 @@ 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.c2r_plan self.c2r_plan
.process_with_scratch( .process_with_scratch(&mut self.complex_fft_buffer, real_fft_buffer, &mut [])
&mut self.complex_fft_buffer,
real_fft_scratch_buffer,
&mut self.fft_scratch_buffer,
)
.unwrap(); .unwrap();
}, },
); );