blob: a9fc9c2c1f8c8f24e8fa8df8b02c25aa92876b6b [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/subtractor.h"
peah522d71b2017-02-23 05:16:26 -080012
13#include <algorithm>
14#include <numeric>
15#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/aec3/aec_state.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010018#include "modules/audio_processing/aec3/render_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/test/echo_canceller_test_tools.h"
20#include "rtc_base/random.h"
21#include "test/gtest.h"
peah522d71b2017-02-23 05:16:26 -080022
23namespace webrtc {
24namespace {
25
26float RunSubtractorTest(int num_blocks_to_process,
27 int delay_samples,
28 bool uncorrelated_inputs,
29 const std::vector<int>& blocks_with_echo_path_changes) {
30 ApmDataDumper data_dumper(42);
31 Subtractor subtractor(&data_dumper, DetectOptimization());
peah86afe9d2017-04-06 15:45:32 -070032 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
peah522d71b2017-02-23 05:16:26 -080033 std::vector<float> y(kBlockSize, 0.f);
34 std::array<float, kBlockSize> x_old;
35 SubtractorOutput output;
Per Åhgren8ba58612017-12-01 23:01:44 +010036 EchoCanceller3Config config;
37 config.delay.min_echo_path_delay_blocks = 0;
Per Åhgrenc59a5762017-12-11 21:34:19 +010038 config.delay.default_delay = 1;
Per Åhgren8ba58612017-12-01 23:01:44 +010039 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
40 RenderDelayBuffer::Create(config, 3));
peah522d71b2017-02-23 05:16:26 -080041 RenderSignalAnalyzer render_signal_analyzer;
42 Random random_generator(42U);
43 Aec3Fft fft;
peah522d71b2017-02-23 05:16:26 -080044 std::array<float, kFftLengthBy2Plus1> Y2;
45 std::array<float, kFftLengthBy2Plus1> E2_main;
46 std::array<float, kFftLengthBy2Plus1> E2_shadow;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020047 AecState aec_state(EchoCanceller3Config{});
peah522d71b2017-02-23 05:16:26 -080048 x_old.fill(0.f);
49 Y2.fill(0.f);
50 E2_main.fill(0.f);
51 E2_shadow.fill(0.f);
52
53 DelayBuffer<float> delay_buffer(delay_samples);
54 for (int k = 0; k < num_blocks_to_process; ++k) {
peah86afe9d2017-04-06 15:45:32 -070055 RandomizeSampleVector(&random_generator, x[0]);
peah522d71b2017-02-23 05:16:26 -080056 if (uncorrelated_inputs) {
57 RandomizeSampleVector(&random_generator, y);
58 } else {
peah86afe9d2017-04-06 15:45:32 -070059 delay_buffer.Delay(x[0], y);
peah522d71b2017-02-23 05:16:26 -080060 }
Per Åhgren8ba58612017-12-01 23:01:44 +010061 render_delay_buffer->Insert(x);
62 if (k == 0) {
63 render_delay_buffer->Reset();
64 }
Per Åhgrenc59a5762017-12-11 21:34:19 +010065 render_delay_buffer->PrepareCaptureProcessing();
66 render_delay_buffer->GetRenderBuffer()->UpdateSpectralSum();
67 render_signal_analyzer.Update(*render_delay_buffer->GetRenderBuffer(),
Per Åhgren8ba58612017-12-01 23:01:44 +010068 aec_state.FilterDelay());
peah522d71b2017-02-23 05:16:26 -080069
70 // Handle echo path changes.
71 if (std::find(blocks_with_echo_path_changes.begin(),
72 blocks_with_echo_path_changes.end(),
73 k) != blocks_with_echo_path_changes.end()) {
Per Åhgren8ba58612017-12-01 23:01:44 +010074 subtractor.HandleEchoPathChange(EchoPathVariability(
75 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay,
76 false));
peah522d71b2017-02-23 05:16:26 -080077 }
Per Åhgrenc59a5762017-12-11 21:34:19 +010078 subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
Per Åhgren8ba58612017-12-01 23:01:44 +010079 render_signal_analyzer, aec_state, &output);
peah522d71b2017-02-23 05:16:26 -080080
Per Åhgren8ba58612017-12-01 23:01:44 +010081 aec_state.HandleEchoPathChange(EchoPathVariability(
82 false, EchoPathVariability::DelayAdjustment::kNone, false));
peah522d71b2017-02-23 05:16:26 -080083 aec_state.Update(subtractor.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -070084 subtractor.FilterImpulseResponse(),
Per Åhgren8ba58612017-12-01 23:01:44 +010085 subtractor.ConvergedFilter(),
86 rtc::Optional<size_t>(delay_samples / kBlockSize),
Per Åhgrenc59a5762017-12-11 21:34:19 +010087 *render_delay_buffer->GetRenderBuffer(), E2_main, Y2, x[0],
Per Åhgren8ba58612017-12-01 23:01:44 +010088 output.s_main, false);
peah522d71b2017-02-23 05:16:26 -080089 }
90
91 const float output_power = std::inner_product(
92 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
93 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
94 if (y_power == 0.f) {
95 ADD_FAILURE();
96 return -1.0;
97 }
98 return output_power / y_power;
99}
100
101std::string ProduceDebugText(size_t delay) {
102 std::ostringstream ss;
103 ss << "Delay: " << delay;
104 return ss.str();
105}
106
107} // namespace
108
109#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
110
111// Verifies that the check for non data dumper works.
112TEST(Subtractor, NullDataDumper) {
113 EXPECT_DEATH(Subtractor(nullptr, DetectOptimization()), "");
114}
115
116// Verifies the check for null subtractor output.
117// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
118// tests on test bots has been fixed.
119TEST(Subtractor, DISABLED_NullOutput) {
120 ApmDataDumper data_dumper(42);
121 Subtractor subtractor(&data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100122 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
123 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
peah522d71b2017-02-23 05:16:26 -0800124 RenderSignalAnalyzer render_signal_analyzer;
125 std::vector<float> y(kBlockSize, 0.f);
126
Per Åhgrenc59a5762017-12-11 21:34:19 +0100127 EXPECT_DEATH(subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
Per Åhgren8ba58612017-12-01 23:01:44 +0100128 render_signal_analyzer,
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200129 AecState(EchoCanceller3Config{}), nullptr),
130 "");
peah522d71b2017-02-23 05:16:26 -0800131}
132
133// Verifies the check for the capture signal size.
134TEST(Subtractor, WrongCaptureSize) {
135 ApmDataDumper data_dumper(42);
136 Subtractor subtractor(&data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100137 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
138 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
peah522d71b2017-02-23 05:16:26 -0800139 RenderSignalAnalyzer render_signal_analyzer;
140 std::vector<float> y(kBlockSize - 1, 0.f);
141 SubtractorOutput output;
142
Per Åhgrenc59a5762017-12-11 21:34:19 +0100143 EXPECT_DEATH(subtractor.Process(*render_delay_buffer->GetRenderBuffer(), y,
Per Åhgren8ba58612017-12-01 23:01:44 +0100144 render_signal_analyzer,
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200145 AecState(EchoCanceller3Config{}), &output),
146 "");
peah522d71b2017-02-23 05:16:26 -0800147}
148
149#endif
150
151// Verifies that the subtractor is able to converge on correlated data.
152TEST(Subtractor, Convergence) {
153 std::vector<int> blocks_with_echo_path_changes;
154 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
155 SCOPED_TRACE(ProduceDebugText(delay_samples));
156
157 float echo_to_nearend_power = RunSubtractorTest(
158 100, delay_samples, false, blocks_with_echo_path_changes);
159 EXPECT_GT(0.1f, echo_to_nearend_power);
160 }
161}
162
163// Verifies that the subtractor does not converge on uncorrelated signals.
164TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
165 std::vector<int> blocks_with_echo_path_changes;
166 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
167 SCOPED_TRACE(ProduceDebugText(delay_samples));
168
169 float echo_to_nearend_power = RunSubtractorTest(
170 100, delay_samples, true, blocks_with_echo_path_changes);
171 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
172 }
173}
174
175// Verifies that the subtractor is properly reset when there is an echo path
176// change.
177TEST(Subtractor, EchoPathChangeReset) {
178 std::vector<int> blocks_with_echo_path_changes;
179 blocks_with_echo_path_changes.push_back(99);
180 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
181 SCOPED_TRACE(ProduceDebugText(delay_samples));
182
183 float echo_to_nearend_power = RunSubtractorTest(
184 100, delay_samples, false, blocks_with_echo_path_changes);
185 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
186 }
187}
188
189} // namespace webrtc