peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/suppression_gain.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | #include <math.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 16 | #include <algorithm> |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 17 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 18 | |
Gustaf Ullberg | 8406c43 | 2018-06-19 12:31:33 +0200 | [diff] [blame] | 19 | #include "modules/audio_processing/aec3/moving_average.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/aec3/vector_math.h" |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 24 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | namespace { |
| 27 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 28 | void PostprocessGains(std::array<float, kFftLengthBy2Plus1>* gain) { |
| 29 | // TODO(gustaf): Investigate if this can be relaxed to achieve higher |
| 30 | // transparency above 2 kHz. |
| 31 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 32 | // Limit the low frequency gains to avoid the impact of the high-pass filter |
| 33 | // on the lower-frequency gain influencing the overall achieved gain. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 34 | (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]); |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 35 | |
| 36 | // Limit the high frequency gains to avoid the impact of the anti-aliasing |
| 37 | // filter on the upper-frequency gains influencing the overall achieved |
| 38 | // gain. TODO(peah): Update this when new anti-aliasing filters are |
| 39 | // implemented. |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 40 | constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 41 | const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit]; |
| 42 | std::for_each( |
| 43 | gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1, |
| 44 | [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); }); |
| 45 | (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1]; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 46 | |
| 47 | // Limits the gain in the frequencies for which the adaptive filter has not |
| 48 | // converged. |
| 49 | // TODO(peah): Make adaptive to take the actual filter error into account. |
| 50 | constexpr size_t kUpperAccurateBandPlus1 = 29; |
| 51 | |
| 52 | constexpr float oneByBandsInSum = |
| 53 | 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20); |
| 54 | const float hf_gain_bound = |
| 55 | std::accumulate(gain->begin() + 20, |
| 56 | gain->begin() + kUpperAccurateBandPlus1, 0.f) * |
| 57 | oneByBandsInSum; |
| 58 | |
| 59 | std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(), |
| 60 | [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); }); |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 63 | // Scales the echo according to assessed audibility at the other end. |
| 64 | void WeightEchoForAudibility(const EchoCanceller3Config& config, |
| 65 | rtc::ArrayView<const float> echo, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 66 | rtc::ArrayView<float> weighted_echo) { |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 67 | RTC_DCHECK_EQ(kFftLengthBy2Plus1, echo.size()); |
| 68 | RTC_DCHECK_EQ(kFftLengthBy2Plus1, weighted_echo.size()); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 69 | |
| 70 | auto weigh = [](float threshold, float normalizer, size_t begin, size_t end, |
| 71 | rtc::ArrayView<const float> echo, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 72 | rtc::ArrayView<float> weighted_echo) { |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 73 | for (size_t k = begin; k < end; ++k) { |
| 74 | if (echo[k] < threshold) { |
| 75 | float tmp = (threshold - echo[k]) * normalizer; |
| 76 | weighted_echo[k] = echo[k] * std::max(0.f, 1.f - tmp * tmp); |
| 77 | } else { |
| 78 | weighted_echo[k] = echo[k]; |
| 79 | } |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
| 83 | float threshold = config.echo_audibility.floor_power * |
| 84 | config.echo_audibility.audibility_threshold_lf; |
| 85 | float normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 86 | weigh(threshold, normalizer, 0, 3, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 87 | |
| 88 | threshold = config.echo_audibility.floor_power * |
| 89 | config.echo_audibility.audibility_threshold_mf; |
| 90 | normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 91 | weigh(threshold, normalizer, 3, 7, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 92 | |
| 93 | threshold = config.echo_audibility.floor_power * |
| 94 | config.echo_audibility.audibility_threshold_hf; |
| 95 | normalizer = 1.f / (threshold - config.echo_audibility.floor_power); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 96 | weigh(threshold, normalizer, 7, kFftLengthBy2Plus1, echo, weighted_echo); |
Per Åhgren | b02644f | 2018-04-17 11:52:17 +0200 | [diff] [blame] | 97 | } |
| 98 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 99 | } // namespace |
| 100 | |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 101 | int SuppressionGain::instance_count_ = 0; |
| 102 | |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 103 | float SuppressionGain::UpperBandsGain( |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 104 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, |
| 105 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 106 | comfort_noise_spectrum, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 107 | const absl::optional<int>& narrow_peak_band, |
| 108 | bool saturated_echo, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 109 | const std::vector<std::vector<std::vector<float>>>& render, |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 110 | const std::array<float, kFftLengthBy2Plus1>& low_band_gain) const { |
| 111 | RTC_DCHECK_LT(0, render.size()); |
| 112 | if (render.size() == 1) { |
| 113 | return 1.f; |
| 114 | } |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 115 | const size_t num_render_channels = render[0].size(); |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 116 | |
| 117 | if (narrow_peak_band && |
| 118 | (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) { |
| 119 | return 0.001f; |
| 120 | } |
| 121 | |
| 122 | constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2; |
| 123 | const float gain_below_8_khz = *std::min_element( |
| 124 | low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end()); |
| 125 | |
| 126 | // Always attenuate the upper bands when there is saturated echo. |
| 127 | if (saturated_echo) { |
| 128 | return std::min(0.001f, gain_below_8_khz); |
| 129 | } |
| 130 | |
| 131 | // Compute the upper and lower band energies. |
| 132 | const auto sum_of_squares = [](float a, float b) { return a + b * b; }; |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 133 | float low_band_energy = 0.f; |
| 134 | for (size_t ch = 0; ch < num_render_channels; ++ch) { |
| 135 | const float channel_energy = std::accumulate( |
| 136 | render[0][0].begin(), render[0][0].end(), 0.f, sum_of_squares); |
| 137 | low_band_energy = std::max(low_band_energy, channel_energy); |
| 138 | } |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 139 | float high_band_energy = 0.f; |
| 140 | for (size_t k = 1; k < render.size(); ++k) { |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 141 | for (size_t ch = 0; ch < num_render_channels; ++ch) { |
| 142 | const float energy = std::accumulate( |
| 143 | render[k][ch].begin(), render[k][ch].end(), 0.f, sum_of_squares); |
| 144 | high_band_energy = std::max(high_band_energy, energy); |
| 145 | } |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // If there is more power in the lower frequencies than the upper frequencies, |
| 149 | // or if the power in upper frequencies is low, do not bound the gain in the |
| 150 | // upper bands. |
| 151 | float anti_howling_gain; |
| 152 | constexpr float kThreshold = kBlockSize * 10.f * 10.f / 4.f; |
| 153 | if (high_band_energy < std::max(low_band_energy, kThreshold)) { |
| 154 | anti_howling_gain = 1.f; |
| 155 | } else { |
| 156 | // In all other cases, bound the gain for upper frequencies. |
| 157 | RTC_DCHECK_LE(low_band_energy, high_band_energy); |
| 158 | RTC_DCHECK_NE(0.f, high_band_energy); |
| 159 | anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy); |
| 160 | } |
| 161 | |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 162 | float gain_bound = 1.f; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 163 | if (!dominant_nearend_detector_.IsNearendState()) { |
| 164 | // Bound the upper gain during significant echo activity. |
| 165 | const auto& cfg = config_.suppressor.high_bands_suppression; |
| 166 | auto low_frequency_energy = [](rtc::ArrayView<const float> spectrum) { |
| 167 | RTC_DCHECK_LE(16, spectrum.size()); |
| 168 | return std::accumulate(spectrum.begin() + 1, spectrum.begin() + 16, 0.f); |
| 169 | }; |
| 170 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 171 | const float echo_sum = low_frequency_energy(echo_spectrum[ch]); |
| 172 | const float noise_sum = low_frequency_energy(comfort_noise_spectrum[ch]); |
| 173 | if (echo_sum > cfg.enr_threshold * noise_sum) { |
| 174 | gain_bound = cfg.max_gain_during_echo; |
| 175 | break; |
| 176 | } |
| 177 | } |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Choose the gain as the minimum of the lower and upper gains. |
| 181 | return std::min(std::min(gain_below_8_khz, anti_howling_gain), gain_bound); |
| 182 | } |
| 183 | |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 184 | // Computes the gain to reduce the echo to a non audible level. |
| 185 | void SuppressionGain::GainToNoAudibleEcho( |
| 186 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 187 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 188 | const std::array<float, kFftLengthBy2Plus1>& masker, |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 189 | std::array<float, kFftLengthBy2Plus1>* gain) const { |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 190 | const auto& p = dominant_nearend_detector_.IsNearendState() ? nearend_params_ |
| 191 | : normal_params_; |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 192 | for (size_t k = 0; k < gain->size(); ++k) { |
| 193 | float enr = echo[k] / (nearend[k] + 1.f); // Echo-to-nearend ratio. |
| 194 | float emr = echo[k] / (masker[k] + 1.f); // Echo-to-masker (noise) ratio. |
| 195 | float g = 1.0f; |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 196 | if (enr > p.enr_transparent_[k] && emr > p.emr_transparent_[k]) { |
| 197 | g = (p.enr_suppress_[k] - enr) / |
| 198 | (p.enr_suppress_[k] - p.enr_transparent_[k]); |
| 199 | g = std::max(g, p.emr_transparent_[k] / emr); |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 200 | } |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 201 | (*gain)[k] = g; |
Gustaf Ullberg | ec64217 | 2018-07-03 13:48:32 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 205 | // Compute the minimum gain as the attenuating gain to put the signal just |
| 206 | // above the zero sample values. |
| 207 | void SuppressionGain::GetMinGain( |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 208 | rtc::ArrayView<const float> weighted_residual_echo, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 209 | rtc::ArrayView<const float> last_nearend, |
| 210 | rtc::ArrayView<const float> last_echo, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 211 | bool low_noise_render, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 212 | bool saturated_echo, |
| 213 | rtc::ArrayView<float> min_gain) const { |
Per Åhgren | 31122d6 | 2018-04-10 16:33:55 +0200 | [diff] [blame] | 214 | if (!saturated_echo) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 215 | const float min_echo_power = |
| 216 | low_noise_render ? config_.echo_audibility.low_render_limit |
| 217 | : config_.echo_audibility.normal_render_limit; |
| 218 | |
Gustaf Ullberg | 2bab5ad | 2019-04-15 17:15:37 +0200 | [diff] [blame] | 219 | for (size_t k = 0; k < min_gain.size(); ++k) { |
| 220 | min_gain[k] = weighted_residual_echo[k] > 0.f |
| 221 | ? min_echo_power / weighted_residual_echo[k] |
| 222 | : 1.f; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 223 | min_gain[k] = std::min(min_gain[k], 1.f); |
| 224 | } |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 225 | for (size_t k = 0; k < 6; ++k) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 226 | const auto& dec = dominant_nearend_detector_.IsNearendState() |
| 227 | ? nearend_params_.max_dec_factor_lf |
| 228 | : normal_params_.max_dec_factor_lf; |
| 229 | |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 230 | // Make sure the gains of the low frequencies do not decrease too |
| 231 | // quickly after strong nearend. |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 232 | if (last_nearend[k] > last_echo[k]) { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 233 | min_gain[k] = std::max(min_gain[k], last_gain_[k] * dec); |
Gustaf Ullberg | ecb2d56 | 2018-08-23 15:11:38 +0200 | [diff] [blame] | 234 | min_gain[k] = std::min(min_gain[k], 1.f); |
Gustaf Ullberg | 0e6375e | 2018-05-04 11:29:02 +0200 | [diff] [blame] | 235 | } |
| 236 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 237 | } else { |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 238 | std::fill(min_gain.begin(), min_gain.end(), 0.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 239 | } |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 240 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 241 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 242 | // Compute the maximum gain by limiting the gain increase from the previous |
| 243 | // gain. |
| 244 | void SuppressionGain::GetMaxGain(rtc::ArrayView<float> max_gain) const { |
| 245 | const auto& inc = dominant_nearend_detector_.IsNearendState() |
| 246 | ? nearend_params_.max_inc_factor |
| 247 | : normal_params_.max_inc_factor; |
| 248 | const auto& floor = config_.suppressor.floor_first_increase; |
| 249 | for (size_t k = 0; k < max_gain.size(); ++k) { |
| 250 | max_gain[k] = std::min(std::max(last_gain_[k] * inc, floor), 1.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 251 | } |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 254 | void SuppressionGain::LowerBandGain( |
| 255 | bool low_noise_render, |
| 256 | const AecState& aec_state, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 257 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 258 | suppressor_input, |
| 259 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> residual_echo, |
| 260 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> comfort_noise, |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 261 | std::array<float, kFftLengthBy2Plus1>* gain) { |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 262 | gain->fill(1.f); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 263 | const bool saturated_echo = aec_state.SaturatedEcho(); |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 264 | std::array<float, kFftLengthBy2Plus1> max_gain; |
| 265 | GetMaxGain(max_gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 266 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 267 | for (size_t ch = 0; ch < num_capture_channels_; ++ch) { |
| 268 | std::array<float, kFftLengthBy2Plus1> G; |
| 269 | std::array<float, kFftLengthBy2Plus1> nearend; |
| 270 | nearend_smoothers_[ch].Average(suppressor_input[ch], nearend); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 271 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 272 | // Weight echo power in terms of audibility. |
| 273 | std::array<float, kFftLengthBy2Plus1> weighted_residual_echo; |
| 274 | WeightEchoForAudibility(config_, residual_echo[ch], weighted_residual_echo); |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 275 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 276 | std::array<float, kFftLengthBy2Plus1> min_gain; |
| 277 | GetMinGain(weighted_residual_echo, last_nearend_[ch], last_echo_[ch], |
| 278 | low_noise_render, saturated_echo, min_gain); |
| 279 | |
| 280 | GainToNoAudibleEcho(nearend, weighted_residual_echo, comfort_noise[0], &G); |
| 281 | |
| 282 | // Clamp gains. |
| 283 | for (size_t k = 0; k < gain->size(); ++k) { |
| 284 | G[k] = std::max(std::min(G[k], max_gain[k]), min_gain[k]); |
| 285 | (*gain)[k] = std::min((*gain)[k], G[k]); |
| 286 | } |
| 287 | |
| 288 | // Store data required for the gain computation of the next block. |
| 289 | std::copy(nearend.begin(), nearend.end(), last_nearend_[ch].begin()); |
| 290 | std::copy(weighted_residual_echo.begin(), weighted_residual_echo.end(), |
| 291 | last_echo_[ch].begin()); |
| 292 | } |
| 293 | |
| 294 | // Limit high-frequency gains. |
| 295 | PostprocessGains(gain); |
| 296 | |
| 297 | // Store computed gains. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 298 | std::copy(gain->begin(), gain->end(), last_gain_.begin()); |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 299 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 300 | // Transform gains to amplitude domain. |
| 301 | aec3::VectorMath(optimization_).Sqrt(*gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 304 | SuppressionGain::SuppressionGain(const EchoCanceller3Config& config, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 305 | Aec3Optimization optimization, |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 306 | int sample_rate_hz, |
| 307 | size_t num_capture_channels) |
Gustaf Ullberg | 216af84 | 2018-04-26 12:39:11 +0200 | [diff] [blame] | 308 | : data_dumper_( |
| 309 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 310 | optimization_(optimization), |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 311 | config_(config), |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 312 | num_capture_channels_(num_capture_channels), |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 313 | state_change_duration_blocks_( |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 314 | static_cast<int>(config_.filter.config_change_duration_blocks)), |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 315 | last_nearend_(num_capture_channels_, {0}), |
| 316 | last_echo_(num_capture_channels_, {0}), |
| 317 | nearend_smoothers_( |
| 318 | num_capture_channels_, |
| 319 | aec3::MovingAverage(kFftLengthBy2Plus1, |
| 320 | config.suppressor.nearend_average_blocks)), |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 321 | nearend_params_(config_.suppressor.nearend_tuning), |
| 322 | normal_params_(config_.suppressor.normal_tuning), |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 323 | dominant_nearend_detector_(config_.suppressor.dominant_nearend_detection, |
| 324 | num_capture_channels_) { |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 325 | RTC_DCHECK_LT(0, state_change_duration_blocks_); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 326 | last_gain_.fill(1.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 329 | SuppressionGain::~SuppressionGain() = default; |
| 330 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 331 | void SuppressionGain::GetGain( |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 332 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 333 | nearend_spectrum, |
| 334 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> echo_spectrum, |
| 335 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 336 | residual_echo_spectrum, |
| 337 | rtc::ArrayView<const std::array<float, kFftLengthBy2Plus1>> |
| 338 | comfort_noise_spectrum, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 339 | const RenderSignalAnalyzer& render_signal_analyzer, |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 340 | const AecState& aec_state, |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 341 | const std::vector<std::vector<std::vector<float>>>& render, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 342 | float* high_bands_gain, |
| 343 | std::array<float, kFftLengthBy2Plus1>* low_band_gain) { |
| 344 | RTC_DCHECK(high_bands_gain); |
| 345 | RTC_DCHECK(low_band_gain); |
| 346 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 347 | // Update the nearend state selection. |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 348 | dominant_nearend_detector_.Update(nearend_spectrum, residual_echo_spectrum, |
Per Åhgren | 700b4a4 | 2018-10-23 21:21:37 +0200 | [diff] [blame] | 349 | comfort_noise_spectrum, initial_state_); |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 350 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 351 | // Compute gain for the lower band. |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 352 | bool low_noise_render = low_render_detector_.Detect(render); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 353 | LowerBandGain(low_noise_render, aec_state, nearend_spectrum, |
Gustaf Ullberg | 2bab5ad | 2019-04-15 17:15:37 +0200 | [diff] [blame] | 354 | residual_echo_spectrum, comfort_noise_spectrum, low_band_gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 355 | |
Gustaf Ullberg | 0cb4a25 | 2018-04-26 15:45:44 +0200 | [diff] [blame] | 356 | // Compute the gain for the upper bands. |
Jesús de Vicente Peña | 0faf082 | 2018-09-24 12:48:28 +0200 | [diff] [blame] | 357 | const absl::optional<int> narrow_peak_band = |
| 358 | render_signal_analyzer.NarrowPeakBand(); |
| 359 | |
Per Åhgren | fde4aa9 | 2018-08-27 14:19:35 +0200 | [diff] [blame] | 360 | *high_bands_gain = |
| 361 | UpperBandsGain(echo_spectrum, comfort_noise_spectrum, narrow_peak_band, |
| 362 | aec_state.SaturatedEcho(), render, *low_band_gain); |
Per Åhgren | 5f1a31c | 2018-03-08 15:54:41 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void SuppressionGain::SetInitialState(bool state) { |
| 366 | initial_state_ = state; |
| 367 | if (state) { |
| 368 | initial_state_change_counter_ = state_change_duration_blocks_; |
| 369 | } else { |
| 370 | initial_state_change_counter_ = 0; |
| 371 | } |
| 372 | } |
| 373 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 374 | // Detects when the render signal can be considered to have low power and |
| 375 | // consist of stationary noise. |
| 376 | bool SuppressionGain::LowNoiseRenderDetector::Detect( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 377 | const std::vector<std::vector<std::vector<float>>>& render) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 378 | float x2_sum = 0.f; |
| 379 | float x2_max = 0.f; |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 380 | for (auto x_ch : render[0]) { |
| 381 | for (auto x_k : x_ch) { |
| 382 | const float x2 = x_k * x_k; |
| 383 | x2_sum += x2; |
| 384 | x2_max = std::max(x2_max, x2); |
| 385 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 386 | } |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 387 | const size_t num_render_channels = render[0].size(); |
| 388 | x2_sum = x2_sum / num_render_channels; |
| 389 | ; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 390 | |
| 391 | constexpr float kThreshold = 50.f * 50.f * 64.f; |
| 392 | const bool low_noise_render = |
| 393 | average_power_ < kThreshold && x2_max < 3 * average_power_; |
| 394 | average_power_ = average_power_ * 0.9f + x2_sum * 0.1f; |
| 395 | return low_noise_render; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Per Åhgren | 524e878 | 2018-08-24 22:48:49 +0200 | [diff] [blame] | 398 | SuppressionGain::GainParameters::GainParameters( |
| 399 | const EchoCanceller3Config::Suppressor::Tuning& tuning) |
| 400 | : max_inc_factor(tuning.max_inc_factor), |
| 401 | max_dec_factor_lf(tuning.max_dec_factor_lf) { |
| 402 | // Compute per-band masking thresholds. |
| 403 | constexpr size_t kLastLfBand = 5; |
| 404 | constexpr size_t kFirstHfBand = 8; |
| 405 | RTC_DCHECK_LT(kLastLfBand, kFirstHfBand); |
| 406 | auto& lf = tuning.mask_lf; |
| 407 | auto& hf = tuning.mask_hf; |
| 408 | RTC_DCHECK_LT(lf.enr_transparent, lf.enr_suppress); |
| 409 | RTC_DCHECK_LT(hf.enr_transparent, hf.enr_suppress); |
| 410 | for (size_t k = 0; k < kFftLengthBy2Plus1; k++) { |
| 411 | float a; |
| 412 | if (k <= kLastLfBand) { |
| 413 | a = 0.f; |
| 414 | } else if (k < kFirstHfBand) { |
| 415 | a = (k - kLastLfBand) / static_cast<float>(kFirstHfBand - kLastLfBand); |
| 416 | } else { |
| 417 | a = 1.f; |
| 418 | } |
| 419 | enr_transparent_[k] = (1 - a) * lf.enr_transparent + a * hf.enr_transparent; |
| 420 | enr_suppress_[k] = (1 - a) * lf.enr_suppress + a * hf.enr_suppress; |
| 421 | emr_transparent_[k] = (1 - a) * lf.emr_transparent + a * hf.emr_transparent; |
| 422 | } |
| 423 | } |
| 424 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 425 | } // namespace webrtc |