You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
9.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include <algorithm>
  12. #include <cmath>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/numerics/safe_minmax.h"
  15. namespace webrtc {
  16. namespace {
  17. bool Limit(float* value, float min, float max) {
  18. float clamped = rtc::SafeClamp(*value, min, max);
  19. clamped = std::isfinite(clamped) ? clamped : min;
  20. bool res = *value == clamped;
  21. *value = clamped;
  22. return res;
  23. }
  24. bool Limit(size_t* value, size_t min, size_t max) {
  25. size_t clamped = rtc::SafeClamp(*value, min, max);
  26. bool res = *value == clamped;
  27. *value = clamped;
  28. return res;
  29. }
  30. bool Limit(int* value, int min, int max) {
  31. int clamped = rtc::SafeClamp(*value, min, max);
  32. bool res = *value == clamped;
  33. *value = clamped;
  34. return res;
  35. }
  36. } // namespace
  37. EchoCanceller3Config::EchoCanceller3Config() = default;
  38. EchoCanceller3Config::EchoCanceller3Config(const EchoCanceller3Config& e) =
  39. default;
  40. EchoCanceller3Config::Delay::Delay() = default;
  41. EchoCanceller3Config::Delay::Delay(const EchoCanceller3Config::Delay& e) =
  42. default;
  43. EchoCanceller3Config::EchoModel::EchoModel() = default;
  44. EchoCanceller3Config::EchoModel::EchoModel(
  45. const EchoCanceller3Config::EchoModel& e) = default;
  46. EchoCanceller3Config::Suppressor::Suppressor() = default;
  47. EchoCanceller3Config::Suppressor::Suppressor(
  48. const EchoCanceller3Config::Suppressor& e) = default;
  49. EchoCanceller3Config::Suppressor::MaskingThresholds::MaskingThresholds(
  50. float enr_transparent,
  51. float enr_suppress,
  52. float emr_transparent)
  53. : enr_transparent(enr_transparent),
  54. enr_suppress(enr_suppress),
  55. emr_transparent(emr_transparent) {}
  56. EchoCanceller3Config::Suppressor::Suppressor::MaskingThresholds::
  57. MaskingThresholds(
  58. const EchoCanceller3Config::Suppressor::MaskingThresholds& e) = default;
  59. EchoCanceller3Config::Suppressor::Tuning::Tuning(MaskingThresholds mask_lf,
  60. MaskingThresholds mask_hf,
  61. float max_inc_factor,
  62. float max_dec_factor_lf)
  63. : mask_lf(mask_lf),
  64. mask_hf(mask_hf),
  65. max_inc_factor(max_inc_factor),
  66. max_dec_factor_lf(max_dec_factor_lf) {}
  67. EchoCanceller3Config::Suppressor::Tuning::Tuning(
  68. const EchoCanceller3Config::Suppressor::Tuning& e) = default;
  69. bool EchoCanceller3Config::Validate(EchoCanceller3Config* config) {
  70. RTC_DCHECK(config);
  71. EchoCanceller3Config* c = config;
  72. bool res = true;
  73. if (c->delay.down_sampling_factor != 4 &&
  74. c->delay.down_sampling_factor != 8) {
  75. c->delay.down_sampling_factor = 4;
  76. res = false;
  77. }
  78. res = res & Limit(&c->delay.default_delay, 0, 5000);
  79. res = res & Limit(&c->delay.num_filters, 0, 5000);
  80. res = res & Limit(&c->delay.delay_headroom_samples, 0, 5000);
  81. res = res & Limit(&c->delay.hysteresis_limit_blocks, 0, 5000);
  82. res = res & Limit(&c->delay.fixed_capture_delay_samples, 0, 5000);
  83. res = res & Limit(&c->delay.delay_estimate_smoothing, 0.f, 1.f);
  84. res = res & Limit(&c->delay.delay_candidate_detection_threshold, 0.f, 1.f);
  85. res = res & Limit(&c->delay.delay_selection_thresholds.initial, 1, 250);
  86. res = res & Limit(&c->delay.delay_selection_thresholds.converged, 1, 250);
  87. res = res & Limit(&c->filter.main.length_blocks, 1, 50);
  88. res = res & Limit(&c->filter.main.leakage_converged, 0.f, 1000.f);
  89. res = res & Limit(&c->filter.main.leakage_diverged, 0.f, 1000.f);
  90. res = res & Limit(&c->filter.main.error_floor, 0.f, 1000.f);
  91. res = res & Limit(&c->filter.main.error_ceil, 0.f, 100000000.f);
  92. res = res & Limit(&c->filter.main.noise_gate, 0.f, 100000000.f);
  93. res = res & Limit(&c->filter.main_initial.length_blocks, 1, 50);
  94. res = res & Limit(&c->filter.main_initial.leakage_converged, 0.f, 1000.f);
  95. res = res & Limit(&c->filter.main_initial.leakage_diverged, 0.f, 1000.f);
  96. res = res & Limit(&c->filter.main_initial.error_floor, 0.f, 1000.f);
  97. res = res & Limit(&c->filter.main_initial.error_ceil, 0.f, 100000000.f);
  98. res = res & Limit(&c->filter.main_initial.noise_gate, 0.f, 100000000.f);
  99. if (c->filter.main.length_blocks < c->filter.main_initial.length_blocks) {
  100. c->filter.main_initial.length_blocks = c->filter.main.length_blocks;
  101. res = false;
  102. }
  103. res = res & Limit(&c->filter.shadow.length_blocks, 1, 50);
  104. res = res & Limit(&c->filter.shadow.rate, 0.f, 1.f);
  105. res = res & Limit(&c->filter.shadow.noise_gate, 0.f, 100000000.f);
  106. res = res & Limit(&c->filter.shadow_initial.length_blocks, 1, 50);
  107. res = res & Limit(&c->filter.shadow_initial.rate, 0.f, 1.f);
  108. res = res & Limit(&c->filter.shadow_initial.noise_gate, 0.f, 100000000.f);
  109. if (c->filter.shadow.length_blocks < c->filter.shadow_initial.length_blocks) {
  110. c->filter.shadow_initial.length_blocks = c->filter.shadow.length_blocks;
  111. res = false;
  112. }
  113. res = res & Limit(&c->filter.config_change_duration_blocks, 0, 100000);
  114. res = res & Limit(&c->filter.initial_state_seconds, 0.f, 100.f);
  115. res = res & Limit(&c->erle.min, 1.f, 100000.f);
  116. res = res & Limit(&c->erle.max_l, 1.f, 100000.f);
  117. res = res & Limit(&c->erle.max_h, 1.f, 100000.f);
  118. if (c->erle.min > c->erle.max_l || c->erle.min > c->erle.max_h) {
  119. c->erle.min = std::min(c->erle.max_l, c->erle.max_h);
  120. res = false;
  121. }
  122. res = res & Limit(&c->erle.num_sections, 1, c->filter.main.length_blocks);
  123. res = res & Limit(&c->ep_strength.default_gain, 0.f, 1000000.f);
  124. res = res & Limit(&c->ep_strength.default_len, -1.f, 1.f);
  125. res =
  126. res & Limit(&c->echo_audibility.low_render_limit, 0.f, 32768.f * 32768.f);
  127. res = res &
  128. Limit(&c->echo_audibility.normal_render_limit, 0.f, 32768.f * 32768.f);
  129. res = res & Limit(&c->echo_audibility.floor_power, 0.f, 32768.f * 32768.f);
  130. res = res & Limit(&c->echo_audibility.audibility_threshold_lf, 0.f,
  131. 32768.f * 32768.f);
  132. res = res & Limit(&c->echo_audibility.audibility_threshold_mf, 0.f,
  133. 32768.f * 32768.f);
  134. res = res & Limit(&c->echo_audibility.audibility_threshold_hf, 0.f,
  135. 32768.f * 32768.f);
  136. res = res &
  137. Limit(&c->render_levels.active_render_limit, 0.f, 32768.f * 32768.f);
  138. res = res & Limit(&c->render_levels.poor_excitation_render_limit, 0.f,
  139. 32768.f * 32768.f);
  140. res = res & Limit(&c->render_levels.poor_excitation_render_limit_ds8, 0.f,
  141. 32768.f * 32768.f);
  142. res = res & Limit(&c->echo_model.noise_floor_hold, 0, 1000);
  143. res = res & Limit(&c->echo_model.min_noise_floor_power, 0, 2000000.f);
  144. res = res & Limit(&c->echo_model.stationary_gate_slope, 0, 1000000.f);
  145. res = res & Limit(&c->echo_model.noise_gate_power, 0, 1000000.f);
  146. res = res & Limit(&c->echo_model.noise_gate_slope, 0, 1000000.f);
  147. res = res & Limit(&c->echo_model.render_pre_window_size, 0, 100);
  148. res = res & Limit(&c->echo_model.render_post_window_size, 0, 100);
  149. res = res & Limit(&c->suppressor.nearend_average_blocks, 1, 5000);
  150. res = res &
  151. Limit(&c->suppressor.normal_tuning.mask_lf.enr_transparent, 0.f, 100.f);
  152. res = res &
  153. Limit(&c->suppressor.normal_tuning.mask_lf.enr_suppress, 0.f, 100.f);
  154. res = res &
  155. Limit(&c->suppressor.normal_tuning.mask_lf.emr_transparent, 0.f, 100.f);
  156. res = res &
  157. Limit(&c->suppressor.normal_tuning.mask_hf.enr_transparent, 0.f, 100.f);
  158. res = res &
  159. Limit(&c->suppressor.normal_tuning.mask_hf.enr_suppress, 0.f, 100.f);
  160. res = res &
  161. Limit(&c->suppressor.normal_tuning.mask_hf.emr_transparent, 0.f, 100.f);
  162. res = res & Limit(&c->suppressor.normal_tuning.max_inc_factor, 0.f, 100.f);
  163. res = res & Limit(&c->suppressor.normal_tuning.max_dec_factor_lf, 0.f, 100.f);
  164. res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.enr_transparent, 0.f,
  165. 100.f);
  166. res = res &
  167. Limit(&c->suppressor.nearend_tuning.mask_lf.enr_suppress, 0.f, 100.f);
  168. res = res & Limit(&c->suppressor.nearend_tuning.mask_lf.emr_transparent, 0.f,
  169. 100.f);
  170. res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.enr_transparent, 0.f,
  171. 100.f);
  172. res = res &
  173. Limit(&c->suppressor.nearend_tuning.mask_hf.enr_suppress, 0.f, 100.f);
  174. res = res & Limit(&c->suppressor.nearend_tuning.mask_hf.emr_transparent, 0.f,
  175. 100.f);
  176. res = res & Limit(&c->suppressor.nearend_tuning.max_inc_factor, 0.f, 100.f);
  177. res =
  178. res & Limit(&c->suppressor.nearend_tuning.max_dec_factor_lf, 0.f, 100.f);
  179. res = res & Limit(&c->suppressor.dominant_nearend_detection.enr_threshold,
  180. 0.f, 1000000.f);
  181. res = res & Limit(&c->suppressor.dominant_nearend_detection.snr_threshold,
  182. 0.f, 1000000.f);
  183. res = res & Limit(&c->suppressor.dominant_nearend_detection.hold_duration, 0,
  184. 10000);
  185. res = res & Limit(&c->suppressor.dominant_nearend_detection.trigger_threshold,
  186. 0, 10000);
  187. res = res & Limit(&c->suppressor.high_bands_suppression.enr_threshold, 0.f,
  188. 1000000.f);
  189. res = res & Limit(&c->suppressor.high_bands_suppression.max_gain_during_echo,
  190. 0.f, 1.f);
  191. res = res & Limit(&c->suppressor.floor_first_increase, 0.f, 1000000.f);
  192. return res;
  193. }
  194. } // namespace webrtc