Use realfft for Puberty Simulator
This commit is contained in:
parent
b4ff09ca33
commit
2211232ed1
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2725,8 +2725,8 @@ checksum = "9145ac0af1d93c638c98c40cf7d25665f427b2a44ad0a99b1dccf3e2f25bb987"
|
||||||
name = "puberty_simulator"
|
name = "puberty_simulator"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"fftw",
|
|
||||||
"nih_plug",
|
"nih_plug",
|
||||||
|
"realfft",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -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"
|
realfft = "3.0"
|
||||||
|
|
|
@ -14,12 +14,12 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
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 MIN_WINDOW_ORDER: usize = 6;
|
const MIN_WINDOW_ORDER: usize = 6;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -52,21 +52,18 @@ struct PubertySimulator {
|
||||||
/// The algorithms for the FFT and IFFT operations, for each supported order so we can switch
|
/// The algorithms for the FFT and IFFT operations, for each supported order so we can switch
|
||||||
/// between them without replanning or allocations. Initialized during `initialize()`.
|
/// between them without replanning or allocations. Initialized during `initialize()`.
|
||||||
plan_for_order: Option<[Plan; MAX_WINDOW_ORDER - MIN_WINDOW_ORDER + 1]>,
|
plan_for_order: Option<[Plan; MAX_WINDOW_ORDER - MIN_WINDOW_ORDER + 1]>,
|
||||||
/// Scratch buffers for computing our FFT. The [`StftHelper`] already contains a buffer for the
|
/// The output of our real->complex FFT.
|
||||||
/// real values. This type cannot be resized, so we'll simply take a slice of it with the
|
complex_fft_buffer: Vec<Complex32>,
|
||||||
/// correct length instead.
|
|
||||||
complex_fft_scratch_buffer: AlignedVec<c32>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FFTW uses raw pointers which aren't Send+Sync, so we'll wrap this in a separate struct.
|
/// A plan for a specific window size, all of which will be precomputed during initilaization.
|
||||||
struct Plan {
|
struct Plan {
|
||||||
r2c_plan: R2CPlan32,
|
/// The algorithm for the FFT operation.
|
||||||
c2r_plan: C2RPlan32,
|
r2c_plan: Arc<dyn RealToComplex<f32>>,
|
||||||
|
/// The algorithm for the IFFT operation.
|
||||||
|
c2r_plan: Arc<dyn ComplexToReal<f32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Plan {}
|
|
||||||
unsafe impl Sync for Plan {}
|
|
||||||
|
|
||||||
#[derive(Params)]
|
#[derive(Params)]
|
||||||
struct PubertySimulatorParams {
|
struct PubertySimulatorParams {
|
||||||
/// The pitch change in octaves.
|
/// The pitch change in octaves.
|
||||||
|
@ -91,7 +88,7 @@ impl Default for PubertySimulator {
|
||||||
window_function: Vec::with_capacity(MAX_WINDOW_SIZE),
|
window_function: Vec::with_capacity(MAX_WINDOW_SIZE),
|
||||||
|
|
||||||
plan_for_order: None,
|
plan_for_order: None,
|
||||||
complex_fft_scratch_buffer: AlignedVec::new(MAX_WINDOW_SIZE / 2 + 1),
|
complex_fft_buffer: Vec::with_capacity(MAX_WINDOW_SIZE / 2 + 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,23 +165,14 @@ impl Plugin for PubertySimulator {
|
||||||
_buffer_config: &BufferConfig,
|
_buffer_config: &BufferConfig,
|
||||||
context: &mut impl ProcessContext,
|
context: &mut impl ProcessContext,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
// Planning with RustFFT is very fast, but it will still allocate we we'll plan all of the
|
||||||
|
// FFTs we might need in advance
|
||||||
if self.plan_for_order.is_none() {
|
if self.plan_for_order.is_none() {
|
||||||
|
let mut planner = RealFftPlanner::new();
|
||||||
let plan_for_order: Vec<Plan> = (MIN_WINDOW_ORDER..=MAX_WINDOW_ORDER)
|
let plan_for_order: Vec<Plan> = (MIN_WINDOW_ORDER..=MAX_WINDOW_ORDER)
|
||||||
// `Flag::MEASURE` is pretty slow above 1024 which hurts initialization time.
|
|
||||||
// `Flag::ESTIMATE` does not seem to hurt performance much at reasonable orders, so
|
|
||||||
// that's good enough for now. An alternative would be to replan on a worker thread,
|
|
||||||
// but this makes switching between window sizes a bit cleaner.
|
|
||||||
.map(|order| Plan {
|
.map(|order| Plan {
|
||||||
r2c_plan: R2CPlan32::aligned(
|
r2c_plan: planner.plan_fft_forward(1 << order),
|
||||||
&[1 << order],
|
c2r_plan: planner.plan_fft_inverse(1 << order),
|
||||||
Flag::ESTIMATE | Flag::DESTROYINPUT,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
c2r_plan: C2RPlan32::aligned(
|
|
||||||
&[1 << order],
|
|
||||||
Flag::ESTIMATE | Flag::DESTROYINPUT,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
self.plan_for_order = Some(
|
self.plan_for_order = Some(
|
||||||
|
@ -227,9 +215,6 @@ impl Plugin for PubertySimulator {
|
||||||
context.set_latency_samples(self.stft.latency_samples());
|
context.set_latency_samples(self.stft.latency_samples());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since this type cannot be resized, we'll simply slice the full buffer instead
|
|
||||||
let complex_fft_scratch_buffer =
|
|
||||||
&mut self.complex_fft_scratch_buffer.as_slice_mut()[..window_size / 2 + 1];
|
|
||||||
// These plans have already been made during initialization we can switch between versions
|
// These plans have already been made during initialization we can switch between versions
|
||||||
// without reallocating
|
// without reallocating
|
||||||
let fft_plan = &mut self.plan_for_order.as_mut().unwrap()
|
let fft_plan = &mut self.plan_for_order.as_mut().unwrap()
|
||||||
|
@ -240,7 +225,7 @@ impl Plugin for PubertySimulator {
|
||||||
buffer,
|
buffer,
|
||||||
&self.window_function,
|
&self.window_function,
|
||||||
overlap_times,
|
overlap_times,
|
||||||
|channel_idx, real_fft_scratch_buffer| {
|
|channel_idx, real_fft_buffer| {
|
||||||
// This loop runs whenever there's a block ready, so we can't easily do any post- or
|
// This loop runs whenever there's a block ready, so we can't easily do any post- or
|
||||||
// pre-processing without muddying up the interface. But if this is channel 0, then
|
// pre-processing without muddying up the interface. But if this is channel 0, then
|
||||||
// we're dealing with a new block. We'll use this for our parameter smoothing.
|
// we're dealing with a new block. We'll use this for our parameter smoothing.
|
||||||
|
@ -255,15 +240,17 @@ impl Plugin for PubertySimulator {
|
||||||
let frequency_multiplier = 2.0f32.powf(-smoothed_pitch_value);
|
let frequency_multiplier = 2.0f32.powf(-smoothed_pitch_value);
|
||||||
|
|
||||||
// Forward FFT, the helper has already applied window function
|
// Forward FFT, the helper has already applied window function
|
||||||
|
// RustFFT doesn't actually need a scratch buffer here, so we'll pass an empty
|
||||||
|
// buffer instead
|
||||||
fft_plan
|
fft_plan
|
||||||
.r2c_plan
|
.r2c_plan
|
||||||
.r2c(real_fft_scratch_buffer, complex_fft_scratch_buffer)
|
.process_with_scratch(real_fft_buffer, &mut self.complex_fft_buffer, &mut [])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// This simply interpolates between the complex sinusoids from the frequency bins
|
// This simply interpolates between the complex sinusoids from the frequency bins
|
||||||
// for this bin's frequency scaled by the octave pitch multiplies. The iteration
|
// for this bin's frequency scaled by the octave pitch multiplies. The iteration
|
||||||
// order dependson the pitch shifting direction since we're doing it in place.
|
// order dependson the pitch shifting direction since we're doing it in place.
|
||||||
let num_bins = complex_fft_scratch_buffer.len();
|
let num_bins = self.complex_fft_buffer.len();
|
||||||
let mut process_bin = |bin_idx| {
|
let mut process_bin = |bin_idx| {
|
||||||
let frequency = bin_idx as f32 / window_size as f32 * sample_rate;
|
let frequency = bin_idx as f32 / window_size as f32 * sample_rate;
|
||||||
let target_frequency = frequency * frequency_multiplier;
|
let target_frequency = frequency * frequency_multiplier;
|
||||||
|
@ -274,16 +261,18 @@ impl Plugin for PubertySimulator {
|
||||||
let target_bin_high = target_bin.ceil() as usize;
|
let target_bin_high = target_bin.ceil() as usize;
|
||||||
let target_low_t = target_bin % 1.0;
|
let target_low_t = target_bin % 1.0;
|
||||||
let target_high_t = 1.0 - target_low_t;
|
let target_high_t = 1.0 - target_low_t;
|
||||||
let target_low = complex_fft_scratch_buffer
|
let target_low = self
|
||||||
|
.complex_fft_buffer
|
||||||
.get(target_bin_low)
|
.get(target_bin_low)
|
||||||
.copied()
|
.copied()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let target_high = complex_fft_scratch_buffer
|
let target_high = self
|
||||||
|
.complex_fft_buffer
|
||||||
.get(target_bin_high)
|
.get(target_bin_high)
|
||||||
.copied()
|
.copied()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
complex_fft_scratch_buffer[bin_idx] = (target_low * target_low_t
|
self.complex_fft_buffer[bin_idx] = (target_low * target_low_t
|
||||||
+ target_high * target_high_t)
|
+ target_high * target_high_t)
|
||||||
* 3.0 // Random extra gain, not sure
|
* 3.0 // Random extra gain, not sure
|
||||||
* gain_compensation;
|
* gain_compensation;
|
||||||
|
@ -299,11 +288,15 @@ impl Plugin for PubertySimulator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure the imaginary components on the first and last bin are zero
|
||||||
|
self.complex_fft_buffer[0].im = 0.0;
|
||||||
|
self.complex_fft_buffer[num_bins - 1].im = 0.0;
|
||||||
|
|
||||||
// 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.
|
||||||
fft_plan
|
fft_plan
|
||||||
.c2r_plan
|
.c2r_plan
|
||||||
.c2r(complex_fft_scratch_buffer, real_fft_scratch_buffer)
|
.process_with_scratch(&mut self.complex_fft_buffer, real_fft_buffer, &mut [])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -326,6 +319,8 @@ impl PubertySimulator {
|
||||||
// The FFT algorithms for this window size have already been planned
|
// The FFT algorithms for this window size have already been planned
|
||||||
self.stft.set_block_size(window_size);
|
self.stft.set_block_size(window_size);
|
||||||
self.window_function.resize(window_size, 0.0);
|
self.window_function.resize(window_size, 0.0);
|
||||||
|
self.complex_fft_buffer
|
||||||
|
.resize(window_size / 2 + 1, Complex32::default());
|
||||||
util::window::hann_in_place(&mut self.window_function);
|
util::window::hann_in_place(&mut self.window_function);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue