OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/modules/audio_processing/aec3/suppression_gain.h" |
| 12 |
| 13 #include <math.h> |
| 14 #include <algorithm> |
| 15 #include <functional> |
| 16 |
| 17 namespace webrtc { |
| 18 namespace { |
| 19 |
| 20 const int kNumIterations = 2; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 SuppressionGain::SuppressionGain() { |
| 25 previous_gain_squared_.fill(1.f); |
| 26 previous_masker_.fill(0.f); |
| 27 } |
| 28 |
| 29 void SuppressionGain::GetGain( |
| 30 const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 31 const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 32 const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
| 33 float strong_nearend_margin, |
| 34 std::array<float, kFftLengthBy2Plus1>* gain) { |
| 35 constexpr float kEchoMaskingMargin = 1.f / 10.f; |
| 36 constexpr float kBandMaskingFactor = 1.f / 3.f; |
| 37 constexpr float kTimeMaskingFactor = 1.f / 10.f; |
| 38 |
| 39 std::array<float, kFftLengthBy2 - 1> masker; |
| 40 std::array<float, kFftLengthBy2 - 1> same_band_masker; |
| 41 std::array<float, kFftLengthBy2 - 1> one_by_residual_echo_power; |
| 42 std::array<bool, kFftLengthBy2 - 1> strong_nearend; |
| 43 std::array<float, kFftLengthBy2Plus1> neighboring_bands_masker; |
| 44 |
| 45 // Precompute 1/residual_echo_power. |
| 46 std::transform(residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 47 one_by_residual_echo_power.begin(), |
| 48 [](float a) { return a > 0.f ? 1.f / a : -1.f; }); |
| 49 |
| 50 // Precompute indicators for bands with strong nearend. |
| 51 std::transform( |
| 52 residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 53 nearend_power.begin() + 1, strong_nearend.begin(), |
| 54 [&](float a, float b) { return a <= strong_nearend_margin * b; }); |
| 55 |
| 56 // Precompute masker for the same band. |
| 57 std::transform(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
| 58 previous_masker_.begin(), same_band_masker.begin(), |
| 59 [&](float a, float b) { return a + kTimeMaskingFactor * b; }); |
| 60 |
| 61 for (int k = 0; k < kNumIterations; ++k) { |
| 62 if (k == 0) { |
| 63 // Add masker from the same band. |
| 64 std::copy(same_band_masker.begin(), same_band_masker.end(), |
| 65 masker.begin()); |
| 66 } else { |
| 67 // Add masker for neightboring bands. |
| 68 std::transform(nearend_power.begin(), nearend_power.end(), gain->begin(), |
| 69 neighboring_bands_masker.begin(), |
| 70 std::multiplies<float>()); |
| 71 std::transform(neighboring_bands_masker.begin(), |
| 72 neighboring_bands_masker.end(), |
| 73 comfort_noise_power.begin(), |
| 74 neighboring_bands_masker.begin(), std::plus<float>()); |
| 75 std::transform( |
| 76 neighboring_bands_masker.begin(), neighboring_bands_masker.end() - 2, |
| 77 neighboring_bands_masker.begin() + 2, masker.begin(), |
| 78 [&](float a, float b) { return kBandMaskingFactor * (a + b); }); |
| 79 |
| 80 // Add masker from the same band. |
| 81 std::transform(same_band_masker.begin(), same_band_masker.end(), |
| 82 masker.begin(), masker.begin(), std::plus<float>()); |
| 83 } |
| 84 |
| 85 // Compute new gain as: |
| 86 // G2(t,f) = (comfort_noise_power(t,f) + G2(t-1)*nearend_power(t-1)) * |
| 87 // kTimeMaskingFactor |
| 88 // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 89 // or |
| 90 // G2(t,f) = ((comfort_noise_power(t,f) + G2(t-1) * |
| 91 // nearend_power(t-1)) * kTimeMaskingFactor + |
| 92 // (comfort_noise_power(t, f-1) + comfort_noise_power(t, f+1) + |
| 93 // (G2(t,f-1)*nearend_power(t, f-1) + |
| 94 // G2(t,f+1)*nearend_power(t, f+1)) * |
| 95 // kTimeMaskingFactor) * kBandMaskingFactor) |
| 96 // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 97 std::transform( |
| 98 masker.begin(), masker.end(), one_by_residual_echo_power.begin(), |
| 99 gain->begin() + 1, [&](float a, float b) { |
| 100 return b >= 0 ? std::min(kEchoMaskingMargin * a * b, 1.f) : 1.f; |
| 101 }); |
| 102 |
| 103 // Limit gain for bands with strong nearend. |
| 104 std::transform(gain->begin() + 1, gain->end() - 1, strong_nearend.begin(), |
| 105 gain->begin() + 1, |
| 106 [](float a, bool b) { return b ? 1.f : a; }); |
| 107 |
| 108 // Limit the allowed gain update over time. |
| 109 std::transform( |
| 110 gain->begin() + 1, gain->end() - 1, previous_gain_squared_.begin(), |
| 111 gain->begin() + 1, [](float a, float b) { |
| 112 return b < 0.0001f ? std::min(a, 0.0001f) : std::min(a, b * 2.f); |
| 113 }); |
| 114 |
| 115 (*gain)[0] = (*gain)[1]; |
| 116 (*gain)[kFftLengthBy2] = (*gain)[kFftLengthBy2 - 1]; |
| 117 } |
| 118 |
| 119 std::copy(gain->begin() + 1, gain->end() - 1, previous_gain_squared_.begin()); |
| 120 |
| 121 std::transform(gain->begin() + 1, gain->end() - 1, nearend_power.begin() + 1, |
| 122 previous_masker_.begin(), std::multiplies<float>()); |
| 123 std::transform(previous_masker_.begin(), previous_masker_.end(), |
| 124 comfort_noise_power.begin() + 1, previous_masker_.begin(), |
| 125 std::plus<float>()); |
| 126 |
| 127 std::transform(gain->begin(), gain->end(), gain->begin(), |
| 128 [](float a) { return sqrtf(a); }); |
| 129 } |
| 130 |
| 131 } // namespace webrtc |
OLD | NEW |