1 /*
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
11 #include "modules/audio_processing/aec3/subtractor.h"
12
13 #include <algorithm>
14 #include <numeric>
15 #include <string>
16
17 #include "modules/audio_processing/aec3/aec_state.h"
18 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
19 #include "rtc_base/random.h"
20 #include "test/gtest.h"
21
22 namespace webrtc {
23 namespace {
24
RunSubtractorTest(int num_blocks_to_process,int delay_samples,bool uncorrelated_inputs,const std::vector<int> & blocks_with_echo_path_changes)25 float RunSubtractorTest(int num_blocks_to_process,
26 int delay_samples,
27 bool uncorrelated_inputs,
28 const std::vector<int>& blocks_with_echo_path_changes) {
29 ApmDataDumper data_dumper(42);
30 Subtractor subtractor(&data_dumper, DetectOptimization());
31 std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
32 std::vector<float> y(kBlockSize, 0.f);
33 std::array<float, kBlockSize> x_old;
34 SubtractorOutput output;
35 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
36 std::vector<size_t>(1, kAdaptiveFilterLength));
37 RenderSignalAnalyzer render_signal_analyzer;
38 Random random_generator(42U);
39 Aec3Fft fft;
40 std::array<float, kFftLengthBy2Plus1> Y2;
41 std::array<float, kFftLengthBy2Plus1> E2_main;
42 std::array<float, kFftLengthBy2Plus1> E2_shadow;
43 AecState aec_state(EchoCanceller3Config{});
44 x_old.fill(0.f);
45 Y2.fill(0.f);
46 E2_main.fill(0.f);
47 E2_shadow.fill(0.f);
48
49 DelayBuffer<float> delay_buffer(delay_samples);
50 for (int k = 0; k < num_blocks_to_process; ++k) {
51 RandomizeSampleVector(&random_generator, x[0]);
52 if (uncorrelated_inputs) {
53 RandomizeSampleVector(&random_generator, y);
54 } else {
55 delay_buffer.Delay(x[0], y);
56 }
57 render_buffer.Insert(x);
58 render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay());
59
60 // Handle echo path changes.
61 if (std::find(blocks_with_echo_path_changes.begin(),
62 blocks_with_echo_path_changes.end(),
63 k) != blocks_with_echo_path_changes.end()) {
64 subtractor.HandleEchoPathChange(EchoPathVariability(true, true));
65 }
66 subtractor.Process(render_buffer, y, render_signal_analyzer, aec_state,
67 &output);
68
69 aec_state.HandleEchoPathChange(EchoPathVariability(false, false));
70 aec_state.Update(subtractor.FilterFrequencyResponse(),
71 subtractor.FilterImpulseResponse(),
72 subtractor.ConvergedFilter(), delay_samples / kBlockSize,
73 render_buffer, E2_main, Y2, x[0], output.s_main, false);
74 }
75
76 const float output_power = std::inner_product(
77 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
78 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
79 if (y_power == 0.f) {
80 ADD_FAILURE();
81 return -1.0;
82 }
83 return output_power / y_power;
84 }
85
ProduceDebugText(size_t delay)86 std::string ProduceDebugText(size_t delay) {
87 std::ostringstream ss;
88 ss << "Delay: " << delay;
89 return ss.str();
90 }
91
92 } // namespace
93
94 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
95
96 // Verifies that the check for non data dumper works.
TEST(Subtractor,NullDataDumper)97 TEST(Subtractor, NullDataDumper) {
98 EXPECT_DEATH(Subtractor(nullptr, DetectOptimization()), "");
99 }
100
101 // Verifies the check for null subtractor output.
102 // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
103 // tests on test bots has been fixed.
TEST(Subtractor,DISABLED_NullOutput)104 TEST(Subtractor, DISABLED_NullOutput) {
105 ApmDataDumper data_dumper(42);
106 Subtractor subtractor(&data_dumper, DetectOptimization());
107 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
108 std::vector<size_t>(1, kAdaptiveFilterLength));
109 RenderSignalAnalyzer render_signal_analyzer;
110 std::vector<float> y(kBlockSize, 0.f);
111
112 EXPECT_DEATH(subtractor.Process(render_buffer, y, render_signal_analyzer,
113 AecState(EchoCanceller3Config{}), nullptr),
114 "");
115 }
116
117 // Verifies the check for the capture signal size.
TEST(Subtractor,WrongCaptureSize)118 TEST(Subtractor, WrongCaptureSize) {
119 ApmDataDumper data_dumper(42);
120 Subtractor subtractor(&data_dumper, DetectOptimization());
121 RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength,
122 std::vector<size_t>(1, kAdaptiveFilterLength));
123 RenderSignalAnalyzer render_signal_analyzer;
124 std::vector<float> y(kBlockSize - 1, 0.f);
125 SubtractorOutput output;
126
127 EXPECT_DEATH(subtractor.Process(render_buffer, y, render_signal_analyzer,
128 AecState(EchoCanceller3Config{}), &output),
129 "");
130 }
131
132 #endif
133
134 // Verifies that the subtractor is able to converge on correlated data.
TEST(Subtractor,Convergence)135 TEST(Subtractor, Convergence) {
136 std::vector<int> blocks_with_echo_path_changes;
137 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
138 SCOPED_TRACE(ProduceDebugText(delay_samples));
139
140 float echo_to_nearend_power = RunSubtractorTest(
141 100, delay_samples, false, blocks_with_echo_path_changes);
142 EXPECT_GT(0.1f, echo_to_nearend_power);
143 }
144 }
145
146 // Verifies that the subtractor does not converge on uncorrelated signals.
TEST(Subtractor,NonConvergenceOnUncorrelatedSignals)147 TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
148 std::vector<int> blocks_with_echo_path_changes;
149 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
150 SCOPED_TRACE(ProduceDebugText(delay_samples));
151
152 float echo_to_nearend_power = RunSubtractorTest(
153 100, delay_samples, true, blocks_with_echo_path_changes);
154 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
155 }
156 }
157
158 // Verifies that the subtractor is properly reset when there is an echo path
159 // change.
TEST(Subtractor,EchoPathChangeReset)160 TEST(Subtractor, EchoPathChangeReset) {
161 std::vector<int> blocks_with_echo_path_changes;
162 blocks_with_echo_path_changes.push_back(99);
163 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
164 SCOPED_TRACE(ProduceDebugText(delay_samples));
165
166 float echo_to_nearend_power = RunSubtractorTest(
167 100, delay_samples, false, blocks_with_echo_path_changes);
168 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
169 }
170 }
171
172 } // namespace webrtc
173