Jesús de Vicente Peña | 075cb2b | 2018-06-13 15:13:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "modules/audio_processing/aec3/reverb_model_fallback.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <functional> |
| 15 | |
| 16 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | ReverbModelFallback::ReverbModelFallback(size_t length_blocks) |
| 22 | : S2_old_(length_blocks) { |
| 23 | Reset(); |
| 24 | } |
| 25 | |
| 26 | ReverbModelFallback::~ReverbModelFallback() = default; |
| 27 | |
| 28 | void ReverbModelFallback::Reset() { |
| 29 | R2_reverb_.fill(0.f); |
| 30 | for (auto& S2_k : S2_old_) { |
| 31 | S2_k.fill(0.f); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void ReverbModelFallback::AddEchoReverb( |
| 36 | const std::array<float, kFftLengthBy2Plus1>& S2, |
| 37 | size_t delay, |
| 38 | float reverb_decay_factor, |
| 39 | std::array<float, kFftLengthBy2Plus1>* R2) { |
| 40 | // Compute the decay factor for how much the echo has decayed before leaving |
| 41 | // the region covered by the linear model. |
| 42 | auto integer_power = [](float base, int exp) { |
| 43 | float result = 1.f; |
| 44 | for (int k = 0; k < exp; ++k) { |
| 45 | result *= base; |
| 46 | } |
| 47 | return result; |
| 48 | }; |
| 49 | RTC_DCHECK_LE(delay, S2_old_.size()); |
| 50 | const float reverb_decay_for_delay = |
| 51 | integer_power(reverb_decay_factor, S2_old_.size() - delay); |
| 52 | |
| 53 | // Update the estimate of the reverberant residual echo power. |
| 54 | S2_old_index_ = S2_old_index_ > 0 ? S2_old_index_ - 1 : S2_old_.size() - 1; |
| 55 | const auto& S2_end = S2_old_[S2_old_index_]; |
| 56 | std::transform( |
| 57 | S2_end.begin(), S2_end.end(), R2_reverb_.begin(), R2_reverb_.begin(), |
| 58 | [reverb_decay_for_delay, reverb_decay_factor](float a, float b) { |
| 59 | return (b + a * reverb_decay_for_delay) * reverb_decay_factor; |
| 60 | }); |
| 61 | |
| 62 | // Update the buffer of old echo powers. |
| 63 | std::copy(S2.begin(), S2.end(), S2_old_[S2_old_index_].begin()); |
| 64 | |
| 65 | // Add the power of the echo reverb to the residual echo power. |
| 66 | std::transform(R2->begin(), R2->end(), R2_reverb_.begin(), R2->begin(), |
| 67 | std::plus<float>()); |
| 68 | } |
| 69 | |
| 70 | } // namespace webrtc |