echo_canceller3_config.cc (13089B)
1 /* 2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 #include "api/audio/echo_canceller3_config.h" 11 12 #include <algorithm> 13 #include <cmath> 14 #include <cstddef> 15 16 #include "rtc_base/checks.h" 17 #include "rtc_base/numerics/safe_minmax.h" 18 19 namespace webrtc { 20 namespace { 21 bool Limit(float* value, float min, float max) { 22 float clamped = SafeClamp(*value, min, max); 23 clamped = std::isfinite(clamped) ? clamped : min; 24 bool res = *value == clamped; 25 *value = clamped; 26 return res; 27 } 28 29 bool Limit(size_t* value, size_t min, size_t max) { 30 size_t clamped = SafeClamp(*value, min, max); 31 bool res = *value == clamped; 32 *value = clamped; 33 return res; 34 } 35 36 bool Limit(int* value, int min, int max) { 37 int clamped = SafeClamp(*value, min, max); 38 bool res = *value == clamped; 39 *value = clamped; 40 return res; 41 } 42 43 bool FloorLimit(size_t* value, size_t min) { 44 size_t clamped = *value >= min ? *value : min; 45 bool res = *value == clamped; 46 *value = clamped; 47 return res; 48 } 49 50 } // namespace 51 52 EchoCanceller3Config::EchoCanceller3Config() = default; 53 EchoCanceller3Config::EchoCanceller3Config(const EchoCanceller3Config& e) = 54 default; 55 EchoCanceller3Config& EchoCanceller3Config::operator=( 56 const EchoCanceller3Config& e) = default; 57 EchoCanceller3Config::Delay::Delay() = default; 58 EchoCanceller3Config::Delay::Delay(const EchoCanceller3Config::Delay& e) = 59 default; 60 EchoCanceller3Config::Delay& EchoCanceller3Config::Delay::operator=( 61 const Delay& e) = default; 62 63 EchoCanceller3Config::EchoModel::EchoModel() = default; 64 EchoCanceller3Config::EchoModel::EchoModel( 65 const EchoCanceller3Config::EchoModel& e) = default; 66 EchoCanceller3Config::EchoModel& EchoCanceller3Config::EchoModel::operator=( 67 const EchoModel& e) = default; 68 69 EchoCanceller3Config::Suppressor::Suppressor() = default; 70 EchoCanceller3Config::Suppressor::Suppressor( 71 const EchoCanceller3Config::Suppressor& e) = default; 72 EchoCanceller3Config::Suppressor& EchoCanceller3Config::Suppressor::operator=( 73 const Suppressor& e) = default; 74 75 EchoCanceller3Config::Suppressor::MaskingThresholds::MaskingThresholds( 76 float enr_transparent, 77 float enr_suppress, 78 float emr_transparent) 79 : enr_transparent(enr_transparent), 80 enr_suppress(enr_suppress), 81 emr_transparent(emr_transparent) {} 82 EchoCanceller3Config::Suppressor::MaskingThresholds::MaskingThresholds( 83 const EchoCanceller3Config::Suppressor::MaskingThresholds& e) = default; 84 EchoCanceller3Config::Suppressor::MaskingThresholds& 85 EchoCanceller3Config::Suppressor::MaskingThresholds::operator=( 86 const MaskingThresholds& e) = default; 87 88 EchoCanceller3Config::Suppressor::Tuning::Tuning(MaskingThresholds mask_lf, 89 MaskingThresholds mask_hf, 90 float max_inc_factor, 91 float max_dec_factor_lf) 92 : mask_lf(mask_lf), 93 mask_hf(mask_hf), 94 max_inc_factor(max_inc_factor), 95 max_dec_factor_lf(max_dec_factor_lf) {} 96 EchoCanceller3Config::Suppressor::Tuning::Tuning( 97 const EchoCanceller3Config::Suppressor::Tuning& e) = default; 98 EchoCanceller3Config::Suppressor::Tuning& 99 EchoCanceller3Config::Suppressor::Tuning::operator=(const Tuning& e) = default; 100 101 bool EchoCanceller3Config::Validate(EchoCanceller3Config* config) { 102 RTC_DCHECK(config); 103 EchoCanceller3Config* c = config; 104 bool res = true; 105 106 if (c->delay.down_sampling_factor != 4 && 107 c->delay.down_sampling_factor != 8) { 108 c->delay.down_sampling_factor = 4; 109 res = false; 110 } 111 112 res = res & Limit(&c->delay.default_delay, 0, 5000); 113 res = res & Limit(&c->delay.num_filters, 0, 5000); 114 res = res & Limit(&c->delay.delay_headroom_samples, 0, 5000); 115 res = res & Limit(&c->delay.hysteresis_limit_blocks, 0, 5000); 116 res = res & Limit(&c->delay.fixed_capture_delay_samples, 0, 5000); 117 res = res & Limit(&c->delay.delay_estimate_smoothing, 0.f, 1.f); 118 res = res & Limit(&c->delay.delay_candidate_detection_threshold, 0.f, 1.f); 119 res = res & Limit(&c->delay.delay_selection_thresholds.initial, 1, 250); 120 res = res & Limit(&c->delay.delay_selection_thresholds.converged, 1, 250); 121 122 res = res & FloorLimit(&c->filter.refined.length_blocks, 1); 123 res = res & Limit(&c->filter.refined.leakage_converged, 0.f, 1000.f); 124 res = res & Limit(&c->filter.refined.leakage_diverged, 0.f, 1000.f); 125 res = res & Limit(&c->filter.refined.error_floor, 0.f, 1000.f); 126 res = res & Limit(&c->filter.refined.error_ceil, 0.f, 100000000.f); 127 res = res & Limit(&c->filter.refined.noise_gate, 0.f, 100000000.f); 128 129 res = res & FloorLimit(&c->filter.refined_initial.length_blocks, 1); 130 res = res & Limit(&c->filter.refined_initial.leakage_converged, 0.f, 1000.f); 131 res = res & Limit(&c->filter.refined_initial.leakage_diverged, 0.f, 1000.f); 132 res = res & Limit(&c->filter.refined_initial.error_floor, 0.f, 1000.f); 133 res = res & Limit(&c->filter.refined_initial.error_ceil, 0.f, 100000000.f); 134 res = res & Limit(&c->filter.refined_initial.noise_gate, 0.f, 100000000.f); 135 136 if (c->filter.refined.length_blocks < 137 c->filter.refined_initial.length_blocks) { 138 c->filter.refined_initial.length_blocks = c->filter.refined.length_blocks; 139 res = false; 140 } 141 142 res = res & FloorLimit(&c->filter.coarse.length_blocks, 1); 143 res = res & Limit(&c->filter.coarse.rate, 0.f, 1.f); 144 res = res & Limit(&c->filter.coarse.noise_gate, 0.f, 100000000.f); 145 146 res = res & FloorLimit(&c->filter.coarse_initial.length_blocks, 1); 147 res = res & Limit(&c->filter.coarse_initial.rate, 0.f, 1.f); 148 res = res & Limit(&c->filter.coarse_initial.noise_gate, 0.f, 100000000.f); 149 150 if (c->filter.coarse.length_blocks < c->filter.coarse_initial.length_blocks) { 151 c->filter.coarse_initial.length_blocks = c->filter.coarse.length_blocks; 152 res = false; 153 } 154 155 res = res & Limit(&c->filter.config_change_duration_blocks, 0, 100000); 156 res = res & Limit(&c->filter.initial_state_seconds, 0.f, 100.f); 157 res = res & Limit(&c->filter.coarse_reset_hangover_blocks, 0, 250000); 158 159 res = res & Limit(&c->erle.min, 1.f, 100000.f); 160 res = res & Limit(&c->erle.max_l, 1.f, 100000.f); 161 res = res & Limit(&c->erle.max_h, 1.f, 100000.f); 162 if (c->erle.min > c->erle.max_l || c->erle.min > c->erle.max_h) { 163 c->erle.min = std::min(c->erle.max_l, c->erle.max_h); 164 res = false; 165 } 166 res = res & Limit(&c->erle.num_sections, 1, c->filter.refined.length_blocks); 167 168 res = res & Limit(&c->ep_strength.default_gain, 0.f, 1000000.f); 169 res = res & Limit(&c->ep_strength.default_len, -1.f, 1.f); 170 res = res & Limit(&c->ep_strength.nearend_len, -1.0f, 1.0f); 171 172 res = 173 res & Limit(&c->echo_audibility.low_render_limit, 0.f, 32768.f * 32768.f); 174 res = res & 175 Limit(&c->echo_audibility.normal_render_limit, 0.f, 32768.f * 32768.f); 176 res = res & Limit(&c->echo_audibility.floor_power, 0.f, 32768.f * 32768.f); 177 res = res & Limit(&c->echo_audibility.audibility_threshold_lf, 0.f, 178 32768.f * 32768.f); 179 res = res & Limit(&c->echo_audibility.audibility_threshold_mf, 0.f, 180 32768.f * 32768.f); 181 res = res & Limit(&c->echo_audibility.audibility_threshold_hf, 0.f, 182 32768.f * 32768.f); 183 184 res = res & 185 Limit(&c->render_levels.active_render_limit, 0.f, 32768.f * 32768.f); 186 res = res & Limit(&c->render_levels.poor_excitation_render_limit, 0.f, 187 32768.f * 32768.f); 188 res = res & Limit(&c->render_levels.poor_excitation_render_limit_ds8, 0.f, 189 32768.f * 32768.f); 190 191 res = res & Limit(&c->echo_model.noise_floor_hold, 0, 1000); 192 res = res & Limit(&c->echo_model.min_noise_floor_power, 0, 2000000.f); 193 res = res & Limit(&c->echo_model.stationary_gate_slope, 0, 1000000.f); 194 res = res & Limit(&c->echo_model.noise_gate_power, 0, 1000000.f); 195 res = res & Limit(&c->echo_model.noise_gate_slope, 0, 1000000.f); 196 res = res & Limit(&c->echo_model.render_pre_window_size, 0, 100); 197 res = res & Limit(&c->echo_model.render_post_window_size, 0, 100); 198 199 res = res & Limit(&c->comfort_noise.noise_floor_dbfs, -200.f, 0.f); 200 201 res = res & Limit(&c->suppressor.nearend_average_blocks, 1, 5000); 202 203 res = res & 204 Limit(&c->suppressor.normal_tuning.mask_lf.enr_transparent, 0.f, 100.f); 205 res = res & 206 Limit(&c->suppressor.normal_tuning.mask_lf.enr_suppress, 0.f, 100.f); 207 res = res & 208 Limit(&c->suppressor.normal_tuning.mask_lf.emr_transparent, 0.f, 100.f); 209 res = res & 210 Limit(&c->suppressor.normal_tuning.mask_hf.enr_transparent, 0.f, 100.f); 211 res = res & 212 Limit(&c->suppressor.normal_tuning.mask_hf.enr_suppress, 0.f, 100.f); 213 res = res & 214 Limit(&c->suppressor.normal_tuning.mask_hf.emr_transparent, 0.f, 100.f); 215 res = res & Limit(&c->suppressor.normal_tuning.max_inc_factor, 0.f, 100.f); 216 res = res & Limit(&c->suppressor.normal_tuning.max_dec_factor_lf, 0.f, 100.f); 217 218 res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.enr_transparent, 0.f, 219 100.f); 220 res = res & 221 Limit(&c->suppressor.nearend_tuning.mask_lf.enr_suppress, 0.f, 100.f); 222 res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.emr_transparent, 0.f, 223 100.f); 224 res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.enr_transparent, 0.f, 225 100.f); 226 res = res & 227 Limit(&c->suppressor.nearend_tuning.mask_hf.enr_suppress, 0.f, 100.f); 228 res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.emr_transparent, 0.f, 229 100.f); 230 res = res & Limit(&c->suppressor.nearend_tuning.max_inc_factor, 0.f, 100.f); 231 res = 232 res & Limit(&c->suppressor.nearend_tuning.max_dec_factor_lf, 0.f, 100.f); 233 234 res = res & Limit(&c->suppressor.last_permanent_lf_smoothing_band, 0, 64); 235 res = res & Limit(&c->suppressor.last_lf_smoothing_band, 0, 64); 236 res = res & Limit(&c->suppressor.last_lf_band, 0, 63); 237 res = res & 238 Limit(&c->suppressor.first_hf_band, c->suppressor.last_lf_band + 1, 64); 239 240 res = res & Limit(&c->suppressor.dominant_nearend_detection.enr_threshold, 241 0.f, 1000000.f); 242 res = res & Limit(&c->suppressor.dominant_nearend_detection.snr_threshold, 243 0.f, 1000000.f); 244 res = res & Limit(&c->suppressor.dominant_nearend_detection.hold_duration, 0, 245 10000); 246 res = res & Limit(&c->suppressor.dominant_nearend_detection.trigger_threshold, 247 0, 10000); 248 249 res = res & 250 Limit(&c->suppressor.subband_nearend_detection.nearend_average_blocks, 251 1, 1024); 252 res = 253 res & Limit(&c->suppressor.subband_nearend_detection.subband1.low, 0, 65); 254 res = res & Limit(&c->suppressor.subband_nearend_detection.subband1.high, 255 c->suppressor.subband_nearend_detection.subband1.low, 65); 256 res = 257 res & Limit(&c->suppressor.subband_nearend_detection.subband2.low, 0, 65); 258 res = res & Limit(&c->suppressor.subband_nearend_detection.subband2.high, 259 c->suppressor.subband_nearend_detection.subband2.low, 65); 260 res = res & Limit(&c->suppressor.subband_nearend_detection.nearend_threshold, 261 0.f, 1.e24f); 262 res = res & Limit(&c->suppressor.subband_nearend_detection.snr_threshold, 0.f, 263 1.e24f); 264 265 res = res & Limit(&c->suppressor.high_bands_suppression.enr_threshold, 0.f, 266 1000000.f); 267 res = res & Limit(&c->suppressor.high_bands_suppression.max_gain_during_echo, 268 0.f, 1.f); 269 res = res & Limit(&c->suppressor.high_bands_suppression 270 .anti_howling_activation_threshold, 271 0.f, 32768.f * 32768.f); 272 res = res & Limit(&c->suppressor.high_bands_suppression.anti_howling_gain, 273 0.f, 1.f); 274 275 res = 276 res & Limit(&c->suppressor.high_frequency_suppression.limiting_gain_band, 277 1, 64); 278 res = 279 res & 280 Limit(&c->suppressor.high_frequency_suppression.bands_in_limiting_gain, 0, 281 64 - c->suppressor.high_frequency_suppression.limiting_gain_band); 282 283 res = res & Limit(&c->suppressor.floor_first_increase, 0.f, 1000000.f); 284 285 return res; 286 } 287 288 EchoCanceller3Config EchoCanceller3Config::CreateDefaultMultichannelConfig() { 289 EchoCanceller3Config cfg; 290 // Use shorter and more rapidly adapting coarse filter to compensate for 291 // the increased number of total filter parameters to adapt. 292 cfg.filter.coarse.length_blocks = 11; 293 cfg.filter.coarse.rate = 0.95f; 294 cfg.filter.coarse_initial.length_blocks = 11; 295 cfg.filter.coarse_initial.rate = 0.95f; 296 297 // Use more conservative suppressor behavior for non-nearend speech. 298 cfg.suppressor.normal_tuning.max_dec_factor_lf = 0.35f; 299 cfg.suppressor.normal_tuning.max_inc_factor = 1.5f; 300 return cfg; 301 } 302 303 } // namespace webrtc