use crate::synth::va::dsp::DelayLine;
use crate::synth::va::m;
const LINES: [usize; 8] = [1557, 1801, 2053, 2311, 2557, 2803, 3061, 3323];
const DIFFUSERS: [(usize, f32); 4] = [(142, 0.75), (107, 0.75), (379, 0.625), (277, 0.625)];
const MOD_DEPTH: f32 = 6.0;
const MOD_RATES: [f32; 8] = [0.31, 0.37, 0.43, 0.53, 0.59, 0.67, 0.73, 0.83];
const MOD_STEP: u32 = 16;
const INPUT_GAIN: f32 = 0.35;
const OUTPUT_GAIN: f32 = 0.6;
const SIGN_L: [f32; 8] = [1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0];
const SIGN_R: [f32; 8] = [1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0];
pub fn hall_rt60(size: f32) -> f32 {
1.2 + 4.8 * size.clamp(0.0, 1.0)
}
pub const HALL_LONGEST_SECS: f32 = (3323.0 + MOD_DEPTH + 2.0) / 44_100.0;
struct Diffuser {
line: DelayLine,
gain: f32,
}
impl Diffuser {
fn process(&mut self, x: f32) -> f32 {
let n = self.line.len();
let delayed = self.line.tap(n - 1);
let v = x + self.gain * delayed;
self.line.push(v);
delayed - self.gain * v
}
}
pub struct Hall {
lines: [DelayLine; 8],
base: [f32; 8],
damp: [f32; 8],
diffusers: [Diffuser; 4],
mod_a: [f32; 8],
mod_b: [f32; 8],
mod_block: u64,
sample_rate: f32,
gains: [f32; 8],
gains_size: f32,
coef: f32,
coef_damp: f32,
}
impl Hall {
pub fn new(sample_rate: u32) -> Self {
let scale = sample_rate as f32 / 44_100.0;
let depth = MOD_DEPTH * scale;
let lines = LINES.map(|t| DelayLine::new((t as f32 * scale + depth) as usize + 4));
let base = LINES.map(|t| t as f32 * scale);
let diffusers = DIFFUSERS.map(|(t, gain)| Diffuser {
line: DelayLine::new(((t as f32 * scale) as usize).max(1)),
gain,
});
Hall {
lines,
base,
damp: [0.0; 8],
diffusers,
mod_a: [0.0; 8],
mod_b: [0.0; 8],
mod_block: u64::MAX,
sample_rate: sample_rate as f32,
gains: [0.0; 8],
gains_size: -1.0,
coef: 0.0,
coef_damp: -1.0,
}
}
fn update(&mut self, size: f32, damp: f32) {
if size != self.gains_size {
let rt60 = hall_rt60(size) * self.sample_rate;
for (g, &len) in self.gains.iter_mut().zip(&self.base) {
*g = m::powf(10.0, -3.0 * len / rt60);
}
self.gains_size = size;
}
if damp != self.coef_damp {
let hz = 1000.0 * m::powf(16.0, 1.0 - damp.clamp(0.0, 1.0));
self.coef = 1.0 - m::exp(-core::f32::consts::TAU * hz / self.sample_rate);
self.coef_damp = damp;
}
}
fn mod_at(&self, i: usize, n: u64) -> f32 {
let depth = MOD_DEPTH * self.sample_rate / 44_100.0;
let x = MOD_RATES[i] as f64 * n as f64 / self.sample_rate as f64 + i as f64 / 8.0;
let phase = (x - (x as u64) as f64) as f32;
depth * m::sin(core::f32::consts::TAU * phase)
}
fn modulation(&mut self, i: usize, t: u64) -> f32 {
let block = t / MOD_STEP as u64;
if block != self.mod_block {
let n = block * MOD_STEP as u64;
for j in 0..8 {
self.mod_a[j] = self.mod_at(j, n);
self.mod_b[j] = self.mod_at(j, n + MOD_STEP as u64);
}
self.mod_block = block;
}
let frac = (t % MOD_STEP as u64) as f32 / MOD_STEP as f32;
self.mod_a[i] + (self.mod_b[i] - self.mod_a[i]) * frac
}
pub fn process(&mut self, input: f32, size: f32, damp: f32, t: u64) -> [f32; 2] {
self.update(size, damp);
let mut x = input * INPUT_GAIN;
for d in &mut self.diffusers {
x = d.process(x);
}
let mut out = [0.0f32; 8];
for (i, o) in out.iter_mut().enumerate() {
let d = self.modulation(i, t);
let y = self.lines[i].tap_frac(self.base[i] + d);
self.damp[i] += self.coef * (y - self.damp[i]);
*o = self.damp[i] * self.gains[i];
}
let sum: f32 = out.iter().sum::<f32>() * (2.0 / 8.0);
for (line, &o) in self.lines.iter_mut().zip(&out) {
line.push(o - sum + x);
}
let mut l = 0.0;
let mut r = 0.0;
for i in 0..8 {
l += SIGN_L[i] * out[i];
r += SIGN_R[i] * out[i];
}
[l * OUTPUT_GAIN, r * OUTPUT_GAIN]
}
pub(crate) fn regions(&self, f: &mut dyn FnMut(&[f32])) {
for l in &self.lines {
f(l.raw());
}
for d in &self.diffusers {
f(d.line.raw());
}
f(&self.damp);
}
pub fn clear(&mut self) {
for l in &mut self.lines {
l.clear();
}
for d in &mut self.diffusers {
d.line.clear();
}
self.damp = [0.0; 8];
}
}