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 std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { | |
ivoc
2017/02/21 17:26:01
Could this be replaced by a call to H2_->fill()?
peah-webrtc
2017/02/21 23:00:40
Great suggestion!
That worked!!!
Done.
| |
38 a = CountedFloat(kHInitial, kUpdateCounterInitial); | |
39 }); | |
40 } | |
41 | |
42 PowerEchoModel::~PowerEchoModel() = default; | |
43 | |
44 void PowerEchoModel::HandleEchoPathChange( | |
45 const EchoPathVariability& variability) { | |
46 if (variability.gain_change) { | |
47 std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { | |
ivoc
2017/02/21 17:26:01
Same here?
peah-webrtc
2017/02/21 23:00:40
Done.
| |
48 a = CountedFloat(kHInitial, kUpdateCounterInitial); | |
49 }); | |
50 } | |
51 } | |
52 | |
53 void PowerEchoModel::EstimateEcho( | |
54 const FftBuffer& render_buffer, | |
55 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, | |
56 const AecState& aec_state, | |
57 std::array<float, kFftLengthBy2Plus1>* echo_spectrum) { | |
58 RTC_DCHECK(echo_spectrum); | |
59 | |
60 const FftBuffer& X_buffer = render_buffer; | |
61 const std::array<float, kFftLengthBy2Plus1> Y2 = capture_spectrum; | |
ivoc
2017/02/21 17:26:01
Is this necessary? it looks like the value of Y2 i
peah-webrtc
2017/02/21 23:00:40
Ouch! That was a mistake! Good find. It should def
| |
62 std::array<float, kFftLengthBy2Plus1>* S2 = echo_spectrum; | |
63 | |
64 // Choose delay to use. | |
65 const rtc::Optional<size_t> delay = | |
66 aec_state.FilterDelay() | |
67 ? aec_state.FilterDelay() | |
68 : (aec_state.ExternalDelay() ? rtc::Optional<size_t>(std::min<size_t>( | |
69 *aec_state.ExternalDelay(), | |
70 X_buffer.Buffer().size() - 1)) | |
71 : rtc::Optional<size_t>()); | |
72 | |
73 // Compute R2. | |
74 std::array<float, kFftLengthBy2Plus1> render_max; | |
75 if (!delay) { | |
76 RecentMaximum(render_buffer, &render_max); | |
77 } | |
78 const std::array<float, kFftLengthBy2Plus1>& X2_active = | |
79 delay ? render_buffer.Spectrum(*delay) : render_max; | |
80 | |
81 if (!aec_state.SaturatedCapture()) { | |
82 // Corresponds of WGN of power -46dBFS. | |
83 constexpr float kX2Min = 44015068.0f; | |
84 const int max_update_counter_value = delay ? 300 : 500; | |
85 | |
86 std::array<float, kFftLengthBy2Plus1> new_H2; | |
87 | |
88 // new_H2 = Y2 / X2. | |
89 std::transform(X2_active.begin(), X2_active.end(), Y2.begin(), | |
90 new_H2.begin(), | |
91 [&](float a, float b) { return a > kX2Min ? b / a : -1.f; }); | |
92 | |
93 // Lambda for updating H2 in a maximum statistics manner. | |
94 auto H2_updater = [&](float a, CountedFloat b) { | |
95 if (a > 0) { | |
96 if (a > b.value) { | |
97 b.counter = max_update_counter_value; | |
98 b.value = a; | |
99 } else if (--b.counter <= 0) { | |
100 b.value = std::max(b.value * 0.9f, 1.f); | |
101 } | |
102 } | |
103 return b; | |
104 }; | |
105 | |
106 std::transform(new_H2.begin(), new_H2.end(), H2_.begin(), H2_.begin(), | |
107 H2_updater); | |
108 } | |
109 | |
110 // S2 = H2*X2_active. | |
111 std::transform(H2_.begin(), H2_.end(), X2_active.begin(), S2->begin(), | |
112 [](CountedFloat a, float b) { return a.value * b; }); | |
113 } | |
114 | |
115 } // namespace webrtc | |
OLD | NEW |