use crate::synth::va::dsp::DelayLine;
use crate::synth::va::engine::param;
use crate::synth::va::fx::{Effect, Params};
const MIN_HZ: f32 = 20.0;
const MIN_DELAY: f32 = 2.0;
const SLEW: f32 = 0.002;
pub(crate) struct CombFx {
lines: [DelayLine; 2],
delay: f32,
target: f32,
mix: f32,
feedback: f32,
on: bool,
}
impl CombFx {
pub(crate) fn new(sample_rate: u32) -> Self {
let len = (sample_rate.max(1) as f32 / MIN_HZ) as usize + 4;
CombFx {
lines: [DelayLine::new(len), DelayLine::new(len)],
delay: 100.0,
target: 100.0,
mix: 0.0,
feedback: 0.0,
on: false,
}
}
}
impl Effect for CombFx {
fn update(&mut self, p: &Params<'_>, sample_rate: f32) -> bool {
let amount = p.get(param::COMB) / 100.0;
if amount <= 0.0 && !self.on {
return false;
}
self.on = true;
self.mix = amount;
let sign = if p.raw(param::COMB_NEG) != 0 {
-1.0
} else {
1.0
};
self.feedback = sign * p.get(param::COMB_FB) / 100.0;
let fixed_hz = p.get(param::COMB_HZ).max(MIN_HZ);
let hz = if p.raw(param::COMB_TRACK) != 0 && p.note_hz > MIN_HZ {
p.note_hz
} else {
fixed_hz
};
let max = self.lines[0].len() as f32 - 3.0;
self.target = (sample_rate / hz).clamp(MIN_DELAY, max);
true
}
fn process(&mut self, x: [f32; 2], _sample_rate: f32) -> [f32; 2] {
self.delay += (self.target - self.delay) * SLEW;
let mut out = [0.0; 2];
for (side, line) in self.lines.iter_mut().enumerate() {
let past = line.tap_frac(self.delay - 1.0);
let y = x[side] + self.feedback * past;
line.push(y);
let wet = y * (1.0 - 0.5 * self.feedback.abs());
out[side] = x[side] + (wet - x[side]) * self.mix;
}
out
}
fn clear(&mut self) {
for l in &mut self.lines {
l.clear();
}
self.delay = self.target;
self.on = false;
}
}