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 #include "webrtc/modules/audio_processing/aec3/power_echo_model.h" |
| 11 |
| 12 #include <string.h> |
| 13 #include <algorithm> |
| 14 |
| 15 #include "webrtc/base/optional.h" |
| 16 |
| 17 namespace webrtc { |
| 18 namespace { |
| 19 |
| 20 // Computes the spectral power over that last 20 frames. |
| 21 void RecentMaximum(const FftBuffer& X_buffer, |
| 22 std::array<float, kFftLengthBy2Plus1>* R2) { |
| 23 R2->fill(0.f); |
| 24 for (size_t j = 0; j < 20; ++j) { |
| 25 std::transform(R2->begin(), R2->end(), X_buffer.Spectrum(j).begin(), |
| 26 R2->begin(), |
| 27 [](float a, float b) { return std::max(a, b); }); |
| 28 } |
| 29 } |
| 30 |
| 31 constexpr float kHInitial = 10.f; |
| 32 constexpr int kUpdateCounterInitial = 300; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 PowerEchoModel::PowerEchoModel() { |
| 37 H2_.fill(CountedFloat(kHInitial, kUpdateCounterInitial)); |
| 38 } |
| 39 |
| 40 PowerEchoModel::~PowerEchoModel() = default; |
| 41 |
| 42 void PowerEchoModel::HandleEchoPathChange( |
| 43 const EchoPathVariability& variability) { |
| 44 if (variability.gain_change) { |
| 45 H2_.fill(CountedFloat(kHInitial, kUpdateCounterInitial)); |
| 46 } |
| 47 } |
| 48 |
| 49 void PowerEchoModel::EstimateEcho( |
| 50 const FftBuffer& render_buffer, |
| 51 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, |
| 52 const AecState& aec_state, |
| 53 std::array<float, kFftLengthBy2Plus1>* echo_spectrum) { |
| 54 RTC_DCHECK(echo_spectrum); |
| 55 |
| 56 const FftBuffer& X_buffer = render_buffer; |
| 57 const auto& Y2 = capture_spectrum; |
| 58 std::array<float, kFftLengthBy2Plus1>* S2 = echo_spectrum; |
| 59 |
| 60 // Choose delay to use. |
| 61 const rtc::Optional<size_t> delay = |
| 62 aec_state.FilterDelay() |
| 63 ? aec_state.FilterDelay() |
| 64 : (aec_state.ExternalDelay() ? rtc::Optional<size_t>(std::min<size_t>( |
| 65 *aec_state.ExternalDelay(), |
| 66 X_buffer.Buffer().size() - 1)) |
| 67 : rtc::Optional<size_t>()); |
| 68 |
| 69 // Compute R2. |
| 70 std::array<float, kFftLengthBy2Plus1> render_max; |
| 71 if (!delay) { |
| 72 RecentMaximum(render_buffer, &render_max); |
| 73 } |
| 74 const std::array<float, kFftLengthBy2Plus1>& X2_active = |
| 75 delay ? render_buffer.Spectrum(*delay) : render_max; |
| 76 |
| 77 if (!aec_state.SaturatedCapture()) { |
| 78 // Corresponds of WGN of power -46dBFS. |
| 79 constexpr float kX2Min = 44015068.0f; |
| 80 const int max_update_counter_value = delay ? 300 : 500; |
| 81 |
| 82 std::array<float, kFftLengthBy2Plus1> new_H2; |
| 83 |
| 84 // new_H2 = Y2 / X2. |
| 85 std::transform(X2_active.begin(), X2_active.end(), Y2.begin(), |
| 86 new_H2.begin(), |
| 87 [&](float a, float b) { return a > kX2Min ? b / a : -1.f; }); |
| 88 |
| 89 // Lambda for updating H2 in a maximum statistics manner. |
| 90 auto H2_updater = [&](float a, CountedFloat b) { |
| 91 if (a > 0) { |
| 92 if (a > b.value) { |
| 93 b.counter = max_update_counter_value; |
| 94 b.value = a; |
| 95 } else if (--b.counter <= 0) { |
| 96 b.value = std::max(b.value * 0.9f, 1.f); |
| 97 } |
| 98 } |
| 99 return b; |
| 100 }; |
| 101 |
| 102 std::transform(new_H2.begin(), new_H2.end(), H2_.begin(), H2_.begin(), |
| 103 H2_updater); |
| 104 } |
| 105 |
| 106 // S2 = H2*X2_active. |
| 107 std::transform(H2_.begin(), H2_.end(), X2_active.begin(), S2->begin(), |
| 108 [](CountedFloat a, float b) { return a.value * b; }); |
| 109 } |
| 110 |
| 111 } // namespace webrtc |
OLD | NEW |