2022-03-08 05:51:38 +11:00
|
|
|
// Puberty Simulator: the next generation in voice change simulation technology
|
|
|
|
// Copyright (C) 2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use nih_plug::prelude::*;
|
2022-03-29 02:51:36 +11:00
|
|
|
use realfft::num_complex::Complex32;
|
|
|
|
use realfft::{ComplexToReal, RealFftPlanner, RealToComplex};
|
2022-03-08 05:51:38 +11:00
|
|
|
use std::f32;
|
2022-03-29 02:51:36 +11:00
|
|
|
use std::sync::Arc;
|
2022-03-08 05:51:38 +11:00
|
|
|
|
2022-03-08 23:27:16 +11:00
|
|
|
const MIN_WINDOW_ORDER: usize = 6;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const MIN_WINDOW_SIZE: usize = 1 << MIN_WINDOW_ORDER; // 64
|
|
|
|
const DEFAULT_WINDOW_ORDER: usize = 10;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const DEFAULT_WINDOW_SIZE: usize = 1 << DEFAULT_WINDOW_ORDER; // 1024
|
|
|
|
const MAX_WINDOW_ORDER: usize = 15;
|
|
|
|
const MAX_WINDOW_SIZE: usize = 1 << MAX_WINDOW_ORDER; // 32768
|
|
|
|
|
2022-05-10 00:22:26 +10:00
|
|
|
const MIN_OVERLAP_ORDER: usize = 2;
|
2022-03-08 23:27:16 +11:00
|
|
|
#[allow(dead_code)]
|
2022-05-10 00:22:26 +10:00
|
|
|
const MIN_OVERLAP_TIMES: usize = 2 << MIN_OVERLAP_ORDER; // 4
|
2022-03-08 23:27:16 +11:00
|
|
|
const DEFAULT_OVERLAP_ORDER: usize = 3;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const DEFAULT_OVERLAP_TIMES: usize = 1 << DEFAULT_OVERLAP_ORDER; // 4
|
|
|
|
const MAX_OVERLAP_ORDER: usize = 5;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const MAX_OVERLAP_TIMES: usize = 1 << MAX_OVERLAP_ORDER; // 32
|
2022-03-08 05:51:38 +11:00
|
|
|
|
|
|
|
struct PubertySimulator {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<PubertySimulatorParams>,
|
2022-03-08 05:51:38 +11:00
|
|
|
|
|
|
|
/// An adapter that performs most of the overlap-add algorithm for us.
|
|
|
|
stft: util::StftHelper,
|
2022-03-08 07:19:38 +11:00
|
|
|
/// Contains a Hann window function of the current window length, passed to the overlap-add
|
|
|
|
/// helper. Allocated with a `MAX_WINDOW_SIZE` initial capacity.
|
2022-03-08 05:51:38 +11:00
|
|
|
window_function: Vec<f32>,
|
|
|
|
|
2022-03-08 23:27:16 +11:00
|
|
|
/// 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()`.
|
|
|
|
plan_for_order: Option<[Plan; MAX_WINDOW_ORDER - MIN_WINDOW_ORDER + 1]>,
|
2022-03-29 02:51:36 +11:00
|
|
|
/// The output of our real->complex FFT.
|
|
|
|
complex_fft_buffer: Vec<Complex32>,
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
|
2022-03-29 02:51:36 +11:00
|
|
|
/// A plan for a specific window size, all of which will be precomputed during initilaization.
|
2022-03-08 05:51:38 +11:00
|
|
|
struct Plan {
|
2022-03-29 02:51:36 +11:00
|
|
|
/// The algorithm for the FFT operation.
|
|
|
|
r2c_plan: Arc<dyn RealToComplex<f32>>,
|
|
|
|
/// The algorithm for the IFFT operation.
|
|
|
|
c2r_plan: Arc<dyn ComplexToReal<f32>>,
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
2022-03-08 06:21:20 +11:00
|
|
|
struct PubertySimulatorParams {
|
2022-03-08 07:19:38 +11:00
|
|
|
/// The pitch change in octaves.
|
2022-03-08 06:21:20 +11:00
|
|
|
#[id = "pitch"]
|
|
|
|
pitch_octaves: FloatParam,
|
2022-03-08 07:19:38 +11:00
|
|
|
|
|
|
|
/// The size of the FFT window as a power of two (to prevent invalid inputs).
|
|
|
|
#[id = "wndsz"]
|
|
|
|
window_size_order: IntParam,
|
2022-03-08 07:26:50 +11:00
|
|
|
/// The amount of overlap to use in the overlap-add algorithm as a power of two (again to
|
|
|
|
/// prevent invalid inputs).
|
|
|
|
#[id = "ovrlap"]
|
|
|
|
overlap_times_order: IntParam,
|
2022-05-10 00:29:36 +10:00
|
|
|
|
|
|
|
/// The type of broken pitch shifting to apply.
|
|
|
|
#[id = "mode"]
|
|
|
|
mode: EnumParam<PitchShiftingMode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Enum, Debug, PartialEq)]
|
|
|
|
enum PitchShiftingMode {
|
|
|
|
/// Directly linearly interpolate sine and cosine waves from different bins. This obviously
|
|
|
|
/// sounds very bad, but it also sounds kind of hilarious.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "interpolated-rectangular"]
|
2022-05-10 00:29:36 +10:00
|
|
|
#[name = "Very broken"]
|
2022-05-10 00:49:43 +10:00
|
|
|
InterpolateRectangular,
|
|
|
|
/// The same as `InterpolateRectangular`, but interpolating the polar forms instead. This sounds
|
|
|
|
/// slightly better, which actually ends up making it sound a lot worse.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "interpolated-polar"]
|
2022-05-10 00:49:43 +10:00
|
|
|
#[name = "Also very broken"]
|
|
|
|
InterpolatePolar,
|
2022-03-08 06:21:20 +11:00
|
|
|
}
|
2022-03-08 05:51:38 +11:00
|
|
|
|
|
|
|
impl Default for PubertySimulator {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc::new(PubertySimulatorParams::default()),
|
2022-03-08 05:51:38 +11:00
|
|
|
|
2022-05-08 09:18:56 +10:00
|
|
|
stft: util::StftHelper::new(2, MAX_WINDOW_SIZE, 0),
|
2022-03-08 07:19:38 +11:00
|
|
|
window_function: Vec::with_capacity(MAX_WINDOW_SIZE),
|
2022-03-08 05:51:38 +11:00
|
|
|
|
2022-03-08 23:27:16 +11:00
|
|
|
plan_for_order: None,
|
2022-03-29 02:51:36 +11:00
|
|
|
complex_fft_buffer: Vec::with_capacity(MAX_WINDOW_SIZE / 2 + 1),
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PubertySimulatorParams {
|
|
|
|
fn default() -> Self {
|
2022-03-27 12:43:39 +11:00
|
|
|
let power_of_two_val2str = formatters::v2s_i32_power_of_two();
|
|
|
|
let power_of_two_str2val = formatters::s2v_i32_power_of_two();
|
2022-03-08 07:26:50 +11:00
|
|
|
|
2022-03-08 06:21:20 +11:00
|
|
|
Self {
|
|
|
|
pitch_octaves: FloatParam::new(
|
|
|
|
"Pitch",
|
|
|
|
-1.0,
|
2022-03-08 06:22:16 +11:00
|
|
|
FloatRange::SymmetricalSkewed {
|
2022-03-08 06:21:20 +11:00
|
|
|
min: -5.0,
|
|
|
|
max: 5.0,
|
2022-03-08 22:48:41 +11:00
|
|
|
factor: FloatRange::skew_factor(-2.0),
|
2022-03-08 06:22:16 +11:00
|
|
|
center: 0.0,
|
2022-03-08 06:21:20 +11:00
|
|
|
},
|
|
|
|
)
|
2022-03-08 06:27:43 +11:00
|
|
|
// This doesn't need smoothing to prevent zippers because we're already going
|
|
|
|
// overlap-add, but sounds kind of slick
|
|
|
|
.with_smoother(SmoothingStyle::Linear(100.0))
|
2022-03-08 06:21:20 +11:00
|
|
|
.with_unit(" Octaves")
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-03-08 07:19:38 +11:00
|
|
|
|
|
|
|
window_size_order: IntParam::new(
|
|
|
|
"Window Size",
|
2022-03-08 23:27:16 +11:00
|
|
|
DEFAULT_WINDOW_ORDER as i32,
|
2022-03-08 07:19:38 +11:00
|
|
|
IntRange::Linear {
|
2022-03-08 23:27:16 +11:00
|
|
|
min: MIN_WINDOW_ORDER as i32,
|
|
|
|
max: MAX_WINDOW_ORDER as i32,
|
2022-03-08 07:19:38 +11:00
|
|
|
},
|
|
|
|
)
|
2022-03-08 07:26:50 +11:00
|
|
|
.with_value_to_string(power_of_two_val2str.clone())
|
|
|
|
.with_string_to_value(power_of_two_str2val.clone()),
|
|
|
|
overlap_times_order: IntParam::new(
|
|
|
|
"Window Overlap",
|
2022-03-08 23:27:16 +11:00
|
|
|
DEFAULT_OVERLAP_ORDER as i32,
|
2022-03-08 07:26:50 +11:00
|
|
|
IntRange::Linear {
|
2022-03-08 23:27:16 +11:00
|
|
|
min: MIN_OVERLAP_ORDER as i32,
|
|
|
|
max: MAX_OVERLAP_ORDER as i32,
|
2022-03-08 07:26:50 +11:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_value_to_string(power_of_two_val2str)
|
|
|
|
.with_string_to_value(power_of_two_str2val),
|
2022-05-10 00:49:43 +10:00
|
|
|
mode: EnumParam::new("Mode", PitchShiftingMode::InterpolateRectangular),
|
2022-03-08 06:21:20 +11:00
|
|
|
}
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for PubertySimulator {
|
|
|
|
const NAME: &'static str = "Puberty Simulator";
|
|
|
|
const VENDOR: &'static str = "Robbert van der Helm";
|
|
|
|
const URL: &'static str = "https://github.com/robbert-vdh/nih-plug";
|
|
|
|
const EMAIL: &'static str = "mail@robbertvanderhelm.nl";
|
|
|
|
|
|
|
|
const VERSION: &'static str = "0.1.0";
|
|
|
|
|
2022-08-19 22:34:21 +10:00
|
|
|
const DEFAULT_INPUT_CHANNELS: u32 = 2;
|
|
|
|
const DEFAULT_OUTPUT_CHANNELS: u32 = 2;
|
2022-03-08 05:51:38 +11:00
|
|
|
|
2022-10-22 08:52:46 +11:00
|
|
|
type AsyncExecutor = ();
|
|
|
|
fn async_executor(&self) -> Self::AsyncExecutor {}
|
|
|
|
|
2022-04-07 23:31:46 +10:00
|
|
|
fn params(&self) -> Arc<dyn Params> {
|
2022-05-25 07:55:48 +10:00
|
|
|
self.params.clone()
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
|
|
|
// We'll only do stereo for simplicity's sake
|
|
|
|
config.num_input_channels == config.num_output_channels && config.num_input_channels == 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
_bus_config: &BusConfig,
|
|
|
|
_buffer_config: &BufferConfig,
|
2022-10-22 09:21:08 +11:00
|
|
|
context: &mut impl InitContext<Self>,
|
2022-03-08 05:51:38 +11:00
|
|
|
) -> bool {
|
2022-03-29 02:51:36 +11:00
|
|
|
// Planning with RustFFT is very fast, but it will still allocate we we'll plan all of the
|
|
|
|
// FFTs we might need in advance
|
2022-03-08 23:27:16 +11:00
|
|
|
if self.plan_for_order.is_none() {
|
2022-03-29 02:51:36 +11:00
|
|
|
let mut planner = RealFftPlanner::new();
|
2022-03-08 23:27:16 +11:00
|
|
|
let plan_for_order: Vec<Plan> = (MIN_WINDOW_ORDER..=MAX_WINDOW_ORDER)
|
|
|
|
.map(|order| Plan {
|
2022-03-29 02:51:36 +11:00
|
|
|
r2c_plan: planner.plan_fft_forward(1 << order),
|
|
|
|
c2r_plan: planner.plan_fft_inverse(1 << order),
|
2022-03-08 23:27:16 +11:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
self.plan_for_order = Some(
|
|
|
|
plan_for_order
|
|
|
|
.try_into()
|
|
|
|
.unwrap_or_else(|_| panic!("Mismatched plan orders")),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-08 05:51:38 +11:00
|
|
|
// Normally we'd also initialize the STFT helper for the correct channel count here, but we
|
|
|
|
// only do stereo so that's not necessary
|
2022-03-08 07:19:38 +11:00
|
|
|
let window_size = self.window_size();
|
|
|
|
if self.window_function.len() != window_size {
|
|
|
|
self.resize_for_window(window_size);
|
|
|
|
|
|
|
|
context.set_latency_samples(self.stft.latency_samples());
|
|
|
|
}
|
2022-03-08 05:51:38 +11:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:42:58 +11:00
|
|
|
fn reset(&mut self) {
|
|
|
|
// This zeroes out the buffers
|
|
|
|
self.stft.set_block_size(self.window_size());
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:30:57 +10:00
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-10-22 10:15:16 +11:00
|
|
|
context: &mut impl ProcessContext<Self>,
|
2022-05-27 10:30:57 +10:00
|
|
|
) -> ProcessStatus {
|
2022-03-08 05:51:38 +11:00
|
|
|
// Compensate for the window function, the overlap, and the extra gain introduced by the
|
|
|
|
// IDFT operation
|
2022-03-08 07:19:38 +11:00
|
|
|
let window_size = self.window_size();
|
2022-03-08 07:26:50 +11:00
|
|
|
let overlap_times = self.overlap_times();
|
2022-03-08 06:21:20 +11:00
|
|
|
let sample_rate = context.transport().sample_rate;
|
2022-05-10 00:22:26 +10:00
|
|
|
// The overlap gain compensation is based on a squared Hann window, which will sum perfectly
|
|
|
|
// at four times overlap or higher. We'll apply a regular Hann window before the analysis
|
|
|
|
// and after the synthesis.
|
|
|
|
let gain_compensation: f32 =
|
|
|
|
((overlap_times as f32 / 4.0) * 1.5).recip() / window_size as f32;
|
2022-03-08 07:19:38 +11:00
|
|
|
|
|
|
|
// If the window size has changed since the last process call, reset the buffers and chance
|
|
|
|
// our latency. All of these buffers already have enough capacity
|
|
|
|
if self.window_function.len() != window_size {
|
|
|
|
self.resize_for_window(window_size);
|
|
|
|
|
|
|
|
context.set_latency_samples(self.stft.latency_samples());
|
|
|
|
}
|
|
|
|
|
2022-03-08 23:27:16 +11:00
|
|
|
// These plans have already been made during initialization we can switch between versions
|
|
|
|
// without reallocating
|
|
|
|
let fft_plan = &mut self.plan_for_order.as_mut().unwrap()
|
2022-09-07 05:55:14 +10:00
|
|
|
[self.params.window_size_order.value() as usize - MIN_WINDOW_ORDER];
|
2022-03-08 06:21:20 +11:00
|
|
|
|
2022-03-08 06:27:43 +11:00
|
|
|
let mut smoothed_pitch_value = 0.0;
|
2022-04-29 01:20:39 +10:00
|
|
|
self.stft
|
|
|
|
.process_overlap_add(buffer, overlap_times, |channel_idx, real_fft_buffer| {
|
2022-03-08 06:27:43 +11:00
|
|
|
// 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
|
|
|
|
// we're dealing with a new block. We'll use this for our parameter smoothing.
|
|
|
|
if channel_idx == 0 {
|
|
|
|
smoothed_pitch_value = self
|
|
|
|
.params
|
|
|
|
.pitch_octaves
|
|
|
|
.smoothed
|
2022-03-08 07:26:50 +11:00
|
|
|
.next_step((window_size / overlap_times) as u32);
|
2022-03-08 06:27:43 +11:00
|
|
|
}
|
|
|
|
// Negated because pitching down should cause us to take values from higher frequency bins
|
|
|
|
let frequency_multiplier = 2.0f32.powf(-smoothed_pitch_value);
|
|
|
|
|
2022-05-10 00:22:26 +10:00
|
|
|
// We'll window the input with a Hann function to avoid spectral leakage
|
|
|
|
util::window::multiply_with_window(real_fft_buffer, &self.window_function);
|
|
|
|
|
2022-03-29 02:51:36 +11:00
|
|
|
// RustFFT doesn't actually need a scratch buffer here, so we'll pass an empty
|
|
|
|
// buffer instead
|
2022-03-08 23:27:16 +11:00
|
|
|
fft_plan
|
2022-03-08 05:51:38 +11:00
|
|
|
.r2c_plan
|
2022-03-29 02:51:36 +11:00
|
|
|
.process_with_scratch(real_fft_buffer, &mut self.complex_fft_buffer, &mut [])
|
2022-03-08 05:51:38 +11:00
|
|
|
.unwrap();
|
|
|
|
|
2022-05-10 00:49:43 +10:00
|
|
|
// TODO: Move this to helper functions. These functions capture a lot of variables
|
|
|
|
// here so that might require some work. And branch preductors are probably
|
|
|
|
// good enough to be able to put the match inside of the `process_bin`
|
|
|
|
// function, but it seems preferable to have it outside of the loop.
|
2022-03-29 02:51:36 +11:00
|
|
|
let num_bins = self.complex_fft_buffer.len();
|
2022-05-10 00:49:43 +10:00
|
|
|
match self.params.mode.value() {
|
|
|
|
PitchShiftingMode::InterpolateRectangular => {
|
|
|
|
// This simply interpolates the sine and cosine waves composing the complex
|
|
|
|
// sinusoids from the frequency bins to neighbouring frequency bins scaled
|
|
|
|
// by the octave pitch multiplies. The iteration order dependson the pitch
|
|
|
|
// shifting direction since we're doing it in place.
|
|
|
|
let mut process_bin = |bin_idx| {
|
|
|
|
let frequency = bin_idx as f32 / window_size as f32 * sample_rate;
|
|
|
|
let target_frequency = frequency * frequency_multiplier;
|
|
|
|
|
|
|
|
// Simple linear interpolation
|
|
|
|
let target_bin = target_frequency / sample_rate * window_size as f32;
|
|
|
|
let target_bin_floor = target_bin.floor() as usize;
|
|
|
|
let target_bin_ceil = target_bin.ceil() as usize;
|
|
|
|
let target_floor_t = target_bin % 1.0;
|
|
|
|
let target_ceil_t = 1.0 - target_floor_t;
|
|
|
|
let target_floor = self
|
|
|
|
.complex_fft_buffer
|
|
|
|
.get(target_bin_floor)
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
|
|
|
let target_ceil = self
|
|
|
|
.complex_fft_buffer
|
|
|
|
.get(target_bin_ceil)
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
self.complex_fft_buffer[bin_idx] = (target_floor * target_floor_t
|
|
|
|
+ target_ceil * target_ceil_t)
|
|
|
|
* 3.0 // Random extra gain, not sure
|
|
|
|
* gain_compensation;
|
|
|
|
};
|
|
|
|
|
|
|
|
if frequency_multiplier >= 1.0 {
|
|
|
|
for bin_idx in 0..num_bins {
|
|
|
|
process_bin(bin_idx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for bin_idx in (0..num_bins).rev() {
|
|
|
|
process_bin(bin_idx);
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 06:21:20 +11:00
|
|
|
}
|
2022-05-10 00:49:43 +10:00
|
|
|
PitchShiftingMode::InterpolatePolar => {
|
|
|
|
// Same as the above, but interpolating in the polar form instead. While
|
|
|
|
// this does sound more correct it doesn't sound nearly as hilarious, and it
|
|
|
|
// just sounds bad at this point. But maybe there's some use for this.
|
|
|
|
let mut process_bin = |bin_idx| {
|
|
|
|
let frequency = bin_idx as f32 / window_size as f32 * sample_rate;
|
|
|
|
let target_frequency = frequency * frequency_multiplier;
|
|
|
|
|
|
|
|
// Simple linear interpolation
|
|
|
|
let target_bin = target_frequency / sample_rate * window_size as f32;
|
|
|
|
let target_bin_floor = target_bin.floor() as usize;
|
|
|
|
let target_bin_ceil = target_bin.ceil() as usize;
|
|
|
|
let target_floor_t = target_bin % 1.0;
|
|
|
|
let target_ceil_t = 1.0 - target_floor_t;
|
|
|
|
let target_floor = self
|
|
|
|
.complex_fft_buffer
|
|
|
|
.get(target_bin_floor)
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
|
|
|
let target_ceil = self
|
|
|
|
.complex_fft_buffer
|
|
|
|
.get(target_bin_ceil)
|
|
|
|
.copied()
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
let target_floor_magnitude = target_floor.norm();
|
|
|
|
let target_floor_phase = target_floor.arg();
|
|
|
|
let target_ceil_magnitude = target_ceil.norm();
|
|
|
|
let target_ceil_phase = target_ceil.arg();
|
|
|
|
|
|
|
|
self.complex_fft_buffer[bin_idx] = Complex32::from_polar(
|
|
|
|
(target_floor_magnitude * target_floor_t)
|
|
|
|
+ (target_ceil_magnitude * target_ceil_t),
|
|
|
|
(target_floor_phase * target_floor_t)
|
|
|
|
+ (target_ceil_phase * target_ceil_t),
|
|
|
|
) * 3.0 // Random extra gain, not sure
|
|
|
|
* gain_compensation;
|
|
|
|
};
|
|
|
|
|
|
|
|
if frequency_multiplier >= 1.0 {
|
|
|
|
for bin_idx in 0..num_bins {
|
|
|
|
process_bin(bin_idx);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for bin_idx in (0..num_bins).rev() {
|
|
|
|
process_bin(bin_idx);
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 06:21:20 +11:00
|
|
|
}
|
2022-03-08 06:44:26 +11:00
|
|
|
}
|
2022-03-08 05:51:38 +11:00
|
|
|
|
2022-03-29 02:51:36 +11:00
|
|
|
// 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;
|
|
|
|
|
2022-03-08 05:51:38 +11:00
|
|
|
// 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.
|
2022-03-08 23:27:16 +11:00
|
|
|
fft_plan
|
2022-03-08 05:51:38 +11:00
|
|
|
.c2r_plan
|
2022-03-29 02:51:36 +11:00
|
|
|
.process_with_scratch(&mut self.complex_fft_buffer, real_fft_buffer, &mut [])
|
2022-03-08 05:51:38 +11:00
|
|
|
.unwrap();
|
2022-04-29 01:20:39 +10:00
|
|
|
|
2022-05-10 00:22:26 +10:00
|
|
|
// Apply the window function once more to reduce time domain aliasing. The gain
|
|
|
|
// compensation compensates for the squared Hann window that would be applied if we
|
|
|
|
// didn't do any processing at all.
|
2022-04-29 01:20:39 +10:00
|
|
|
util::window::multiply_with_window(real_fft_buffer, &self.window_function);
|
|
|
|
});
|
2022-03-08 05:51:38 +11:00
|
|
|
|
|
|
|
ProcessStatus::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 07:19:38 +11:00
|
|
|
impl PubertySimulator {
|
|
|
|
fn window_size(&self) -> usize {
|
2022-09-07 05:55:14 +10:00
|
|
|
1 << self.params.window_size_order.value() as usize
|
2022-03-08 07:19:38 +11:00
|
|
|
}
|
|
|
|
|
2022-03-08 07:26:50 +11:00
|
|
|
fn overlap_times(&self) -> usize {
|
2022-09-07 05:55:14 +10:00
|
|
|
1 << self.params.overlap_times_order.value() as usize
|
2022-03-08 07:26:50 +11:00
|
|
|
}
|
|
|
|
|
2022-03-08 07:19:38 +11:00
|
|
|
/// `window_size` should not exceed `MAX_WINDOW_SIZE` or this will allocate.
|
|
|
|
fn resize_for_window(&mut self, window_size: usize) {
|
2022-03-08 23:27:16 +11:00
|
|
|
// The FFT algorithms for this window size have already been planned
|
2022-03-08 07:19:38 +11:00
|
|
|
self.stft.set_block_size(window_size);
|
|
|
|
self.window_function.resize(window_size, 0.0);
|
2022-03-29 02:51:36 +11:00
|
|
|
self.complex_fft_buffer
|
|
|
|
.resize(window_size / 2 + 1, Complex32::default());
|
2022-03-08 07:19:38 +11:00
|
|
|
util::window::hann_in_place(&mut self.window_function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 05:51:38 +11:00
|
|
|
impl ClapPlugin for PubertySimulator {
|
|
|
|
const CLAP_ID: &'static str = "nl.robbertvanderhelm.puberty-simulator";
|
2022-07-04 20:44:30 +10:00
|
|
|
const CLAP_DESCRIPTION: Option<&'static str> = Some("Simulates a pitched down cracking voice");
|
|
|
|
const CLAP_MANUAL_URL: Option<&'static str> = Some(Self::URL);
|
|
|
|
const CLAP_SUPPORT_URL: Option<&'static str> = None;
|
2022-06-02 09:16:30 +10:00
|
|
|
const CLAP_FEATURES: &'static [ClapFeature] = &[
|
|
|
|
ClapFeature::AudioEffect,
|
|
|
|
ClapFeature::Stereo,
|
|
|
|
ClapFeature::Glitch,
|
|
|
|
ClapFeature::PitchShifter,
|
|
|
|
];
|
2022-03-08 05:51:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vst3Plugin for PubertySimulator {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"PubertySim..RvdH";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Pitch Shift";
|
|
|
|
}
|
|
|
|
|
|
|
|
nih_export_clap!(PubertySimulator);
|
|
|
|
nih_export_vst3!(PubertySimulator);
|