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