BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / mic_chain.rs

mic_chain.rs

mic_chain.rs 4.2 KiB Download Raw
1use std::time::Instant;
2
3use libwebrtc::native::apm::AudioProcessingModule;
4
5use crate::audio::SAMPLE_RATE;
6use crate::gain_stage::GainStage;
7use crate::mic_processing::ApmConfig;
8use crate::noise_stage::NoiseStage;
9use crate::voice_high_pass::VoiceHighPass;
10use crate::wav_dump::StageDumps;
11
12/// DeepFilterNet drops quiet laptop-mic speech as noise and takes about 6 dB off reverberant speech,
13/// so one AGC2 brings the level up before it and a second one restores the level after it.
14pub struct MicChain {
15 processing: ApmConfig,
16 high_pass: Option<VoiceHighPass>,
17 front: Option<AudioProcessingModule>,
18 ml_noise: Option<NoiseStage>,
19 lift: Option<GainStage>,
20 output_gain: Option<GainStage>,
21 dumps: Option<StageDumps>,
22}
23
24impl MicChain {
25 pub fn new(processing: ApmConfig, ml_noise: bool, dumps: Option<StageDumps>) -> MicChain {
26 let ml_wanted = ml_noise;
27 let ml_noise = ml_wanted && !crate::noise_stage::overloaded();
28 if ml_noise {
29 crate::noise_stage::prepare();
30 }
31 let processing = ApmConfig {
32 noise_suppression: processing.noise_suppression || (ml_wanted && !ml_noise),
33 ..processing
34 };
35 MicChain {
36 processing,
37 high_pass: processing.high_pass_filter.then(VoiceHighPass::new),
38 front: processing.front_module(),
39 ml_noise: ml_noise.then(NoiseStage::new),
40 lift: (processing.gain_controller && ml_noise).then(GainStage::lift),
41 output_gain: processing.gain_controller.then(GainStage::output),
42 dumps,
43 }
44 }
45
46 pub fn process_far_end(&mut self, frame: &mut [i16]) {
47 self.dump(0, frame);
48 if let Some(front) = self.front.as_mut() {
49 let _ = front.process_reverse_stream(frame, SAMPLE_RATE as i32, 1);
50 }
51 }
52
53 pub fn process(&mut self, frame: &mut [i16]) {
54 self.dump(1, frame);
55 if let Some(high_pass) = self.high_pass.as_mut() {
56 high_pass.process(frame);
57 }
58 if let Some(front) = self.front.as_mut() {
59 let _ = front.set_stream_delay_ms(40);
60 let _ = front.process_stream(frame, SAMPLE_RATE as i32, 1);
61 }
62 self.dump(2, frame);
63 if let Some(lift) = self.lift.as_mut() {
64 lift.process(frame);
65 }
66 self.dump(3, frame);
67 if let Some(noise_stage) = self.ml_noise.as_mut() {
68 let started = Instant::now();
69 noise_stage.process_pcm(0, frame);
70 if noise_stage.over_budget(started.elapsed()) {
71 self.fall_back_to_webrtc_noise_suppression();
72 }
73 }
74 if let Some(output_gain) = self.output_gain.as_mut() {
75 output_gain.process(frame);
76 }
77 self.dump(4, frame);
78 }
79
80 fn fall_back_to_webrtc_noise_suppression(&mut self) {
81 self.ml_noise = None;
82 self.lift = None;
83 self.processing.noise_suppression = true;
84 self.front = self.processing.front_module();
85 }
86
87 fn dump(&mut self, stage_index: usize, frame: &[i16]) {
88 if let Some(dumps) = self.dumps.as_mut() {
89 dumps.write(stage_index, frame);
90 }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::audio::SineGenerator;
98 use crate::mic_processing::MicSettings;
99
100 #[test]
101 fn a_slow_deep_filter_hands_over_to_webrtc_noise_suppression() {
102 let auto = MicSettings::default();
103 let mut chain = MicChain::new(auto.apm_config(), true, None);
104 chain.fall_back_to_webrtc_noise_suppression();
105 assert!(chain.ml_noise.is_none());
106 assert!(chain.processing.noise_suppression);
107 assert!(chain.front.is_some());
108 }
109
110 #[test]
111 fn off_leaves_the_frame_untouched() {
112 let off = MicSettings {
113 noise_suppression: crate::mic_processing::NoiseSuppression::Off,
114 music_mode: false,
115 };
116 let mut chain = MicChain::new(off.apm_config(), off.ml_noise_suppression(), None);
117 let mut generator = SineGenerator::new(300.0, 0.2);
118 let original = generator.next_frame();
119 let mut frame = original.clone();
120 chain.process(&mut frame);
121 assert_eq!(frame, original);
122 }
123}