Index: webrtc/modules/audio_processing/aec3/suppression_gain.cc |
diff --git a/webrtc/modules/audio_processing/aec3/suppression_gain.cc b/webrtc/modules/audio_processing/aec3/suppression_gain.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f88e0bfc0195456fcb7cc05b35492f25001b1433 |
--- /dev/null |
+++ b/webrtc/modules/audio_processing/aec3/suppression_gain.cc |
@@ -0,0 +1,131 @@ |
+/* |
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/modules/audio_processing/aec3/suppression_gain.h" |
+ |
+#include <math.h> |
+#include <algorithm> |
+#include <functional> |
+ |
+namespace webrtc { |
+namespace { |
+ |
+const int kNumIterations = 2; |
+ |
+} // namespace |
+ |
+SuppressionGain::SuppressionGain() { |
+ previous_gain_squared_.fill(1.f); |
+ previous_masker_.fill(0.f); |
+} |
+ |
+void SuppressionGain::GetGain( |
+ const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
+ const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
+ const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
+ float strong_nearend_margin, |
+ std::array<float, kFftLengthBy2Plus1>* gain) { |
+ constexpr float kEchoMaskingMargin = 1.f / 10.f; |
+ constexpr float kBandMaskingFactor = 1.f / 3.f; |
+ constexpr float kTimeMaskingFactor = 1.f / 10.f; |
+ |
+ std::array<float, kFftLengthBy2 - 1> masker; |
+ std::array<float, kFftLengthBy2 - 1> same_band_masker; |
+ std::array<float, kFftLengthBy2 - 1> one_by_residual_echo_power; |
+ std::array<bool, kFftLengthBy2 - 1> strong_nearend; |
+ std::array<float, kFftLengthBy2Plus1> neighboring_bands_masker; |
+ |
+ // Precompute 1/residual_echo_power. |
+ std::transform(residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
+ one_by_residual_echo_power.begin(), |
+ [](float a) { return a > 0.f ? 1.f / a : -1.f; }); |
+ |
+ // Precompute indicators for bands with strong nearend. |
+ std::transform( |
+ residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
+ nearend_power.begin() + 1, strong_nearend.begin(), |
+ [&](float a, float b) { return a <= strong_nearend_margin * b; }); |
+ |
+ // Precompute masker for the same band. |
+ std::transform(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
+ previous_masker_.begin(), same_band_masker.begin(), |
+ [&](float a, float b) { return a + kTimeMaskingFactor * b; }); |
+ |
+ for (int k = 0; k < kNumIterations; ++k) { |
+ if (k == 0) { |
+ // Add masker from the same band. |
+ std::copy(same_band_masker.begin(), same_band_masker.end(), |
+ masker.begin()); |
+ } else { |
+ // Add masker for neightboring bands. |
+ std::transform(nearend_power.begin(), nearend_power.end(), gain->begin(), |
+ neighboring_bands_masker.begin(), |
+ std::multiplies<float>()); |
+ std::transform(neighboring_bands_masker.begin(), |
+ neighboring_bands_masker.end(), |
+ comfort_noise_power.begin(), |
+ neighboring_bands_masker.begin(), std::plus<float>()); |
+ std::transform( |
+ neighboring_bands_masker.begin(), neighboring_bands_masker.end() - 2, |
+ neighboring_bands_masker.begin() + 2, masker.begin(), |
+ [&](float a, float b) { return kBandMaskingFactor * (a + b); }); |
+ |
+ // Add masker from the same band. |
+ std::transform(same_band_masker.begin(), same_band_masker.end(), |
+ masker.begin(), masker.begin(), std::plus<float>()); |
+ } |
+ |
+ // Compute new gain as: |
+ // G2(t,f) = (comfort_noise_power(t,f) + G2(t-1)*nearend_power(t-1)) * |
+ // kTimeMaskingFactor |
+ // * kEchoMaskingMargin / residual_echo_power(t,f). |
+ // or |
+ // G2(t,f) = ((comfort_noise_power(t,f) + G2(t-1) * |
+ // nearend_power(t-1)) * kTimeMaskingFactor + |
+ // (comfort_noise_power(t, f-1) + comfort_noise_power(t, f+1) + |
+ // (G2(t,f-1)*nearend_power(t, f-1) + |
+ // G2(t,f+1)*nearend_power(t, f+1)) * |
+ // kTimeMaskingFactor) * kBandMaskingFactor) |
+ // * kEchoMaskingMargin / residual_echo_power(t,f). |
+ std::transform( |
+ masker.begin(), masker.end(), one_by_residual_echo_power.begin(), |
+ gain->begin() + 1, [&](float a, float b) { |
+ return b >= 0 ? std::min(kEchoMaskingMargin * a * b, 1.f) : 1.f; |
+ }); |
+ |
+ // Limit gain for bands with strong nearend. |
+ std::transform(gain->begin() + 1, gain->end() - 1, strong_nearend.begin(), |
+ gain->begin() + 1, |
+ [](float a, bool b) { return b ? 1.f : a; }); |
+ |
+ // Limit the allowed gain update over time. |
+ std::transform( |
+ gain->begin() + 1, gain->end() - 1, previous_gain_squared_.begin(), |
+ gain->begin() + 1, [](float a, float b) { |
+ return b < 0.0001f ? std::min(a, 0.0001f) : std::min(a, b * 2.f); |
+ }); |
+ |
+ (*gain)[0] = (*gain)[1]; |
+ (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2 - 1]; |
+ } |
+ |
+ std::copy(gain->begin() + 1, gain->end() - 1, previous_gain_squared_.begin()); |
+ |
+ std::transform(gain->begin() + 1, gain->end() - 1, nearend_power.begin() + 1, |
+ previous_masker_.begin(), std::multiplies<float>()); |
+ std::transform(previous_masker_.begin(), previous_masker_.end(), |
+ comfort_noise_power.begin() + 1, previous_masker_.begin(), |
+ std::plus<float>()); |
+ |
+ std::transform(gain->begin(), gain->end(), gain->begin(), |
+ [](float a) { return sqrtf(a); }); |
+} |
+ |
+} // namespace webrtc |