use crate::synth::va::dsp::DelayLine;
use crate::synth::va::engine::param;
use crate::synth::va::fx::send::{SendCtx, SendFx};
use crate::synth::va::fx::Params;
use crate::synth::va::m;
pub struct Chorus {
line: DelayLine,
phase: f32,
}
impl Chorus {
const BASE: f32 = 0.012;
const SWING: f32 = 0.006;
pub const LONGEST_SECS: f32 = Self::BASE + Self::SWING;
pub fn new(sample_rate: u32) -> Self {
let len = ((Self::BASE + Self::SWING) * sample_rate as f32) as usize + 4;
Chorus {
line: DelayLine::new(len),
phase: 0.0,
}
}
pub fn process(&mut self, input: f32, rate: f32, depth: f32, sample_rate: f32) -> [f32; 2] {
self.line.push(input);
let base = Self::BASE * sample_rate;
let swing = Self::SWING * sample_rate * depth;
let tau = core::f32::consts::TAU;
let l = self.line.tap_frac(base + swing * m::sin(self.phase * tau));
let r = self
.line
.tap_frac(base + swing * m::sin((self.phase + 0.25) * tau));
self.phase += rate / sample_rate;
if self.phase >= 1.0 {
self.phase -= self.phase.floor();
}
[l, r]
}
pub fn clear(&mut self) {
self.line.clear();
self.phase = 0.0;
}
}
pub(crate) struct ChorusFx {
dsp: Chorus,
active: bool,
}
impl ChorusFx {
pub(crate) fn new(sample_rate: u32) -> Self {
ChorusFx {
dsp: Chorus::new(sample_rate),
active: false,
}
}
}
impl SendFx for ChorusFx {
fn process(&mut self, p: &Params<'_>, input: f32, ctx: &SendCtx) -> [f32; 2] {
if input != 0.0 || self.active {
self.active = true;
self.dsp.process(
input,
p.get(param::CHO_RATE) * 0.1,
p.get(param::CHO_DEPTH) / 100.0,
ctx.sample_rate,
)
} else {
[0.0, 0.0]
}
}
fn clear(&mut self) {
self.dsp.clear();
self.active = false;
}
}