Chromium Code Reviews| Index: webrtc/modules/audio_processing/aec3/power_echo_model.cc |
| diff --git a/webrtc/modules/audio_processing/aec3/power_echo_model.cc b/webrtc/modules/audio_processing/aec3/power_echo_model.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c106969859e4b616bb564717401bf4cc2f9e7cdf |
| --- /dev/null |
| +++ b/webrtc/modules/audio_processing/aec3/power_echo_model.cc |
| @@ -0,0 +1,115 @@ |
| +/* |
| + * 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/power_echo_model.h" |
| + |
| +#include <string.h> |
| +#include <algorithm> |
| + |
| +#include "webrtc/base/optional.h" |
| + |
| +namespace webrtc { |
| +namespace { |
| + |
| +void RecentMaximum(const FftBuffer& X_buffer, |
| + std::array<float, kFftLengthBy2Plus1>* R2) { |
| + R2->fill(0.f); |
| + for (size_t j = 0; j < 20; ++j) { |
|
hlundin-webrtc
2017/02/13 21:37:01
Why 20?
peah-webrtc
2017/02/20 07:37:17
Tuning parameter. Ok?
hlundin-webrtc
2017/02/21 09:40:03
Acknowledged.
peah-webrtc
2017/02/21 23:00:39
Acknowledged.
|
| + std::transform(R2->begin(), R2->end(), X_buffer.Spectrum(j).begin(), |
| + R2->begin(), |
| + [](float a, float b) { return std::max(a, b); }); |
| + } |
| +} |
| + |
| +constexpr float kHInitial = 10.f; |
| +constexpr int kUpdateCounterInitial = 300; |
| + |
| +} // namespace |
| + |
| +PowerEchoModel::PowerEchoModel() { |
| + std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { |
| + a = CountedFloat(kHInitial, kUpdateCounterInitial); |
| + }); |
| +} |
| + |
| +PowerEchoModel::~PowerEchoModel() = default; |
| + |
| +void PowerEchoModel::HandleEchoPathChange( |
| + const EchoPathVariability& variability) { |
| + if (variability.gain_change) { |
| + std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { |
| + a.value = kHInitial; |
|
hlundin-webrtc
2017/02/13 21:37:01
Can't you just as well say
a = CountedFloat(kHInit
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + a.counter = kUpdateCounterInitial; |
| + }); |
| + } |
| +} |
| + |
| +void PowerEchoModel::EstimateEcho( |
| + const FftBuffer& render_buffer, |
| + const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, |
| + const DelayHandler& delay_handler, |
| + bool saturated_capture_signal, |
| + std::array<float, kFftLengthBy2Plus1>* echo_spectrum) { |
| + const FftBuffer& X_buffer = render_buffer; |
| + const std::array<float, kFftLengthBy2Plus1> Y2 = capture_spectrum; |
| + std::array<float, kFftLengthBy2Plus1>* S2 = echo_spectrum; |
| + |
| + // Choose delay to use. |
| + const rtc::Optional<size_t> delay = |
| + delay_handler.FilterDelay() |
| + ? delay_handler.FilterDelay() |
| + : delay_handler.ExternalDelay() |
|
hlundin-webrtc
2017/02/13 21:37:01
Put () around the "else" result, for readability.
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + ? rtc::Optional<size_t>( |
| + std::min<size_t>(*delay_handler.ExternalDelay(), |
| + X_buffer.Buffer().size() - 1)) |
| + : rtc::Optional<size_t>(); |
| + |
| + // Compute the R2 |
|
hlundin-webrtc
2017/02/13 21:37:01
.
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + std::array<float, kFftLengthBy2Plus1> render_max; |
| + if (!delay) { |
|
hlundin-webrtc
2017/02/13 21:37:01
If no delay, then calculate RecentMaximum. Care to
peah-webrtc
2017/02/20 07:37:18
The purpose of this is to estimate the echo power
|
| + RecentMaximum(render_buffer, &render_max); |
| + } |
| + const std::array<float, kFftLengthBy2Plus1>& X2_active = |
| + delay ? render_buffer.Spectrum(*delay) : render_max; |
| + |
| + if (!saturated_capture_signal) { |
| + // Corresponds of WGN of power -46dBFS. |
| + constexpr float kX2Min = 44015068.0f; |
| + const int max_update_counter_value = delay ? 300 : 500; |
| + |
| + std::array<float, kFftLengthBy2Plus1> new_H2; |
| + std::transform(X2_active.begin(), X2_active.end(), Y2.begin(), |
|
hlundin-webrtc
2017/02/13 21:37:01
Math comment, please.
peah-webrtc
2017/02/20 07:37:17
Done.
|
| + new_H2.begin(), |
| + [&](float a, float b) { return a > kX2Min ? b / a : -1.f; }); |
| + |
| + auto H2_updater = [&](float a, CountedFloat b) { |
|
hlundin-webrtc
2017/02/13 21:37:00
What math does this lambda implement?
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + if (a > 0) { |
| + b.counter = a > b.value ? max_update_counter_value : b.counter - 1; |
|
hlundin-webrtc
2017/02/13 21:37:01
You're making this branch again just below. You ma
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + if (a > b.value) { |
| + b.value = a; |
| + } else if (b.counter <= 0) { |
| + b.value = std::max(b.value * 0.9f, 1.f); |
| + } |
| + } |
| + return b; |
| + }; |
| + |
| + std::transform(new_H2.begin(), new_H2.end(), H2_.begin(), H2_.begin(), |
| + H2_updater); |
| + } |
| + |
| + std::transform(H2_.begin(), H2_.end(), X2_active.begin(), S2->begin(), |
|
hlundin-webrtc
2017/02/13 21:37:01
Math comment
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + [](CountedFloat a, float b) { return a.value * b; }); |
| + |
| + std::array<float, kFftLengthBy2Plus1> H2_tmp; |
|
hlundin-webrtc
2017/02/13 21:37:00
Why do you calculate H2_tmp? It is not used.
peah-webrtc
2017/02/20 07:37:18
Done.
|
| + std::transform(H2_.begin(), H2_.end(), H2_tmp.begin(), |
| + [](CountedFloat a) { return a.value; }); |
| +} |
| + |
| +} // namespace webrtc |