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