2022-02-13 03:27:23 +11:00
|
|
|
// Diopser: a phase rotation plugin
|
|
|
|
// Copyright (C) 2021-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/>.
|
|
|
|
|
2022-03-02 06:59:31 +11:00
|
|
|
#![cfg_attr(feature = "simd", feature(portable_simd))]
|
|
|
|
|
2022-03-04 22:49:36 +11:00
|
|
|
#[cfg(not(feature = "simd"))]
|
|
|
|
compile_error!("Compiling without SIMD support is currently not supported");
|
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
use atomic_float::AtomicF32;
|
2022-03-04 09:23:51 +11:00
|
|
|
use nih_plug::prelude::*;
|
2022-03-23 04:32:02 +11:00
|
|
|
use nih_plug_vizia::ViziaState;
|
2022-03-04 22:49:36 +11:00
|
|
|
use std::simd::f32x2;
|
2022-02-13 07:12:08 +11:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
2022-02-13 02:27:57 +11:00
|
|
|
|
2022-03-07 23:57:24 +11:00
|
|
|
use crate::spectrum::{SpectrumInput, SpectrumOutput};
|
|
|
|
|
2022-03-06 05:39:52 +11:00
|
|
|
mod editor;
|
2022-02-13 04:07:40 +11:00
|
|
|
mod filter;
|
2022-03-07 23:57:24 +11:00
|
|
|
mod spectrum;
|
2022-02-13 04:07:40 +11:00
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
/// How many all-pass filters we can have in series at most. The filter stages parameter determines
|
|
|
|
/// how many filters are actually active.
|
|
|
|
const MAX_NUM_FILTERS: usize = 512;
|
2022-04-25 02:34:40 +10:00
|
|
|
/// The minimum step size for smoothing the filter parameters.
|
2022-02-14 05:49:18 +11:00
|
|
|
const MIN_AUTOMATION_STEP_SIZE: u32 = 1;
|
2022-02-14 05:27:50 +11:00
|
|
|
/// The maximum step size for smoothing the filter parameters. Updating these parameters can be
|
|
|
|
/// expensive, so updating them in larger steps can be useful.
|
2022-02-14 05:49:18 +11:00
|
|
|
const MAX_AUTOMATION_STEP_SIZE: u32 = 512;
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
/// The maximum number of samples to iterate over at a time.
|
|
|
|
const MAX_BLOCK_SIZE: usize = 64;
|
|
|
|
|
2022-02-15 09:59:40 +11:00
|
|
|
// All features from the original Diopser have been implemented (and the spread control has been
|
|
|
|
// improved). Other features I want to implement are:
|
2022-02-14 06:21:59 +11:00
|
|
|
// - Briefly muting the output when changing the number of filters to get rid of the clicks
|
2022-03-06 05:39:52 +11:00
|
|
|
// - A proper GUI
|
2022-11-04 07:54:53 +11:00
|
|
|
pub struct Diopser {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<DiopserParams>,
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
/// Needed for computing the filter coefficients. Also used to update `bypass_smoother`, hence
|
|
|
|
/// why this needs to be an `Arc<AtomicF32>`.
|
|
|
|
sample_rate: Arc<AtomicF32>,
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-16 04:04:26 +11:00
|
|
|
/// All of the all-pass filters, with vectorized coefficients so they can be calculated for
|
2022-03-04 09:05:01 +11:00
|
|
|
/// multiple channels at once. [`DiopserParams::num_stages`] controls how many filters are
|
2022-02-16 04:04:26 +11:00
|
|
|
/// actually active.
|
|
|
|
filters: [filter::Biquad<f32x2>; MAX_NUM_FILTERS],
|
2022-11-12 07:23:39 +11:00
|
|
|
/// When the bypass parameter is toggled, this smoother fades between 0.0 and 1.0. This lets us
|
|
|
|
/// crossfade the dry and the wet signal to avoid clicks. The smoothing target is set in a
|
|
|
|
/// callback handler on the bypass parameter.
|
|
|
|
bypass_smoother: Arc<Smoother<f32>>,
|
2022-02-16 04:30:45 +11:00
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
/// If this is set at the start of the processing cycle, then the filter coefficients should be
|
|
|
|
/// updated. For the regular filter parameters we can look at the smoothers, but this is needed
|
|
|
|
/// when changing the number of active filters.
|
|
|
|
should_update_filters: Arc<AtomicBool>,
|
2022-02-14 05:49:18 +11:00
|
|
|
/// If this is 1 and any of the filter parameters are still smoothing, thenn the filter
|
|
|
|
/// coefficients should be recalculated on the next sample. After that, this gets reset to
|
2022-09-07 05:55:14 +10:00
|
|
|
/// `unnormalize_automation_precision(self.params.automation_precision.value())`. This is to
|
2022-02-14 05:49:18 +11:00
|
|
|
/// reduce the DSP load of automation parameters. It can also cause some fun sounding glitchy
|
|
|
|
/// effects when the precision is low.
|
|
|
|
next_filter_smoothing_in: i32,
|
2022-03-07 23:57:24 +11:00
|
|
|
|
|
|
|
/// When the GUI is open we compute the spectrum on the audio thread and send it to the GUI.
|
|
|
|
spectrum_input: SpectrumInput,
|
|
|
|
/// This can be cloned and moved into the editor.
|
|
|
|
spectrum_output: Arc<SpectrumOutput>,
|
2022-02-13 02:27:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
2022-03-24 09:29:49 +11:00
|
|
|
struct DiopserParams {
|
2022-07-14 07:16:29 +10:00
|
|
|
/// The editor state, saved together with the parameter state so the custom scaling can be
|
|
|
|
/// restored.
|
|
|
|
#[persist = "editor-state"]
|
|
|
|
editor_state: Arc<ViziaState>,
|
2022-11-05 04:32:35 +11:00
|
|
|
/// If this option is enabled, then the filter stages parameter is limited to `[0, 40]`. This is
|
|
|
|
/// editor-only state, and doesn't affect host automation.
|
|
|
|
#[persist = "safe-mode"]
|
|
|
|
safe_mode: Arc<AtomicBool>,
|
2022-07-14 07:16:29 +10:00
|
|
|
|
2022-11-05 02:01:13 +11:00
|
|
|
/// This plugin really doesn't need its own bypass parameter, but it's still useful to have a
|
|
|
|
/// dedicated one so it can be shown in the GUI. This is linked to the host's bypass if the host
|
|
|
|
/// supports it.
|
|
|
|
#[id = "bypass"]
|
|
|
|
bypass: BoolParam,
|
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
/// The number of all-pass filters applied in series.
|
|
|
|
#[id = "stages"]
|
|
|
|
filter_stages: IntParam,
|
|
|
|
|
2022-02-15 09:59:40 +11:00
|
|
|
/// The filter's center frequqency. When this is applied, the filters are spread around this
|
2022-02-13 07:12:08 +11:00
|
|
|
/// frequency.
|
|
|
|
#[id = "cutoff"]
|
|
|
|
filter_frequency: FloatParam,
|
|
|
|
/// The Q parameter for the filters.
|
|
|
|
#[id = "res"]
|
|
|
|
filter_resonance: FloatParam,
|
2022-02-15 09:59:40 +11:00
|
|
|
/// Controls a frequency spread between the filter stages in octaves. When this value is 0, the
|
|
|
|
/// same coefficients are used for every filter. Otherwise, the earliest stage's frequency will
|
|
|
|
/// be offset by `-filter_spread_octave_amount`, while the latest stage will be offset by
|
|
|
|
/// `filter_spread_octave_amount`. If the filter spread style is set to linear then the negative
|
|
|
|
/// range will cover the same frequency range as the positive range.
|
|
|
|
#[id = "spread"]
|
|
|
|
filter_spread_octaves: FloatParam,
|
|
|
|
/// How the spread range should be distributed. The octaves mode will sound more musical while
|
|
|
|
/// the linear mode can be useful for sound design purposes.
|
|
|
|
#[id = "spstyl"]
|
|
|
|
filter_spread_style: EnumParam<SpreadStyle>,
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-14 05:27:50 +11:00
|
|
|
/// The precision of the automation, determines the step size. This is presented to the userq as
|
|
|
|
/// a percentage, and it's stored here as `[0, 1]` float because smaller step sizes are more
|
|
|
|
/// precise so having this be an integer would result in odd situations.
|
|
|
|
#[id = "autopr"]
|
|
|
|
automation_precision: FloatParam,
|
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
/// Very important.
|
|
|
|
#[id = "ignore"]
|
|
|
|
very_important: BoolParam,
|
|
|
|
}
|
2022-02-13 02:27:57 +11:00
|
|
|
|
|
|
|
impl Default for Diopser {
|
|
|
|
fn default() -> Self {
|
2022-11-12 07:23:39 +11:00
|
|
|
let sample_rate = Arc::new(AtomicF32::new(1.0));
|
2022-02-13 07:12:08 +11:00
|
|
|
let should_update_filters = Arc::new(AtomicBool::new(false));
|
2022-11-12 07:23:39 +11:00
|
|
|
let bypass_smoother = Arc::new(Smoother::new(SmoothingStyle::Linear(10.0)));
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-03-07 23:57:24 +11:00
|
|
|
// We only do stereo right now so this is simple
|
|
|
|
let (spectrum_input, spectrum_output) =
|
2022-08-19 22:34:21 +10:00
|
|
|
SpectrumInput::new(Self::DEFAULT_OUTPUT_CHANNELS as usize);
|
2022-03-07 23:57:24 +11:00
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
Self {
|
2022-11-12 07:23:39 +11:00
|
|
|
params: Arc::new(DiopserParams::new(
|
|
|
|
sample_rate.clone(),
|
|
|
|
should_update_filters.clone(),
|
|
|
|
bypass_smoother.clone(),
|
|
|
|
)),
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
sample_rate,
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-16 04:04:26 +11:00
|
|
|
filters: [filter::Biquad::default(); MAX_NUM_FILTERS],
|
2022-11-12 07:23:39 +11:00
|
|
|
bypass_smoother,
|
2022-02-16 04:30:45 +11:00
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
should_update_filters,
|
2022-02-14 05:49:18 +11:00
|
|
|
next_filter_smoothing_in: 1,
|
2022-03-07 23:57:24 +11:00
|
|
|
|
|
|
|
spectrum_input,
|
|
|
|
spectrum_output: Arc::new(spectrum_output),
|
2022-02-13 02:27:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
impl DiopserParams {
|
2022-11-12 07:23:39 +11:00
|
|
|
fn new(
|
|
|
|
sample_rate: Arc<AtomicF32>,
|
|
|
|
should_update_filters: Arc<AtomicBool>,
|
|
|
|
bypass_smoother: Arc<Smoother<f32>>,
|
|
|
|
) -> Self {
|
2022-02-13 07:12:08 +11:00
|
|
|
Self {
|
2022-07-14 07:16:29 +10:00
|
|
|
editor_state: editor::default_state(),
|
2022-11-05 04:32:35 +11:00
|
|
|
safe_mode: Arc::new(AtomicBool::new(true)),
|
2022-07-14 07:16:29 +10:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
bypass: BoolParam::new("Bypass", false)
|
|
|
|
.with_callback(Arc::new(move |value| {
|
|
|
|
bypass_smoother.set_target(
|
|
|
|
sample_rate.load(Ordering::Relaxed),
|
|
|
|
if value { 1.0 } else { 0.0 },
|
|
|
|
);
|
|
|
|
}))
|
|
|
|
.make_bypass(),
|
2022-11-05 02:01:13 +11:00
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
filter_stages: IntParam::new(
|
|
|
|
"Filter Stages",
|
|
|
|
0,
|
2022-03-04 05:24:40 +11:00
|
|
|
IntRange::Linear {
|
2022-02-13 07:12:08 +11:00
|
|
|
min: 0,
|
|
|
|
max: MAX_NUM_FILTERS as i32,
|
|
|
|
},
|
|
|
|
)
|
2022-02-15 09:59:40 +11:00
|
|
|
.with_callback({
|
|
|
|
let should_update_filters = should_update_filters.clone();
|
|
|
|
Arc::new(move |_| should_update_filters.store(true, Ordering::Release))
|
|
|
|
}),
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-13 07:53:19 +11:00
|
|
|
// Smoothed parameters don't need the callback as we can just look at whether the
|
|
|
|
// smoother is still smoothing
|
2022-02-13 07:12:08 +11:00
|
|
|
filter_frequency: FloatParam::new(
|
|
|
|
"Filter Frequency",
|
|
|
|
200.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Skewed {
|
2022-02-13 07:12:08 +11:00
|
|
|
min: 5.0, // This must never reach 0
|
|
|
|
max: 20_000.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
factor: FloatRange::skew_factor(-2.5),
|
2022-02-13 07:12:08 +11:00
|
|
|
},
|
|
|
|
)
|
|
|
|
// This needs quite a bit of smoothing to avoid artifacts
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-22 00:44:26 +11:00
|
|
|
// This includes the unit
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_hz_then_khz(0))
|
|
|
|
.with_string_to_value(formatters::s2v_f32_hz_then_khz()),
|
2022-02-13 07:12:08 +11:00
|
|
|
filter_resonance: FloatParam::new(
|
|
|
|
"Filter Resonance",
|
|
|
|
// The actual default neutral Q-value would be `sqrt(2) / 2`, but this value
|
|
|
|
// produces slightly less ringing.
|
|
|
|
0.5,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Skewed {
|
2022-02-13 07:12:08 +11:00
|
|
|
min: 0.01, // This must also never reach 0
|
|
|
|
max: 30.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
factor: FloatRange::skew_factor(-2.5),
|
2022-02-13 07:12:08 +11:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-02-15 09:59:40 +11:00
|
|
|
filter_spread_octaves: FloatParam::new(
|
|
|
|
"Filter Spread Octaves",
|
|
|
|
0.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::SymmetricalSkewed {
|
2022-02-15 09:59:40 +11:00
|
|
|
min: -5.0,
|
|
|
|
max: 5.0,
|
2022-03-04 05:24:40 +11:00
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
2022-02-15 09:59:40 +11:00
|
|
|
center: 0.0,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_step_size(0.01)
|
|
|
|
.with_smoother(SmoothingStyle::Linear(100.0)),
|
|
|
|
filter_spread_style: EnumParam::new("Filter Spread Style", SpreadStyle::Octaves)
|
|
|
|
.with_callback(Arc::new(move |_| {
|
|
|
|
should_update_filters.store(true, Ordering::Release)
|
|
|
|
})),
|
|
|
|
|
2022-03-23 23:17:47 +11:00
|
|
|
very_important: BoolParam::new("Don't touch this", true)
|
|
|
|
.with_value_to_string(Arc::new(|value| {
|
|
|
|
String::from(if value { "please don't" } else { "stop it" })
|
|
|
|
}))
|
2022-07-02 08:45:01 +10:00
|
|
|
.with_string_to_value(Arc::new(|string| {
|
|
|
|
let string = string.trim();
|
|
|
|
if string.eq_ignore_ascii_case("please don't") {
|
|
|
|
Some(true)
|
|
|
|
} else if string.eq_ignore_ascii_case("stop it") {
|
|
|
|
Some(false)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}))
|
2022-03-23 23:17:47 +11:00
|
|
|
.hide_in_generic_ui(),
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-14 05:27:50 +11:00
|
|
|
automation_precision: FloatParam::new(
|
|
|
|
"Automation precision",
|
|
|
|
normalize_automation_precision(128),
|
2022-03-04 05:24:40 +11:00
|
|
|
FloatRange::Linear { min: 0.0, max: 1.0 },
|
2022-02-14 05:27:50 +11:00
|
|
|
)
|
|
|
|
.with_unit("%")
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_percentage(0))
|
|
|
|
.with_string_to_value(formatters::s2v_f32_percentage()),
|
2022-02-13 07:12:08 +11:00
|
|
|
}
|
2022-02-13 02:27:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 05:50:12 +11:00
|
|
|
#[derive(Enum, Debug, PartialEq)]
|
2022-02-15 09:59:40 +11:00
|
|
|
enum SpreadStyle {
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "octaves"]
|
2022-02-15 09:59:40 +11:00
|
|
|
Octaves,
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "linear"]
|
2022-02-15 09:59:40 +11:00
|
|
|
Linear,
|
|
|
|
}
|
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
impl Plugin for Diopser {
|
2022-02-15 10:00:41 +11:00
|
|
|
const NAME: &'static str = "Diopser";
|
2022-02-13 02:27:57 +11:00
|
|
|
const VENDOR: &'static str = "Robbert van der Helm";
|
2022-11-12 04:08:10 +11:00
|
|
|
const URL: &'static str = env!("CARGO_PKG_HOMEPAGE");
|
2022-02-13 02:27:57 +11:00
|
|
|
const EMAIL: &'static str = "mail@robbertvanderhelm.nl";
|
|
|
|
|
2022-11-10 01:50:21 +11:00
|
|
|
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2022-02-13 02:27:57 +11:00
|
|
|
|
2022-08-19 22:34:21 +10:00
|
|
|
const DEFAULT_INPUT_CHANNELS: u32 = 2;
|
|
|
|
const DEFAULT_OUTPUT_CHANNELS: u32 = 2;
|
2022-02-13 02:27:57 +11:00
|
|
|
|
2022-03-11 06:32:36 +11:00
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
|
|
|
|
2022-10-22 10:59:29 +11:00
|
|
|
type BackgroundTask = ();
|
2022-10-22 08:52:46 +11:00
|
|
|
|
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-02-13 02:27:57 +11:00
|
|
|
}
|
|
|
|
|
2022-10-23 00:05:39 +11:00
|
|
|
fn editor(&self, _async_executor: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
|
2022-07-14 07:16:29 +10:00
|
|
|
editor::create(self.params.clone(), self.params.editor_state.clone())
|
2022-03-06 05:39:52 +11:00
|
|
|
}
|
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
2022-03-04 22:49:36 +11:00
|
|
|
// The SIMD version only supports stereo
|
|
|
|
config.num_input_channels == config.num_output_channels && config.num_input_channels == 2
|
2022-02-13 02:27:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
2022-02-16 04:04:26 +11:00
|
|
|
_bus_config: &BusConfig,
|
2022-02-13 07:12:08 +11:00
|
|
|
buffer_config: &BufferConfig,
|
2022-10-22 09:21:08 +11:00
|
|
|
_context: &mut impl InitContext<Self>,
|
2022-02-13 02:27:57 +11:00
|
|
|
) -> bool {
|
2022-11-12 07:23:39 +11:00
|
|
|
self.sample_rate
|
|
|
|
.store(buffer_config.sample_rate, Ordering::Relaxed);
|
2022-02-13 07:12:08 +11:00
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:42:58 +11:00
|
|
|
fn reset(&mut self) {
|
|
|
|
// Initialize and/or reset the filters on the next process call
|
|
|
|
self.should_update_filters.store(true, Ordering::Release);
|
2022-11-12 07:23:39 +11:00
|
|
|
self.bypass_smoother
|
|
|
|
.reset(if self.params.bypass.value() { 1.0 } else { 0.0 });
|
2022-03-08 10:42:58 +11:00
|
|
|
}
|
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
fn process(
|
|
|
|
&mut self,
|
2022-02-13 07:12:08 +11:00
|
|
|
buffer: &mut Buffer,
|
2022-05-27 10:30:57 +10:00
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-10-22 10:15:16 +11:00
|
|
|
_context: &mut impl ProcessContext<Self>,
|
2022-02-13 02:27:57 +11:00
|
|
|
) -> ProcessStatus {
|
2022-02-14 05:49:18 +11:00
|
|
|
// Since this is an expensive operation, only update the filters when it's actually
|
|
|
|
// necessary, and allow smoothing only every n samples using the automation precision
|
|
|
|
// parameter
|
|
|
|
let smoothing_interval =
|
2022-09-07 05:55:14 +10:00
|
|
|
unnormalize_automation_precision(self.params.automation_precision.value());
|
2022-02-14 05:49:18 +11:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
// The bypass parameter controls a smoother so we can crossfade between the dry and the wet
|
|
|
|
// signals as needed
|
|
|
|
if !self.params.bypass.value() || self.bypass_smoother.is_smoothing() {
|
|
|
|
// We'll iterate in blocks to make the blending relatively cheap without having to
|
|
|
|
// duplicate code or add a bunch of per-sample conditionals
|
|
|
|
for (_, mut block) in buffer.iter_blocks(MAX_BLOCK_SIZE) {
|
|
|
|
// We'll blend this with the dry signal as needed
|
|
|
|
let mut dry = [f32x2::default(); MAX_BLOCK_SIZE];
|
|
|
|
let mut wet = [f32x2::default(); MAX_BLOCK_SIZE];
|
|
|
|
for (input_samples, (dry_samples, wet_samples)) in block
|
|
|
|
.iter_samples()
|
|
|
|
.zip(std::iter::zip(dry.iter_mut(), wet.iter_mut()))
|
2022-11-05 02:01:13 +11:00
|
|
|
{
|
2022-11-12 07:23:39 +11:00
|
|
|
self.maybe_update_filters(smoothing_interval);
|
|
|
|
|
|
|
|
// We can compute the filters for both channels at once. The SIMD version thus now
|
|
|
|
// only supports steroo audio.
|
|
|
|
*dry_samples = unsafe { input_samples.to_simd_unchecked() };
|
|
|
|
*wet_samples = *dry_samples;
|
|
|
|
|
|
|
|
for filter in self
|
|
|
|
.filters
|
|
|
|
.iter_mut()
|
|
|
|
.take(self.params.filter_stages.value() as usize)
|
|
|
|
{
|
|
|
|
*wet_samples = filter.process(*wet_samples);
|
|
|
|
}
|
2022-11-05 02:01:13 +11:00
|
|
|
}
|
2022-02-16 04:04:26 +11:00
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
// If the bypass smoother is activated then the bypass switch has just been flipped to
|
|
|
|
// either the on or the off position
|
|
|
|
if self.bypass_smoother.is_smoothing() {
|
|
|
|
for (mut channel_samples, (dry_samples, wet_samples)) in block
|
|
|
|
.iter_samples()
|
|
|
|
.zip(std::iter::zip(dry.iter_mut(), wet.iter_mut()))
|
|
|
|
{
|
|
|
|
// We'll do an equal-power fade
|
|
|
|
let dry_t_squared = self.bypass_smoother.next();
|
|
|
|
let dry_t = dry_t_squared.sqrt();
|
|
|
|
let wet_t = (1.0 - dry_t_squared).sqrt();
|
|
|
|
|
|
|
|
let dry_weighted = *dry_samples * f32x2::splat(dry_t);
|
|
|
|
let wet_weighted = *wet_samples * f32x2::splat(wet_t);
|
|
|
|
|
|
|
|
unsafe { channel_samples.from_simd_unchecked(dry_weighted + wet_weighted) };
|
|
|
|
}
|
|
|
|
} else if self.params.bypass.value() {
|
|
|
|
// If the bypass is enabled and we're no longer smoothing then the output should
|
|
|
|
// just be the origianl dry signal
|
|
|
|
} else {
|
|
|
|
// Otherwise the signal is 100% wet
|
|
|
|
for (mut channel_samples, wet_samples) in block.iter_samples().zip(wet.iter()) {
|
|
|
|
unsafe { channel_samples.from_simd_unchecked(*wet_samples) };
|
|
|
|
}
|
|
|
|
}
|
2022-11-05 02:01:13 +11:00
|
|
|
}
|
2022-02-13 07:12:08 +11:00
|
|
|
}
|
|
|
|
|
2022-03-07 23:57:24 +11:00
|
|
|
// Compute a spectrum for the GUI if needed
|
2022-07-14 07:16:29 +10:00
|
|
|
if self.params.editor_state.is_open() {
|
2022-03-07 23:57:24 +11:00
|
|
|
self.spectrum_input.compute(buffer);
|
|
|
|
}
|
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
ProcessStatus::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 07:12:08 +11:00
|
|
|
impl Diopser {
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Check if the filters need to be updated beased on
|
|
|
|
/// [`should_update_filters`][Self::should_update_filters] and the smoothing interval, and
|
|
|
|
/// update them as needed.
|
2022-02-14 05:49:18 +11:00
|
|
|
fn maybe_update_filters(&mut self, smoothing_interval: u32) {
|
2022-02-16 05:48:54 +11:00
|
|
|
// In addition to updating the filters, we should also clear the filter's state when
|
|
|
|
// changing a setting we can't neatly interpolate between.
|
|
|
|
let reset_filters = self
|
2022-02-14 05:49:18 +11:00
|
|
|
.should_update_filters
|
|
|
|
.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed)
|
2022-02-16 05:48:54 +11:00
|
|
|
.is_ok();
|
|
|
|
let should_update_filters = reset_filters
|
2022-02-14 05:49:18 +11:00
|
|
|
|| ((self.params.filter_frequency.smoothed.is_smoothing()
|
2022-02-15 09:59:40 +11:00
|
|
|
|| self.params.filter_resonance.smoothed.is_smoothing()
|
|
|
|
|| self.params.filter_spread_octaves.smoothed.is_smoothing())
|
2022-02-14 05:49:18 +11:00
|
|
|
&& self.next_filter_smoothing_in <= 1);
|
|
|
|
if should_update_filters {
|
2022-02-16 05:48:54 +11:00
|
|
|
self.update_filters(smoothing_interval, reset_filters);
|
2022-02-14 05:49:18 +11:00
|
|
|
self.next_filter_smoothing_in = smoothing_interval as i32;
|
|
|
|
} else {
|
|
|
|
self.next_filter_smoothing_in -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recompute the filter coefficients based on the smoothed paraetersm. We can skip forwardq in
|
|
|
|
/// larger steps to reduce the DSP load.
|
2022-02-16 05:48:54 +11:00
|
|
|
fn update_filters(&mut self, smoothing_interval: u32, reset_filters: bool) {
|
2022-02-15 09:59:40 +11:00
|
|
|
if self.filters.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-12 07:23:39 +11:00
|
|
|
let sample_rate = self.sample_rate.load(Ordering::Relaxed);
|
2022-02-15 09:59:40 +11:00
|
|
|
let frequency = self
|
|
|
|
.params
|
|
|
|
.filter_frequency
|
|
|
|
.smoothed
|
|
|
|
.next_step(smoothing_interval);
|
|
|
|
let resonance = self
|
|
|
|
.params
|
|
|
|
.filter_resonance
|
|
|
|
.smoothed
|
|
|
|
.next_step(smoothing_interval);
|
|
|
|
let spread_octaves = self
|
|
|
|
.params
|
|
|
|
.filter_spread_octaves
|
|
|
|
.smoothed
|
|
|
|
.next_step(smoothing_interval);
|
|
|
|
let spread_style = self.params.filter_spread_style.value();
|
|
|
|
|
2022-03-03 02:49:16 +11:00
|
|
|
// Used to calculate the linear spread. This is calculated in such a way that the range
|
|
|
|
// never dips below 0.
|
|
|
|
let max_octave_spread = if spread_octaves >= 0.0 {
|
|
|
|
frequency - (frequency * 2.0f32.powf(-spread_octaves))
|
|
|
|
} else {
|
|
|
|
(frequency * 2.0f32.powf(spread_octaves)) - frequency
|
|
|
|
};
|
|
|
|
|
2022-03-03 03:04:01 +11:00
|
|
|
// TODO: This wrecks the DSP load at high smoothing accuracy, perhaps also use SIMD here
|
2022-02-15 09:59:40 +11:00
|
|
|
const MIN_FREQUENCY: f32 = 5.0;
|
2022-11-12 07:23:39 +11:00
|
|
|
let max_frequency = sample_rate / 2.05;
|
2022-09-07 05:55:14 +10:00
|
|
|
for filter_idx in 0..self.params.filter_stages.value() as usize {
|
2022-02-15 09:59:40 +11:00
|
|
|
// The index of the filter normalized to range [-1, 1]
|
|
|
|
let filter_proportion =
|
2022-09-07 05:55:14 +10:00
|
|
|
(filter_idx as f32 / self.params.filter_stages.value() as f32) * 2.0 - 1.0;
|
2022-02-15 09:59:40 +11:00
|
|
|
|
|
|
|
// The spread parameter adds an offset to the frequency depending on the number of the
|
|
|
|
// filter
|
|
|
|
let filter_frequency = match spread_style {
|
|
|
|
SpreadStyle::Octaves => frequency * 2.0f32.powf(spread_octaves * filter_proportion),
|
2022-03-03 02:49:16 +11:00
|
|
|
SpreadStyle::Linear => frequency + (max_octave_spread * filter_proportion),
|
2022-02-15 09:59:40 +11:00
|
|
|
}
|
|
|
|
.clamp(MIN_FREQUENCY, max_frequency);
|
|
|
|
|
2022-03-04 22:49:36 +11:00
|
|
|
self.filters[filter_idx].coefficients =
|
2022-11-12 07:23:39 +11:00
|
|
|
filter::BiquadCoefficients::allpass(sample_rate, filter_frequency, resonance);
|
2022-03-04 22:49:36 +11:00
|
|
|
if reset_filters {
|
|
|
|
self.filters[filter_idx].reset();
|
2022-02-16 04:30:45 +11:00
|
|
|
}
|
2022-02-13 07:12:08 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:49:18 +11:00
|
|
|
fn normalize_automation_precision(step_size: u32) -> f32 {
|
2022-02-14 05:27:50 +11:00
|
|
|
(MAX_AUTOMATION_STEP_SIZE - step_size) as f32
|
|
|
|
/ (MAX_AUTOMATION_STEP_SIZE - MIN_AUTOMATION_STEP_SIZE) as f32
|
|
|
|
}
|
|
|
|
|
2022-02-14 05:49:18 +11:00
|
|
|
fn unnormalize_automation_precision(normalized: f32) -> u32 {
|
2022-02-14 05:27:50 +11:00
|
|
|
MAX_AUTOMATION_STEP_SIZE
|
2022-02-14 05:49:18 +11:00
|
|
|
- (normalized * (MAX_AUTOMATION_STEP_SIZE - MIN_AUTOMATION_STEP_SIZE) as f32).round() as u32
|
2022-02-14 05:27:50 +11:00
|
|
|
}
|
|
|
|
|
2022-03-01 03:04:39 +11:00
|
|
|
impl ClapPlugin for Diopser {
|
|
|
|
const CLAP_ID: &'static str = "nl.robbertvanderhelm.diopser";
|
2022-07-04 20:44:30 +10:00
|
|
|
const CLAP_DESCRIPTION: Option<&'static str> = Some("A totally original phase rotation plugin");
|
|
|
|
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::Filter,
|
|
|
|
ClapFeature::Utility,
|
|
|
|
];
|
2022-03-01 03:04:39 +11:00
|
|
|
}
|
2022-03-01 00:45:07 +11:00
|
|
|
|
2022-02-13 02:27:57 +11:00
|
|
|
impl Vst3Plugin for Diopser {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"DiopserPlugRvdH.";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Filter";
|
|
|
|
}
|
|
|
|
|
2022-03-01 00:45:31 +11:00
|
|
|
nih_export_clap!(Diopser);
|
2022-02-13 02:27:57 +11:00
|
|
|
nih_export_vst3!(Diopser);
|