2022-03-09 03:41:23 +11:00
|
|
|
// Crisp: a distortion plugin but not quite
|
2022-03-09 23:17:26 +11:00
|
|
|
// Copyright (C) 2022 Robbert van der Helm
|
2022-03-09 03:41:23 +11:00
|
|
|
//
|
|
|
|
// 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-23 04:53:32 +11:00
|
|
|
use nih_plug_vizia::ViziaState;
|
2022-03-09 07:10:36 +11:00
|
|
|
use pcg::Pcg32iState;
|
2022-03-09 09:30:12 +11:00
|
|
|
use std::sync::Arc;
|
2022-03-09 03:41:23 +11:00
|
|
|
|
2022-03-10 01:15:09 +11:00
|
|
|
mod editor;
|
2022-03-09 23:24:57 +11:00
|
|
|
mod filter;
|
2022-03-09 06:42:59 +11:00
|
|
|
mod pcg;
|
|
|
|
|
2022-03-09 23:48:23 +11:00
|
|
|
/// The number of channels we support. Hardcoded to allow for easier SIMD-ifying in the future.
|
|
|
|
const NUM_CHANNELS: u32 = 2;
|
2022-03-10 05:35:12 +11:00
|
|
|
/// The number of channels to iterate over at a time.
|
|
|
|
const BLOCK_SIZE: usize = 64;
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-09 05:02:21 +11:00
|
|
|
/// These seeds being fixed makes bouncing deterministic.
|
2022-03-09 07:10:36 +11:00
|
|
|
const INITIAL_PRNG_SEED: Pcg32iState = Pcg32iState::new(69, 420);
|
2022-03-09 05:02:21 +11:00
|
|
|
|
2022-03-09 09:20:30 +11:00
|
|
|
/// Allow 100% amount to scale the gain to a bit above 100%, to make the effect even less subtle.
|
|
|
|
const AMOUNT_GAIN_MULTIPLIER: f32 = 2.0;
|
2022-03-10 04:26:55 +11:00
|
|
|
const MIN_FILTER_FREQUENCY: f32 = 5.0;
|
|
|
|
const MAX_FILTER_FREQUENCY: f32 = 22_000.0;
|
2022-03-09 09:20:30 +11:00
|
|
|
|
2022-03-09 10:51:52 +11:00
|
|
|
/// This plugin essentially layers the sound with another copy of the signal ring modulated with
|
2022-03-09 23:24:57 +11:00
|
|
|
/// white (or filtered) noise. That other copy of the sound may have a low-pass filter applied to it
|
2022-03-09 10:51:52 +11:00
|
|
|
/// since this effect just turns into literal noise at high frequencies.
|
2022-03-09 03:41:23 +11:00
|
|
|
struct Crisp {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc<CrispParams>,
|
2022-03-09 05:02:21 +11:00
|
|
|
|
2022-03-09 23:48:23 +11:00
|
|
|
/// Needed for computing the filter coefficients.
|
|
|
|
sample_rate: f32,
|
|
|
|
|
2022-03-09 07:10:36 +11:00
|
|
|
/// A PRNG for generating noise, after that we'll implement PCG ourselves so we can easily
|
|
|
|
/// SIMD-ify this in the future.
|
|
|
|
prng: Pcg32iState,
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-10 01:11:41 +11:00
|
|
|
/// Resonant filters for low passing the input signal before RM'ing, to allow this to work with
|
|
|
|
/// inputs that already contain a lot of high freuqency content.
|
|
|
|
rm_input_lpf: [filter::Biquad<f32>; NUM_CHANNELS as usize],
|
2022-03-10 04:25:27 +11:00
|
|
|
/// Resonant filters for high- and then low- passing the noise signal, to make it even brighter.
|
2022-03-09 23:48:23 +11:00
|
|
|
noise_hpf: [filter::Biquad<f32>; NUM_CHANNELS as usize],
|
2022-03-10 04:25:27 +11:00
|
|
|
noise_lpf: [filter::Biquad<f32>; NUM_CHANNELS as usize],
|
2022-03-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Params)]
|
2022-03-24 09:29:49 +11:00
|
|
|
struct CrispParams {
|
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-03-09 05:02:21 +11:00
|
|
|
/// On a range of `[0, 1]`, how much of the modulated sound to mix in.
|
|
|
|
#[id = "amount"]
|
|
|
|
amount: FloatParam,
|
2022-03-09 10:51:52 +11:00
|
|
|
/// What kind of RM to apply. The preset this was modelled after whether intentional or not only
|
|
|
|
/// RMs the positive part of the waveform.
|
2022-03-09 09:46:05 +11:00
|
|
|
#[id = "mode"]
|
|
|
|
mode: EnumParam<Mode>,
|
2022-03-09 10:03:28 +11:00
|
|
|
/// How to handle stereo signals. See [`StereoMode`].
|
|
|
|
#[id = "stereo"]
|
|
|
|
stereo_mode: EnumParam<StereoMode>,
|
2022-03-09 09:30:12 +11:00
|
|
|
|
2022-03-10 01:11:41 +11:00
|
|
|
/// The cutoff frequency for the low-pass filter applied to the input before RM'ing.
|
|
|
|
#[id = "rmlpff"]
|
|
|
|
rm_input_lpf_freq: FloatParam,
|
|
|
|
/// The Q frequency for the low-pass filter applied to the input before RM'ing.
|
|
|
|
#[id = "rmlpfq"]
|
|
|
|
rm_input_lpf_q: FloatParam,
|
|
|
|
/// The cutoff frequency for the high-pass filter applied to the noise.
|
2022-03-09 23:48:23 +11:00
|
|
|
#[id = "nzhpff"]
|
|
|
|
noise_hpf_freq: FloatParam,
|
2022-03-10 01:11:41 +11:00
|
|
|
/// The Q parameter for the high pass-filter applied to the noise.
|
2022-03-09 23:48:23 +11:00
|
|
|
#[id = "nzhpfq"]
|
|
|
|
noise_hpf_q: FloatParam,
|
2022-03-10 04:25:27 +11:00
|
|
|
/// The cutoff frequency for the low-pass filter applied to the noise.
|
|
|
|
#[id = "nzlpff"]
|
|
|
|
noise_lpf_freq: FloatParam,
|
|
|
|
/// The Q parameter for the low pass-filter applied to the noise.
|
|
|
|
#[id = "nzlpfq"]
|
|
|
|
noise_lpf_q: FloatParam,
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-09 09:30:12 +11:00
|
|
|
/// Output gain, as voltage gain. Displayed in decibels.
|
|
|
|
#[id = "output"]
|
|
|
|
output_gain: FloatParam,
|
2022-03-10 05:38:57 +11:00
|
|
|
/// If set, only output the RM'ed signal. Can be useful for further processing.
|
|
|
|
#[id = "wtonly"]
|
|
|
|
wet_only: BoolParam,
|
2022-03-09 05:02:21 +11:00
|
|
|
}
|
2022-03-09 03:41:23 +11:00
|
|
|
|
2022-03-09 09:46:05 +11:00
|
|
|
/// Controls the type of modulation to apply.
|
|
|
|
#[derive(Enum, Debug, PartialEq)]
|
|
|
|
enum Mode {
|
2022-03-09 10:51:52 +11:00
|
|
|
/// RM the entire waveform.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "soggy"]
|
2022-03-26 01:51:40 +11:00
|
|
|
Soggy,
|
2022-03-09 10:51:52 +11:00
|
|
|
/// RM only the positive part of the waveform.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "crispy"]
|
2022-03-26 01:51:40 +11:00
|
|
|
Crispy,
|
2022-03-09 10:51:52 +11:00
|
|
|
/// RM only the negative part of the waveform.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "crispy-negated"]
|
2022-03-26 01:51:40 +11:00
|
|
|
#[name = "Crispy (alt)"]
|
|
|
|
CrispyNegated,
|
2022-03-09 09:46:05 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 10:03:28 +11:00
|
|
|
/// Controls how to handle stereo input.
|
|
|
|
#[derive(Enum, Debug, PartialEq)]
|
|
|
|
enum StereoMode {
|
|
|
|
/// Use the same noise for both channels.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "mono"]
|
2022-03-09 10:03:28 +11:00
|
|
|
Mono,
|
|
|
|
/// Use a different noise source per channel.
|
2022-06-04 21:36:17 +10:00
|
|
|
#[id = "stereo"]
|
2022-03-09 10:03:28 +11:00
|
|
|
Stereo,
|
|
|
|
}
|
|
|
|
|
2022-03-09 03:41:23 +11:00
|
|
|
impl Default for Crisp {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2022-04-07 23:31:46 +10:00
|
|
|
params: Arc::new(CrispParams::default()),
|
2022-03-09 05:02:21 +11:00
|
|
|
|
2022-03-09 23:48:23 +11:00
|
|
|
sample_rate: 1.0,
|
|
|
|
|
2022-03-09 07:10:36 +11:00
|
|
|
prng: INITIAL_PRNG_SEED,
|
2022-03-10 01:11:41 +11:00
|
|
|
rm_input_lpf: [filter::Biquad::default(); NUM_CHANNELS as usize],
|
2022-03-09 23:48:23 +11:00
|
|
|
noise_hpf: [filter::Biquad::default(); NUM_CHANNELS as usize],
|
2022-03-10 04:25:27 +11:00
|
|
|
noise_lpf: [filter::Biquad::default(); NUM_CHANNELS as usize],
|
2022-03-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CrispParams {
|
|
|
|
#[allow(clippy::derivable_impls)]
|
|
|
|
fn default() -> Self {
|
2022-03-27 12:43:39 +11:00
|
|
|
let f32_hz_then_khz = formatters::v2s_f32_hz_then_khz(0);
|
|
|
|
let from_f32_hz_then_khz = formatters::s2v_f32_hz_then_khz();
|
2022-03-22 00:51:51 +11:00
|
|
|
|
2022-03-09 05:02:21 +11:00
|
|
|
Self {
|
2022-07-14 07:16:29 +10:00
|
|
|
editor_state: editor::default_state(),
|
|
|
|
|
2022-03-09 10:05:37 +11:00
|
|
|
amount: FloatParam::new("Amount", 0.35, FloatRange::Linear { min: 0.0, max: 1.0 })
|
2022-03-09 05:02:21 +11:00
|
|
|
.with_smoother(SmoothingStyle::Linear(10.0))
|
|
|
|
.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-03-09 23:48:23 +11:00
|
|
|
|
2022-03-26 01:51:40 +11:00
|
|
|
mode: EnumParam::new("Mode", Mode::Crispy),
|
2022-03-09 10:03:28 +11:00
|
|
|
stereo_mode: EnumParam::new("Stereo Mode", StereoMode::Stereo),
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-10 01:11:41 +11:00
|
|
|
rm_input_lpf_freq: FloatParam::new(
|
|
|
|
"RM LP Frequency",
|
2022-03-10 04:26:55 +11:00
|
|
|
MAX_FILTER_FREQUENCY,
|
2022-03-10 01:11:41 +11:00
|
|
|
FloatRange::Skewed {
|
2022-03-10 04:26:55 +11:00
|
|
|
min: MIN_FILTER_FREQUENCY,
|
|
|
|
max: MAX_FILTER_FREQUENCY,
|
2022-03-10 01:11:41 +11:00
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-10 01:17:38 +11:00
|
|
|
// The unit is baked into the value so we can show the disabled string
|
2022-03-10 01:11:41 +11:00
|
|
|
.with_value_to_string(Arc::new(|value| {
|
2022-03-10 04:26:55 +11:00
|
|
|
if value >= MAX_FILTER_FREQUENCY {
|
2022-03-10 01:11:41 +11:00
|
|
|
String::from("Disabled")
|
|
|
|
} else {
|
2022-03-10 01:17:38 +11:00
|
|
|
format!("{:.0} Hz", value)
|
2022-03-10 01:11:41 +11:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
.with_string_to_value(Arc::new(|string| {
|
|
|
|
if string == "Disabled" {
|
2022-03-10 04:26:55 +11:00
|
|
|
Some(MAX_FILTER_FREQUENCY)
|
2022-03-10 01:11:41 +11:00
|
|
|
} else {
|
|
|
|
string.trim().trim_end_matches(" Hz").parse().ok()
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
rm_input_lpf_q: FloatParam::new(
|
|
|
|
"RM LP Resonance",
|
|
|
|
2.0f32.sqrt() / 2.0,
|
|
|
|
FloatRange::Skewed {
|
|
|
|
min: 2.0f32.sqrt() / 2.0,
|
|
|
|
max: 10.0,
|
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-03-09 23:48:23 +11:00
|
|
|
noise_hpf_freq: FloatParam::new(
|
2022-03-10 01:11:41 +11:00
|
|
|
"Noise HP Frequency",
|
2022-03-10 04:26:55 +11:00
|
|
|
MIN_FILTER_FREQUENCY,
|
2022-03-09 23:48:23 +11:00
|
|
|
FloatRange::Skewed {
|
2022-03-10 04:26:55 +11:00
|
|
|
min: MIN_FILTER_FREQUENCY,
|
|
|
|
max: MAX_FILTER_FREQUENCY,
|
2022-03-09 23:48:23 +11:00
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-10 01:17:38 +11:00
|
|
|
// The unit is baked into the value so we can show the disabled string
|
2022-03-22 00:51:51 +11:00
|
|
|
.with_value_to_string({
|
|
|
|
let f32_hz_then_khz = f32_hz_then_khz.clone();
|
|
|
|
Arc::new(move |value| {
|
|
|
|
if value <= MIN_FILTER_FREQUENCY {
|
|
|
|
String::from("Disabled")
|
|
|
|
} else {
|
|
|
|
f32_hz_then_khz(value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.with_string_to_value({
|
|
|
|
let from_f32_hz_then_khz = from_f32_hz_then_khz.clone();
|
|
|
|
Arc::new(move |string| {
|
|
|
|
if string == "Disabled" {
|
|
|
|
Some(MIN_FILTER_FREQUENCY)
|
|
|
|
} else {
|
|
|
|
from_f32_hz_then_khz(string)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}),
|
2022-03-09 23:48:23 +11:00
|
|
|
noise_hpf_q: FloatParam::new(
|
2022-03-10 01:11:41 +11:00
|
|
|
"Noise HP Resonance",
|
2022-03-09 23:48:23 +11:00
|
|
|
2.0f32.sqrt() / 2.0,
|
|
|
|
FloatRange::Skewed {
|
|
|
|
min: 2.0f32.sqrt() / 2.0,
|
|
|
|
max: 10.0,
|
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-03-10 04:25:27 +11:00
|
|
|
noise_lpf_freq: FloatParam::new(
|
|
|
|
"Noise LP Frequency",
|
2022-03-10 04:26:55 +11:00
|
|
|
MAX_FILTER_FREQUENCY,
|
2022-03-10 04:25:27 +11:00
|
|
|
FloatRange::Skewed {
|
2022-03-10 04:26:55 +11:00
|
|
|
min: MIN_FILTER_FREQUENCY,
|
|
|
|
max: MAX_FILTER_FREQUENCY,
|
2022-03-10 04:25:27 +11:00
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
|
|
|
// The unit is baked into the value so we can show the disabled string
|
2022-03-22 00:51:51 +11:00
|
|
|
.with_value_to_string(Arc::new(move |value| {
|
2022-03-10 04:26:55 +11:00
|
|
|
if value >= MAX_FILTER_FREQUENCY {
|
2022-03-10 04:25:27 +11:00
|
|
|
String::from("Disabled")
|
|
|
|
} else {
|
2022-03-22 00:51:51 +11:00
|
|
|
f32_hz_then_khz(value)
|
2022-03-10 04:25:27 +11:00
|
|
|
}
|
|
|
|
}))
|
2022-03-22 00:51:51 +11:00
|
|
|
.with_string_to_value(Arc::new(move |string| {
|
2022-03-10 04:25:27 +11:00
|
|
|
if string == "Disabled" {
|
2022-03-10 04:26:55 +11:00
|
|
|
Some(MAX_FILTER_FREQUENCY)
|
2022-03-10 04:25:27 +11:00
|
|
|
} else {
|
2022-03-22 00:51:51 +11:00
|
|
|
from_f32_hz_then_khz(string)
|
2022-03-10 04:25:27 +11:00
|
|
|
}
|
|
|
|
})),
|
|
|
|
noise_lpf_q: FloatParam::new(
|
|
|
|
"Noise LP Resonance",
|
|
|
|
2.0f32.sqrt() / 2.0,
|
|
|
|
FloatRange::Skewed {
|
|
|
|
min: 2.0f32.sqrt() / 2.0,
|
|
|
|
max: 10.0,
|
|
|
|
factor: FloatRange::skew_factor(-1.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(100.0))
|
2022-03-27 12:43:39 +11:00
|
|
|
.with_value_to_string(formatters::v2s_f32_rounded(2)),
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-09 09:30:12 +11:00
|
|
|
output_gain: FloatParam::new(
|
|
|
|
"Output",
|
|
|
|
1.0,
|
|
|
|
// Because we're representing gain as decibels the range is already logarithmic
|
|
|
|
FloatRange::Linear {
|
|
|
|
min: util::db_to_gain(-24.0),
|
|
|
|
max: util::db_to_gain(0.0),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_smoother(SmoothingStyle::Logarithmic(10.0))
|
|
|
|
.with_unit(" dB")
|
2022-04-27 23:12:31 +10:00
|
|
|
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
|
|
|
|
.with_string_to_value(formatters::s2v_f32_gain_to_db()),
|
2022-03-10 05:38:57 +11:00
|
|
|
wet_only: BoolParam::new("Wet Only", false),
|
2022-03-09 05:02:21 +11:00
|
|
|
}
|
2022-03-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Plugin for Crisp {
|
|
|
|
const NAME: &'static str = "Crisp";
|
|
|
|
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-03-09 23:48:23 +11:00
|
|
|
const DEFAULT_NUM_INPUTS: u32 = NUM_CHANNELS;
|
|
|
|
const DEFAULT_NUM_OUTPUTS: u32 = NUM_CHANNELS;
|
2022-03-09 03:41:23 +11:00
|
|
|
|
2022-03-11 04:57:17 +11:00
|
|
|
const SAMPLE_ACCURATE_AUTOMATION: bool = true;
|
|
|
|
|
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-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
|
2022-03-10 01:15:09 +11:00
|
|
|
fn editor(&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-10 01:15:09 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 03:41:23 +11:00
|
|
|
fn accepts_bus_config(&self, config: &BusConfig) -> bool {
|
|
|
|
// We'll add a SIMD version in a bit which only supports stereo
|
2022-03-09 23:48:23 +11:00
|
|
|
config.num_input_channels == config.num_output_channels
|
|
|
|
&& config.num_input_channels == NUM_CHANNELS
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
bus_config: &BusConfig,
|
|
|
|
buffer_config: &BufferConfig,
|
2022-05-27 09:17:15 +10:00
|
|
|
_context: &mut impl InitContext,
|
2022-03-09 23:48:23 +11:00
|
|
|
) -> bool {
|
|
|
|
nih_debug_assert_eq!(bus_config.num_input_channels, NUM_CHANNELS);
|
|
|
|
nih_debug_assert_eq!(bus_config.num_output_channels, NUM_CHANNELS);
|
|
|
|
self.sample_rate = buffer_config.sample_rate;
|
|
|
|
|
|
|
|
true
|
2022-03-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 05:02:21 +11:00
|
|
|
fn reset(&mut self) {
|
|
|
|
// By using the same seeds each time bouncing can be made deterministic
|
2022-03-09 07:10:36 +11:00
|
|
|
self.prng = INITIAL_PRNG_SEED;
|
2022-03-09 23:48:23 +11:00
|
|
|
|
2022-03-10 01:11:41 +11:00
|
|
|
for filter in &mut self.rm_input_lpf {
|
|
|
|
filter.reset();
|
|
|
|
}
|
2022-03-09 23:48:23 +11:00
|
|
|
for filter in &mut self.noise_hpf {
|
|
|
|
filter.reset();
|
|
|
|
}
|
2022-03-10 04:25:27 +11:00
|
|
|
for filter in &mut self.noise_lpf {
|
|
|
|
filter.reset();
|
|
|
|
}
|
2022-03-09 05:02:21 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 03:41:23 +11:00
|
|
|
fn process(
|
|
|
|
&mut self,
|
|
|
|
buffer: &mut Buffer,
|
2022-05-27 10:30:57 +10:00
|
|
|
_aux: &mut AuxiliaryBuffers,
|
2022-03-09 03:41:23 +11:00
|
|
|
_context: &mut impl ProcessContext,
|
|
|
|
) -> ProcessStatus {
|
2022-03-10 05:35:12 +11:00
|
|
|
for (_, mut block) in buffer.iter_blocks(BLOCK_SIZE) {
|
|
|
|
let mut rm_outputs = [[0.0; NUM_CHANNELS as usize]; BLOCK_SIZE];
|
|
|
|
|
|
|
|
// Reduce per-sample branching a bit by iterating over smaller blocks and only then
|
2022-03-10 05:42:13 +11:00
|
|
|
// deciding what to do with the output. This version branches only once per sample (in
|
|
|
|
// `do_ring_mod()`) which can be trivially optimized to a masked min/max later.
|
|
|
|
// TODO: SIMD-ize this to process both channels at once
|
|
|
|
match self.params.stereo_mode.value() {
|
|
|
|
StereoMode::Mono => {
|
|
|
|
for (channel_samples, rm_outputs) in block.iter_samples().zip(&mut rm_outputs) {
|
|
|
|
let amount = self.params.amount.smoothed.next() * AMOUNT_GAIN_MULTIPLIER;
|
|
|
|
|
|
|
|
// Controls the pre-RM LPF and the HPF applied to the noise signal
|
|
|
|
self.maybe_update_filters();
|
|
|
|
|
2022-03-10 05:35:12 +11:00
|
|
|
let noise = self.gen_noise(0);
|
|
|
|
for (channel_idx, (sample, rm_output)) in
|
|
|
|
channel_samples.into_iter().zip(rm_outputs).enumerate()
|
|
|
|
{
|
|
|
|
*rm_output = self.do_ring_mod(*sample, channel_idx, noise) * amount;
|
|
|
|
}
|
2022-03-09 10:03:28 +11:00
|
|
|
}
|
2022-03-10 05:42:13 +11:00
|
|
|
}
|
|
|
|
StereoMode::Stereo => {
|
|
|
|
for (channel_samples, rm_outputs) in block.iter_samples().zip(&mut rm_outputs) {
|
|
|
|
let amount = self.params.amount.smoothed.next() * AMOUNT_GAIN_MULTIPLIER;
|
|
|
|
self.maybe_update_filters();
|
2022-03-10 05:35:12 +11:00
|
|
|
for (channel_idx, (sample, rm_output)) in
|
|
|
|
channel_samples.into_iter().zip(rm_outputs).enumerate()
|
|
|
|
{
|
|
|
|
let noise = self.gen_noise(channel_idx);
|
|
|
|
*rm_output = self.do_ring_mod(*sample, channel_idx, noise) * amount;
|
|
|
|
}
|
2022-03-09 10:03:28 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 05:02:21 +11:00
|
|
|
}
|
2022-03-10 05:35:12 +11:00
|
|
|
|
2022-03-10 05:38:57 +11:00
|
|
|
if self.params.wet_only.value {
|
|
|
|
for (channel_samples, rm_outputs) in block.iter_samples().zip(&mut rm_outputs) {
|
|
|
|
let output_gain = self.params.output_gain.smoothed.next();
|
|
|
|
for (sample, rm_output) in channel_samples.into_iter().zip(rm_outputs) {
|
|
|
|
*sample = *rm_output * output_gain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (channel_samples, rm_outputs) in block.iter_samples().zip(&mut rm_outputs) {
|
|
|
|
let output_gain = self.params.output_gain.smoothed.next();
|
|
|
|
for (sample, rm_output) in channel_samples.into_iter().zip(rm_outputs) {
|
|
|
|
*sample = (*sample + *rm_output) * output_gain;
|
|
|
|
}
|
2022-03-10 05:35:12 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 05:02:21 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 03:41:23 +11:00
|
|
|
ProcessStatus::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 10:03:28 +11:00
|
|
|
impl Crisp {
|
2022-03-09 23:48:23 +11:00
|
|
|
/// Generate a new noise sample with the high pass filter applied.
|
|
|
|
fn gen_noise(&mut self, channel: usize) -> f32 {
|
|
|
|
let noise = self.prng.next_f32() * 2.0 - 1.0;
|
2022-03-10 04:25:27 +11:00
|
|
|
let high_passed = self.noise_hpf[channel].process(noise);
|
|
|
|
self.noise_lpf[channel].process(high_passed)
|
2022-03-09 10:03:28 +11:00
|
|
|
}
|
|
|
|
|
2022-03-10 01:11:41 +11:00
|
|
|
/// Perform the RM step depending on the mode. This applies a low pass filter to the input
|
|
|
|
/// before RM'ing.
|
|
|
|
fn do_ring_mod(&mut self, sample: f32, channel_idx: usize, noise: f32) -> f32 {
|
|
|
|
let sample = self.rm_input_lpf[channel_idx].process(sample);
|
|
|
|
|
2022-03-09 10:03:28 +11:00
|
|
|
// TODO: Avoid branching in the main loop, this just makes it a bit easier to prototype
|
|
|
|
match self.params.mode.value() {
|
2022-03-26 01:51:40 +11:00
|
|
|
Mode::Soggy => sample * noise,
|
|
|
|
Mode::Crispy => sample.max(0.0) * noise,
|
|
|
|
Mode::CrispyNegated => sample.max(0.0) * noise,
|
2022-03-09 10:03:28 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 23:48:23 +11:00
|
|
|
|
|
|
|
/// Update the filter coefficients if needed. Should be called once per sample.
|
|
|
|
fn maybe_update_filters(&mut self) {
|
2022-03-10 01:11:41 +11:00
|
|
|
if self.params.rm_input_lpf_freq.smoothed.is_smoothing()
|
|
|
|
|| self.params.rm_input_lpf_q.smoothed.is_smoothing()
|
|
|
|
{
|
|
|
|
self.update_rm_input_lpf();
|
|
|
|
}
|
2022-03-09 23:48:23 +11:00
|
|
|
if self.params.noise_hpf_freq.smoothed.is_smoothing()
|
|
|
|
|| self.params.noise_hpf_q.smoothed.is_smoothing()
|
|
|
|
{
|
2022-03-10 01:11:41 +11:00
|
|
|
self.update_noise_hpf();
|
|
|
|
}
|
2022-03-10 04:25:27 +11:00
|
|
|
if self.params.noise_lpf_freq.smoothed.is_smoothing()
|
|
|
|
|| self.params.noise_lpf_q.smoothed.is_smoothing()
|
|
|
|
{
|
|
|
|
self.update_noise_lpf();
|
|
|
|
}
|
2022-03-10 01:11:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the filter coefficients if needed. Should be called explicitly from `initialize()`.
|
|
|
|
fn update_rm_input_lpf(&mut self) {
|
|
|
|
let frequency = self.params.rm_input_lpf_freq.smoothed.next();
|
|
|
|
let q = self.params.rm_input_lpf_q.smoothed.next();
|
|
|
|
let coefficients = filter::BiquadCoefficients::lowpass(self.sample_rate, frequency, q);
|
|
|
|
for filter in &mut self.rm_input_lpf {
|
|
|
|
filter.coefficients = coefficients;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the filter coefficients if needed. Should be called explicitly from `initialize()`.
|
|
|
|
fn update_noise_hpf(&mut self) {
|
|
|
|
let frequency = self.params.noise_hpf_freq.smoothed.next();
|
|
|
|
let q = self.params.noise_hpf_q.smoothed.next();
|
|
|
|
let coefficients = filter::BiquadCoefficients::highpass(self.sample_rate, frequency, q);
|
|
|
|
for filter in &mut self.noise_hpf {
|
|
|
|
filter.coefficients = coefficients;
|
2022-03-09 23:48:23 +11:00
|
|
|
}
|
|
|
|
}
|
2022-03-10 04:25:27 +11:00
|
|
|
|
|
|
|
/// Update the filter coefficients if needed. Should be called explicitly from `initialize()`.
|
|
|
|
fn update_noise_lpf(&mut self) {
|
|
|
|
let frequency = self.params.noise_lpf_freq.smoothed.next();
|
|
|
|
let q = self.params.noise_lpf_q.smoothed.next();
|
|
|
|
let coefficients = filter::BiquadCoefficients::lowpass(self.sample_rate, frequency, q);
|
|
|
|
for filter in &mut self.noise_lpf {
|
|
|
|
filter.coefficients = coefficients;
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 10:03:28 +11:00
|
|
|
}
|
|
|
|
|
2022-03-09 03:41:23 +11:00
|
|
|
impl ClapPlugin for Crisp {
|
|
|
|
const CLAP_ID: &'static str = "nl.robbertvanderhelm.crisp";
|
2022-07-04 20:44:30 +10:00
|
|
|
const CLAP_DESCRIPTION: Option<&'static str> =
|
|
|
|
Some("Adds a bright crispy top end to low bass sounds");
|
|
|
|
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::Mono,
|
|
|
|
ClapFeature::Distortion,
|
|
|
|
];
|
2022-03-09 03:41:23 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Vst3Plugin for Crisp {
|
|
|
|
const VST3_CLASS_ID: [u8; 16] = *b"CrispPluginRvdH.";
|
|
|
|
const VST3_CATEGORIES: &'static str = "Fx|Filter|Distortion";
|
|
|
|
}
|
|
|
|
|
|
|
|
nih_export_clap!(Crisp);
|
|
|
|
nih_export_vst3!(Crisp);
|