fix(calling): Windows mic chain falls back to WebRTC noise suppression when DeepFilterNet runs slow
- Attenuation limit back to 30 dB; the 40 dB from the hook rework also hit the Windows chain and swallowed speech - MicChain times DeepFilterNet like the Linux hook: 5 frames over 6 ms in a row switch it off for the call and turn on WebRTC NS; a restarted chain in the same call starts without it - Timeline records the fallback at hangup Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -1,3 +1,5 @@
1
use std::time::Instant;
2
1
3
use libwebrtc::native::apm::AudioProcessingModule;
2
4
3
5
use crate::audio::SAMPLE_RATE;
@@ -10,6 +12,7 @@ use crate::wav_dump::StageDumps;
10
12
/// DeepFilterNet drops quiet laptop-mic speech as noise and takes about 6 dB off reverberant speech,
11
13
/// so one AGC2 brings the level up before it and a second one restores the level after it.
12
14
pub struct MicChain {
15
processing: ApmConfig,
13
16
high_pass: Option<VoiceHighPass>,
14
17
front: Option<AudioProcessingModule>,
15
18
ml_noise: Option<NoiseStage>,
@@ -20,10 +23,17 @@ pub struct MicChain {
20
23
21
24
impl MicChain {
22
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();
23
28
if ml_noise {
24
29
crate::noise_stage::prepare();
25
30
}
31
let processing = ApmConfig {
32
noise_suppression: processing.noise_suppression || (ml_wanted && !ml_noise),
33
..processing
34
};
26
35
MicChain {
36
processing,
27
37
high_pass: processing.high_pass_filter.then(VoiceHighPass::new),
28
38
front: processing.front_module(),
29
39
ml_noise: ml_noise.then(NoiseStage::new),
@@ -54,7 +64,11 @@ impl MicChain {
54
64
}
55
65
self.dump(3, frame);
56
66
if let Some(noise_stage) = self.ml_noise.as_mut() {
67
let started = Instant::now();
57
68
noise_stage.process_pcm(0, frame);
69
if noise_stage.over_budget(started.elapsed()) {
70
self.fall_back_to_webrtc_noise_suppression();
71
}
58
72
}
59
73
if let Some(output_gain) = self.output_gain.as_mut() {
60
74
output_gain.process(frame);
@@ -62,6 +76,13 @@ impl MicChain {
62
76
self.dump(4, frame);
63
77
}
64
78
79
fn fall_back_to_webrtc_noise_suppression(&mut self) {
80
self.ml_noise = None;
81
self.lift = None;
82
self.processing.noise_suppression = true;
83
self.front = self.processing.front_module();
84
}
85
65
86
fn dump(&mut self, stage_index: usize, frame: &[i16]) {
66
87
if let Some(dumps) = self.dumps.as_mut() {
67
88
dumps.write(stage_index, frame);
@@ -75,6 +96,16 @@ mod tests {
75
96
use crate::audio::SineGenerator;
76
97
use crate::mic_processing::MicSettings;
77
98
99
#[test]
100
fn a_slow_deep_filter_hands_over_to_webrtc_noise_suppression() {
101
let auto = MicSettings::default();
102
let mut chain = MicChain::new(auto.apm_config(), true, None);
103
chain.fall_back_to_webrtc_noise_suppression();
104
assert!(chain.ml_noise.is_none());
105
assert!(chain.processing.noise_suppression);
106
assert!(chain.front.is_some());
107
}
108
78
109
#[test]
79
110
fn off_leaves_the_frame_untouched() {
80
111
let off = MicSettings {
@@ -1,3 +1,5 @@1
use std::time::Instant;2
1
use libwebrtc::native::apm::AudioProcessingModule;3
use libwebrtc::native::apm::AudioProcessingModule;2
4
3
use crate::audio::SAMPLE_RATE;5
use crate::audio::SAMPLE_RATE;@@ -10,6 +12,7 @@ use crate::wav_dump::StageDumps;10
/// DeepFilterNet drops quiet laptop-mic speech as noise and takes about 6 dB off reverberant speech,12
/// DeepFilterNet drops quiet laptop-mic speech as noise and takes about 6 dB off reverberant speech,11
/// so one AGC2 brings the level up before it and a second one restores the level after it.13
/// so one AGC2 brings the level up before it and a second one restores the level after it.12
pub struct MicChain {14
pub struct MicChain {15
processing: ApmConfig,13
high_pass: Option<VoiceHighPass>,16
high_pass: Option<VoiceHighPass>,14
front: Option<AudioProcessingModule>,17
front: Option<AudioProcessingModule>,15
ml_noise: Option<NoiseStage>,18
ml_noise: Option<NoiseStage>,@@ -20,10 +23,17 @@ pub struct MicChain {20
23
21
impl MicChain {24
impl MicChain {22
pub fn new(processing: ApmConfig, ml_noise: bool, dumps: Option<StageDumps>) -> 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();23
if ml_noise {28
if ml_noise {24
crate::noise_stage::prepare();29
crate::noise_stage::prepare();25
}30
}31
let processing = ApmConfig {32
noise_suppression: processing.noise_suppression || (ml_wanted && !ml_noise),33
..processing34
};26
MicChain {35
MicChain {36
processing,27
high_pass: processing.high_pass_filter.then(VoiceHighPass::new),37
high_pass: processing.high_pass_filter.then(VoiceHighPass::new),28
front: processing.front_module(),38
front: processing.front_module(),29
ml_noise: ml_noise.then(NoiseStage::new),39
ml_noise: ml_noise.then(NoiseStage::new),@@ -54,7 +64,11 @@ impl MicChain {54
}64
}55
self.dump(3, frame);65
self.dump(3, frame);56
if let Some(noise_stage) = self.ml_noise.as_mut() {66
if let Some(noise_stage) = self.ml_noise.as_mut() {67
let started = Instant::now();57
noise_stage.process_pcm(0, frame);68
noise_stage.process_pcm(0, frame);69
if noise_stage.over_budget(started.elapsed()) {70
self.fall_back_to_webrtc_noise_suppression();71
}58
}72
}59
if let Some(output_gain) = self.output_gain.as_mut() {73
if let Some(output_gain) = self.output_gain.as_mut() {60
output_gain.process(frame);74
output_gain.process(frame);@@ -62,6 +76,13 @@ impl MicChain {62
self.dump(4, frame);76
self.dump(4, frame);63
}77
}64
78
79
fn fall_back_to_webrtc_noise_suppression(&mut self) {80
self.ml_noise = None;81
self.lift = None;82
self.processing.noise_suppression = true;83
self.front = self.processing.front_module();84
}85
65
fn dump(&mut self, stage_index: usize, frame: &[i16]) {86
fn dump(&mut self, stage_index: usize, frame: &[i16]) {66
if let Some(dumps) = self.dumps.as_mut() {87
if let Some(dumps) = self.dumps.as_mut() {67
dumps.write(stage_index, frame);88
dumps.write(stage_index, frame);@@ -75,6 +96,16 @@ mod tests {75
use crate::audio::SineGenerator;96
use crate::audio::SineGenerator;76
use crate::mic_processing::MicSettings;97
use crate::mic_processing::MicSettings;77
98
99
#[test]100
fn a_slow_deep_filter_hands_over_to_webrtc_noise_suppression() {101
let auto = MicSettings::default();102
let mut chain = MicChain::new(auto.apm_config(), true, None);103
chain.fall_back_to_webrtc_noise_suppression();104
assert!(chain.ml_noise.is_none());105
assert!(chain.processing.noise_suppression);106
assert!(chain.front.is_some());107
}108
78
#[test]109
#[test]79
fn off_leaves_the_frame_untouched() {110
fn off_leaves_the_frame_untouched() {80
let off = MicSettings {111
let off = MicSettings {@@ -9,7 +9,7 @@ use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
9
9
use crate::mic_processing::MicSettings;
10
10
11
11
const PCM_SCALE: f32 = 32768.0;
12
const ATTENUATION_LIMIT_DB: f32 = 40.0;
12
const ATTENUATION_LIMIT_DB: f32 = 30.0;
13
13
const MIN_LOCAL_SNR_DB: f32 = -15.0;
14
14
const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;
15
15
const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;
@@ -117,8 +117,7 @@ fn capture_hook(channel_index: usize, samples: &mut [f32]) {
117
117
};
118
118
let started = Instant::now();
119
119
stage.process(channel_index, samples);
120
if stage.budget.record(started.elapsed()) {
121
OVERLOADED.store(true, Ordering::Relaxed);
120
if stage.over_budget(started.elapsed()) {
122
121
HOOK_ARMED.store(false, Ordering::Relaxed);
123
122
}
124
123
}
@@ -223,6 +222,15 @@ impl NoiseStage {
223
222
}
224
223
}
225
224
225
/// Reports and remembers for this call when frames keep running over their budget.
226
pub fn over_budget(&mut self, elapsed: Duration) -> bool {
227
let over = self.budget.record(elapsed);
228
if over {
229
OVERLOADED.store(true, Ordering::Relaxed);
230
}
231
over
232
}
233
226
234
pub fn process_pcm(&mut self, channel_index: usize, samples: &mut [i16]) {
227
235
let mut floats = std::mem::take(&mut self.pcm);
228
236
floats.clear();
@@ -9,7 +9,7 @@ use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};9
use crate::mic_processing::MicSettings;9
use crate::mic_processing::MicSettings;10
10
11
const PCM_SCALE: f32 = 32768.0;11
const PCM_SCALE: f32 = 32768.0;12
const ATTENUATION_LIMIT_DB: f32 = 40.0;12
const ATTENUATION_LIMIT_DB: f32 = 30.0;13
const MIN_LOCAL_SNR_DB: f32 = -15.0;13
const MIN_LOCAL_SNR_DB: f32 = -15.0;14
const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;14
const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;15
const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;15
const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;@@ -117,8 +117,7 @@ fn capture_hook(channel_index: usize, samples: &mut [f32]) {117
};117
};118
let started = Instant::now();118
let started = Instant::now();119
stage.process(channel_index, samples);119
stage.process(channel_index, samples);120
if stage.budget.record(started.elapsed()) {120
if stage.over_budget(started.elapsed()) {121
OVERLOADED.store(true, Ordering::Relaxed);122
HOOK_ARMED.store(false, Ordering::Relaxed);121
HOOK_ARMED.store(false, Ordering::Relaxed);123
}122
}124
}123
}@@ -223,6 +222,15 @@ impl NoiseStage {223
}222
}224
}223
}225
224
225
/// Reports and remembers for this call when frames keep running over their budget.226
pub fn over_budget(&mut self, elapsed: Duration) -> bool {227
let over = self.budget.record(elapsed);228
if over {229
OVERLOADED.store(true, Ordering::Relaxed);230
}231
over232
}233
226
pub fn process_pcm(&mut self, channel_index: usize, samples: &mut [i16]) {234
pub fn process_pcm(&mut self, channel_index: usize, samples: &mut [i16]) {227
let mut floats = std::mem::take(&mut self.pcm);235
let mut floats = std::mem::take(&mut self.pcm);228
floats.clear();236
floats.clear();