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 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 13 | #include "typedefs.h" // NOLINT(build/include) |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 14 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 15 | #include <emmintrin.h> |
| 16 | #endif |
| 17 | #include <math.h> |
| 18 | #include <algorithm> |
| 19 | #include <functional> |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 20 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/vector_math.h" |
| 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 | |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 28 | // Reduce gain to avoid narrow band echo leakage. |
| 29 | void NarrowBandAttenuation(int narrow_bin, |
| 30 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 31 | const int upper_bin = |
| 32 | std::min(narrow_bin + 6, static_cast<int>(kFftLengthBy2Plus1 - 1)); |
| 33 | for (int k = std::max(0, narrow_bin - 6); k <= upper_bin; ++k) { |
| 34 | (*gain)[k] = std::min((*gain)[k], 0.001f); |
| 35 | } |
| 36 | } |
| 37 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 38 | // Adjust the gains according to the presence of known external filters. |
| 39 | void AdjustForExternalFilters(std::array<float, kFftLengthBy2Plus1>* gain) { |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 40 | // Limit the low frequency gains to avoid the impact of the high-pass filter |
| 41 | // on the lower-frequency gain influencing the overall achieved gain. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 42 | (*gain)[0] = (*gain)[1] = std::min((*gain)[1], (*gain)[2]); |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 43 | |
| 44 | // Limit the high frequency gains to avoid the impact of the anti-aliasing |
| 45 | // filter on the upper-frequency gains influencing the overall achieved |
| 46 | // gain. TODO(peah): Update this when new anti-aliasing filters are |
| 47 | // implemented. |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 48 | constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 49 | const float min_upper_gain = (*gain)[kAntiAliasingImpactLimit]; |
| 50 | std::for_each( |
| 51 | gain->begin() + kAntiAliasingImpactLimit, gain->end() - 1, |
| 52 | [min_upper_gain](float& a) { a = std::min(a, min_upper_gain); }); |
| 53 | (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2Minus1]; |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 54 | } |
| 55 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 56 | // Computes the gain to apply for the bands beyond the first band. |
| 57 | float UpperBandsGain( |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 58 | const rtc::Optional<int>& narrow_peak_band, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 59 | bool saturated_echo, |
| 60 | const std::vector<std::vector<float>>& render, |
| 61 | const std::array<float, kFftLengthBy2Plus1>& low_band_gain) { |
| 62 | RTC_DCHECK_LT(0, render.size()); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 63 | if (render.size() == 1) { |
| 64 | return 1.f; |
| 65 | } |
| 66 | |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 67 | if (narrow_peak_band && |
| 68 | (*narrow_peak_band > static_cast<int>(kFftLengthBy2Plus1 - 10))) { |
| 69 | return 0.001f; |
| 70 | } |
| 71 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 72 | constexpr size_t kLowBandGainLimit = kFftLengthBy2 / 2; |
| 73 | const float gain_below_8_khz = *std::min_element( |
| 74 | low_band_gain.begin() + kLowBandGainLimit, low_band_gain.end()); |
| 75 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 76 | // Always attenuate the upper bands when there is saturated echo. |
| 77 | if (saturated_echo) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 78 | return std::min(0.001f, gain_below_8_khz); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Compute the upper and lower band energies. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 82 | const auto sum_of_squares = [](float a, float b) { return a + b * b; }; |
| 83 | const float low_band_energy = |
| 84 | std::accumulate(render[0].begin(), render[0].end(), 0.f, sum_of_squares); |
| 85 | float high_band_energy = 0.f; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 86 | for (size_t k = 1; k < render.size(); ++k) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 87 | const float energy = std::accumulate(render[k].begin(), render[k].end(), |
| 88 | 0.f, sum_of_squares); |
| 89 | high_band_energy = std::max(high_band_energy, energy); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // If there is more power in the lower frequencies than the upper frequencies, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 93 | // or if the power in upper frequencies is low, do not bound the gain in the |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 94 | // upper bands. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 95 | float anti_howling_gain; |
| 96 | constexpr float kThreshold = kSubBlockSize * 10.f * 10.f; |
| 97 | if (high_band_energy < std::max(low_band_energy, kThreshold)) { |
| 98 | anti_howling_gain = 1.f; |
| 99 | } else { |
| 100 | // In all other cases, bound the gain for upper frequencies. |
| 101 | RTC_DCHECK_LE(low_band_energy, high_band_energy); |
| 102 | RTC_DCHECK_NE(0.f, high_band_energy); |
| 103 | anti_howling_gain = 0.01f * sqrtf(low_band_energy / high_band_energy); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 104 | } |
| 105 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 106 | // Choose the gain as the minimum of the lower and upper gains. |
| 107 | return std::min(gain_below_8_khz, anti_howling_gain); |
| 108 | } |
| 109 | |
| 110 | // Limits the gain increase. |
| 111 | void UpdateMaxGainIncrease( |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 112 | const AudioProcessing::Config::EchoCanceller3& config, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 113 | size_t no_saturation_counter, |
| 114 | bool low_noise_render, |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 115 | bool linear_echo_estimate, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 116 | const std::array<float, kFftLengthBy2Plus1>& last_echo, |
| 117 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 118 | const std::array<float, kFftLengthBy2Plus1>& last_gain, |
| 119 | const std::array<float, kFftLengthBy2Plus1>& new_gain, |
| 120 | std::array<float, kFftLengthBy2Plus1>* gain_increase) { |
| 121 | float max_increasing; |
| 122 | float max_decreasing; |
| 123 | float rate_increasing; |
| 124 | float rate_decreasing; |
| 125 | float min_increasing; |
| 126 | float min_decreasing; |
| 127 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 128 | auto& param = config.param.gain_updates; |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 129 | if (!linear_echo_estimate) { |
| 130 | max_increasing = param.nonlinear.max_inc; |
| 131 | max_decreasing = param.nonlinear.max_dec; |
| 132 | rate_increasing = param.nonlinear.rate_inc; |
| 133 | rate_decreasing = param.nonlinear.rate_dec; |
| 134 | min_increasing = param.nonlinear.min_inc; |
| 135 | min_decreasing = param.nonlinear.min_dec; |
| 136 | } else if (low_noise_render) { |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 137 | max_increasing = param.low_noise.max_inc; |
| 138 | max_decreasing = param.low_noise.max_dec; |
| 139 | rate_increasing = param.low_noise.rate_inc; |
| 140 | rate_decreasing = param.low_noise.rate_dec; |
| 141 | min_increasing = param.low_noise.min_inc; |
| 142 | min_decreasing = param.low_noise.min_dec; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 143 | } else if (no_saturation_counter > 10) { |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 144 | max_increasing = param.normal.max_inc; |
| 145 | max_decreasing = param.normal.max_dec; |
| 146 | rate_increasing = param.normal.rate_inc; |
| 147 | rate_decreasing = param.normal.rate_dec; |
| 148 | min_increasing = param.normal.min_inc; |
| 149 | min_decreasing = param.normal.min_dec; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 150 | } else { |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 151 | max_increasing = param.saturation.max_inc; |
| 152 | max_decreasing = param.saturation.max_dec; |
| 153 | rate_increasing = param.saturation.rate_inc; |
| 154 | rate_decreasing = param.saturation.rate_dec; |
| 155 | min_increasing = param.saturation.min_inc; |
| 156 | min_decreasing = param.saturation.min_dec; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | for (size_t k = 0; k < new_gain.size(); ++k) { |
| 160 | if (echo[k] > last_echo[k]) { |
| 161 | (*gain_increase)[k] = |
| 162 | new_gain[k] > last_gain[k] |
| 163 | ? std::min(max_increasing, (*gain_increase)[k] * rate_increasing) |
| 164 | : min_increasing; |
| 165 | } else { |
| 166 | (*gain_increase)[k] = |
| 167 | new_gain[k] > last_gain[k] |
| 168 | ? std::min(max_decreasing, (*gain_increase)[k] * rate_decreasing) |
| 169 | : min_decreasing; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Computes the gain to reduce the echo to a non audible level. |
| 175 | void GainToNoAudibleEcho( |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 176 | const AudioProcessing::Config::EchoCanceller3& config, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 177 | bool low_noise_render, |
| 178 | bool saturated_echo, |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 179 | bool linear_echo_estimate, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 180 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 181 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 182 | const std::array<float, kFftLengthBy2Plus1>& masker, |
| 183 | const std::array<float, kFftLengthBy2Plus1>& min_gain, |
| 184 | const std::array<float, kFftLengthBy2Plus1>& max_gain, |
| 185 | const std::array<float, kFftLengthBy2Plus1>& one_by_echo, |
| 186 | std::array<float, kFftLengthBy2Plus1>* gain) { |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 187 | float nearend_masking_margin = 0.f; |
| 188 | if (linear_echo_estimate) { |
| 189 | nearend_masking_margin = low_noise_render |
Per Åhgren | d309b00 | 2017-10-09 23:50:44 +0200 | [diff] [blame] | 190 | ? config.param.gain_mask.m9 |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 191 | : (saturated_echo ? config.param.gain_mask.m2 |
| 192 | : config.param.gain_mask.m3); |
| 193 | } else { |
| 194 | nearend_masking_margin = config.param.gain_mask.m7; |
| 195 | } |
Per Åhgren | d309b00 | 2017-10-09 23:50:44 +0200 | [diff] [blame] | 196 | RTC_DCHECK_LE(0.f, nearend_masking_margin); |
| 197 | RTC_DCHECK_GT(1.f, nearend_masking_margin); |
| 198 | const float one_by_one_minus_nearend_masking_margin = |
| 199 | 1.f / (1.0f - nearend_masking_margin); |
| 200 | |
| 201 | const float masker_margin = linear_echo_estimate ? config.param.gain_mask.m1 |
| 202 | : config.param.gain_mask.m8; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 203 | |
| 204 | for (size_t k = 0; k < gain->size(); ++k) { |
Per Åhgren | 7106d93 | 2017-10-09 08:25:18 +0200 | [diff] [blame] | 205 | const float unity_gain_masker = std::max(nearend[k], masker[k]); |
| 206 | RTC_DCHECK_LE(0.f, nearend_masking_margin * unity_gain_masker); |
| 207 | if (echo[k] <= nearend_masking_margin * unity_gain_masker || |
| 208 | unity_gain_masker <= 0.f) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 209 | (*gain)[k] = 1.f; |
| 210 | } else { |
Per Åhgren | d309b00 | 2017-10-09 23:50:44 +0200 | [diff] [blame] | 211 | RTC_DCHECK_LT(0.f, unity_gain_masker); |
| 212 | (*gain)[k] = std::max(0.f, (1.f - echo[k] / unity_gain_masker) * |
| 213 | one_by_one_minus_nearend_masking_margin); |
| 214 | (*gain)[k] = |
| 215 | std::max(masker_margin * masker[k] * one_by_echo[k], (*gain)[k]); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | (*gain)[k] = std::min(std::max((*gain)[k], min_gain[k]), max_gain[k]); |
| 219 | } |
| 220 | } |
| 221 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 222 | // TODO(peah): Make adaptive to take the actual filter error into account. |
| 223 | constexpr size_t kUpperAccurateBandPlus1 = 29; |
| 224 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 225 | // Computes the signal output power that masks the echo signal. |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 226 | void MaskingPower(const AudioProcessing::Config::EchoCanceller3& config, |
| 227 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 228 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise, |
| 229 | const std::array<float, kFftLengthBy2Plus1>& last_masker, |
| 230 | const std::array<float, kFftLengthBy2Plus1>& gain, |
| 231 | std::array<float, kFftLengthBy2Plus1>* masker) { |
| 232 | std::array<float, kFftLengthBy2Plus1> side_band_masker; |
Per Åhgren | 7106d93 | 2017-10-09 08:25:18 +0200 | [diff] [blame] | 233 | float max_nearend_after_gain = 0.f; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 234 | for (size_t k = 0; k < gain.size(); ++k) { |
Per Åhgren | 7106d93 | 2017-10-09 08:25:18 +0200 | [diff] [blame] | 235 | const float nearend_after_gain = nearend[k] * gain[k]; |
| 236 | max_nearend_after_gain = |
| 237 | std::max(max_nearend_after_gain, nearend_after_gain); |
| 238 | side_band_masker[k] = nearend_after_gain + comfort_noise[k]; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 239 | (*masker)[k] = |
| 240 | comfort_noise[k] + config.param.gain_mask.m4 * last_masker[k]; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 241 | } |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 242 | |
| 243 | // Apply masking only between lower frequency bands. |
| 244 | RTC_DCHECK_LT(kUpperAccurateBandPlus1, gain.size()); |
| 245 | for (size_t k = 1; k < kUpperAccurateBandPlus1; ++k) { |
Per Åhgren | 7106d93 | 2017-10-09 08:25:18 +0200 | [diff] [blame] | 246 | (*masker)[k] += config.param.gain_mask.m5 * |
| 247 | (side_band_masker[k - 1] + side_band_masker[k + 1]); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 248 | } |
Per Åhgren | 7106d93 | 2017-10-09 08:25:18 +0200 | [diff] [blame] | 249 | |
| 250 | // Add full-band masking as a minimum value for the masker. |
| 251 | const float min_masker = max_nearend_after_gain * config.param.gain_mask.m6; |
| 252 | std::for_each(masker->begin(), masker->end(), |
| 253 | [min_masker](float& a) { a = std::max(a, min_masker); }); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 256 | // Limits the gain in the frequencies for which the adaptive filter has not |
| 257 | // converged. Currently, these frequencies are not hardcoded to the frequencies |
| 258 | // which are typically not excited by speech. |
| 259 | // TODO(peah): Make adaptive to take the actual filter error into account. |
| 260 | void AdjustNonConvergedFrequencies( |
| 261 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 262 | constexpr float oneByBandsInSum = |
| 263 | 1 / static_cast<float>(kUpperAccurateBandPlus1 - 20); |
| 264 | const float hf_gain_bound = |
| 265 | std::accumulate(gain->begin() + 20, |
| 266 | gain->begin() + kUpperAccurateBandPlus1, 0.f) * |
| 267 | oneByBandsInSum; |
| 268 | |
| 269 | std::for_each(gain->begin() + kUpperAccurateBandPlus1, gain->end(), |
| 270 | [hf_gain_bound](float& a) { a = std::min(a, hf_gain_bound); }); |
| 271 | } |
| 272 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 273 | } // namespace |
| 274 | |
| 275 | // TODO(peah): Add further optimizations, in particular for the divisions. |
| 276 | void SuppressionGain::LowerBandGain( |
| 277 | bool low_noise_render, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 278 | const rtc::Optional<int>& narrow_peak_band, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 279 | bool saturated_echo, |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 280 | bool linear_echo_estimate, |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 281 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 282 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 283 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise, |
| 284 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 285 | // Count the number of blocks since saturation. |
| 286 | no_saturation_counter_ = saturated_echo ? 0 : no_saturation_counter_ + 1; |
| 287 | |
| 288 | // Precompute 1/echo (note that when the echo is zero, the precomputed value |
| 289 | // is never used). |
| 290 | std::array<float, kFftLengthBy2Plus1> one_by_echo; |
| 291 | std::transform(echo.begin(), echo.end(), one_by_echo.begin(), |
| 292 | [](float a) { return a > 0.f ? 1.f / a : 1.f; }); |
| 293 | |
| 294 | // Compute the minimum gain as the attenuating gain to put the signal just |
| 295 | // above the zero sample values. |
| 296 | std::array<float, kFftLengthBy2Plus1> min_gain; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 297 | const float min_echo_power = |
| 298 | low_noise_render ? config_.param.echo_audibility.low_render_limit |
| 299 | : config_.param.echo_audibility.normal_render_limit; |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 300 | if (no_saturation_counter_ > 10) { |
| 301 | for (size_t k = 0; k < nearend.size(); ++k) { |
| 302 | const float denom = std::min(nearend[k], echo[k]); |
| 303 | min_gain[k] = denom > 0.f ? min_echo_power / denom : 1.f; |
| 304 | min_gain[k] = std::min(min_gain[k], 1.f); |
| 305 | } |
| 306 | } else { |
| 307 | min_gain.fill(0.f); |
| 308 | } |
| 309 | |
| 310 | // Compute the maximum gain by limiting the gain increase from the previous |
| 311 | // gain. |
| 312 | std::array<float, kFftLengthBy2Plus1> max_gain; |
| 313 | for (size_t k = 0; k < gain->size(); ++k) { |
| 314 | max_gain[k] = |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 315 | std::min(std::max(last_gain_[k] * gain_increase_[k], |
| 316 | config_.param.gain_updates.floor_first_increase), |
| 317 | 1.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Iteratively compute the gain required to attenuate the echo to a non |
| 321 | // noticeable level. |
| 322 | gain->fill(0.f); |
| 323 | for (int k = 0; k < 2; ++k) { |
| 324 | std::array<float, kFftLengthBy2Plus1> masker; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 325 | MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, &masker); |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 326 | GainToNoAudibleEcho(config_, low_noise_render, saturated_echo, |
| 327 | linear_echo_estimate, nearend, echo, masker, min_gain, |
| 328 | max_gain, one_by_echo, gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 329 | AdjustForExternalFilters(gain); |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 330 | if (narrow_peak_band) { |
| 331 | NarrowBandAttenuation(*narrow_peak_band, gain); |
| 332 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Per Åhgren | 85a11a3 | 2017-10-02 14:42:06 +0200 | [diff] [blame] | 335 | // Adjust the gain for frequencies which have not yet converged. |
| 336 | AdjustNonConvergedFrequencies(gain); |
| 337 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 338 | // Update the allowed maximum gain increase. |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 339 | UpdateMaxGainIncrease(config_, no_saturation_counter_, low_noise_render, |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 340 | linear_echo_estimate, last_echo_, echo, last_gain_, |
| 341 | *gain, &gain_increase_); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 342 | |
| 343 | // Store data required for the gain computation of the next block. |
| 344 | std::copy(echo.begin(), echo.end(), last_echo_.begin()); |
| 345 | std::copy(gain->begin(), gain->end(), last_gain_.begin()); |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 346 | MaskingPower(config_, nearend, comfort_noise, last_masker_, *gain, |
| 347 | &last_masker_); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 348 | aec3::VectorMath(optimization_).Sqrt(*gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 349 | } |
| 350 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 351 | SuppressionGain::SuppressionGain( |
| 352 | const AudioProcessing::Config::EchoCanceller3& config, |
| 353 | Aec3Optimization optimization) |
| 354 | : optimization_(optimization), config_(config) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 355 | last_gain_.fill(1.f); |
| 356 | last_masker_.fill(0.f); |
| 357 | gain_increase_.fill(1.f); |
| 358 | last_echo_.fill(0.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void SuppressionGain::GetGain( |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 362 | const std::array<float, kFftLengthBy2Plus1>& nearend, |
| 363 | const std::array<float, kFftLengthBy2Plus1>& echo, |
| 364 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 365 | const RenderSignalAnalyzer& render_signal_analyzer, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 366 | bool saturated_echo, |
| 367 | const std::vector<std::vector<float>>& render, |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 368 | bool force_zero_gain, |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 369 | bool linear_echo_estimate, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 370 | float* high_bands_gain, |
| 371 | std::array<float, kFftLengthBy2Plus1>* low_band_gain) { |
| 372 | RTC_DCHECK(high_bands_gain); |
| 373 | RTC_DCHECK(low_band_gain); |
| 374 | |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 375 | if (force_zero_gain) { |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 376 | last_gain_.fill(0.f); |
| 377 | std::copy(comfort_noise.begin(), comfort_noise.end(), last_masker_.begin()); |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 378 | low_band_gain->fill(0.f); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 379 | gain_increase_.fill(1.f); |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 380 | *high_bands_gain = 0.f; |
| 381 | return; |
| 382 | } |
| 383 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 384 | bool low_noise_render = low_render_detector_.Detect(render); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 385 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 386 | // Compute gain for the lower band. |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 387 | const rtc::Optional<int> narrow_peak_band = |
| 388 | render_signal_analyzer.NarrowPeakBand(); |
Per Åhgren | c65ce78 | 2017-10-09 13:01:39 +0200 | [diff] [blame] | 389 | LowerBandGain(low_noise_render, narrow_peak_band, saturated_echo, |
| 390 | linear_echo_estimate, nearend, echo, comfort_noise, |
| 391 | low_band_gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 392 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 393 | // Compute the gain for the upper bands. |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 394 | *high_bands_gain = |
| 395 | UpperBandsGain(narrow_peak_band, saturated_echo, render, *low_band_gain); |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 396 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 397 | |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 398 | // Detects when the render signal can be considered to have low power and |
| 399 | // consist of stationary noise. |
| 400 | bool SuppressionGain::LowNoiseRenderDetector::Detect( |
| 401 | const std::vector<std::vector<float>>& render) { |
| 402 | float x2_sum = 0.f; |
| 403 | float x2_max = 0.f; |
| 404 | for (auto x_k : render[0]) { |
| 405 | const float x2 = x_k * x_k; |
| 406 | x2_sum += x2; |
| 407 | x2_max = std::max(x2_max, x2); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 408 | } |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 409 | |
| 410 | constexpr float kThreshold = 50.f * 50.f * 64.f; |
| 411 | const bool low_noise_render = |
| 412 | average_power_ < kThreshold && x2_max < 3 * average_power_; |
| 413 | average_power_ = average_power_ * 0.9f + x2_sum * 0.1f; |
| 414 | return low_noise_render; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | } // namespace webrtc |