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/shadow_filter_update_gain.h"
12
13 #include <algorithm>
14 #include <functional>
15
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19
HandleEchoPathChange()20 void ShadowFilterUpdateGain::HandleEchoPathChange() {
21 // TODO(peah): Check whether this counter should instead be initialized to a
22 // large value.
23 poor_signal_excitation_counter_ = 0;
24 call_counter_ = 0;
25 }
26
Compute(const RenderBuffer & render_buffer,const RenderSignalAnalyzer & render_signal_analyzer,const FftData & E_shadow,size_t size_partitions,bool saturated_capture_signal,FftData * G)27 void ShadowFilterUpdateGain::Compute(
28 const RenderBuffer& render_buffer,
29 const RenderSignalAnalyzer& render_signal_analyzer,
30 const FftData& E_shadow,
31 size_t size_partitions,
32 bool saturated_capture_signal,
33 FftData* G) {
34 RTC_DCHECK(G);
35 ++call_counter_;
36
37 if (render_signal_analyzer.PoorSignalExcitation()) {
38 poor_signal_excitation_counter_ = 0;
39 }
40
41 // Do not update the filter if the render is not sufficiently excited.
42 if (++poor_signal_excitation_counter_ < size_partitions ||
43 saturated_capture_signal || call_counter_ <= size_partitions) {
44 G->re.fill(0.f);
45 G->im.fill(0.f);
46 return;
47 }
48
49 // Compute mu.
50 // Corresponds to WGN of power -39 dBFS.
51 constexpr float kNoiseGatePower = 220075344.f;
52 constexpr float kMuFixed = .5f;
53 std::array<float, kFftLengthBy2Plus1> mu;
54 const auto& X2 = render_buffer.SpectralSum(size_partitions);
55 std::transform(X2.begin(), X2.end(), mu.begin(), [&](float a) {
56 return a > kNoiseGatePower ? kMuFixed / a : 0.f;
57 });
58
59 // Avoid updating the filter close to narrow bands in the render signals.
60 render_signal_analyzer.MaskRegionsAroundNarrowBands(&mu);
61
62 // G = mu * E * X2.
63 std::transform(mu.begin(), mu.end(), E_shadow.re.begin(), G->re.begin(),
64 std::multiplies<float>());
65 std::transform(mu.begin(), mu.end(), E_shadow.im.begin(), G->im.begin(),
66 std::multiplies<float>());
67 }
68
69 } // namespace webrtc
70