use super::biquad::Coeffs;
use crate::kit::m;
use core::f32::consts::PI;
pub const MIN_HZ: f32 = 10.0;
pub const MAX_HZ_RATIO: f32 = 0.45;
pub const Q_RANGE: (f32, f32) = (0.1, 18.0);
pub const GAIN_DB_RANGE: (f32, f32) = (-24.0, 24.0);
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Band {
LowCut1 { hz: f32 },
LowCut { hz: f32, q: f32 },
HighCut1 { hz: f32 },
HighCut { hz: f32, q: f32 },
LowShelf { hz: f32, gain_db: f32 },
HighShelf { hz: f32, gain_db: f32 },
Peak { hz: f32, gain_db: f32, q: f32 },
}
impl Band {
fn is_finite(&self) -> bool {
match *self {
Band::LowCut1 { hz } | Band::HighCut1 { hz } => hz.is_finite(),
Band::LowCut { hz, q } | Band::HighCut { hz, q } => hz.is_finite() && q.is_finite(),
Band::LowShelf { hz, gain_db } | Band::HighShelf { hz, gain_db } => {
hz.is_finite() && gain_db.is_finite()
}
Band::Peak { hz, gain_db, q } => hz.is_finite() && gain_db.is_finite() && q.is_finite(),
}
}
pub(super) fn coeffs(&self, sample_rate: u32) -> Coeffs {
if sample_rate == 0 || !self.is_finite() {
return Coeffs::IDENTITY;
}
let fs = sample_rate as f32;
let c = match *self {
Band::LowCut1 { hz } => first_order(hz, fs, false),
Band::HighCut1 { hz } => first_order(hz, fs, true),
Band::LowCut { hz, q } => second_order(hz, q, fs, false),
Band::HighCut { hz, q } => second_order(hz, q, fs, true),
Band::LowShelf { hz, gain_db } => shelf(hz, gain_db, fs, false),
Band::HighShelf { hz, gain_db } => shelf(hz, gain_db, fs, true),
Band::Peak { hz, gain_db, q } => peak(hz, gain_db, q, fs),
};
if c.is_finite() {
c
} else {
Coeffs::IDENTITY
}
}
}
fn clamp_hz(hz: f32, fs: f32) -> f32 {
hz.max(MIN_HZ).min(fs * MAX_HZ_RATIO)
}
fn clamp_q(q: f32) -> f32 {
q.clamp(Q_RANGE.0, Q_RANGE.1)
}
fn shelf_a(gain_db: f32) -> f32 {
m::powf(10.0, gain_db.clamp(GAIN_DB_RANGE.0, GAIN_DB_RANGE.1) / 40.0)
}
fn first_order(hz: f32, fs: f32, lowpass: bool) -> Coeffs {
let k = m::tan(PI * clamp_hz(hz, fs) / fs);
let a1 = (k - 1.0) / (k + 1.0);
let (b0, b1) = if lowpass {
let b = k / (1.0 + k);
(b, b)
} else {
let b = 1.0 / (1.0 + k);
(b, -b)
};
Coeffs {
b0,
b1,
b2: 0.0,
a1,
a2: 0.0,
}
}
fn w0(hz: f32, fs: f32) -> (f32, f32) {
let w = 2.0 * PI * clamp_hz(hz, fs) / fs;
(m::cos(w), m::sin(w))
}
fn normalize(b0: f32, b1: f32, b2: f32, a0: f32, a1: f32, a2: f32) -> Coeffs {
Coeffs {
b0: b0 / a0,
b1: b1 / a0,
b2: b2 / a0,
a1: a1 / a0,
a2: a2 / a0,
}
}
fn second_order(hz: f32, q: f32, fs: f32, lowpass: bool) -> Coeffs {
let (cos, sin) = w0(hz, fs);
let alpha = sin / (2.0 * clamp_q(q));
let (b0, b1) = if lowpass {
((1.0 - cos) / 2.0, 1.0 - cos)
} else {
((1.0 + cos) / 2.0, -(1.0 + cos))
};
normalize(b0, b1, b0, 1.0 + alpha, -2.0 * cos, 1.0 - alpha)
}
fn shelf(hz: f32, gain_db: f32, fs: f32, high: bool) -> Coeffs {
let a = shelf_a(gain_db);
let (cos, sin) = w0(hz, fs);
let alpha = sin / 2.0 * core::f32::consts::SQRT_2;
let two_sqrt_a_alpha = 2.0 * m::powf(a, 0.5) * alpha;
let (ap, am) = (a + 1.0, a - 1.0);
if high {
normalize(
a * (ap + am * cos + two_sqrt_a_alpha),
-2.0 * a * (am + ap * cos),
a * (ap + am * cos - two_sqrt_a_alpha),
ap - am * cos + two_sqrt_a_alpha,
2.0 * (am - ap * cos),
ap - am * cos - two_sqrt_a_alpha,
)
} else {
normalize(
a * (ap - am * cos + two_sqrt_a_alpha),
2.0 * a * (am - ap * cos),
a * (ap - am * cos - two_sqrt_a_alpha),
ap + am * cos + two_sqrt_a_alpha,
-2.0 * (am + ap * cos),
ap + am * cos - two_sqrt_a_alpha,
)
}
}
fn peak(hz: f32, gain_db: f32, q: f32, fs: f32) -> Coeffs {
let a = shelf_a(gain_db);
let (cos, sin) = w0(hz, fs);
let alpha = sin / (2.0 * clamp_q(q));
normalize(
1.0 + alpha * a,
-2.0 * cos,
1.0 - alpha * a,
1.0 + alpha / a,
-2.0 * cos,
1.0 - alpha / a,
)
}
#[cfg(test)]
mod tests {
use super::{Band, Coeffs};
#[test]
fn nan_and_zero_rate_give_identity() {
assert_eq!(
Band::Peak {
hz: f32::NAN,
gain_db: 6.0,
q: 1.0
}
.coeffs(48_000),
Coeffs::IDENTITY
);
assert_eq!(Band::LowCut1 { hz: 100.0 }.coeffs(0), Coeffs::IDENTITY);
}
#[test]
fn zero_gain_peak_and_shelf_are_flat() {
for b in [
Band::Peak {
hz: 1000.0,
gain_db: 0.0,
q: 1.0,
},
Band::LowShelf {
hz: 200.0,
gain_db: 0.0,
},
Band::HighShelf {
hz: 5000.0,
gain_db: 0.0,
},
] {
let c = b.coeffs(48_000);
assert!((c.b0 - 1.0).abs() < 1e-5, "{b:?} {c:?}");
assert!((c.b1 - c.a1).abs() < 1e-5, "{b:?} {c:?}");
assert!((c.b2 - c.a2).abs() < 1e-5, "{b:?} {c:?}");
}
}
}