blob: 3ca8220471cf615f74b45b250d69363b584c7e27 [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 {
peah522d71b2017-02-23 05:16:26 -080021
Sam Zackrisson8f736c02019-10-01 12:47:53 +020022void RunNormalUsageTest(size_t num_render_channels,
23 size_t num_capture_channels) {
24 // TODO(bugs.webrtc.org/10913): Test with different content in different
25 // channels.
Per Åhgrence202a02019-09-02 17:01:19 +020026 constexpr int kSampleRateHz = 48000;
27 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
peah522d71b2017-02-23 05:16:26 -080028 ApmDataDumper data_dumper(42);
Per Åhgren8ba58612017-12-01 23:01:44 +010029 EchoCanceller3Config config;
Sam Zackrisson8f736c02019-10-01 12:47:53 +020030 AecState state(config, num_capture_channels);
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020031 absl::optional<DelayEstimate> delay_estimate =
Per Åhgren5c532d32018-03-22 00:29:25 +010032 DelayEstimate(DelayEstimate::Quality::kRefined, 10);
Per Åhgren8ba58612017-12-01 23:01:44 +010033 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Sam Zackrisson8f736c02019-10-01 12:47:53 +020034 RenderDelayBuffer::Create(config, kSampleRateHz, num_render_channels));
Per Åhgrenf9807252019-10-09 13:57:07 +020035 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_main(
36 num_capture_channels);
37 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(num_capture_channels);
Per Åhgrence202a02019-09-02 17:01:19 +020038 std::vector<std::vector<std::vector<float>>> x(
39 kNumBands, std::vector<std::vector<float>>(
Sam Zackrisson8f736c02019-10-01 12:47:53 +020040 num_render_channels, std::vector<float>(kBlockSize, 0.f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010041 EchoPathVariability echo_path_variability(
42 false, EchoPathVariability::DelayAdjustment::kNone, false);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020043 std::vector<std::array<float, kBlockSize>> y(num_capture_channels);
44 std::vector<SubtractorOutput> subtractor_output(num_capture_channels);
45 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
46 subtractor_output[ch].Reset();
47 subtractor_output[ch].s_main.fill(100.f);
48 subtractor_output[ch].e_main.fill(100.f);
49 y[ch].fill(1000.f);
Per Åhgrenf9807252019-10-09 13:57:07 +020050 E2_main[ch].fill(0.f);
51 Y2[ch].fill(0.f);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020052 }
Per Åhgren8ba58612017-12-01 23:01:44 +010053 Aec3Fft fft;
Sam Zackrisson46b01402019-10-08 16:17:48 +020054 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
55 converged_filter_frequency_response(
56 num_capture_channels,
57 std::vector<std::array<float, kFftLengthBy2Plus1>>(10));
58 for (auto& v_ch : converged_filter_frequency_response) {
59 for (auto& v : v_ch) {
60 v.fill(0.01f);
61 }
peah522d71b2017-02-23 05:16:26 -080062 }
Sam Zackrisson46b01402019-10-08 16:17:48 +020063 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
peah522d71b2017-02-23 05:16:26 -080064 diverged_filter_frequency_response = converged_filter_frequency_response;
Sam Zackrisson46b01402019-10-08 16:17:48 +020065 converged_filter_frequency_response[0][2].fill(100.f);
66 converged_filter_frequency_response[0][2][0] = 1.f;
67 std::vector<std::vector<float>> impulse_response(
68 num_capture_channels,
69 std::vector<float>(GetTimeDomainLength(config.filter.main.length_blocks),
70 0.f));
peah29103572017-07-11 02:54:02 -070071
peah522d71b2017-02-23 05:16:26 -080072 // Verify that linear AEC usability is true when the filter is converged
Per Åhgrence202a02019-09-02 17:01:19 +020073 for (size_t band = 0; band < kNumBands; ++band) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +020074 for (size_t ch = 0; ch < num_render_channels; ++ch) {
75 std::fill(x[band][ch].begin(), x[band][ch].end(), 101.f);
Per Åhgrence202a02019-09-02 17:01:19 +020076 }
77 }
peah86afe9d2017-04-06 15:45:32 -070078 for (int k = 0; k < 3000; ++k) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +010079 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020080 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
81 subtractor_output[ch].ComputeMetrics(y[ch]);
82 }
Per Åhgren5c532d32018-03-22 00:29:25 +010083 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +020084 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +020085 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -080086 }
87 EXPECT_TRUE(state.UsableLinearEstimate());
88
Sam Zackrisson8f736c02019-10-01 12:47:53 +020089 // Verify that linear AEC usability becomes false after an echo path
90 // change is reported
91 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
92 subtractor_output[ch].ComputeMetrics(y[ch]);
93 }
Per Åhgren8ba58612017-12-01 23:01:44 +010094 state.HandleEchoPathChange(EchoPathVariability(
Per Åhgren88cf0502018-07-16 17:08:41 +020095 false, EchoPathVariability::DelayAdjustment::kBufferReadjustment, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +010096 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +020097 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +020098 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -080099 EXPECT_FALSE(state.UsableLinearEstimate());
100
101 // Verify that the active render detection works as intended.
Sam Zackrisson6e5433c2019-10-18 16:49:13 +0200102 for (size_t ch = 0; ch < num_render_channels; ++ch) {
103 std::fill(x[0][ch].begin(), x[0][ch].end(), 101.f);
104 }
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100105 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200106 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
107 subtractor_output[ch].ComputeMetrics(y[ch]);
108 }
Per Åhgren8ba58612017-12-01 23:01:44 +0100109 state.HandleEchoPathChange(EchoPathVariability(
110 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay, false));
Per Åhgren3ab308f2018-02-21 08:46:03 +0100111 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200112 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200113 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800114 EXPECT_FALSE(state.ActiveRender());
115
peah86afe9d2017-04-06 15:45:32 -0700116 for (int k = 0; k < 1000; ++k) {
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100117 render_delay_buffer->Insert(x);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200118 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
119 subtractor_output[ch].ComputeMetrics(y[ch]);
120 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100121 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200122 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200123 E2_main, Y2, subtractor_output);
peah86afe9d2017-04-06 15:45:32 -0700124 }
peah522d71b2017-02-23 05:16:26 -0800125 EXPECT_TRUE(state.ActiveRender());
126
peah522d71b2017-02-23 05:16:26 -0800127 // Verify that the ERL is properly estimated
Per Åhgrence202a02019-09-02 17:01:19 +0200128 for (auto& band : x) {
129 for (auto& channel : band) {
130 channel = std::vector<float>(kBlockSize, 0.f);
131 }
peah86afe9d2017-04-06 15:45:32 -0700132 }
133
Sam Zackrisson6e5433c2019-10-18 16:49:13 +0200134 for (size_t ch = 0; ch < num_render_channels; ++ch) {
135 x[0][ch][0] = 5000.f;
136 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100137 for (size_t k = 0;
Per Åhgrenec22e3f2017-12-20 15:20:37 +0100138 k < render_delay_buffer->GetRenderBuffer()->GetFftBuffer().size(); ++k) {
Per Åhgren8ba58612017-12-01 23:01:44 +0100139 render_delay_buffer->Insert(x);
140 if (k == 0) {
141 render_delay_buffer->Reset();
142 }
Per Åhgrenc59a5762017-12-11 21:34:19 +0100143 render_delay_buffer->PrepareCaptureProcessing();
peah86afe9d2017-04-06 15:45:32 -0700144 }
145
Per Åhgrenf9807252019-10-09 13:57:07 +0200146 for (auto& Y2_ch : Y2) {
147 Y2_ch.fill(10.f * 10000.f * 10000.f);
148 }
peah86afe9d2017-04-06 15:45:32 -0700149 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200150 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
151 subtractor_output[ch].ComputeMetrics(y[ch]);
152 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100153 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200154 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200155 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800156 }
157
158 ASSERT_TRUE(state.UsableLinearEstimate());
159 const std::array<float, kFftLengthBy2Plus1>& erl = state.Erl();
peah86afe9d2017-04-06 15:45:32 -0700160 EXPECT_EQ(erl[0], erl[1]);
161 for (size_t k = 1; k < erl.size() - 1; ++k) {
162 EXPECT_NEAR(k % 2 == 0 ? 10.f : 1000.f, erl[k], 0.1);
163 }
164 EXPECT_EQ(erl[erl.size() - 2], erl[erl.size() - 1]);
peah522d71b2017-02-23 05:16:26 -0800165
166 // Verify that the ERLE is properly estimated
Per Åhgrenf9807252019-10-09 13:57:07 +0200167 for (auto& E2_main_ch : E2_main) {
168 E2_main_ch.fill(1.f * 10000.f * 10000.f);
169 }
170 for (auto& Y2_ch : Y2) {
171 Y2_ch.fill(10.f * E2_main[0][0]);
172 }
peah86afe9d2017-04-06 15:45:32 -0700173 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200174 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
175 subtractor_output[ch].ComputeMetrics(y[ch]);
176 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100177 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200178 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200179 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800180 }
181 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700182 {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200183 // Note that the render spectrum is built so it does not have energy in
184 // the odd bands but just in the even bands.
Per Åhgrenb4161d32019-10-08 12:35:47 +0200185 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700186 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700187 constexpr size_t kLowFrequencyLimit = 32;
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200188 for (size_t k = 2; k < kLowFrequencyLimit; k = k + 2) {
189 EXPECT_NEAR(4.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700190 }
Jesús de Vicente Peñae9a7e902018-09-27 11:49:39 +0200191 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; k = k + 2) {
192 EXPECT_NEAR(1.5f, erle[k], 0.1);
peah1d680892017-05-23 04:07:10 -0700193 }
peah86afe9d2017-04-06 15:45:32 -0700194 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
195 }
Per Åhgrenf9807252019-10-09 13:57:07 +0200196 for (auto& E2_main_ch : E2_main) {
197 E2_main_ch.fill(1.f * 10000.f * 10000.f);
198 }
199 for (auto& Y2_ch : Y2) {
200 Y2_ch.fill(5.f * E2_main[0][0]);
201 }
peah86afe9d2017-04-06 15:45:32 -0700202 for (size_t k = 0; k < 1000; ++k) {
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200203 for (size_t ch = 0; ch < num_capture_channels; ++ch) {
204 subtractor_output[ch].ComputeMetrics(y[ch]);
205 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100206 state.Update(delay_estimate, converged_filter_frequency_response,
Per Åhgrenb20b9372018-07-13 00:22:54 +0200207 impulse_response, *render_delay_buffer->GetRenderBuffer(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200208 E2_main, Y2, subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800209 }
peah86afe9d2017-04-06 15:45:32 -0700210
peah522d71b2017-02-23 05:16:26 -0800211 ASSERT_TRUE(state.UsableLinearEstimate());
peah86afe9d2017-04-06 15:45:32 -0700212 {
Per Åhgrenb4161d32019-10-08 12:35:47 +0200213 const auto& erle = state.Erle()[0];
peah86afe9d2017-04-06 15:45:32 -0700214 EXPECT_EQ(erle[0], erle[1]);
peah1d680892017-05-23 04:07:10 -0700215 constexpr size_t kLowFrequencyLimit = 32;
216 for (size_t k = 1; k < kLowFrequencyLimit; ++k) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100217 EXPECT_NEAR(k % 2 == 0 ? 4.f : 1.f, erle[k], 0.1);
peah86afe9d2017-04-06 15:45:32 -0700218 }
peah1d680892017-05-23 04:07:10 -0700219 for (size_t k = kLowFrequencyLimit; k < erle.size() - 1; ++k) {
220 EXPECT_NEAR(k % 2 == 0 ? 1.5f : 1.f, erle[k], 0.1);
221 }
peah86afe9d2017-04-06 15:45:32 -0700222 EXPECT_EQ(erle[erle.size() - 2], erle[erle.size() - 1]);
223 }
peah522d71b2017-02-23 05:16:26 -0800224}
225
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200226} // namespace
227
Sam Zackrissonb18c4eb2020-01-24 12:55:17 +0100228class AecStateMultiChannel
229 : public ::testing::Test,
230 public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {};
231
232INSTANTIATE_TEST_SUITE_P(MultiChannel,
233 AecStateMultiChannel,
234 ::testing::Combine(::testing::Values(1, 2, 8),
235 ::testing::Values(1, 2, 8)));
236
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200237// Verify the general functionality of AecState
Sam Zackrissonb18c4eb2020-01-24 12:55:17 +0100238TEST_P(AecStateMultiChannel, NormalUsage) {
239 const size_t num_render_channels = std::get<0>(GetParam());
240 const size_t num_capture_channels = std::get<1>(GetParam());
241 RunNormalUsageTest(num_render_channels, num_capture_channels);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200242}
243
peah522d71b2017-02-23 05:16:26 -0800244// Verifies the delay for a converged filter is correctly identified.
245TEST(AecState, ConvergedFilterDelay) {
Per Åhgren5c532d32018-03-22 00:29:25 +0100246 constexpr int kFilterLengthBlocks = 10;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200247 constexpr size_t kNumCaptureChannels = 1;
Per Åhgren8ba58612017-12-01 23:01:44 +0100248 EchoCanceller3Config config;
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200249 AecState state(config, kNumCaptureChannels);
Per Åhgren8ba58612017-12-01 23:01:44 +0100250 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +0200251 RenderDelayBuffer::Create(config, 48000, 1));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +0200252 absl::optional<DelayEstimate> delay_estimate;
Per Åhgrenf9807252019-10-09 13:57:07 +0200253 std::vector<std::array<float, kFftLengthBy2Plus1>> E2_main(
254 kNumCaptureChannels);
255 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -0800256 std::array<float, kBlockSize> x;
Per Åhgren8ba58612017-12-01 23:01:44 +0100257 EchoPathVariability echo_path_variability(
258 false, EchoPathVariability::DelayAdjustment::kNone, false);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200259 std::vector<SubtractorOutput> subtractor_output(kNumCaptureChannels);
260 for (auto& output : subtractor_output) {
261 output.Reset();
262 output.s_main.fill(100.f);
263 }
Per Åhgrenb20b9372018-07-13 00:22:54 +0200264 std::array<float, kBlockSize> y;
peah522d71b2017-02-23 05:16:26 -0800265 x.fill(0.f);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200266 y.fill(0.f);
peah522d71b2017-02-23 05:16:26 -0800267
Sam Zackrisson46b01402019-10-08 16:17:48 +0200268 std::vector<std::vector<std::array<float, kFftLengthBy2Plus1>>>
269 frequency_response(
270 kNumCaptureChannels,
271 std::vector<std::array<float, kFftLengthBy2Plus1>>(kFilterLengthBlocks));
272 for (auto& v_ch : frequency_response) {
273 for (auto& v : v_ch) {
274 v.fill(0.01f);
275 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100276 }
peah522d71b2017-02-23 05:16:26 -0800277
Sam Zackrisson46b01402019-10-08 16:17:48 +0200278 std::vector<std::vector<float>> impulse_response(
279 kNumCaptureChannels,
280 std::vector<float>(GetTimeDomainLength(config.filter.main.length_blocks),
281 0.f));
peah29103572017-07-11 02:54:02 -0700282
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200283 // Verify that the filter delay for a converged filter is properly
284 // identified.
Per Åhgren5c532d32018-03-22 00:29:25 +0100285 for (int k = 0; k < kFilterLengthBlocks; ++k) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200286 for (auto& ir : impulse_response) {
287 std::fill(ir.begin(), ir.end(), 0.f);
288 ir[k * kBlockSize + 1] = 1.f;
289 }
Per Åhgren5c532d32018-03-22 00:29:25 +0100290
peah86afe9d2017-04-06 15:45:32 -0700291 state.HandleEchoPathChange(echo_path_variability);
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200292 subtractor_output[0].ComputeMetrics(y);
Per Åhgrenb20b9372018-07-13 00:22:54 +0200293 state.Update(delay_estimate, frequency_response, impulse_response,
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200294 *render_delay_buffer->GetRenderBuffer(), E2_main, Y2,
295 subtractor_output);
peah522d71b2017-02-23 05:16:26 -0800296 }
297}
298
peah522d71b2017-02-23 05:16:26 -0800299} // namespace webrtc