1
0
Fork 0

Add a stereo control parameter for Crisp

This commit is contained in:
Robbert van der Helm 2022-03-09 00:03:28 +01:00
parent e8301f6c9d
commit 6072eb103d

View file

@ -38,8 +38,8 @@ struct Crisp {
prng: Pcg32iState, prng: Pcg32iState,
} }
// TODO: Filters // TODO: Add a filter for the AM input
// TODO: Mono/stereo/mid-side switch // TODO: Add more kinds of noise
#[derive(Params)] #[derive(Params)]
pub struct CrispParams { pub struct CrispParams {
/// On a range of `[0, 1]`, how much of the modulated sound to mix in. /// On a range of `[0, 1]`, how much of the modulated sound to mix in.
@ -49,6 +49,9 @@ pub struct CrispParams {
/// AMs the positive part of the waveform. /// AMs the positive part of the waveform.
#[id = "mode"] #[id = "mode"]
mode: EnumParam<Mode>, mode: EnumParam<Mode>,
/// How to handle stereo signals. See [`StereoMode`].
#[id = "stereo"]
stereo_mode: EnumParam<StereoMode>,
/// Output gain, as voltage gain. Displayed in decibels. /// Output gain, as voltage gain. Displayed in decibels.
#[id = "output"] #[id = "output"]
@ -68,6 +71,15 @@ enum Mode {
EvenCrispierNegated, EvenCrispierNegated,
} }
/// Controls how to handle stereo input.
#[derive(Enum, Debug, PartialEq)]
enum StereoMode {
/// Use the same noise for both channels.
Mono,
/// Use a different noise source per channel.
Stereo,
}
impl Default for Crisp { impl Default for Crisp {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -88,6 +100,7 @@ impl Default for CrispParams {
.with_value_to_string(formatters::f32_percentage(0)) .with_value_to_string(formatters::f32_percentage(0))
.with_string_to_value(formatters::from_f32_percentage()), .with_string_to_value(formatters::from_f32_percentage()),
mode: EnumParam::new("Mode", Mode::EvenCrispier), mode: EnumParam::new("Mode", Mode::EvenCrispier),
stereo_mode: EnumParam::new("Stereo Mode", StereoMode::Stereo),
output_gain: FloatParam::new( output_gain: FloatParam::new(
"Output", "Output",
1.0, 1.0,
@ -147,17 +160,23 @@ impl Plugin for Crisp {
let output_gain = self.params.output_gain.smoothed.next(); let output_gain = self.params.output_gain.smoothed.next();
// TODO: SIMD-ize this to process both channels at once // TODO: SIMD-ize this to process both channels at once
for sample in channel_samples.into_iter() { // TODO: Avoid branching twice here. Modern branch predictors are pretty good at this
let noise = self.prng.next_f32() * 2.0 - 1.0; // though.
// TODO: Avoid branching here later match self.params.stereo_mode.value() {
let am_result = match self.params.mode.value() { StereoMode::Mono => {
Mode::Crispy => *sample * noise, let noise = self.gen_noise();
Mode::EvenCrispier => sample.max(0.0) * noise, for sample in channel_samples {
Mode::EvenCrispierNegated => sample.max(0.0) * noise, *sample += self.do_am(*sample, noise) * amount;
}; *sample *= output_gain;
}
*sample += am_result * amount; }
*sample *= output_gain; StereoMode::Stereo => {
for sample in channel_samples {
let noise = self.gen_noise();
*sample += self.do_am(*sample, noise) * amount;
*sample *= output_gain;
}
}
} }
} }
@ -165,6 +184,23 @@ impl Plugin for Crisp {
} }
} }
impl Crisp {
/// Generate a new uniform noise sample.
fn gen_noise(&mut self) -> f32 {
self.prng.next_f32() * 2.0 - 1.0
}
/// Perform the AM step depending on the mode.
fn do_am(&self, sample: f32, noise: f32) -> f32 {
// TODO: Avoid branching in the main loop, this just makes it a bit easier to prototype
match self.params.mode.value() {
Mode::Crispy => sample * noise,
Mode::EvenCrispier => sample.max(0.0) * noise,
Mode::EvenCrispierNegated => sample.max(0.0) * noise,
}
}
}
impl ClapPlugin for Crisp { impl ClapPlugin for Crisp {
const CLAP_ID: &'static str = "nl.robbertvanderhelm.crisp"; const CLAP_ID: &'static str = "nl.robbertvanderhelm.crisp";
const CLAP_DESCRIPTION: &'static str = "Adds a bright crispy top end to low bass sounds"; const CLAP_DESCRIPTION: &'static str = "Adds a bright crispy top end to low bass sounds";