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