use crate::synth::va::dsp::Svf;
use crate::synth::va::engine::{cutoff_hz, param};
use crate::synth::va::fx::{Effect, Params, Quiet, SilenceGate};
const DRIVE: f32 = 6.0;
const MAX_MIX: f32 = 0.6;
fn soft(x: f32) -> f32 {
let x = x.clamp(-3.0, 3.0);
x * (27.0 + x * x) / (27.0 + 9.0 * x * x)
}
pub(crate) struct ExciterFx {
hp_in: [Svf; 2],
hp_out: [Svf; 2],
amount: f32,
on: bool,
silence: SilenceGate,
}
impl ExciterFx {
pub(crate) fn new() -> Self {
ExciterFx {
hp_in: [Svf::default(); 2],
hp_out: [Svf::default(); 2],
amount: 0.0,
on: false,
silence: SilenceGate::default(),
}
}
}
impl Effect for ExciterFx {
fn update(&mut self, p: &Params<'_>, sample_rate: f32) -> bool {
let amount = p.get_hi(param::EXC) / 100.0;
if amount <= 0.0 && !self.on {
return false;
}
self.on = true;
self.amount = amount * MAX_MIX;
let hz = cutoff_hz(p.get_hi(param::EXC_FREQ));
for f in self.hp_in.iter_mut().chain(&mut self.hp_out) {
f.set(hz, core::f32::consts::SQRT_2, sample_rate);
}
true
}
fn process(&mut self, x: [f32; 2], sample_rate: f32) -> [f32; 2] {
match self.silence.tick(x, sample_rate) {
Quiet::Skip => return x,
Quiet::Flush => {
for f in self.hp_in.iter_mut().chain(&mut self.hp_out) {
f.clear();
}
return x;
}
Quiet::Run => {}
}
let mut out = x;
for (c, o) in out.iter_mut().enumerate() {
let band = self.hp_in[c].process(x[c]).hp;
let harmonics = self.hp_out[c].process(soft(band * DRIVE)).hp;
*o += harmonics * self.amount;
}
out
}
fn clear(&mut self) {
for f in self.hp_in.iter_mut().chain(&mut self.hp_out) {
f.clear();
}
self.on = false;
self.silence.reset();
}
}