BlueGit

ict rusty-teams

fix(calling): steady mic level, softer noise removal, echo calls wait for the bot

- output AGC2 is now a fixed +8 dB with limiter; only the lift stage adapts, so the two no longer pump against each other
- the lift stage starts each call at the gain the last call settled on, so the first words already have the right level
- DeepFilterNet attenuation capped at 30 dB: short sounds and word edges are no longer cut to silence
- GainProcessor wrapper lives in webrtc-sys, so calling no longer depends on cxx directly
- echo calls count as connected only once the echo bot joined, not when the media link is up

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-16 · 6 weeks ago

Hash
0094cf57dcfcfea61c689a05e0a8ac6fa2d94e9d
Parent
80c7b5b9
8 files changed +118 −30
Cargo.lock +0 −1
@@ -1197,7 +1197,6 @@ dependencies = [
1197 1197 "chatsvc",
1198 1198 "chrono",
1199 1199 "cpal",
1200 "cxx",
1201 1200 "deep_filter",
1202 1201 "flate2",
1203 1202 "futures-util",
@@ -1197,7 +1197,6 @@ dependencies = [
1197 "chatsvc",
1197 "chatsvc",
1198 "chrono",
1198 "chrono",
1199 "cpal",
1199 "cpal",
1200 "cxx",
1201 "deep_filter",
1200 "deep_filter",
1202 "flate2",
1201 "flate2",
1203 "futures-util",
1202 "futures-util",
crates/calling/Cargo.toml +0 −1
@@ -14,7 +14,6 @@ flate2 = "1"
14 14 image = { version = "0.25", default-features = false, features = ["jpeg", "png"] }
15 15 futures-util.workspace = true
16 16 libwebrtc = { version = "=0.3.51", features = ["glib-main-loop"] }
17 cxx = "1"
18 17 deep_filter = { path = "../../vendor/deep_filter" }
19 18 ndarray = "0.17"
20 19 nokhwa = { version = "0.10", default-features = false, features = ["input-native"] }
@@ -14,7 +14,6 @@ flate2 = "1"
14image = { version = "0.25", default-features = false, features = ["jpeg", "png"] }
14image = { version = "0.25", default-features = false, features = ["jpeg", "png"] }
15futures-util.workspace = true
15futures-util.workspace = true
16libwebrtc = { version = "=0.3.51", features = ["glib-main-loop"] }
16libwebrtc = { version = "=0.3.51", features = ["glib-main-loop"] }
17cxx = "1"
18deep_filter = { path = "../../vendor/deep_filter" }
17deep_filter = { path = "../../vendor/deep_filter" }
19ndarray = "0.17"
18ndarray = "0.17"
20nokhwa = { version = "0.10", default-features = false, features = ["input-native"] }
19nokhwa = { version = "0.10", default-features = false, features = ["input-native"] }
crates/calling/src/call.rs +9 −1
@@ -216,7 +216,8 @@ impl CallSpec {
216 216 fn callee_count(&self) -> usize {
217 217 match self {
218 218 CallSpec::People { callees, .. } => callees.len(),
219 CallSpec::Echo | CallSpec::Meeting(_) => 0,
219 CallSpec::Echo => 1,
220 CallSpec::Meeting(_) => 0,
220 221 }
221 222 }
222 223 }
@@ -3182,6 +3183,13 @@ async fn collect_stats(peer: &PeerConnection, share_mids: &[String]) -> StatsSna
3182 3183 mod tests {
3183 3184 use super::*;
3184 3185
3186 #[test]
3187 fn an_echo_call_waits_for_the_echo_bot_before_it_counts_as_connected() {
3188 let mut gate = CalleeGate::new(CallSpec::Echo.callee_count());
3189 assert!(!gate.media_connected());
3190 assert!(gate.callee_joined());
3191 }
3192
3185 3193 #[test]
3186 3194 fn only_a_conflict_answer_retries_a_recording_stop() {
3187 3195 assert!(recorder_busy(&Error::Signaling("recording command answered HTTP 409 no answer".into())));
@@ -216,7 +216,8 @@ impl CallSpec {
216 fn callee_count(&self) -> usize {
216 fn callee_count(&self) -> usize {
217 match self {
217 match self {
218 CallSpec::People { callees, .. } => callees.len(),
218 CallSpec::People { callees, .. } => callees.len(),
219 CallSpec::Echo | CallSpec::Meeting(_) => 0,
219 CallSpec::Echo => 1,
220 CallSpec::Meeting(_) => 0,
220 }
221 }
221 }
222 }
222}
223}
@@ -3182,6 +3183,13 @@ async fn collect_stats(peer: &PeerConnection, share_mids: &[String]) -> StatsSna
3182mod tests {
3183mod tests {
3183 use super::*;
3184 use super::*;
3184
3185
3186 #[test]
3187 fn an_echo_call_waits_for_the_echo_bot_before_it_counts_as_connected() {
3188 let mut gate = CalleeGate::new(CallSpec::Echo.callee_count());
3189 assert!(!gate.media_connected());
3190 assert!(gate.callee_joined());
3191 }
3192
3185 #[test]
3193 #[test]
3186 fn only_a_conflict_answer_retries_a_recording_stop() {
3194 fn only_a_conflict_answer_retries_a_recording_stop() {
3187 assert!(recorder_busy(&Error::Signaling("recording command answered HTTP 409 no answer".into())));
3195 assert!(recorder_busy(&Error::Signaling("recording command answered HTTP 409 no answer".into())));
crates/calling/src/gain_stage.rs +82 −24
@@ -1,37 +1,72 @@
1 use cxx::UniquePtr;
2 use webrtc_sys::apm::ffi::{AudioProcessingModule, create_gain_apm};
1 use std::sync::atomic::{AtomicU32, Ordering};
2
3 use webrtc_sys::apm::GainProcessor;
3 4
4 5 use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
5 6
6 const LIFT_INITIAL_GAIN_DB: f32 = 10.0;
7 const LIFT_GAIN_CHANGE_DB_PER_SECOND: f32 = 10.0;
8 const OUTPUT_FIXED_GAIN_DB: f32 = 3.0;
9 const OUTPUT_GAIN_CHANGE_DB_PER_SECOND: f32 = 6.0;
7 const LIFT_DEFAULT_GAIN_DB: f32 = 12.0;
8 const LIFT_GAIN_CHANGE_DB_PER_SECOND: f32 = 6.0;
9 const OUTPUT_FIXED_GAIN_DB: f32 = 8.0;
10 const SPEECH_FLOOR_DBFS: f32 = -55.0;
11 const MIN_SPEECH_FRAMES: u32 = 100;
12
13 static LEARNED_LIFT_GAIN_DB: AtomicU32 = AtomicU32::new(LIFT_DEFAULT_GAIN_DB.to_bits());
14
15 fn frame_dbfs(frame: &[i16]) -> f32 {
16 let mean_square = frame.iter().map(|sample| f32::from(*sample).powi(2)).sum::<f32>() / frame.len().max(1) as f32;
17 10.0 * (mean_square / 32768.0_f32.powi(2) + 1e-12).log10()
18 }
10 19
11 /// One AGC2 (adaptive digital gain, then fixed gain, then its -1 dBFS limiter) on 10 ms mono frames.
20 /// One AGC2 (optional adaptive digital gain, then fixed gain, then its limiter) on 10 ms mono frames.
12 21 pub struct GainStage {
13 module: UniquePtr<AudioProcessingModule>,
22 module: GainProcessor,
23 measured_gain_db_sum: f32,
24 measured_frames: u32,
25 learns: bool,
14 26 }
15 27
16 28 impl GainStage {
17 /// Before DeepFilterNet: brings a quiet laptop mic up fast, settles near AGC2's own -22 dBFS.
29 /// Before DeepFilterNet: brings a quiet laptop mic up; starts at the gain the last call settled on, so the first words already have the right level.
18 30 pub fn lift() -> GainStage {
19 GainStage { module: create_gain_apm(LIFT_INITIAL_GAIN_DB, 0.0, LIFT_GAIN_CHANGE_DB_PER_SECOND) }
31 let initial_gain_db = f32::from_bits(LEARNED_LIFT_GAIN_DB.load(Ordering::Relaxed));
32 GainStage::new(true, initial_gain_db, 0.0, LIFT_GAIN_CHANGE_DB_PER_SECOND, true)
20 33 }
21 34
22 /// Last stage: +6 dB on top of AGC2's target puts speech near -16 dBFS like Teams.
35 /// Last stage, fixed: makes up the level DeepFilterNet takes off and limits peaks; a second adaptive stage pumps against the first.
23 36 pub fn output() -> GainStage {
24 GainStage { module: create_gain_apm(0.0, OUTPUT_FIXED_GAIN_DB, OUTPUT_GAIN_CHANGE_DB_PER_SECOND) }
37 GainStage::new(false, 0.0, OUTPUT_FIXED_GAIN_DB, 1.0, false)
38 }
39
40 fn new(adaptive: bool, initial_gain_db: f32, fixed_gain_db: f32, max_gain_change_db_per_second: f32, learns: bool) -> GainStage {
41 GainStage {
42 module: GainProcessor::new(adaptive, initial_gain_db, fixed_gain_db, max_gain_change_db_per_second),
43 measured_gain_db_sum: 0.0,
44 measured_frames: 0,
45 learns,
46 }
25 47 }
26 48
27 49 pub fn process(&mut self, frame: &mut [i16]) {
28 if frame.len() != FRAME_SAMPLES || self.module.is_null() {
50 if frame.len() != FRAME_SAMPLES {
29 51 return;
30 52 }
31 let length = frame.len();
32 let pointer = frame.as_mut_ptr();
33 unsafe {
34 self.module.pin_mut().process_stream(pointer, length, pointer, length, SAMPLE_RATE as i32, 1);
53 let input_dbfs = if self.learns { frame_dbfs(frame) } else { 0.0 };
54 let processed = self.module.process(frame, SAMPLE_RATE as i32);
55 if processed && self.learns && input_dbfs > SPEECH_FLOOR_DBFS {
56 self.measured_gain_db_sum += frame_dbfs(frame) - input_dbfs;
57 self.measured_frames += 1;
58 }
59 }
60
61 fn learned_gain_db(&self) -> Option<f32> {
62 (self.measured_frames >= MIN_SPEECH_FRAMES).then(|| self.measured_gain_db_sum / self.measured_frames as f32)
63 }
64 }
65
66 impl Drop for GainStage {
67 fn drop(&mut self) {
68 if let Some(gain_db) = self.learned_gain_db() {
69 LEARNED_LIFT_GAIN_DB.store(gain_db.clamp(0.0, 30.0).to_bits(), Ordering::Relaxed);
35 70 }
36 71 }
37 72 }
@@ -40,16 +75,39 @@ impl GainStage {
40 75 mod tests {
41 76 use super::*;
42 77
43 fn rms_dbfs(samples: &[i16]) -> f32 {
44 let mean_square = samples.iter().map(|sample| f32::from(*sample).powi(2)).sum::<f32>() / samples.len() as f32;
45 10.0 * (mean_square / 32768.0_f32.powi(2)).log10()
46 }
47
48 78 #[test]
49 79 fn other_frame_lengths_pass_through() {
50 let mut stage = GainStage::lift();
80 let mut stage = GainStage::output();
51 81 let mut frame = vec![1000i16; 160];
52 82 stage.process(&mut frame);
53 assert_eq!(rms_dbfs(&frame), rms_dbfs(&[1000i16; 160]));
83 assert_eq!(frame, vec![1000i16; 160]);
84 }
85
86 #[test]
87 fn the_fixed_output_stage_adds_its_gain() {
88 let mut stage = GainStage::output();
89 let mut last = Vec::new();
90 for frame_index in 0..100 {
91 let mut frame: Vec<i16> = (0..FRAME_SAMPLES)
92 .map(|sample| (((frame_index * FRAME_SAMPLES + sample) as f32 * 0.04).sin() * 2000.0) as i16)
93 .collect();
94 stage.process(&mut frame);
95 last = frame;
96 }
97 let added = frame_dbfs(&last) - 20.0 * (2000.0_f32 / std::f32::consts::SQRT_2 / 32768.0).log10();
98 assert!((added - OUTPUT_FIXED_GAIN_DB).abs() < 0.5, "added {added:.2} dB");
99 }
100
101 #[test]
102 fn learned_gain_needs_a_second_of_speech() {
103 let mut stage = GainStage::lift();
104 stage.learns = true;
105 stage.measured_frames = 99;
106 stage.measured_gain_db_sum = 99.0 * 15.0;
107 assert_eq!(stage.learned_gain_db(), None);
108 stage.measured_frames = 100;
109 stage.measured_gain_db_sum = 100.0 * 15.0;
110 assert_eq!(stage.learned_gain_db(), Some(15.0));
111 stage.measured_frames = 0;
54 112 }
55 113 }
@@ -1,37 +1,72 @@
1use cxx::UniquePtr;
1use std::sync::atomic::{AtomicU32, Ordering};
2use webrtc_sys::apm::ffi::{AudioProcessingModule, create_gain_apm};
2
3use webrtc_sys::apm::GainProcessor;
3
4
4use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
5use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
5
6
6const LIFT_INITIAL_GAIN_DB: f32 = 10.0;
7const LIFT_DEFAULT_GAIN_DB: f32 = 12.0;
7const LIFT_GAIN_CHANGE_DB_PER_SECOND: f32 = 10.0;
8const LIFT_GAIN_CHANGE_DB_PER_SECOND: f32 = 6.0;
8const OUTPUT_FIXED_GAIN_DB: f32 = 3.0;
9const OUTPUT_FIXED_GAIN_DB: f32 = 8.0;
9const OUTPUT_GAIN_CHANGE_DB_PER_SECOND: f32 = 6.0;
10const SPEECH_FLOOR_DBFS: f32 = -55.0;
11const MIN_SPEECH_FRAMES: u32 = 100;
12
13static LEARNED_LIFT_GAIN_DB: AtomicU32 = AtomicU32::new(LIFT_DEFAULT_GAIN_DB.to_bits());
14
15fn frame_dbfs(frame: &[i16]) -> f32 {
16 let mean_square = frame.iter().map(|sample| f32::from(*sample).powi(2)).sum::<f32>() / frame.len().max(1) as f32;
17 10.0 * (mean_square / 32768.0_f32.powi(2) + 1e-12).log10()
18}
10
19
11/// One AGC2 (adaptive digital gain, then fixed gain, then its -1 dBFS limiter) on 10 ms mono frames.
20/// One AGC2 (optional adaptive digital gain, then fixed gain, then its limiter) on 10 ms mono frames.
12pub struct GainStage {
21pub struct GainStage {
13 module: UniquePtr<AudioProcessingModule>,
22 module: GainProcessor,
23 measured_gain_db_sum: f32,
24 measured_frames: u32,
25 learns: bool,
14}
26}
15
27
16impl GainStage {
28impl GainStage {
17 /// Before DeepFilterNet: brings a quiet laptop mic up fast, settles near AGC2's own -22 dBFS.
29 /// Before DeepFilterNet: brings a quiet laptop mic up; starts at the gain the last call settled on, so the first words already have the right level.
18 pub fn lift() -> GainStage {
30 pub fn lift() -> GainStage {
19 GainStage { module: create_gain_apm(LIFT_INITIAL_GAIN_DB, 0.0, LIFT_GAIN_CHANGE_DB_PER_SECOND) }
31 let initial_gain_db = f32::from_bits(LEARNED_LIFT_GAIN_DB.load(Ordering::Relaxed));
32 GainStage::new(true, initial_gain_db, 0.0, LIFT_GAIN_CHANGE_DB_PER_SECOND, true)
20 }
33 }
21
34
22 /// Last stage: +6 dB on top of AGC2's target puts speech near -16 dBFS like Teams.
35 /// Last stage, fixed: makes up the level DeepFilterNet takes off and limits peaks; a second adaptive stage pumps against the first.
23 pub fn output() -> GainStage {
36 pub fn output() -> GainStage {
24 GainStage { module: create_gain_apm(0.0, OUTPUT_FIXED_GAIN_DB, OUTPUT_GAIN_CHANGE_DB_PER_SECOND) }
37 GainStage::new(false, 0.0, OUTPUT_FIXED_GAIN_DB, 1.0, false)
38 }
39
40 fn new(adaptive: bool, initial_gain_db: f32, fixed_gain_db: f32, max_gain_change_db_per_second: f32, learns: bool) -> GainStage {
41 GainStage {
42 module: GainProcessor::new(adaptive, initial_gain_db, fixed_gain_db, max_gain_change_db_per_second),
43 measured_gain_db_sum: 0.0,
44 measured_frames: 0,
45 learns,
46 }
25 }
47 }
26
48
27 pub fn process(&mut self, frame: &mut [i16]) {
49 pub fn process(&mut self, frame: &mut [i16]) {
28 if frame.len() != FRAME_SAMPLES || self.module.is_null() {
50 if frame.len() != FRAME_SAMPLES {
29 return;
51 return;
30 }
52 }
31 let length = frame.len();
53 let input_dbfs = if self.learns { frame_dbfs(frame) } else { 0.0 };
32 let pointer = frame.as_mut_ptr();
54 let processed = self.module.process(frame, SAMPLE_RATE as i32);
33 unsafe {
55 if processed && self.learns && input_dbfs > SPEECH_FLOOR_DBFS {
34 self.module.pin_mut().process_stream(pointer, length, pointer, length, SAMPLE_RATE as i32, 1);
56 self.measured_gain_db_sum += frame_dbfs(frame) - input_dbfs;
57 self.measured_frames += 1;
58 }
59 }
60
61 fn learned_gain_db(&self) -> Option<f32> {
62 (self.measured_frames >= MIN_SPEECH_FRAMES).then(|| self.measured_gain_db_sum / self.measured_frames as f32)
63 }
64}
65
66impl Drop for GainStage {
67 fn drop(&mut self) {
68 if let Some(gain_db) = self.learned_gain_db() {
69 LEARNED_LIFT_GAIN_DB.store(gain_db.clamp(0.0, 30.0).to_bits(), Ordering::Relaxed);
35 }
70 }
36 }
71 }
37}
72}
@@ -40,16 +75,39 @@ impl GainStage {
40mod tests {
75mod tests {
41 use super::*;
76 use super::*;
42
77
43 fn rms_dbfs(samples: &[i16]) -> f32 {
44 let mean_square = samples.iter().map(|sample| f32::from(*sample).powi(2)).sum::<f32>() / samples.len() as f32;
45 10.0 * (mean_square / 32768.0_f32.powi(2)).log10()
46 }
47
48 #[test]
78 #[test]
49 fn other_frame_lengths_pass_through() {
79 fn other_frame_lengths_pass_through() {
50 let mut stage = GainStage::lift();
80 let mut stage = GainStage::output();
51 let mut frame = vec![1000i16; 160];
81 let mut frame = vec![1000i16; 160];
52 stage.process(&mut frame);
82 stage.process(&mut frame);
53 assert_eq!(rms_dbfs(&frame), rms_dbfs(&[1000i16; 160]));
83 assert_eq!(frame, vec![1000i16; 160]);
84 }
85
86 #[test]
87 fn the_fixed_output_stage_adds_its_gain() {
88 let mut stage = GainStage::output();
89 let mut last = Vec::new();
90 for frame_index in 0..100 {
91 let mut frame: Vec<i16> = (0..FRAME_SAMPLES)
92 .map(|sample| (((frame_index * FRAME_SAMPLES + sample) as f32 * 0.04).sin() * 2000.0) as i16)
93 .collect();
94 stage.process(&mut frame);
95 last = frame;
96 }
97 let added = frame_dbfs(&last) - 20.0 * (2000.0_f32 / std::f32::consts::SQRT_2 / 32768.0).log10();
98 assert!((added - OUTPUT_FIXED_GAIN_DB).abs() < 0.5, "added {added:.2} dB");
99 }
100
101 #[test]
102 fn learned_gain_needs_a_second_of_speech() {
103 let mut stage = GainStage::lift();
104 stage.learns = true;
105 stage.measured_frames = 99;
106 stage.measured_gain_db_sum = 99.0 * 15.0;
107 assert_eq!(stage.learned_gain_db(), None);
108 stage.measured_frames = 100;
109 stage.measured_gain_db_sum = 100.0 * 15.0;
110 assert_eq!(stage.learned_gain_db(), Some(15.0));
111 stage.measured_frames = 0;
54 }
112 }
55}
113}
crates/calling/src/noise_stage.rs +1 −1
@@ -7,7 +7,7 @@ use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
7 7 use crate::mic_processing::MicSettings;
8 8
9 9 const PCM_SCALE: f32 = 32768.0;
10 const ATTENUATION_LIMIT_DB: f32 = 100.0;
10 const ATTENUATION_LIMIT_DB: f32 = 30.0;
11 11 const MIN_LOCAL_SNR_DB: f32 = -15.0;
12 12 const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;
13 13 const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;
@@ -7,7 +7,7 @@ use crate::audio::{FRAME_SAMPLES, SAMPLE_RATE};
7use crate::mic_processing::MicSettings;
7use crate::mic_processing::MicSettings;
8
8
9const PCM_SCALE: f32 = 32768.0;
9const PCM_SCALE: f32 = 32768.0;
10const ATTENUATION_LIMIT_DB: f32 = 100.0;
10const ATTENUATION_LIMIT_DB: f32 = 30.0;
11const MIN_LOCAL_SNR_DB: f32 = -15.0;
11const MIN_LOCAL_SNR_DB: f32 = -15.0;
12const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;
12const MAX_LOCAL_SNR_ERB_DB: f32 = 35.0;
13const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;
13const MAX_LOCAL_SNR_DF_DB: f32 = 35.0;
vendor/webrtc-sys/include/livekit/apm.h +2 −1
@@ -74,7 +74,8 @@ std::unique_ptr<AudioProcessingModule> create_apm(
74 74 bool high_pass_filter_enabled,
75 75 bool noise_suppression_enabled);
76 76
77 std::unique_ptr<AudioProcessingModule> create_gain_apm(float initial_gain_db,
77 std::unique_ptr<AudioProcessingModule> create_gain_apm(bool adaptive,
78 float initial_gain_db,
78 79 float fixed_gain_db,
79 80 float max_gain_change_db_per_second);
80 81
@@ -74,7 +74,8 @@ std::unique_ptr<AudioProcessingModule> create_apm(
74 bool high_pass_filter_enabled,
74 bool high_pass_filter_enabled,
75 bool noise_suppression_enabled);
75 bool noise_suppression_enabled);
76
76
77std::unique_ptr<AudioProcessingModule> create_gain_apm(float initial_gain_db,
77std::unique_ptr<AudioProcessingModule> create_gain_apm(bool adaptive,
78 float initial_gain_db,
78 float fixed_gain_db,
79 float fixed_gain_db,
79 float max_gain_change_db_per_second);
80 float max_gain_change_db_per_second);
80
81
vendor/webrtc-sys/src/apm.cpp +2 −1
@@ -75,12 +75,13 @@ std::unique_ptr<AudioProcessingModule> create_apm(
75 75 }
76 76
77 77 std::unique_ptr<AudioProcessingModule> create_gain_apm(
78 bool adaptive,
78 79 float initial_gain_db,
79 80 float fixed_gain_db,
80 81 float max_gain_change_db_per_second) {
81 82 webrtc::AudioProcessing::Config config;
82 83 config.gain_controller2.enabled = true;
83 config.gain_controller2.adaptive_digital.enabled = true;
84 config.gain_controller2.adaptive_digital.enabled = adaptive;
84 85 config.gain_controller2.adaptive_digital.initial_gain_db = initial_gain_db;
85 86 config.gain_controller2.adaptive_digital.max_gain_change_db_per_second =
86 87 max_gain_change_db_per_second;
@@ -75,12 +75,13 @@ std::unique_ptr<AudioProcessingModule> create_apm(
75}
75}
76
76
77std::unique_ptr<AudioProcessingModule> create_gain_apm(
77std::unique_ptr<AudioProcessingModule> create_gain_apm(
78 bool adaptive,
78 float initial_gain_db,
79 float initial_gain_db,
79 float fixed_gain_db,
80 float fixed_gain_db,
80 float max_gain_change_db_per_second) {
81 float max_gain_change_db_per_second) {
81 webrtc::AudioProcessing::Config config;
82 webrtc::AudioProcessing::Config config;
82 config.gain_controller2.enabled = true;
83 config.gain_controller2.enabled = true;
83 config.gain_controller2.adaptive_digital.enabled = true;
84 config.gain_controller2.adaptive_digital.enabled = adaptive;
84 config.gain_controller2.adaptive_digital.initial_gain_db = initial_gain_db;
85 config.gain_controller2.adaptive_digital.initial_gain_db = initial_gain_db;
85 config.gain_controller2.adaptive_digital.max_gain_change_db_per_second =
86 config.gain_controller2.adaptive_digital.max_gain_change_db_per_second =
86 max_gain_change_db_per_second;
87 max_gain_change_db_per_second;
vendor/webrtc-sys/src/apm.rs +22 −0
@@ -51,6 +51,7 @@ pub mod ffi {
51 51 ) -> UniquePtr<AudioProcessingModule>;
52 52
53 53 fn create_gain_apm(
54 adaptive: bool,
54 55 initial_gain_db: f32,
55 56 fixed_gain_db: f32,
56 57 max_gain_change_db_per_second: f32,
@@ -59,3 +60,24 @@ pub mod ffi {
59 60 }
60 61
61 62 impl_thread_safety!(ffi::AudioProcessingModule, Send + Sync);
63
64 /// A gain-only AGC2 module, so callers do not need the cxx crate themselves.
65 pub struct GainProcessor {
66 handle: cxx::UniquePtr<ffi::AudioProcessingModule>,
67 }
68
69 impl GainProcessor {
70 pub fn new(adaptive: bool, initial_gain_db: f32, fixed_gain_db: f32, max_gain_change_db_per_second: f32) -> Self {
71 Self { handle: ffi::create_gain_apm(adaptive, initial_gain_db, fixed_gain_db, max_gain_change_db_per_second) }
72 }
73
74 /// Processes one 10 ms mono frame in place; returns false when the module is missing or rejects the frame.
75 pub fn process(&mut self, frame: &mut [i16], sample_rate: i32) -> bool {
76 if self.handle.is_null() {
77 return false;
78 }
79 let length = frame.len();
80 let pointer = frame.as_mut_ptr();
81 unsafe { self.handle.pin_mut().process_stream(pointer, length, pointer, length, sample_rate, 1) == 0 }
82 }
83 }
@@ -51,6 +51,7 @@ pub mod ffi {
51 ) -> UniquePtr<AudioProcessingModule>;
51 ) -> UniquePtr<AudioProcessingModule>;
52
52
53 fn create_gain_apm(
53 fn create_gain_apm(
54 adaptive: bool,
54 initial_gain_db: f32,
55 initial_gain_db: f32,
55 fixed_gain_db: f32,
56 fixed_gain_db: f32,
56 max_gain_change_db_per_second: f32,
57 max_gain_change_db_per_second: f32,
@@ -59,3 +60,24 @@ pub mod ffi {
59}
60}
60
61
61impl_thread_safety!(ffi::AudioProcessingModule, Send + Sync);
62impl_thread_safety!(ffi::AudioProcessingModule, Send + Sync);
63
64/// A gain-only AGC2 module, so callers do not need the cxx crate themselves.
65pub struct GainProcessor {
66 handle: cxx::UniquePtr<ffi::AudioProcessingModule>,
67}
68
69impl GainProcessor {
70 pub fn new(adaptive: bool, initial_gain_db: f32, fixed_gain_db: f32, max_gain_change_db_per_second: f32) -> Self {
71 Self { handle: ffi::create_gain_apm(adaptive, initial_gain_db, fixed_gain_db, max_gain_change_db_per_second) }
72 }
73
74 /// Processes one 10 ms mono frame in place; returns false when the module is missing or rejects the frame.
75 pub fn process(&mut self, frame: &mut [i16], sample_rate: i32) -> bool {
76 if self.handle.is_null() {
77 return false;
78 }
79 let length = frame.len();
80 let pointer = frame.as_mut_ptr();
81 unsafe { self.handle.pin_mut().process_stream(pointer, length, pointer, length, sample_rate, 1) == 0 }
82 }
83}