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 "modules/audio_processing/aec3/coarse_filter_update_gain.h"
12
13 #include <algorithm>
14 #include <functional>
15
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19
CoarseFilterUpdateGain(const EchoCanceller3Config::Filter::CoarseConfiguration & config,size_t config_change_duration_blocks)20 CoarseFilterUpdateGain::CoarseFilterUpdateGain(
21 const EchoCanceller3Config::Filter::CoarseConfiguration& config,
22 size_t config_change_duration_blocks)
23 : config_change_duration_blocks_(
24 static_cast<int>(config_change_duration_blocks)) {
25 SetConfig(config, true);
26 RTC_DCHECK_LT(0, config_change_duration_blocks_);
27 one_by_config_change_duration_blocks_ = 1.f / config_change_duration_blocks_;
28 }
29
HandleEchoPathChange()30 void CoarseFilterUpdateGain::HandleEchoPathChange() {
31 poor_signal_excitation_counter_ = 0;
32 call_counter_ = 0;
33 }
34
Compute(const std::array<float,kFftLengthBy2Plus1> & render_power,const RenderSignalAnalyzer & render_signal_analyzer,const FftData & E_coarse,size_t size_partitions,bool saturated_capture_signal,FftData * G)35 void CoarseFilterUpdateGain::Compute(
36 const std::array<float, kFftLengthBy2Plus1>& render_power,
37 const RenderSignalAnalyzer& render_signal_analyzer,
38 const FftData& E_coarse,
39 size_t size_partitions,
40 bool saturated_capture_signal,
41 FftData* G) {
42 RTC_DCHECK(G);
43 ++call_counter_;
44
45 UpdateCurrentConfig();
46
47 if (render_signal_analyzer.PoorSignalExcitation()) {
48 poor_signal_excitation_counter_ = 0;
49 }
50
51 // Do not update the filter if the render is not sufficiently excited.
52 if (++poor_signal_excitation_counter_ < size_partitions ||
53 saturated_capture_signal || call_counter_ <= size_partitions) {
54 G->re.fill(0.f);
55 G->im.fill(0.f);
56 return;
57 }
58
59 // Compute mu.
60 std::array<float, kFftLengthBy2Plus1> mu;
61 const auto& X2 = render_power;
62 for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
63 if (X2[k] > current_config_.noise_gate) {
64 mu[k] = current_config_.rate / X2[k];
65 } else {
66 mu[k] = 0.f;
67 }
68 }
69
70 // Avoid updating the filter close to narrow bands in the render signals.
71 render_signal_analyzer.MaskRegionsAroundNarrowBands(&mu);
72
73 // G = mu * E * X2.
74 for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
75 G->re[k] = mu[k] * E_coarse.re[k];
76 G->im[k] = mu[k] * E_coarse.im[k];
77 }
78 }
79
UpdateCurrentConfig()80 void CoarseFilterUpdateGain::UpdateCurrentConfig() {
81 RTC_DCHECK_GE(config_change_duration_blocks_, config_change_counter_);
82 if (config_change_counter_ > 0) {
83 if (--config_change_counter_ > 0) {
84 auto average = [](float from, float to, float from_weight) {
85 return from * from_weight + to * (1.f - from_weight);
86 };
87
88 float change_factor =
89 config_change_counter_ * one_by_config_change_duration_blocks_;
90
91 current_config_.rate =
92 average(old_target_config_.rate, target_config_.rate, change_factor);
93 current_config_.noise_gate =
94 average(old_target_config_.noise_gate, target_config_.noise_gate,
95 change_factor);
96 } else {
97 current_config_ = old_target_config_ = target_config_;
98 }
99 }
100 RTC_DCHECK_LE(0, config_change_counter_);
101 }
102
103 } // namespace webrtc
104