OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h" | 11 #include "webrtc/modules/audio_processing/aec3/residual_echo_estimator.h" |
12 | 12 |
13 #include <math.h> | 13 #include <math.h> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 namespace { | 19 namespace { |
20 | 20 |
21 constexpr float kSaturationLeakageFactor = 10.f; | 21 constexpr float kSaturationLeakageFactor = 10.f; |
22 constexpr size_t kSaturationLeakageBlocks = 10; | 22 constexpr size_t kSaturationLeakageBlocks = 10; |
23 constexpr size_t kEchoPathChangeConvergenceBlocks = 3 * 250; | |
23 | 24 |
24 // Estimates the residual echo power when there is no detection correlation | 25 // Estimates the residual echo power when there is no detection correlation |
25 // between the render and capture signals. | 26 // between the render and capture signals. |
26 void InfiniteErlPowerEstimate( | 27 void InfiniteErlPowerEstimate( |
27 size_t active_render_counter, | 28 size_t active_render_blocks, |
28 size_t blocks_since_last_saturation, | 29 size_t blocks_since_last_saturation, |
29 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, | 30 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, |
30 std::array<float, kFftLengthBy2Plus1>* R2) { | 31 std::array<float, kFftLengthBy2Plus1>* R2) { |
31 if (active_render_counter > 5 * 250) { | 32 if (active_render_blocks > 5 * 250) { |
32 // After an amount of active render samples for which an echo should have | 33 // After an amount of active render samples for which an echo should have |
33 // been detected in the capture signal if the ERL was not infinite, set the | 34 // been detected in the capture signal if the ERL was not infinite, set the |
34 // residual echo to 0. | 35 // residual echo to 0. |
35 R2->fill(0.f); | 36 R2->fill(0.f); |
36 } else { | 37 } else { |
37 // Before certainty has been reached about the presence of echo, use the | 38 // Before certainty has been reached about the presence of echo, use the |
38 // fallback echo power estimate as the residual echo estimate. Add a leakage | 39 // fallback echo power estimate as the residual echo estimate. Add a leakage |
39 // factor when there is saturation. | 40 // factor when there is saturation. |
40 std::copy(S2_fallback.begin(), S2_fallback.end(), R2->begin()); | 41 std::copy(S2_fallback.begin(), S2_fallback.end(), R2->begin()); |
41 if (blocks_since_last_saturation < kSaturationLeakageBlocks) { | 42 if (blocks_since_last_saturation < kSaturationLeakageBlocks) { |
(...skipping 13 matching lines...) Expand all Loading... | |
55 } else { | 56 } else { |
56 R2->fill(0.f); | 57 R2->fill(0.f); |
57 } | 58 } |
58 } | 59 } |
59 | 60 |
60 // Estimates the residual echo power based on gains. | 61 // Estimates the residual echo power based on gains. |
61 void GainBasedPowerEstimate( | 62 void GainBasedPowerEstimate( |
62 size_t external_delay, | 63 size_t external_delay, |
63 const FftBuffer& X_buffer, | 64 const FftBuffer& X_buffer, |
64 size_t blocks_since_last_saturation, | 65 size_t blocks_since_last_saturation, |
66 size_t active_render_blocks, | |
65 const std::array<bool, kFftLengthBy2Plus1>& bands_with_reliable_filter, | 67 const std::array<bool, kFftLengthBy2Plus1>& bands_with_reliable_filter, |
66 const std::array<float, kFftLengthBy2Plus1>& echo_path_gain, | 68 const std::array<float, kFftLengthBy2Plus1>& echo_path_gain, |
67 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, | 69 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, |
68 std::array<float, kFftLengthBy2Plus1>* R2) { | 70 std::array<float, kFftLengthBy2Plus1>* R2) { |
69 const auto& X2 = X_buffer.Spectrum(external_delay); | 71 const auto& X2 = X_buffer.Spectrum(external_delay); |
70 | 72 |
71 // Base the residual echo power on gain of the linear echo path estimate if | 73 // Base the residual echo power on gain of the linear echo path estimate if |
72 // that is reliable, otherwise use the fallback echo path estimate. Add a | 74 // that is reliable, otherwise use the fallback echo path estimate. Add a |
73 // leakage factor when there is saturation. | 75 // leakage factor when there is saturation. |
74 for (size_t k = 0; k < R2->size(); ++k) { | 76 if (active_render_blocks > kEchoPathChangeConvergenceBlocks) { |
peah-webrtc
2017/02/27 14:26:03
The filter-based echo path gain cannot be used unt
| |
75 (*R2)[k] = bands_with_reliable_filter[k] ? echo_path_gain[k] * X2[k] | 77 for (size_t k = 0; k < R2->size(); ++k) { |
76 : S2_fallback[k]; | 78 (*R2)[k] = bands_with_reliable_filter[k] ? echo_path_gain[k] * X2[k] |
79 : S2_fallback[k]; | |
80 } | |
81 } else { | |
82 for (size_t k = 0; k < R2->size(); ++k) { | |
83 (*R2)[k] = S2_fallback[k]; | |
84 } | |
77 } | 85 } |
86 | |
78 if (blocks_since_last_saturation < kSaturationLeakageBlocks) { | 87 if (blocks_since_last_saturation < kSaturationLeakageBlocks) { |
79 std::for_each(R2->begin(), R2->end(), | 88 std::for_each(R2->begin(), R2->end(), |
80 [](float& a) { a *= kSaturationLeakageFactor; }); | 89 [](float& a) { a *= kSaturationLeakageFactor; }); |
81 } | 90 } |
82 } | 91 } |
83 | 92 |
84 // Estimates the residual echo power based on the linear echo path. | 93 // Estimates the residual echo power based on the linear echo path. |
85 void ErleBasedPowerEstimate( | 94 void ErleBasedPowerEstimate( |
86 bool headset_detected, | 95 bool headset_detected, |
87 const FftBuffer& X_buffer, | 96 const FftBuffer& X_buffer, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 const auto& X2 = X_buffer.Spectrum(linear_filter_based_delay); | 147 const auto& X2 = X_buffer.Spectrum(linear_filter_based_delay); |
139 (*R2)[k] = bands_with_reliable_filter[k] && using_subtractor_output | 148 (*R2)[k] = bands_with_reliable_filter[k] && using_subtractor_output |
140 ? S2_linear[k] / erle[k] | 149 ? S2_linear[k] / erle[k] |
141 : std::min(echo_path_gain[k] * X2[k], Y2[k]); | 150 : std::min(echo_path_gain[k] * X2[k], Y2[k]); |
142 } | 151 } |
143 } | 152 } |
144 | 153 |
145 } // namespace | 154 } // namespace |
146 | 155 |
147 ResidualEchoEstimator::ResidualEchoEstimator() { | 156 ResidualEchoEstimator::ResidualEchoEstimator() { |
148 echo_path_gain_.fill(0.f); | 157 echo_path_gain_.fill(100.f); |
peah-webrtc
2017/02/27 14:26:03
Not important, but more appropriate.
| |
149 } | 158 } |
150 | 159 |
151 ResidualEchoEstimator::~ResidualEchoEstimator() = default; | 160 ResidualEchoEstimator::~ResidualEchoEstimator() = default; |
152 | 161 |
153 void ResidualEchoEstimator::Estimate( | 162 void ResidualEchoEstimator::Estimate( |
154 bool using_subtractor_output, | 163 bool using_subtractor_output, |
155 const AecState& aec_state, | 164 const AecState& aec_state, |
156 const FftBuffer& X_buffer, | 165 const FftBuffer& X_buffer, |
157 const std::vector<std::array<float, kFftLengthBy2Plus1>>& H2, | 166 const std::vector<std::array<float, kFftLengthBy2Plus1>>& H2, |
158 const std::array<float, kFftLengthBy2Plus1>& E2_main, | 167 const std::array<float, kFftLengthBy2Plus1>& E2_main, |
159 const std::array<float, kFftLengthBy2Plus1>& E2_shadow, | 168 const std::array<float, kFftLengthBy2Plus1>& E2_shadow, |
160 const std::array<float, kFftLengthBy2Plus1>& S2_linear, | 169 const std::array<float, kFftLengthBy2Plus1>& S2_linear, |
161 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, | 170 const std::array<float, kFftLengthBy2Plus1>& S2_fallback, |
162 const std::array<float, kFftLengthBy2Plus1>& Y2, | 171 const std::array<float, kFftLengthBy2Plus1>& Y2, |
163 std::array<float, kFftLengthBy2Plus1>* R2) { | 172 std::array<float, kFftLengthBy2Plus1>* R2) { |
164 RTC_DCHECK(R2); | 173 RTC_DCHECK(R2); |
165 const rtc::Optional<size_t>& linear_filter_based_delay = | 174 const rtc::Optional<size_t>& linear_filter_based_delay = |
166 aec_state.FilterDelay(); | 175 aec_state.FilterDelay(); |
167 | 176 |
168 // Update the echo path gain. | 177 // Update the echo path gain. |
169 if (linear_filter_based_delay) { | 178 if (linear_filter_based_delay) { |
170 std::copy(H2[*linear_filter_based_delay].begin(), | 179 std::copy(H2[*linear_filter_based_delay].begin(), |
171 H2[*linear_filter_based_delay].end(), echo_path_gain_.begin()); | 180 H2[*linear_filter_based_delay].end(), echo_path_gain_.begin()); |
181 constexpr float kEchoPathGainHeadroom = 10.f; | |
182 std::for_each( | |
183 echo_path_gain_.begin(), echo_path_gain_.end(), | |
184 [kEchoPathGainHeadroom](float& a) { a *= kEchoPathGainHeadroom; }); | |
peah-webrtc
2017/02/27 14:26:03
Since the filter-based frequency response of the e
| |
172 } | 185 } |
173 | 186 |
174 // Counts the blocks since saturation. | 187 // Counts the blocks since saturation. |
175 if (aec_state.SaturatedCapture()) { | 188 if (aec_state.SaturatedCapture()) { |
176 blocks_since_last_saturation_ = 0; | 189 blocks_since_last_saturation_ = 0; |
177 } else { | 190 } else { |
178 ++blocks_since_last_saturation_; | 191 ++blocks_since_last_saturation_; |
179 } | 192 } |
180 | 193 |
181 // Counts the number of active render blocks that are in a row. | |
peah-webrtc
2017/02/27 14:26:03
This counter was not correctly done.
| |
182 if (aec_state.ActiveRender()) { | |
183 ++active_render_counter_; | |
184 } | |
185 | |
186 const auto& bands_with_reliable_filter = aec_state.BandsWithReliableFilter(); | 194 const auto& bands_with_reliable_filter = aec_state.BandsWithReliableFilter(); |
187 | 195 |
188 if (aec_state.UsableLinearEstimate()) { | 196 if (aec_state.UsableLinearEstimate()) { |
189 // Residual echo power estimation when the adaptive filter is reliable. | 197 // Residual echo power estimation when the adaptive filter is reliable. |
190 RTC_DCHECK(linear_filter_based_delay); | 198 RTC_DCHECK(linear_filter_based_delay); |
191 ErleBasedPowerEstimate( | 199 ErleBasedPowerEstimate( |
192 aec_state.HeadsetDetected(), X_buffer, using_subtractor_output, | 200 aec_state.HeadsetDetected(), X_buffer, using_subtractor_output, |
193 *linear_filter_based_delay, blocks_since_last_saturation_, | 201 *linear_filter_based_delay, blocks_since_last_saturation_, |
194 aec_state.PoorlyAlignedFilter(), bands_with_reliable_filter, | 202 aec_state.PoorlyAlignedFilter(), bands_with_reliable_filter, |
195 echo_path_gain_, S2_fallback, S2_linear, Y2, aec_state.Erle(), | 203 echo_path_gain_, S2_fallback, S2_linear, Y2, aec_state.Erle(), |
196 aec_state.Erl(), R2); | 204 aec_state.Erl(), R2); |
197 } else if (aec_state.ModelBasedAecFeasible()) { | 205 } else if (aec_state.ModelBasedAecFeasible()) { |
198 // Residual echo power when the adaptive filter is not reliable but still an | 206 // Residual echo power when the adaptive filter is not reliable but still an |
199 // external echo path delay is provided (and hence can be estimated). | 207 // external echo path delay is provided (and hence can be estimated). |
200 RTC_DCHECK(aec_state.ExternalDelay()); | 208 RTC_DCHECK(aec_state.ExternalDelay()); |
201 GainBasedPowerEstimate( | 209 GainBasedPowerEstimate( |
202 *aec_state.ExternalDelay(), X_buffer, blocks_since_last_saturation_, | 210 *aec_state.ExternalDelay(), X_buffer, blocks_since_last_saturation_, |
203 bands_with_reliable_filter, echo_path_gain_, S2_fallback, R2); | 211 aec_state.ActiveRenderBlocks(), bands_with_reliable_filter, |
212 echo_path_gain_, S2_fallback, R2); | |
204 } else if (aec_state.EchoLeakageDetected()) { | 213 } else if (aec_state.EchoLeakageDetected()) { |
205 // Residual echo power when an external residual echo detection algorithm | 214 // Residual echo power when an external residual echo detection algorithm |
206 // has deemed the echo canceller to leak echoes. | 215 // has deemed the echo canceller to leak echoes. |
207 HalfDuplexPowerEstimate(aec_state.ActiveRender(), Y2, R2); | 216 HalfDuplexPowerEstimate(aec_state.ActiveRender(), Y2, R2); |
208 } else { | 217 } else { |
209 // Residual echo power when none of the other cases are fulfilled. | 218 // Residual echo power when none of the other cases are fulfilled. |
210 InfiniteErlPowerEstimate(active_render_counter_, | 219 InfiniteErlPowerEstimate(aec_state.ActiveRenderBlocks(), |
211 blocks_since_last_saturation_, S2_fallback, R2); | 220 blocks_since_last_saturation_, S2_fallback, R2); |
212 } | 221 } |
213 } | 222 } |
214 | 223 |
224 void ResidualEchoEstimator::HandleEchoPathChange( | |
225 const EchoPathVariability& echo_path_variability) { | |
226 if (echo_path_variability.AudioPathChanged()) { | |
227 blocks_since_last_saturation_ = 0; | |
228 echo_path_gain_.fill(100.f); | |
229 } | |
230 } | |
231 | |
215 } // namespace webrtc | 232 } // namespace webrtc |
OLD | NEW |