use crate::synth::va::dsp::DelayLine;
use crate::synth::va::engine::param;
use crate::synth::va::fx::{Effect, Params};
use crate::synth::va::m;
const CROSSOVER_HZ: f32 = 800.0;
const HORN_SLOW: f32 = 0.8;
const HORN_FAST: f32 = 6.7;
const DRUM_SLOW: f32 = 0.7;
const DRUM_FAST: f32 = 5.7;
const HORN_TAU: f32 = 0.5;
const DRUM_TAU: f32 = 1.5;
const HORN_AM: f32 = 0.45;
const DRUM_AM: f32 = 0.2;
const HORN_DOPPLER: f32 = 0.0004;
const DRUM_DOPPLER: f32 = 0.0008;
const CENTER_SECS: f32 = 0.0012;
const LINE_SECS: f32 = 0.004;
struct Rotor {
line: DelayLine,
phase: f32,
speed: f32,
}
impl Rotor {
fn new(len: usize, speed: f32) -> Self {
Rotor {
line: DelayLine::new(len),
phase: 0.0,
speed,
}
}
fn process(
&mut self,
x: f32,
target: f32,
slew: f32,
am: f32,
doppler: f32,
sample_rate: f32,
) -> [f32; 2] {
self.speed += (target - self.speed) * slew;
self.phase += self.speed / sample_rate;
if self.phase >= 1.0 {
self.phase -= self.phase.floor();
}
self.line.push(x);
let tau = core::f32::consts::TAU;
let mut out = [0.0; 2];
for (side, o) in out.iter_mut().enumerate() {
let ph = (self.phase + 0.25 * side as f32) * tau;
let delay = (CENTER_SECS + doppler * m::sin(ph)) * sample_rate;
*o = self.line.tap_frac(delay) * (1.0 + am * m::cos(ph));
}
out
}
fn reset(&mut self, speed: f32) {
self.line.clear();
self.phase = 0.0;
self.speed = speed;
}
}
pub(crate) struct RotaryFx {
horn: Rotor,
drum: Rotor,
lp: f32,
lp_coef: f32,
mix: f32,
horn_share: f32,
horn_target: f32,
drum_target: f32,
horn_slew: f32,
drum_slew: f32,
on: bool,
}
impl RotaryFx {
pub(crate) fn new(sample_rate: u32) -> Self {
let len = (LINE_SECS * sample_rate.max(1) as f32) as usize + 4;
RotaryFx {
horn: Rotor::new(len, HORN_SLOW),
drum: Rotor::new(len, DRUM_SLOW),
lp: 0.0,
lp_coef: 0.1,
mix: 0.0,
horn_share: 0.5,
horn_target: HORN_SLOW,
drum_target: DRUM_SLOW,
horn_slew: 0.0,
drum_slew: 0.0,
on: false,
}
}
}
impl Effect for RotaryFx {
fn update(&mut self, p: &Params<'_>, sample_rate: f32) -> bool {
let amount = p.get(param::ROT) / 100.0;
if amount <= 0.0 {
if self.on {
self.clear();
}
return false;
}
self.on = true;
self.mix = amount;
let speed = p.get(param::ROT_SPEED) / 100.0;
self.horn_target = HORN_SLOW + (HORN_FAST - HORN_SLOW) * speed;
self.drum_target = DRUM_SLOW + (DRUM_FAST - DRUM_SLOW) * speed;
self.horn_share = p.get(param::ROT_HORN) / 100.0;
self.horn_slew = 1.0 - m::exp(-1.0 / (HORN_TAU * sample_rate));
self.drum_slew = 1.0 - m::exp(-1.0 / (DRUM_TAU * sample_rate));
self.lp_coef = 1.0 - m::exp(-core::f32::consts::TAU * CROSSOVER_HZ / sample_rate);
true
}
fn process(&mut self, x: [f32; 2], sample_rate: f32) -> [f32; 2] {
let mono = 0.5 * (x[0] + x[1]);
self.lp += (mono - self.lp) * self.lp_coef;
let low = self.lp;
let high = mono - self.lp;
let h = self.horn.process(
high,
self.horn_target,
self.horn_slew,
HORN_AM,
HORN_DOPPLER,
sample_rate,
);
let d = self.drum.process(
low,
self.drum_target,
self.drum_slew,
DRUM_AM,
DRUM_DOPPLER,
sample_rate,
);
let (gh, gd) = (2.0 * self.horn_share, 2.0 * (1.0 - self.horn_share));
let mut out = [0.0; 2];
for side in 0..2 {
let wet = h[side] * gh + d[side] * gd;
out[side] = x[side] + (wet - x[side]) * self.mix;
}
out
}
fn clear(&mut self) {
self.horn.reset(HORN_SLOW);
self.drum.reset(DRUM_SLOW);
self.lp = 0.0;
self.on = false;
}
}