blob: b038770b118d8ced0c49e3058b36f1ddebc1e7b7 [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.
Per Åhgrence202a02019-09-02 17:01:19 +0200109 std::fill(x[0][0].begin(), x[0][0].end(), 101.f);
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100110 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200111 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
112 subtractor_output[ch].ComputeMetrics(y[ch]);
113 }
Per Åhgren8ba58612017-12-01 23:01:44 +0100114 state.HandleEchoPathChange(EchoPathVariability(
115 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +0100116 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200117 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200118 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800119 EXPECT_FALSE(state.ActiveRender());
120
peah86afe9d2017-04-06 15:45:32 -0700121 for (int k = 0; k < 1000; ++k) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100122 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200123 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
124 subtractor_output[ch].ComputeMetrics(y[ch]);
125 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100126 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200127 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200128 E2_main, Y2, subtractor_output);
peah86afe9d2017-04-06 15:45:32 -0700129 }
peah522d71b2017-02-23 05:16:26 -0800130 EXPECT_TRUE(state.ActiveRender());
131
peah522d71b2017-02-23 05:16:26 -0800132 // Verify that the ERL is properly estimated
Per Åhgrence202a02019-09-02 17:01:19 +0200133 for (auto& band : x) {
134 for (auto& channel : band) {
135 channel = std::vector<float>(kBlockSize, 0.f);
136 }
peah86afe9d2017-04-06 15:45:32 -0700137 }
138
Per Åhgrence202a02019-09-02 17:01:19 +0200139 x[0][0][0] = 5000.f;
Per Åhgrenc59a5762017-12-11 21:34:19 +0100140 for (size_t k = 0;
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100141 k < render_delay_buffer->GetRenderBuffer()->GetFftBuffer().size(); ++k) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100142 render_delay_buffer->Insert(x);
143 if (k == 0) {
144 render_delay_buffer->Reset();
145 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100146 render_delay_buffer->PrepareCaptureProcessing();
peah86afe9d2017-04-06 15:45:32 -0700147 }
148
Per Åhgrenf9807252019-10-09 13:57:07 +0200149 for (auto& Y2_ch : Y2) {
150 Y2_ch.fill(10.f * 10000.f * 10000.f);
151 }
peah86afe9d2017-04-06 15:45:32 -0700152 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200153 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
154 subtractor_output[ch].ComputeMetrics(y[ch]);
155 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100156 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200157 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200158 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800159 }
160
161 ASSERT_TRUE(state.UsableLinearEstimate());
162 const std::array<float, kFftLengthBy2Plus1>& erl = state.Erl();
peah86afe9d2017-04-06 15:45:32 -0700163 EXPECT_EQ(erl[0], erl[1]);
164 for (size_t k = 1; k < erl.size() - 1; ++k) {
165 EXPECT_NEAR(k % 2 == 0 ? 10.f : 1000.f, erl[k], 0.1);
166 }
167 EXPECT_EQ(erl[erl.size() - 2], erl[erl.size() - 1]);
peah522d71b2017-02-23 05:16:26 -0800168
169 // Verify that the ERLE is properly estimated
Per Åhgrenf9807252019-10-09 13:57:07 +0200170 for (auto& E2_main_ch : E2_main) {
171 E2_main_ch.fill(1.f * 10000.f * 10000.f);
172 }
173 for (auto& Y2_ch : Y2) {
174 Y2_ch.fill(10.f * E2_main[0][0]);
175 }
peah86afe9d2017-04-06 15:45:32 -0700176 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200177 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
178 subtractor_output[ch].ComputeMetrics(y[ch]);
179 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100180 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200181 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200182 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800183 }
184 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700185 {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200186 // Note that the render spectrum is built so it does not have energy in
187 // the odd bands but just in the even bands.
Per Åhgrenb4161d32019-10-08 12:35:47 +0200188 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700189 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700190 constexpr size_t kLowFrequencyLimit = 32;
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200191 for (size_t k = 2; k < kLowFrequencyLimit; k = k + 2) {
192 EXPECT_NEAR(4.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700193 }
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200194 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; k = k + 2) {
195 EXPECT_NEAR(1.5f, erle[k], 0.1);
peah1d680892017-05-23 04:07:10 -0700196 }
peah86afe9d2017-04-06 15:45:32 -0700197 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
198 }
Per Åhgrenf9807252019-10-09 13:57:07 +0200199 for (auto& E2_main_ch : E2_main) {
200 E2_main_ch.fill(1.f * 10000.f * 10000.f);
201 }
202 for (auto& Y2_ch : Y2) {
203 Y2_ch.fill(5.f * E2_main[0][0]);
204 }
peah86afe9d2017-04-06 15:45:32 -0700205 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200206 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
207 subtractor_output[ch].ComputeMetrics(y[ch]);
208 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100209 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200210 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200211 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800212 }
peah86afe9d2017-04-06 15:45:32 -0700213
peah522d71b2017-02-23 05:16:26 -0800214 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700215 {
Per Åhgrenb4161d32019-10-08 12:35:47 +0200216 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700217 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700218 constexpr size_t kLowFrequencyLimit = 32;
219 for (size_t k = 1; k < kLowFrequencyLimit; ++k) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100220 EXPECT_NEAR(k % 2 == 0 ? 4.f : 1.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700221 }
peah1d680892017-05-23 04:07:10 -0700222 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; ++k) {
223 EXPECT_NEAR(k % 2 == 0 ? 1.5f : 1.f, erle[k], 0.1);
224 }
peah86afe9d2017-04-06 15:45:32 -0700225 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
226 }
peah522d71b2017-02-23 05:16:26 -0800227}
228
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200229} // namespace
230
231// Verify the general functionality of AecState
232TEST(AecState, NormalUsage) {
233 for (size_t num_render_channels : {1, 2, 8}) {
234 for (size_t num_capture_channels : {1, 2, 8}) {
235 SCOPED_TRACE(ProduceDebugText(num_render_channels, num_capture_channels));
236 RunNormalUsageTest(num_render_channels, num_capture_channels);
237 }
238 }
239}
240
peah522d71b2017-02-23 05:16:26 -0800241// Verifies the delay for a converged filter is correctly identified.
242TEST(AecState, ConvergedFilterDelay) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100243 constexpr int kFilterLengthBlocks = 10;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200244 constexpr size_t kNumCaptureChannels = 1;
Per Åhgren8ba58612017-12-01 23:01:44 +0100245 EchoCanceller3Config config;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200246 AecState state(config, kNumCaptureChannels);
Per Åhgren8ba58612017-12-01 23:01:44 +0100247 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200248 RenderDelayBuffer::Create(config, 48000, 1));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200249 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrenf9807252019-10-09 13:57:07 +0200250 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_main(
251 kNumCaptureChannels);
252 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -0800253 std::array<float, kBlockSize> x;
Per Åhgren8ba58612017-12-01 23:01:44 +0100254 EchoPathVariability echo_path_variability(
255 false, EchoPathVariability::DelayAdjustment::kNone, false);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200256 std::vector<SubtractorOutput> subtractor_output(kNumCaptureChannels);
257 for (auto& output : subtractor_output) {
258 output.Reset();
259 output.s_main.fill(100.f);
260 }
Per Åhgrenb20b9372018-07-13 00:22:54 +0200261 std::array<float, kBlockSize> y;
peah522d71b2017-02-23 05:16:26 -0800262 x.fill(0.f);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200263 y.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800264
Sam Zackrisson46b01402019-10-08 16:17:48 +0200265 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
266 frequency_response(
267 kNumCaptureChannels,
268 std::vector<std::array<float, kFftLengthBy2Plus1>>(kFilterLengthBlocks));
269 for (auto& v_ch : frequency_response) {
270 for (auto& v : v_ch) {
271 v.fill(0.01f);
272 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100273 }
peah522d71b2017-02-23 05:16:26 -0800274
Sam Zackrisson46b01402019-10-08 16:17:48 +0200275 std::vector<std::vector<float>> impulse_response(
276 kNumCaptureChannels,
277 std::vector<float>(GetTimeDomainLength(config.filter.main.length_blocks),
278 0.f));
peah29103572017-07-11 02:54:02 -0700279
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200280 // Verify that the filter delay for a converged filter is properly
281 // identified.
Per Åhgren5c532d32018-03-22 00:29:25 +0100282 for (int k = 0; k < kFilterLengthBlocks; ++k) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200283 for (auto& ir : impulse_response) {
284 std::fill(ir.begin(), ir.end(), 0.f);
285 ir[k * kBlockSize + 1] = 1.f;
286 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100287
peah86afe9d2017-04-06 15:45:32 -0700288 state.HandleEchoPathChange(echo_path_variability);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200289 subtractor_output[0].ComputeMetrics(y);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200290 state.Update(delay_estimate, frequency_response, impulse_response,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200291 *render_delay_buffer->GetRenderBuffer(), E2_main, Y2,
292 subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800293 }
294}
295
peah522d71b2017-02-23 05:16:26 -0800296} // namespace webrtc