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