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 void RecentMaximum(const FftBuffer& X_buffer, | |
21 std::array<float, kFftLengthBy2Plus1>* R2) { | |
22 R2->fill(0.f); | |
23 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.
| |
24 std::transform(R2->begin(), R2->end(), X_buffer.Spectrum(j).begin(), | |
25 R2->begin(), | |
26 [](float a, float b) { return std::max(a, b); }); | |
27 } | |
28 } | |
29 | |
30 constexpr float kHInitial = 10.f; | |
31 constexpr int kUpdateCounterInitial = 300; | |
32 | |
33 } // namespace | |
34 | |
35 PowerEchoModel::PowerEchoModel() { | |
36 std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { | |
37 a = CountedFloat(kHInitial, kUpdateCounterInitial); | |
38 }); | |
39 } | |
40 | |
41 PowerEchoModel::~PowerEchoModel() = default; | |
42 | |
43 void PowerEchoModel::HandleEchoPathChange( | |
44 const EchoPathVariability& variability) { | |
45 if (variability.gain_change) { | |
46 std::for_each(H2_.begin(), H2_.end(), [](CountedFloat& a) { | |
47 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.
| |
48 a.counter = kUpdateCounterInitial; | |
49 }); | |
50 } | |
51 } | |
52 | |
53 void PowerEchoModel::EstimateEcho( | |
54 const FftBuffer& render_buffer, | |
55 const std::array<float, kFftLengthBy2Plus1>& capture_spectrum, | |
56 const DelayHandler& delay_handler, | |
57 bool saturated_capture_signal, | |
58 std::array<float, kFftLengthBy2Plus1>* echo_spectrum) { | |
59 const FftBuffer& X_buffer = render_buffer; | |
60 const std::array<float, kFftLengthBy2Plus1> Y2 = capture_spectrum; | |
61 std::array<float, kFftLengthBy2Plus1>* S2 = echo_spectrum; | |
62 | |
63 // Choose delay to use. | |
64 const rtc::Optional<size_t> delay = | |
65 delay_handler.FilterDelay() | |
66 ? delay_handler.FilterDelay() | |
67 : 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.
| |
68 ? rtc::Optional<size_t>( | |
69 std::min<size_t>(*delay_handler.ExternalDelay(), | |
70 X_buffer.Buffer().size() - 1)) | |
71 : rtc::Optional<size_t>(); | |
72 | |
73 // Compute the R2 | |
hlundin-webrtc
2017/02/13 21:37:01
.
peah-webrtc
2017/02/20 07:37:18
Done.
| |
74 std::array<float, kFftLengthBy2Plus1> render_max; | |
75 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
| |
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 (!saturated_capture_signal) { | |
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 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.
| |
88 new_H2.begin(), | |
89 [&](float a, float b) { return a > kX2Min ? b / a : -1.f; }); | |
90 | |
91 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.
| |
92 if (a > 0) { | |
93 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.
| |
94 if (a > b.value) { | |
95 b.value = a; | |
96 } else if (b.counter <= 0) { | |
97 b.value = std::max(b.value * 0.9f, 1.f); | |
98 } | |
99 } | |
100 return b; | |
101 }; | |
102 | |
103 std::transform(new_H2.begin(), new_H2.end(), H2_.begin(), H2_.begin(), | |
104 H2_updater); | |
105 } | |
106 | |
107 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.
| |
108 [](CountedFloat a, float b) { return a.value * b; }); | |
109 | |
110 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.
| |
111 std::transform(H2_.begin(), H2_.end(), H2_tmp.begin(), | |
112 [](CountedFloat a) { return a.value; }); | |
113 } | |
114 | |
115 } // namespace webrtc | |
OLD | NEW |