use crate::synth::va::dsp::{DelayLine, Svf};
use crate::synth::va::engine::param;
use crate::synth::va::engine::{cutoff_hz, FILTER_UPDATE};
use crate::synth::va::fx::send::tail::{Tail, TailWatch};
use crate::synth::va::fx::send::{SendCtx, SendFx};
use crate::synth::va::fx::Params;
use crate::synth::va::m;
pub struct PingPong {
left: DelayLine,
right: DelayLine,
damp_l: f32,
damp_r: f32,
}
impl PingPong {
pub fn new(max_samples: usize) -> Self {
PingPong {
left: DelayLine::new(max_samples),
right: DelayLine::new(max_samples),
damp_l: 0.0,
damp_r: 0.0,
}
}
pub fn process_free(
&mut self,
input: f32,
delay: [f32; 2],
feedback: f32,
tone: f32,
sat: f32,
) -> [f32; 2] {
let dl = self.left.tap_frac(delay[0]);
let dr = self.right.tap_frac(delay[1]);
self.damp_l += (dl - self.damp_l) * tone;
self.damp_r += (dr - self.damp_r) * tone;
self.left
.push(input + soft_clip(self.damp_l * feedback, sat));
self.right
.push(input + soft_clip(self.damp_r * feedback, sat));
[dl, dr]
}
pub fn process_pingpong_free(
&mut self,
input: f32,
delay: [f32; 2],
feedback: f32,
tone: f32,
sat: f32,
) -> [f32; 2] {
let dl = self.left.tap_frac(delay[0]);
let dr = self.right.tap_frac(delay[1]);
self.damp_l += (dr - self.damp_l) * tone;
self.damp_r += (dl - self.damp_r) * tone;
self.left
.push(input + soft_clip(self.damp_l * feedback, sat));
self.right.push(soft_clip(self.damp_r * feedback, sat));
[dl, dr]
}
pub fn process(&mut self, input: f32, delay: usize, feedback: f32, tone: f32) -> [f32; 2] {
let dl = self.left.tap(delay);
let dr = self.right.tap(delay);
self.damp_l += (dr - self.damp_l) * tone;
self.damp_r += (dl - self.damp_r) * tone;
self.left.push(input + self.damp_l * feedback);
self.right.push(self.damp_r * feedback);
[dl, dr]
}
fn regions(&self, f: &mut dyn FnMut(&[f32])) {
f(self.left.raw());
f(self.right.raw());
f(&[self.damp_l, self.damp_r]);
}
pub fn clear(&mut self) {
self.left.clear();
self.right.clear();
self.damp_l = 0.0;
self.damp_r = 0.0;
}
}
fn soft_clip(x: f32, amount: f32) -> f32 {
if amount <= 0.0 {
return x;
}
let a = amount * 3.0;
x * (1.0 + a) / (1.0 + a * x.abs())
}
pub(crate) fn delay_steps(steps: f32, steps_r: f32, feel: f32) -> [f32; 2] {
let k = match feel as u32 {
1 => 1.5,
2 => 2.0 / 3.0,
_ => 1.0,
};
let r = if steps_r > 0.0 { steps_r } else { steps };
[steps * k, r * k]
}
const WOW_HZ: f32 = 0.6;
const WOW_SECS: f32 = 0.004;
pub(crate) fn delay_samples(bpm: f32, steps: f32, sample_rate: f32) -> usize {
let step = 60.0 / bpm / 4.0;
(step * steps * sample_rate) as usize
}
pub(crate) struct DelayFx {
dsp: PingPong,
wow_phase: f32,
lowcut: Svf,
counter: u32,
active: bool,
watch: TailWatch,
}
impl Tail for DelayFx {
fn regions(&self, f: &mut dyn FnMut(&[f32])) {
self.dsp.regions(f);
f(&[self.lowcut.max_abs()]);
}
}
impl DelayFx {
#[cfg(test)]
pub(crate) fn is_idle(&self) -> bool {
!self.active
}
#[cfg(test)]
pub(crate) fn disable_tail_flush(&mut self) {
self.watch.disable();
}
fn flush_tail(&mut self) {
self.dsp.clear();
self.lowcut.clear();
self.active = false;
}
}
impl DelayFx {
#[cfg(test)]
pub(crate) fn force_active(&mut self) {
self.active = true;
}
pub(crate) fn new(sample_rate: u32) -> Self {
DelayFx {
dsp: PingPong::new(sample_rate as usize * 9 + 512),
wow_phase: 0.0,
lowcut: Svf::default(),
counter: 0,
active: false,
watch: TailWatch::default(),
}
}
fn step(&mut self, p: &Params<'_>, input: f32, ctx: &SendCtx) -> [f32; 2] {
let sr = ctx.sample_rate;
let mode = p.get_hi(param::DLY_MODE) as u32;
let sat = p.get_hi(param::DLY_SAT) / 100.0;
let wow = p.get_hi(param::DLY_WOW) / 100.0;
if input != 0.0 {
self.active = true;
} else if !self.active && wow <= 0.0 && mode != 2 && p.get(param::DLY_LOWCUT) <= 0.0 {
self.counter = self.counter.wrapping_add(1);
return [0.0, 0.0];
}
let steps = delay_steps(
p.get(param::DLY_STEPS),
p.get_hi(param::DLY_STEPS_R),
p.get_hi(param::DLY_FEEL),
);
let delay = delay_samples(ctx.bpm, steps[0], sr);
let lowcut = p.get(param::DLY_LOWCUT);
let input = if lowcut > 0.0 {
let k = core::f32::consts::SQRT_2;
if self.counter % FILTER_UPDATE == 0 {
self.lowcut.set(cutoff_hz(lowcut), k, sr);
}
self.lowcut.process(input).hp
} else {
input
};
self.counter = self.counter.wrapping_add(1);
let fb = p.get(param::DLY_FB) / 100.0;
let tone = p.get(param::DLY_TONE) / 100.0;
if mode == 0
&& sat <= 0.0
&& wow <= 0.0
&& steps[0] == steps[1]
&& p.get_hi(param::DLY_FEEL) == 0.0
{
return self.dsp.process(input, delay, fb, tone);
}
let mut t = [delay as f32, delay_samples(ctx.bpm, steps[1], sr) as f32];
if wow > 0.0 || mode == 2 {
let depth = if mode == 2 { wow.max(0.15) } else { wow };
let s = m::sin(self.wow_phase * core::f32::consts::TAU);
self.wow_phase += WOW_HZ / sr;
if self.wow_phase >= 1.0 {
self.wow_phase -= self.wow_phase.floor();
}
let swing = s * depth * WOW_SECS * sr;
t[0] += swing;
t[1] -= swing;
}
let sat = if mode == 2 { sat.max(0.2) } else { sat };
if mode == 1 {
self.dsp.process_free(input, t, fb, tone, sat)
} else {
self.dsp.process_pingpong_free(input, t, fb, tone, sat)
}
}
}
impl SendFx for DelayFx {
fn process(&mut self, p: &Params<'_>, input: f32, ctx: &SendCtx) -> [f32; 2] {
let out = self.step(p, input, ctx);
if input != 0.0 || !self.active {
self.watch.reset();
return out;
}
let mode = p.get_hi(param::DLY_MODE) as u32;
let mut sat = p.get_hi(param::DLY_SAT) / 100.0;
if mode == 2 {
sat = sat.max(0.2);
}
if p.get(param::DLY_FB) / 100.0 * (1.0 + 3.0 * sat) >= 1.0 {
self.watch.reset();
return out;
}
let mut watch = core::mem::take(&mut self.watch);
let flush = watch.tick(&*self, ctx.sample_rate);
self.watch = watch;
if flush {
self.flush_tail();
}
out
}
fn clear(&mut self) {
self.watch.reset();
self.dsp.clear();
self.lowcut.clear();
self.counter = 0;
self.wow_phase = 0.0;
self.active = false;
}
}