use crate::synth::va::engine::cutoff_hz;
use crate::synth::va::engine::param;
use crate::synth::va::fx::{Effect, Params};
use crate::synth::va::m;
use core::f32::consts::PI;
pub mod kind {
pub const NONE: u32 = 0;
pub const PEAK: u32 = 1;
pub const LOW_SHELF: u32 = 2;
pub const HIGH_SHELF: u32 = 3;
pub const LOW_PASS: u32 = 4;
pub const HIGH_PASS: u32 = 5;
pub const NOTCH: u32 = 6;
}
#[derive(Clone, Copy)]
pub struct Biquad {
b0: f32,
b1: f32,
b2: f32,
a1: f32,
a2: f32,
z: [[f32; 2]; 2],
key: (u32, f32, f32, f32),
}
impl Biquad {
pub const fn new() -> Self {
Biquad {
b0: 1.0,
b1: 0.0,
b2: 0.0,
a1: 0.0,
a2: 0.0,
z: [[0.0; 2]; 2],
key: (kind::PEAK, 0.0, 0.0, 0.0),
}
}
fn set(&mut self, kind: u32, hz: f32, gain_db: f32, q: f32, sample_rate: f32) {
let key = (kind, hz, gain_db, q);
if key == self.key {
return;
}
self.key = key;
let a = m::powf(10.0, gain_db / 40.0);
let w = 2.0 * PI * hz.clamp(10.0, sample_rate * 0.45) / sample_rate;
let alpha = m::sin(w) / (2.0 * q.max(0.1));
let cos = m::cos(w);
let (b0, b1, b2, a0, a1, a2) = match kind {
kind::LOW_SHELF => {
let s = 2.0 * a.sqrt() * alpha;
(
a * ((a + 1.0) - (a - 1.0) * cos + s),
2.0 * a * ((a - 1.0) - (a + 1.0) * cos),
a * ((a + 1.0) - (a - 1.0) * cos - s),
(a + 1.0) + (a - 1.0) * cos + s,
-2.0 * ((a - 1.0) + (a + 1.0) * cos),
(a + 1.0) + (a - 1.0) * cos - s,
)
}
kind::HIGH_SHELF => {
let s = 2.0 * a.sqrt() * alpha;
(
a * ((a + 1.0) + (a - 1.0) * cos + s),
-2.0 * a * ((a - 1.0) + (a + 1.0) * cos),
a * ((a + 1.0) + (a - 1.0) * cos - s),
(a + 1.0) - (a - 1.0) * cos + s,
2.0 * ((a - 1.0) - (a + 1.0) * cos),
(a + 1.0) - (a - 1.0) * cos - s,
)
}
kind::LOW_PASS => (
(1.0 - cos) / 2.0,
1.0 - cos,
(1.0 - cos) / 2.0,
1.0 + alpha,
-2.0 * cos,
1.0 - alpha,
),
kind::HIGH_PASS => (
(1.0 + cos) / 2.0,
-(1.0 + cos),
(1.0 + cos) / 2.0,
1.0 + alpha,
-2.0 * cos,
1.0 - alpha,
),
kind::NOTCH => (1.0, -2.0 * cos, 1.0, 1.0 + alpha, -2.0 * cos, 1.0 - alpha),
_ => {
let a0 = 1.0 + alpha / a;
self.b0 = (1.0 + alpha * a) / a0;
self.b1 = -2.0 * cos / a0;
self.b2 = (1.0 - alpha * a) / a0;
self.a1 = -2.0 * cos / a0;
self.a2 = (1.0 - alpha / a) / a0;
return;
}
};
self.b0 = b0 / a0;
self.b1 = b1 / a0;
self.b2 = b2 / a0;
self.a1 = a1 / a0;
self.a2 = a2 / a0;
}
pub fn process(
&mut self,
x: [f32; 2],
kind: u32,
hz: f32,
gain_db: f32,
q: f32,
sample_rate: f32,
) -> [f32; 2] {
self.set(kind, hz, gain_db, q, sample_rate);
let mut out = [0.0; 2];
for (side, o) in out.iter_mut().enumerate() {
let z = &mut self.z[side];
let y = self.b0 * x[side] + z[0];
z[0] = self.b1 * x[side] - self.a1 * y + z[1];
z[1] = self.b2 * x[side] - self.a2 * y;
*o = y;
}
out
}
pub fn clear(&mut self) {
*self = Self::new();
}
}
struct BandParams {
kind: u8,
freq: u8,
gain: u8,
q: u8,
pre: u8,
}
const BANDS: [BandParams; 4] = [
BandParams {
kind: param::EQ_TYPE,
freq: param::EQ_FREQ,
gain: param::EQ_GAIN,
q: param::EQ_Q,
pre: param::EQ_PRE,
},
BandParams {
kind: param::EQ2_TYPE,
freq: param::EQ2_FREQ,
gain: param::EQ2_GAIN,
q: param::EQ2_Q,
pre: param::EQ2_PRE,
},
BandParams {
kind: param::EQ3_TYPE,
freq: param::EQ3_FREQ,
gain: param::EQ3_GAIN,
q: param::EQ3_Q,
pre: param::EQ3_PRE,
},
BandParams {
kind: param::EQ4_TYPE,
freq: param::EQ4_FREQ,
gain: param::EQ4_GAIN,
q: param::EQ4_Q,
pre: param::EQ4_PRE,
},
];
#[derive(Clone, Copy)]
struct Band {
dsp: Biquad,
on: bool,
active: bool,
kind: u32,
hz: f32,
gain_db: f32,
q: f32,
}
impl Band {
const fn new() -> Self {
Band {
dsp: Biquad::new(),
on: false,
active: false,
kind: kind::PEAK,
hz: 0.0,
gain_db: 0.0,
q: 0.0,
}
}
}
pub(crate) struct EqFx {
bands: [Band; 4],
pre: bool,
}
impl EqFx {
pub(crate) const fn post() -> Self {
EqFx {
bands: [Band::new(); 4],
pre: false,
}
}
pub(crate) const fn pre() -> Self {
EqFx {
bands: [Band::new(); 4],
pre: true,
}
}
}
impl Effect for EqFx {
fn update(&mut self, p: &Params<'_>, _sample_rate: f32) -> bool {
let mut any = false;
for (i, (band, bp)) in self.bands.iter_mut().zip(&BANDS).enumerate() {
band.active = false;
if (p.raw(bp.pre) == 1) != self.pre {
continue;
}
let kind = p.raw(bp.kind);
band.gain_db = p.get(bp.gain) - 24.0;
let wanted = kind != kind::NONE || (i == 0 && band.gain_db != 0.0);
band.on |= wanted;
if !band.on {
continue;
}
band.active = true;
any = true;
band.kind = if kind == kind::NONE { kind::PEAK } else { kind };
let mut freq = p.get(bp.freq);
if i == 0 && p.eq_freq_mod != 0.0 {
freq = (freq + p.eq_freq_mod).clamp(0.0, 127.0);
}
band.hz = cutoff_hz(freq);
band.q = p.get(bp.q) / 10.0;
}
any
}
fn process(&mut self, x: [f32; 2], sample_rate: f32) -> [f32; 2] {
let mut y = x;
for band in self.bands.iter_mut().filter(|b| b.active) {
y = band
.dsp
.process(y, band.kind, band.hz, band.gain_db, band.q, sample_rate);
}
y
}
fn clear(&mut self) {
for band in &mut self.bands {
band.dsp.clear();
band.on = false;
band.active = false;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::synth::va::VA;
const SR: f32 = 48_000.0;
fn gain_db(kind: u32, fc: f32, gain: f32, q: f32, hz: f32) -> f32 {
let mut f = Biquad::new();
let (mut sin, mut sout) = (0.0f64, 0.0f64);
for i in 0..48_000 {
let x = m::sin(2.0 * PI * hz * i as f32 / SR) * 0.5;
let y = f.process([x, x], kind, fc, gain, q, SR)[0];
if i >= 24_000 {
sin += (x * x) as f64;
sout += (y * y) as f64;
}
}
(10.0 * (sout / sin).log10()) as f32
}
fn near(v: f32, want: f32, tol: f32) -> bool {
(v - want).abs() <= tol
}
#[test]
fn each_kind_has_the_expected_response() {
let g = gain_db(kind::PEAK, 1000.0, 12.0, 1.0, 1000.0);
assert!(near(g, 12.0, 0.5), "peak center {g}");
let g = gain_db(kind::PEAK, 1000.0, 12.0, 1.0, 100.0);
assert!(near(g, 0.0, 1.0), "peak far {g}");
let g = gain_db(kind::LOW_SHELF, 500.0, 9.0, 0.7, 50.0);
assert!(near(g, 9.0, 0.7), "low shelf low {g}");
let g = gain_db(kind::LOW_SHELF, 500.0, 9.0, 0.7, 8000.0);
assert!(near(g, 0.0, 0.7), "low shelf high {g}");
let g = gain_db(kind::HIGH_SHELF, 2000.0, -9.0, 0.7, 15000.0);
assert!(near(g, -9.0, 0.7), "high shelf high {g}");
let g = gain_db(kind::HIGH_SHELF, 2000.0, -9.0, 0.7, 100.0);
assert!(near(g, 0.0, 0.7), "high shelf low {g}");
let g = gain_db(kind::LOW_PASS, 1000.0, 0.0, 0.707, 200.0);
assert!(near(g, 0.0, 0.7), "lp pass {g}");
let g = gain_db(kind::LOW_PASS, 1000.0, 0.0, 0.707, 4000.0);
assert!(g < -20.0, "lp stop {g}");
let g = gain_db(kind::HIGH_PASS, 1000.0, 0.0, 0.707, 5000.0);
assert!(near(g, 0.0, 0.7), "hp pass {g}");
let g = gain_db(kind::HIGH_PASS, 1000.0, 0.0, 0.707, 250.0);
assert!(g < -20.0, "hp stop {g}");
let g = gain_db(kind::NOTCH, 1000.0, 0.0, 2.0, 1000.0);
assert!(g < -30.0, "notch center {g}");
let g = gain_db(kind::NOTCH, 1000.0, 0.0, 2.0, 4000.0);
assert!(near(g, 0.0, 0.7), "notch far {g}");
}
fn defaults() -> Vec<u32> {
VA.params.iter().map(|p| p.default).collect()
}
fn run(fx: &mut EqFx, d: &[u32]) -> Vec<[f32; 2]> {
let p = Params::new(d);
(0..512)
.map(|i| {
let x = ((i % 29) as f32 / 14.0 - 1.0) * 0.5;
if fx.update(&p, SR) {
fx.process([x, -x], SR)
} else {
[x, -x]
}
})
.collect()
}
#[test]
fn first_band_without_a_kind_is_a_peak() {
let mut d = defaults();
d[param::EQ_GAIN as usize] = 36;
let old = run(&mut EqFx::post(), &d);
d[param::EQ_TYPE as usize] = kind::PEAK;
assert_eq!(run(&mut EqFx::post(), &d), old);
}
#[test]
fn bands_go_to_the_stage_they_ask_for() {
let d = defaults();
let dry = run(&mut EqFx::post(), &d);
let mut d2 = d.clone();
d2[param::EQ3_TYPE as usize] = kind::LOW_PASS;
d2[param::EQ3_FREQ as usize] = 40;
assert_ne!(run(&mut EqFx::post(), &d2), dry);
assert_eq!(run(&mut EqFx::pre(), &d2), dry);
d2[param::EQ3_PRE as usize] = 1;
assert_eq!(run(&mut EqFx::post(), &d2), dry);
assert_ne!(run(&mut EqFx::pre(), &d2), dry);
}
}