| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | use std::f64::consts::PI; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 24 | pub enum BandKind { |
| 25 | LowShelf, |
| 26 | Peak, |
| 27 | HighShelf, |
| 28 | |
| 29 | LowCut, |
| 30 | |
| 31 | HighCut, |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 38 | pub struct Band { |
| 39 | pub freq_hz: f32, |
| 40 | pub gain_db: f32, |
| 41 | |
| 42 | pub q: f32, |
| 43 | pub kind: BandKind, |
| 44 | } |
| 45 | |
| 46 | |
| 47 | #[derive(Debug, Clone, PartialEq, Default)] |
| 48 | pub struct EqParams { |
| 49 | pub bands: Vec<Band>, |
| 50 | } |
| 51 | |
| 52 | impl EqParams { |
| 53 | |
| 54 | pub fn default_layout() -> Self { |
| 55 | let band = |freq_hz, kind| Band { |
| 56 | freq_hz, |
| 57 | gain_db: 0.0, |
| 58 | q: 0.707, |
| 59 | kind, |
| 60 | }; |
| 61 | Self { |
| 62 | bands: vec![ |
| 63 | band(80.0, BandKind::LowShelf), |
| 64 | band(250.0, BandKind::Peak), |
| 65 | band(1000.0, BandKind::Peak), |
| 66 | band(4000.0, BandKind::Peak), |
| 67 | band(12000.0, BandKind::HighShelf), |
| 68 | ], |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | pub fn is_identity(&self) -> bool { |
| 76 | self.bands.iter().all(|b| match b.kind { |
| 77 | BandKind::LowCut | BandKind::HighCut => false, |
| 78 | _ => b.gain_db.abs() < 1e-4, |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | pub fn response_db(&self, freq_hz: f32, sample_rate: u32) -> f32 { |
| 88 | self.bands |
| 89 | .iter() |
| 90 | .map(|b| Coeffs::new(b, sample_rate).magnitude_db(freq_hz, sample_rate)) |
| 91 | .sum() |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | #[derive(Debug, Clone, Copy, Default)] |
| 97 | struct Coeffs { |
| 98 | b0: f32, |
| 99 | b1: f32, |
| 100 | b2: f32, |
| 101 | a1: f32, |
| 102 | a2: f32, |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | pub const MAX_BANDS: usize = 20; |
| 126 | |
| 127 | pub const MAX_BAND_GAIN_DB: f32 = 40.0; |
| 128 | |
| 129 | |
| 130 | pub const MIN_BAND_GAIN_DB: f32 = -40.0; |
| 131 | |
| 132 | impl Coeffs { |
| 133 | |
| 134 | fn new(band: &Band, sample_rate: u32) -> Self { |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | let fs = f64::from(sample_rate.max(1)); |
| 141 | let f0 = f64::from(band.freq_hz).clamp(1.0, fs * 0.49); |
| 142 | let q = f64::from(band.q).clamp(0.05, 40.0); |
| 143 | let gain = |
| 144 | f64::from(band.gain_db).clamp(f64::from(MIN_BAND_GAIN_DB), f64::from(MAX_BAND_GAIN_DB)); |
| 145 | let a = 10f64.powf(gain / 40.0); |
| 146 | let w0 = 2.0 * PI * f0 / fs; |
| 147 | let (sin_w0, cos_w0) = (w0.sin(), w0.cos()); |
| 148 | let alpha = sin_w0 / (2.0 * q); |
| 149 | let two_sqrt_a_alpha = 2.0 * a.sqrt() * alpha; |
| 150 | |
| 151 | let (b0, b1, b2, a0, a1, a2) = match band.kind { |
| 152 | BandKind::Peak => ( |
| 153 | 1.0 + alpha * a, |
| 154 | -2.0 * cos_w0, |
| 155 | 1.0 - alpha * a, |
| 156 | 1.0 + alpha / a, |
| 157 | -2.0 * cos_w0, |
| 158 | 1.0 - alpha / a, |
| 159 | ), |
| 160 | BandKind::LowShelf => ( |
| 161 | a * ((a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha), |
| 162 | 2.0 * a * ((a - 1.0) - (a + 1.0) * cos_w0), |
| 163 | a * ((a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha), |
| 164 | (a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha, |
| 165 | -2.0 * ((a - 1.0) + (a + 1.0) * cos_w0), |
| 166 | (a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha, |
| 167 | ), |
| 168 | BandKind::HighShelf => ( |
| 169 | a * ((a + 1.0) + (a - 1.0) * cos_w0 + two_sqrt_a_alpha), |
| 170 | -2.0 * a * ((a - 1.0) + (a + 1.0) * cos_w0), |
| 171 | a * ((a + 1.0) + (a - 1.0) * cos_w0 - two_sqrt_a_alpha), |
| 172 | (a + 1.0) - (a - 1.0) * cos_w0 + two_sqrt_a_alpha, |
| 173 | 2.0 * ((a - 1.0) - (a + 1.0) * cos_w0), |
| 174 | (a + 1.0) - (a - 1.0) * cos_w0 - two_sqrt_a_alpha, |
| 175 | ), |
| 176 | |
| 177 | BandKind::LowCut => ( |
| 178 | (1.0 + cos_w0) / 2.0, |
| 179 | -(1.0 + cos_w0), |
| 180 | (1.0 + cos_w0) / 2.0, |
| 181 | 1.0 + alpha, |
| 182 | -2.0 * cos_w0, |
| 183 | 1.0 - alpha, |
| 184 | ), |
| 185 | BandKind::HighCut => ( |
| 186 | (1.0 - cos_w0) / 2.0, |
| 187 | 1.0 - cos_w0, |
| 188 | (1.0 - cos_w0) / 2.0, |
| 189 | 1.0 + alpha, |
| 190 | -2.0 * cos_w0, |
| 191 | 1.0 - alpha, |
| 192 | ), |
| 193 | }; |
| 194 | Self { |
| 195 | b0: (b0 / a0) as f32, |
| 196 | b1: (b1 / a0) as f32, |
| 197 | b2: (b2 / a0) as f32, |
| 198 | a1: (a1 / a0) as f32, |
| 199 | a2: (a2 / a0) as f32, |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | fn magnitude_db(&self, freq_hz: f32, sample_rate: u32) -> f32 { |
| 206 | let fs = f64::from(sample_rate.max(1)); |
| 207 | let w = 2.0 * PI * f64::from(freq_hz).clamp(0.0, fs * 0.5) / fs; |
| 208 | let (sin1, cos1) = w.sin_cos(); |
| 209 | let (sin2, cos2) = (2.0 * w).sin_cos(); |
| 210 | let (b0, b1, b2) = (f64::from(self.b0), f64::from(self.b1), f64::from(self.b2)); |
| 211 | let (a1, a2) = (f64::from(self.a1), f64::from(self.a2)); |
| 212 | let num = ((b0 + b1 * cos1 + b2 * cos2).powi(2) + (b1 * sin1 + b2 * sin2).powi(2)).sqrt(); |
| 213 | |
| 214 | |
| 215 | |
| 216 | let den = ((1.0 + a1 * cos1 + a2 * cos2).powi(2) + (a1 * sin1 + a2 * sin2).powi(2)) |
| 217 | .sqrt() |
| 218 | .max(1e-12); |
| 219 | (20.0 * (num / den).log10()) as f32 |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | const DENORMAL_FLOOR: f32 = 1e-20; |
| 227 | |
| 228 | |
| 229 | |
| 230 | pub struct EqState { |
| 231 | sample_rate: u32, |
| 232 | channels: usize, |
| 233 | coeffs: Vec<Coeffs>, |
| 234 | |
| 235 | state: Vec<[f32; 2]>, |
| 236 | |
| 237 | |
| 238 | identity: bool, |
| 239 | } |
| 240 | |
| 241 | impl EqState { |
| 242 | |
| 243 | |
| 244 | pub fn new(params: &EqParams, sample_rate: u32, channels: u16) -> Self { |
| 245 | let mut eq = Self { |
| 246 | sample_rate, |
| 247 | channels: (channels as usize).max(1), |
| 248 | coeffs: Vec::new(), |
| 249 | state: Vec::new(), |
| 250 | identity: true, |
| 251 | }; |
| 252 | eq.set_params(params); |
| 253 | eq |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | pub fn set_params(&mut self, params: &EqParams) { |
| 259 | self.identity = params.is_identity(); |
| 260 | self.coeffs.clear(); |
| 261 | self.coeffs.extend( |
| 262 | params |
| 263 | .bands |
| 264 | .iter() |
| 265 | .map(|b| Coeffs::new(b, self.sample_rate)), |
| 266 | ); |
| 267 | self.state |
| 268 | .resize(self.coeffs.len() * self.channels, [0.0; 2]); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | pub fn reset(&mut self) { |
| 274 | self.state.fill([0.0; 2]); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | pub fn process(&mut self, interleaved: &mut [f32]) { |
| 283 | if self.identity || self.coeffs.is_empty() { |
| 284 | return; |
| 285 | } |
| 286 | let channels = self.channels; |
| 287 | for (band, c) in self.coeffs.iter().enumerate() { |
| 288 | let state = &mut self.state[band * channels..][..channels]; |
| 289 | for frame in interleaved.chunks_exact_mut(channels) { |
| 290 | for (ch, x) in frame.iter_mut().enumerate() { |
| 291 | let s = &mut state[ch]; |
| 292 | let y = c.b0 * *x + s[0]; |
| 293 | s[0] = c.b1 * *x - c.a1 * y + s[1]; |
| 294 | s[1] = c.b2 * *x - c.a2 * y; |
| 295 | if s[0].abs() < DENORMAL_FLOOR { |
| 296 | s[0] = 0.0; |
| 297 | } |
| 298 | if s[1].abs() < DENORMAL_FLOOR { |
| 299 | s[1] = 0.0; |
| 300 | } |
| 301 | *x = y; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #[cfg(test)] |
| 309 | mod tests { |
| 310 | use std::f32::consts::TAU; |
| 311 | |
| 312 | use super::{Band, BandKind, EqParams, EqState}; |
| 313 | |
| 314 | const FS: u32 = 48_000; |
| 315 | |
| 316 | fn peak(freq_hz: f32, gain_db: f32, q: f32) -> EqParams { |
| 317 | EqParams { |
| 318 | bands: vec![Band { |
| 319 | freq_hz, |
| 320 | gain_db, |
| 321 | q, |
| 322 | kind: BandKind::Peak, |
| 323 | }], |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | fn sine(freq_hz: f32, frames: usize, channels: usize) -> Vec<f32> { |
| 328 | (0..frames * channels) |
| 329 | .map(|i| (TAU * freq_hz * (i / channels) as f32 / FS as f32).sin()) |
| 330 | .collect() |
| 331 | } |
| 332 | |
| 333 | |
| 334 | fn rms(samples: &[f32]) -> f32 { |
| 335 | let tail = &samples[samples.len() / 2..]; |
| 336 | (tail.iter().map(|v| v * v).sum::<f32>() / tail.len() as f32).sqrt() |
| 337 | } |
| 338 | |
| 339 | fn db(measured: f32, reference: f32) -> f32 { |
| 340 | 20.0 * (measured / reference).log10() |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn flat_params_pass_samples_through_bit_identical() { |
| 345 | let input = sine(440.0, 4800, 2); |
| 346 | let mut out = input.clone(); |
| 347 | EqState::new(&EqParams::default_layout(), FS, 2).process(&mut out); |
| 348 | assert_eq!(out, input, "a flat EQ must not touch the samples at all"); |
| 349 | |
| 350 | |
| 351 | |
| 352 | let mut params = EqParams::default_layout(); |
| 353 | params.bands[2].q = 4.0; |
| 354 | assert!(params.is_identity()); |
| 355 | } |
| 356 | |
| 357 | #[test] |
| 358 | fn a_band_moves_its_own_center_and_leaves_two_octaves_away_alone() { |
| 359 | for gain in [6.0, -6.0] { |
| 360 | let center = sine(1000.0, 48_000, 1); |
| 361 | let mut out = center.clone(); |
| 362 | EqState::new(&peak(1000.0, gain, 1.0), FS, 1).process(&mut out); |
| 363 | let at_center = db(rms(&out), rms(¢er)); |
| 364 | assert!( |
| 365 | (at_center - gain).abs() < 0.5, |
| 366 | "1 kHz sine through a {gain:+} dB band at 1 kHz measured {at_center:+.2} dB" |
| 367 | ); |
| 368 | |
| 369 | for off in [250.0, 4000.0] { |
| 370 | let away = sine(off, 48_000, 1); |
| 371 | let mut out = away.clone(); |
| 372 | EqState::new(&peak(1000.0, gain, 1.0), FS, 1).process(&mut out); |
| 373 | let leak = db(rms(&out), rms(&away)); |
| 374 | assert!( |
| 375 | leak.abs() < 1.0, |
| 376 | "{off} Hz is two octaves off but moved {leak:+.2} dB" |
| 377 | ); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | #[test] |
| 386 | fn the_drawn_response_is_the_gain_the_samples_actually_get() { |
| 387 | let mut params = EqParams::default_layout(); |
| 388 | params.bands[0].gain_db = -8.0; |
| 389 | params.bands[2].gain_db = 10.0; |
| 390 | params.bands[2].q = 1.4; |
| 391 | params.bands[4].gain_db = 5.0; |
| 392 | |
| 393 | |
| 394 | |
| 395 | for freq in [ |
| 396 | 40.0, 80.0, 250.0, 600.0, 1000.0, 2000.0, 4000.0, 12000.0, 16000.0, |
| 397 | ] { |
| 398 | let input = sine(freq, 48_000, 1); |
| 399 | let mut out = input.clone(); |
| 400 | EqState::new(¶ms, FS, 1).process(&mut out); |
| 401 | let measured = db(rms(&out), rms(&input)); |
| 402 | let drawn = params.response_db(freq, FS); |
| 403 | assert!( |
| 404 | (measured - drawn).abs() < 0.5, |
| 405 | "{freq} Hz: curve says {drawn:+.2} dB, samples measured {measured:+.2} dB" |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | let flat = EqParams::default_layout(); |
| 411 | assert!(flat.response_db(1000.0, FS).abs() < 1e-4); |
| 412 | assert!(params.response_db(FS as f32, FS).is_finite()); |
| 413 | } |
| 414 | |
| 415 | #[test] |
| 416 | fn ten_seconds_of_noise_through_every_band_boosted_stays_bounded() { |
| 417 | let mut params = EqParams::default_layout(); |
| 418 | for band in &mut params.bands { |
| 419 | band.gain_db = 12.0; |
| 420 | } |
| 421 | let mut eq = EqState::new(¶ms, FS, 2); |
| 422 | |
| 423 | |
| 424 | let mut seed = 0x2545_f491_4f6c_dd1du64; |
| 425 | let mut noise = || { |
| 426 | seed ^= seed << 13; |
| 427 | seed ^= seed >> 7; |
| 428 | seed ^= seed << 17; |
| 429 | (seed >> 40) as f32 / 8_388_608.0 - 1.0 |
| 430 | }; |
| 431 | |
| 432 | let mut peak = 0.0f32; |
| 433 | |
| 434 | |
| 435 | for _ in 0..(10 * FS as usize / 1024) { |
| 436 | let mut block: Vec<f32> = (0..1024 * 2).map(|_| noise()).collect(); |
| 437 | eq.process(&mut block); |
| 438 | for v in block { |
| 439 | assert!(v.is_finite(), "EQ output went to {v}"); |
| 440 | peak = peak.max(v.abs()); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | |
| 445 | assert!( |
| 446 | (1.0..20.0).contains(&peak), |
| 447 | "peak {peak} out of the plausible band for +12 dB x5" |
| 448 | ); |
| 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn a_decaying_tail_leaves_the_state_at_a_hard_zero() { |
| 453 | let mut params = EqParams::default_layout(); |
| 454 | params.bands[1].gain_db = 9.0; |
| 455 | let mut eq = EqState::new(¶ms, FS, 2); |
| 456 | |
| 457 | let mut decay: Vec<f32> = (0..4800 * 2) |
| 458 | .map(|i| 0.9f32.powi(i / 2) * if i % 2 == 0 { 1.0 } else { -1.0 }) |
| 459 | .collect(); |
| 460 | eq.process(&mut decay); |
| 461 | let mut silence = vec![0.0f32; 4800 * 2]; |
| 462 | eq.process(&mut silence); |
| 463 | |
| 464 | for (i, s) in eq.state.iter().enumerate() { |
| 465 | assert!( |
| 466 | s[0] == 0.0 && s[1] == 0.0, |
| 467 | "state {i} still holds {s:?} after a decayed tail — denormal territory" |
| 468 | ); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn an_impulse_in_the_left_channel_never_reaches_the_right() { |
| 474 | let mut params = EqParams::default_layout(); |
| 475 | params.bands[3].gain_db = -10.0; |
| 476 | let mut eq = EqState::new(¶ms, FS, 2); |
| 477 | |
| 478 | let mut buf = vec![0.0f32; 512 * 2]; |
| 479 | buf[0] = 1.0; |
| 480 | eq.process(&mut buf); |
| 481 | |
| 482 | assert!(buf[2] != 0.0, "the left channel should be ringing"); |
| 483 | for (frame, pair) in buf.as_chunks::<2>().0.iter().enumerate() { |
| 484 | assert_eq!( |
| 485 | pair[1], 0.0, |
| 486 | "frame {frame}: right channel picked up {pair:?}" |
| 487 | ); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | #[test] |
| 509 | fn a_gain_a_file_can_name_leaves_the_poles_strictly_inside_the_unit_circle() { |
| 510 | for gain in [ |
| 511 | 12.0f32, 40.0, 100.0, 200.0, 400.0, 10_000.0, -200.0, -10_000.0, |
| 512 | ] { |
| 513 | let c = super::Coeffs::new( |
| 514 | &Band { |
| 515 | freq_hz: 1000.0, |
| 516 | gain_db: gain, |
| 517 | q: 1.0, |
| 518 | kind: BandKind::Peak, |
| 519 | }, |
| 520 | FS, |
| 521 | ); |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | let (a1, a2) = (c.a1 as f64, c.a2 as f64); |
| 527 | let disc = (a1 * a1 - 4.0 * a2).max(0.0).sqrt(); |
| 528 | let radius = [(-a1 + disc) / 2.0, (-a1 - disc) / 2.0] |
| 529 | .into_iter() |
| 530 | .fold(0.0f64, |m, r| m.max(r.abs())); |
| 531 | assert!( |
| 532 | radius < 0.999, |
| 533 | "gain {gain} dB gives a pole radius of {radius:.9} -- \ |
| 534 | the section rings instead of decaying" |
| 535 | ); |
| 536 | assert!( |
| 537 | c.b0.is_finite() && c.b1.is_finite() && c.b2.is_finite(), |
| 538 | "gain {gain} dB produced non-finite coefficients {c:?}" |
| 539 | ); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | #[test] |
| 551 | fn a_gain_a_file_can_name_does_not_blow_the_samples_up() { |
| 552 | for gain in [40.0f32, 100.0, 200.0, 400.0, 10_000.0] { |
| 553 | let mut out = vec![0.0f32; 48_000 * 2]; |
| 554 | |
| 555 | |
| 556 | for (i, o) in out.iter_mut().enumerate().take(4_800 * 2) { |
| 557 | *o = (TAU * 1000.0 * (i / 2) as f32 / FS as f32).sin(); |
| 558 | } |
| 559 | EqState::new(&peak(1000.0, gain, 1.0), FS, 2).process(&mut out); |
| 560 | let peak = out.iter().fold(0.0f32, |m, v| m.max(v.abs())); |
| 561 | assert!( |
| 562 | peak.is_finite() && peak < 200.0, |
| 563 | "gain {gain} dB sent a full-scale burst to {peak}: the clamp ceiling is \ |
| 564 | +40 dB, a linear gain of 100, and 200x that is a section ringing, not a band" |
| 565 | ); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | fn cut(kind: BandKind, freq_hz: f32) -> EqParams { |
| 570 | EqParams { |
| 571 | bands: vec![Band { |
| 572 | freq_hz, |
| 573 | gain_db: 0.0, |
| 574 | q: 0.707, |
| 575 | kind, |
| 576 | }], |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | #[test] |
| 584 | fn a_cut_attenuates_past_its_corner_and_passes_the_other_side() { |
| 585 | let low = cut(BandKind::LowCut, 1000.0); |
| 586 | assert!( |
| 587 | low.response_db(100.0, FS) < -30.0, |
| 588 | "high-pass at 1 kHz, 100 Hz" |
| 589 | ); |
| 590 | assert!( |
| 591 | low.response_db(10_000.0, FS).abs() < 0.1, |
| 592 | "high-pass passes 10 kHz" |
| 593 | ); |
| 594 | |
| 595 | let high = cut(BandKind::HighCut, 1000.0); |
| 596 | assert!( |
| 597 | high.response_db(10_000.0, FS) < -30.0, |
| 598 | "low-pass at 1 kHz, 10 kHz" |
| 599 | ); |
| 600 | assert!( |
| 601 | high.response_db(100.0, FS).abs() < 0.1, |
| 602 | "low-pass passes 100 Hz" |
| 603 | ); |
| 604 | |
| 605 | |
| 606 | for kind in [BandKind::LowCut, BandKind::HighCut] { |
| 607 | let at_corner = cut(kind, 1000.0).response_db(1000.0, FS); |
| 608 | assert!( |
| 609 | (at_corner + 3.0).abs() < 0.2, |
| 610 | "{kind:?} at its corner measured {at_corner:+.2} dB, want -3" |
| 611 | ); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | |
| 616 | |
| 617 | #[test] |
| 618 | fn a_cut_removes_the_tone_it_names_from_the_samples() { |
| 619 | for (kind, freq, want_below) in [ |
| 620 | (BandKind::LowCut, 50.0, -30.0), |
| 621 | (BandKind::HighCut, 10_000.0, -30.0), |
| 622 | ] { |
| 623 | let input = sine(freq, 48_000, 1); |
| 624 | let mut out = input.clone(); |
| 625 | EqState::new(&cut(kind, 1000.0), FS, 1).process(&mut out); |
| 626 | let measured = db(rms(&out), rms(&input)); |
| 627 | assert!( |
| 628 | measured < want_below, |
| 629 | "{kind:?} at 1 kHz left {freq} Hz at {measured:+.2} dB" |
| 630 | ); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | #[test] |
| 637 | fn a_cut_ignores_its_gain() { |
| 638 | for kind in [BandKind::LowCut, BandKind::HighCut] { |
| 639 | let flat = cut(kind, 1000.0); |
| 640 | let mut loud = flat.clone(); |
| 641 | loud.bands[0].gain_db = 12.0; |
| 642 | let mut quiet = flat.clone(); |
| 643 | quiet.bands[0].gain_db = -12.0; |
| 644 | for freq in [50.0, 300.0, 1000.0, 3000.0, 15_000.0] { |
| 645 | let want = flat.response_db(freq, FS); |
| 646 | assert_eq!(loud.response_db(freq, FS), want, "{kind:?} at {freq} Hz"); |
| 647 | assert_eq!(quiet.response_db(freq, FS), want, "{kind:?} at {freq} Hz"); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |
| 654 | #[test] |
| 655 | fn a_cut_band_is_never_identity() { |
| 656 | for kind in [BandKind::LowCut, BandKind::HighCut] { |
| 657 | let params = cut(kind, 1000.0); |
| 658 | assert!(!params.is_identity(), "{kind:?} at 0 dB reads as flat"); |
| 659 | let input = sine(50.0, 4800, 1); |
| 660 | let mut out = input.clone(); |
| 661 | EqState::new(¶ms, FS, 1).process(&mut out); |
| 662 | assert_ne!(out, input, "{kind:?} left the samples untouched"); |
| 663 | } |
| 664 | assert!(peak(1000.0, 0.0, 1.0).is_identity()); |
| 665 | } |
| 666 | } |