use crate::synth::va::dsp::Svf;
use crate::synth::va::engine::{cutoff_hz, param};
use crate::synth::va::fx::{Effect, Params};
use crate::synth::va::m;
const ENV_ATTACK: f32 = 0.0005;
const ENV_RELEASE: f32 = 0.030;
fn coef(secs: f32, sample_rate: f32) -> f32 {
1.0 - m::exp(-1.0 / (secs.max(1.0e-5) * sample_rate))
}
pub(crate) struct DeessFx {
hp: [Svf; 2],
env: f32,
amount: f32,
th: f32,
k_env: [f32; 2],
on: bool,
}
impl DeessFx {
pub(crate) fn new() -> Self {
DeessFx {
hp: [Svf::default(); 2],
env: 0.0,
amount: 0.0,
th: 0.1,
k_env: [0.0; 2],
on: false,
}
}
}
impl Effect for DeessFx {
fn update(&mut self, p: &Params<'_>, sample_rate: f32) -> bool {
let amount = p.get_hi(param::DEESS) / 100.0;
if amount <= 0.0 && !self.on {
return false;
}
self.on = true;
self.amount = amount;
self.th = (p.get_hi(param::DEESS_TH) / 100.0 * 0.5).max(1.0e-4);
let hz = cutoff_hz(p.get_hi(param::DEESS_FREQ));
for f in &mut self.hp {
f.set(hz, core::f32::consts::SQRT_2, sample_rate);
}
self.k_env = [
coef(ENV_ATTACK, sample_rate),
coef(ENV_RELEASE, sample_rate),
];
true
}
fn process(&mut self, x: [f32; 2], _sample_rate: f32) -> [f32; 2] {
let h = [self.hp[0].process(x[0]).hp, self.hp[1].process(x[1]).hp];
let level = h[0].abs().max(h[1].abs());
let c = if level > self.env {
self.k_env[0]
} else {
self.k_env[1]
};
self.env += (level - self.env) * c;
let over = if self.env > self.th {
1.0 - self.th / self.env
} else {
0.0
};
let cut = (over * self.amount).min(1.0);
[x[0] - h[0] * cut, x[1] - h[1] * cut]
}
fn clear(&mut self) {
for f in &mut self.hp {
f.clear();
}
self.env = 0.0;
self.on = false;
}
}