| 1 | use crate::audio::SAMPLE_RATE; |
| 2 | |
| 3 | pub const CUTOFF_HZ: f32 = 80.0; |
| 4 | const BUTTERWORTH_FOURTH_ORDER_Q: [f32; 2] = [0.541_196_1, 1.306_563]; |
| 5 | |
| 6 | #[derive(Debug, Clone, Copy)] |
| 7 | struct Biquad { |
| 8 | b0: f32, |
| 9 | b1: f32, |
| 10 | b2: f32, |
| 11 | a1: f32, |
| 12 | a2: f32, |
| 13 | x1: f32, |
| 14 | x2: f32, |
| 15 | y1: f32, |
| 16 | y2: f32, |
| 17 | } |
| 18 | |
| 19 | impl Biquad { |
| 20 | fn high_pass(cutoff_hz: f32, quality: f32) -> Biquad { |
| 21 | let omega = std::f32::consts::TAU * cutoff_hz / SAMPLE_RATE as f32; |
| 22 | let alpha = omega.sin() / (2.0 * quality); |
| 23 | let cosine = omega.cos(); |
| 24 | let a0 = 1.0 + alpha; |
| 25 | Biquad { |
| 26 | b0: (1.0 + cosine) / 2.0 / a0, |
| 27 | b1: -(1.0 + cosine) / a0, |
| 28 | b2: (1.0 + cosine) / 2.0 / a0, |
| 29 | a1: -2.0 * cosine / a0, |
| 30 | a2: (1.0 - alpha) / a0, |
| 31 | x1: 0.0, |
| 32 | x2: 0.0, |
| 33 | y1: 0.0, |
| 34 | y2: 0.0, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn process(&mut self, input: f32) -> f32 { |
| 39 | let output = self.b0 * input + self.b1 * self.x1 + self.b2 * self.x2 |
| 40 | - self.a1 * self.y1 |
| 41 | - self.a2 * self.y2; |
| 42 | self.x2 = self.x1; |
| 43 | self.x1 = input; |
| 44 | self.y2 = self.y1; |
| 45 | self.y1 = output; |
| 46 | output |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Fourth-order Butterworth high-pass for speech; WebRTC's own high-pass only removes rumble below about 80 Hz. |
| 51 | pub struct VoiceHighPass { |
| 52 | sections: [Biquad; 2], |
| 53 | } |
| 54 | |
| 55 | impl VoiceHighPass { |
| 56 | pub fn new() -> VoiceHighPass { |
| 57 | VoiceHighPass { |
| 58 | sections: BUTTERWORTH_FOURTH_ORDER_Q |
| 59 | .map(|quality| Biquad::high_pass(CUTOFF_HZ, quality)), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn process(&mut self, frame: &mut [i16]) { |
| 64 | for sample in frame { |
| 65 | let filtered = self |
| 66 | .sections |
| 67 | .iter_mut() |
| 68 | .fold(f32::from(*sample), |value, section| section.process(value)); |
| 69 | *sample = filtered |
| 70 | .round() |
| 71 | .clamp(f32::from(i16::MIN), f32::from(i16::MAX)) as i16; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl Default for VoiceHighPass { |
| 77 | fn default() -> Self { |
| 78 | VoiceHighPass::new() |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[cfg(test)] |
| 83 | mod tests { |
| 84 | use super::*; |
| 85 | use crate::audio::SineGenerator; |
| 86 | |
| 87 | fn gain_db(frequency: f32) -> f32 { |
| 88 | let mut filter = VoiceHighPass::new(); |
| 89 | let mut generator = SineGenerator::new(frequency, 0.5); |
| 90 | let (mut input_energy, mut output_energy) = (0.0_f64, 0.0_f64); |
| 91 | for frame_index in 0..200 { |
| 92 | let input = generator.next_frame(); |
| 93 | let mut output = input.clone(); |
| 94 | filter.process(&mut output); |
| 95 | if frame_index >= 100 { |
| 96 | input_energy += input |
| 97 | .iter() |
| 98 | .map(|sample| f64::from(*sample).powi(2)) |
| 99 | .sum::<f64>(); |
| 100 | output_energy += output |
| 101 | .iter() |
| 102 | .map(|sample| f64::from(*sample).powi(2)) |
| 103 | .sum::<f64>(); |
| 104 | } |
| 105 | } |
| 106 | (10.0 * (output_energy / input_energy).log10()) as f32 |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn speech_band_passes_and_rumble_is_cut() { |
| 111 | assert!(gain_db(1000.0).abs() < 0.2, "1 kHz {}", gain_db(1000.0)); |
| 112 | assert!(gain_db(200.0) > -1.0, "200 Hz {}", gain_db(200.0)); |
| 113 | assert!( |
| 114 | (gain_db(CUTOFF_HZ) + 3.0).abs() < 0.5, |
| 115 | "cutoff {}", |
| 116 | gain_db(CUTOFF_HZ) |
| 117 | ); |
| 118 | assert!(gain_db(40.0) < -20.0, "40 Hz {}", gain_db(40.0)); |
| 119 | } |
| 120 | } |