blob: c068b6e5f49bf4b750547d18b4d86bd042b91347 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec3/aec_state.h"
peah522d71b2017-02-23 05:16:26 -080012
Per Åhgren8ba58612017-12-01 23:01:44 +010013#include "modules/audio_processing/aec3/aec3_fft.h"
14#include "modules/audio_processing/aec3/render_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_processing/logging/apm_data_dumper.h"
Sam Zackrisson8f736c02019-10-01 12:47:53 +020016#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/gtest.h"
peah522d71b2017-02-23 05:16:26 -080018
19namespace webrtc {
Sam Zackrisson8f736c02019-10-01 12:47:53 +020020namespace {
21std::string ProduceDebugText(size_t num_render_channels,
22 size_t num_capture_channels) {
23 rtc::StringBuilder ss;
24 ss << "Render channels: " << num_render_channels;
25 ss << ", Capture channels: " << num_capture_channels;
26 return ss.Release();
27}
peah522d71b2017-02-23 05:16:26 -080028
Sam Zackrisson8f736c02019-10-01 12:47:53 +020029void RunNormalUsageTest(size_t num_render_channels,
30 size_t num_capture_channels) {
31 // TODO(bugs.webrtc.org/10913): Test with different content in different
32 // channels.
Per Åhgrence202a02019-09-02 17:01:19 +020033 constexpr int kSampleRateHz = 48000;
34 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
peah522d71b2017-02-23 05:16:26 -080035 ApmDataDumper data_dumper(42);
Per Åhgren8ba58612017-12-01 23:01:44 +010036 EchoCanceller3Config config;
Sam Zackrisson8f736c02019-10-01 12:47:53 +020037 AecState state(config, num_capture_channels);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020038 absl::optional<DelayEstimate> delay_estimate =
Per Åhgren5c532d32018-03-22 00:29:25 +010039 DelayEstimate(DelayEstimate::Quality::kRefined, 10);
Per Åhgren8ba58612017-12-01 23:01:44 +010040 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Sam Zackrisson8f736c02019-10-01 12:47:53 +020041 RenderDelayBuffer::Create(config, kSampleRateHz, num_render_channels));
Per Åhgrenf9807252019-10-09 13:57:07 +020042 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_main(
43 num_capture_channels);
44 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(num_capture_channels);
Per Åhgrence202a02019-09-02 17:01:19 +020045 std::vector<std::vector<std::vector<float>>> x(
46 kNumBands, std::vector<std::vector<float>>(
Sam Zackrisson8f736c02019-10-01 12:47:53 +020047 num_render_channels, std::vector<float>(kBlockSize, 0.f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010048 EchoPathVariability echo_path_variability(
49 false, EchoPathVariability::DelayAdjustment::kNone, false);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020050 std::vector<std::array<float, kBlockSize>> y(num_capture_channels);
51 std::vector<SubtractorOutput> subtractor_output(num_capture_channels);
52 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
53 subtractor_output[ch].Reset();
54 subtractor_output[ch].s_main.fill(100.f);
55 subtractor_output[ch].e_main.fill(100.f);
56 y[ch].fill(1000.f);
Per Åhgrenf9807252019-10-09 13:57:07 +020057 E2_main[ch].fill(0.f);
58 Y2[ch].fill(0.f);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020059 }
Per Åhgren8ba58612017-12-01 23:01:44 +010060 Aec3Fft fft;
Sam Zackrisson46b01402019-10-08 16:17:48 +020061 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
62 converged_filter_frequency_response(
63 num_capture_channels,
64 std::vector<std::array<float, kFftLengthBy2Plus1>>(10));
65 for (auto& v_ch : converged_filter_frequency_response) {
66 for (auto& v : v_ch) {
67 v.fill(0.01f);
68 }
peah522d71b2017-02-23 05:16:26 -080069 }
Sam Zackrisson46b01402019-10-08 16:17:48 +020070 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
peah522d71b2017-02-23 05:16:26 -080071 diverged_filter_frequency_response = converged_filter_frequency_response;
Sam Zackrisson46b01402019-10-08 16:17:48 +020072 converged_filter_frequency_response[0][2].fill(100.f);
73 converged_filter_frequency_response[0][2][0] = 1.f;
74 std::vector<std::vector<float>> impulse_response(
75 num_capture_channels,
76 std::vector<float>(GetTimeDomainLength(config.filter.main.length_blocks),
77 0.f));
peah29103572017-07-11 02:54:02 -070078
peah522d71b2017-02-23 05:16:26 -080079 // Verify that linear AEC usability is true when the filter is converged
Per Åhgrence202a02019-09-02 17:01:19 +020080 for (size_t band = 0; band < kNumBands; ++band) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +020081 for (size_t ch = 0; ch < num_render_channels; ++ch) {
82 std::fill(x[band][ch].begin(), x[band][ch].end(), 101.f);
Per Åhgrence202a02019-09-02 17:01:19 +020083 }
84 }
peah86afe9d2017-04-06 15:45:32 -070085 for (int k = 0; k < 3000; ++k) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +010086 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020087 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
88 subtractor_output[ch].ComputeMetrics(y[ch]);
89 }
Per Åhgren5c532d32018-03-22 00:29:25 +010090 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +020091 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +020092 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -080093 }
94 EXPECT_TRUE(state.UsableLinearEstimate());
95
Sam Zackrisson8f736c02019-10-01 12:47:53 +020096 // Verify that linear AEC usability becomes false after an echo path
97 // change is reported
98 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
99 subtractor_output[ch].ComputeMetrics(y[ch]);
100 }
Per Åhgren8ba58612017-12-01 23:01:44 +0100101 state.HandleEchoPathChange(EchoPathVariability(
Per Åhgren88cf0502018-07-16 17:08:41 +0200102 false, EchoPathVariability::DelayAdjustment::kBufferReadjustment, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +0100103 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200104 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200105 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800106 EXPECT_FALSE(state.UsableLinearEstimate());
107
108 // Verify that the active render detection works as intended.
Sam Zackrisson6e5433c2019-10-18 16:49:13 +0200109 for (size_t ch = 0; ch < num_render_channels; ++ch) {
110 std::fill(x[0][ch].begin(), x[0][ch].end(), 101.f);
111 }
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100112 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200113 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
114 subtractor_output[ch].ComputeMetrics(y[ch]);
115 }
Per Åhgren8ba58612017-12-01 23:01:44 +0100116 state.HandleEchoPathChange(EchoPathVariability(
117 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +0100118 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200119 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200120 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800121 EXPECT_FALSE(state.ActiveRender());
122
peah86afe9d2017-04-06 15:45:32 -0700123 for (int k = 0; k < 1000; ++k) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100124 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200125 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
126 subtractor_output[ch].ComputeMetrics(y[ch]);
127 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100128 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200129 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200130 E2_main, Y2, subtractor_output);
peah86afe9d2017-04-06 15:45:32 -0700131 }
peah522d71b2017-02-23 05:16:26 -0800132 EXPECT_TRUE(state.ActiveRender());
133
peah522d71b2017-02-23 05:16:26 -0800134 // Verify that the ERL is properly estimated
Per Åhgrence202a02019-09-02 17:01:19 +0200135 for (auto& band : x) {
136 for (auto& channel : band) {
137 channel = std::vector<float>(kBlockSize, 0.f);
138 }
peah86afe9d2017-04-06 15:45:32 -0700139 }
140
Sam Zackrisson6e5433c2019-10-18 16:49:13 +0200141 for (size_t ch = 0; ch < num_render_channels; ++ch) {
142 x[0][ch][0] = 5000.f;
143 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100144 for (size_t k = 0;
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100145 k < render_delay_buffer->GetRenderBuffer()->GetFftBuffer().size(); ++k) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100146 render_delay_buffer->Insert(x);
147 if (k == 0) {
148 render_delay_buffer->Reset();
149 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100150 render_delay_buffer->PrepareCaptureProcessing();
peah86afe9d2017-04-06 15:45:32 -0700151 }
152
Per Åhgrenf9807252019-10-09 13:57:07 +0200153 for (auto& Y2_ch : Y2) {
154 Y2_ch.fill(10.f * 10000.f * 10000.f);
155 }
peah86afe9d2017-04-06 15:45:32 -0700156 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200157 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
158 subtractor_output[ch].ComputeMetrics(y[ch]);
159 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100160 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200161 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200162 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800163 }
164
165 ASSERT_TRUE(state.UsableLinearEstimate());
166 const std::array<float, kFftLengthBy2Plus1>& erl = state.Erl();
peah86afe9d2017-04-06 15:45:32 -0700167 EXPECT_EQ(erl[0], erl[1]);
168 for (size_t k = 1; k < erl.size() - 1; ++k) {
169 EXPECT_NEAR(k % 2 == 0 ? 10.f : 1000.f, erl[k], 0.1);
170 }
171 EXPECT_EQ(erl[erl.size() - 2], erl[erl.size() - 1]);
peah522d71b2017-02-23 05:16:26 -0800172
173 // Verify that the ERLE is properly estimated
Per Åhgrenf9807252019-10-09 13:57:07 +0200174 for (auto& E2_main_ch : E2_main) {
175 E2_main_ch.fill(1.f * 10000.f * 10000.f);
176 }
177 for (auto& Y2_ch : Y2) {
178 Y2_ch.fill(10.f * E2_main[0][0]);
179 }
peah86afe9d2017-04-06 15:45:32 -0700180 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200181 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
182 subtractor_output[ch].ComputeMetrics(y[ch]);
183 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100184 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200185 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200186 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800187 }
188 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700189 {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200190 // Note that the render spectrum is built so it does not have energy in
191 // the odd bands but just in the even bands.
Per Åhgrenb4161d32019-10-08 12:35:47 +0200192 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700193 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700194 constexpr size_t kLowFrequencyLimit = 32;
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200195 for (size_t k = 2; k < kLowFrequencyLimit; k = k + 2) {
196 EXPECT_NEAR(4.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700197 }
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200198 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; k = k + 2) {
199 EXPECT_NEAR(1.5f, erle[k], 0.1);
peah1d680892017-05-23 04:07:10 -0700200 }
peah86afe9d2017-04-06 15:45:32 -0700201 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
202 }
Per Åhgrenf9807252019-10-09 13:57:07 +0200203 for (auto& E2_main_ch : E2_main) {
204 E2_main_ch.fill(1.f * 10000.f * 10000.f);
205 }
206 for (auto& Y2_ch : Y2) {
207 Y2_ch.fill(5.f * E2_main[0][0]);
208 }
peah86afe9d2017-04-06 15:45:32 -0700209 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200210 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
211 subtractor_output[ch].ComputeMetrics(y[ch]);
212 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100213 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200214 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200215 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800216 }
peah86afe9d2017-04-06 15:45:32 -0700217
peah522d71b2017-02-23 05:16:26 -0800218 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700219 {
Per Åhgrenb4161d32019-10-08 12:35:47 +0200220 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700221 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700222 constexpr size_t kLowFrequencyLimit = 32;
223 for (size_t k = 1; k < kLowFrequencyLimit; ++k) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100224 EXPECT_NEAR(k % 2 == 0 ? 4.f : 1.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700225 }
peah1d680892017-05-23 04:07:10 -0700226 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; ++k) {
227 EXPECT_NEAR(k % 2 == 0 ? 1.5f : 1.f, erle[k], 0.1);
228 }
peah86afe9d2017-04-06 15:45:32 -0700229 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
230 }
peah522d71b2017-02-23 05:16:26 -0800231}
232
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200233} // namespace
234
235// Verify the general functionality of AecState
236TEST(AecState, NormalUsage) {
237 for (size_t num_render_channels : {1, 2, 8}) {
238 for (size_t num_capture_channels : {1, 2, 8}) {
239 SCOPED_TRACE(ProduceDebugText(num_render_channels, num_capture_channels));
240 RunNormalUsageTest(num_render_channels, num_capture_channels);
241 }
242 }
243}
244
peah522d71b2017-02-23 05:16:26 -0800245// Verifies the delay for a converged filter is correctly identified.
246TEST(AecState, ConvergedFilterDelay) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100247 constexpr int kFilterLengthBlocks = 10;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200248 constexpr size_t kNumCaptureChannels = 1;
Per Åhgren8ba58612017-12-01 23:01:44 +0100249 EchoCanceller3Config config;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200250 AecState state(config, kNumCaptureChannels);
Per Åhgren8ba58612017-12-01 23:01:44 +0100251 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200252 RenderDelayBuffer::Create(config, 48000, 1));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200253 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrenf9807252019-10-09 13:57:07 +0200254 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_main(
255 kNumCaptureChannels);
256 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -0800257 std::array<float, kBlockSize> x;
Per Åhgren8ba58612017-12-01 23:01:44 +0100258 EchoPathVariability echo_path_variability(
259 false, EchoPathVariability::DelayAdjustment::kNone, false);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200260 std::vector<SubtractorOutput> subtractor_output(kNumCaptureChannels);
261 for (auto& output : subtractor_output) {
262 output.Reset();
263 output.s_main.fill(100.f);
264 }
Per Åhgrenb20b9372018-07-13 00:22:54 +0200265 std::array<float, kBlockSize> y;
peah522d71b2017-02-23 05:16:26 -0800266 x.fill(0.f);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200267 y.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800268
Sam Zackrisson46b01402019-10-08 16:17:48 +0200269 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
270 frequency_response(
271 kNumCaptureChannels,
272 std::vector<std::array<float, kFftLengthBy2Plus1>>(kFilterLengthBlocks));
273 for (auto& v_ch : frequency_response) {
274 for (auto& v : v_ch) {
275 v.fill(0.01f);
276 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100277 }
peah522d71b2017-02-23 05:16:26 -0800278
Sam Zackrisson46b01402019-10-08 16:17:48 +0200279 std::vector<std::vector<float>> impulse_response(
280 kNumCaptureChannels,
281 std::vector<float>(GetTimeDomainLength(config.filter.main.length_blocks),
282 0.f));
peah29103572017-07-11 02:54:02 -0700283
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200284 // Verify that the filter delay for a converged filter is properly
285 // identified.
Per Åhgren5c532d32018-03-22 00:29:25 +0100286 for (int k = 0; k < kFilterLengthBlocks; ++k) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200287 for (auto& ir : impulse_response) {
288 std::fill(ir.begin(), ir.end(), 0.f);
289 ir[k * kBlockSize + 1] = 1.f;
290 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100291
peah86afe9d2017-04-06 15:45:32 -0700292 state.HandleEchoPathChange(echo_path_variability);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200293 subtractor_output[0].ComputeMetrics(y);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200294 state.Update(delay_estimate, frequency_response, impulse_response,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200295 *render_delay_buffer->GetRenderBuffer(), E2_main, Y2,
296 subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800297 }
298}
299
peah522d71b2017-02-23 05:16:26 -0800300} // namespace webrtc