blob: bd8c7777c9064ca836e33410a94662bd8e736951 [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;
38 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
39 RenderDelayBuffer::Create(config, 3));
peah522d71b2017-02-23 05:16:26 -080040 RenderSignalAnalyzer render_signal_analyzer;
41 Random random_generator(42U);
42 Aec3Fft fft;
peah522d71b2017-02-23 05:16:26 -080043 std::array<float, kFftLengthBy2Plus1> Y2;
44 std::array<float, kFftLengthBy2Plus1> E2_main;
45 std::array<float, kFftLengthBy2Plus1> E2_shadow;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020046 AecState aec_state(EchoCanceller3Config{});
peah522d71b2017-02-23 05:16:26 -080047 x_old.fill(0.f);
48 Y2.fill(0.f);
49 E2_main.fill(0.f);
50 E2_shadow.fill(0.f);
51
52 DelayBuffer<float> delay_buffer(delay_samples);
53 for (int k = 0; k < num_blocks_to_process; ++k) {
peah86afe9d2017-04-06 15:45:32 -070054 RandomizeSampleVector(&random_generator, x[0]);
peah522d71b2017-02-23 05:16:26 -080055 if (uncorrelated_inputs) {
56 RandomizeSampleVector(&random_generator, y);
57 } else {
peah86afe9d2017-04-06 15:45:32 -070058 delay_buffer.Delay(x[0], y);
peah522d71b2017-02-23 05:16:26 -080059 }
Per Åhgren8ba58612017-12-01 23:01:44 +010060 render_delay_buffer->Insert(x);
61 if (k == 0) {
62 render_delay_buffer->Reset();
63 }
64 render_delay_buffer->PrepareCaptureCall();
65 render_signal_analyzer.Update(render_delay_buffer->GetRenderBuffer(),
66 aec_state.FilterDelay());
peah522d71b2017-02-23 05:16:26 -080067
68 // Handle echo path changes.
69 if (std::find(blocks_with_echo_path_changes.begin(),
70 blocks_with_echo_path_changes.end(),
71 k) != blocks_with_echo_path_changes.end()) {
Per Åhgren8ba58612017-12-01 23:01:44 +010072 subtractor.HandleEchoPathChange(EchoPathVariability(
73 true, EchoPathVariability::DelayAdjustment::kNewDetectedDelay,
74 false));
peah522d71b2017-02-23 05:16:26 -080075 }
Per Åhgren8ba58612017-12-01 23:01:44 +010076 subtractor.Process(render_delay_buffer->GetRenderBuffer(), y,
77 render_signal_analyzer, aec_state, &output);
peah522d71b2017-02-23 05:16:26 -080078
Per Åhgren8ba58612017-12-01 23:01:44 +010079 aec_state.HandleEchoPathChange(EchoPathVariability(
80 false, EchoPathVariability::DelayAdjustment::kNone, false));
peah522d71b2017-02-23 05:16:26 -080081 aec_state.Update(subtractor.FilterFrequencyResponse(),
peah29103572017-07-11 02:54:02 -070082 subtractor.FilterImpulseResponse(),
Per Åhgren8ba58612017-12-01 23:01:44 +010083 subtractor.ConvergedFilter(),
84 rtc::Optional<size_t>(delay_samples / kBlockSize),
85 render_delay_buffer->GetRenderBuffer(), E2_main, Y2, x[0],
86 output.s_main, false);
peah522d71b2017-02-23 05:16:26 -080087 }
88
89 const float output_power = std::inner_product(
90 output.e_main.begin(), output.e_main.end(), output.e_main.begin(), 0.f);
91 const float y_power = std::inner_product(y.begin(), y.end(), y.begin(), 0.f);
92 if (y_power == 0.f) {
93 ADD_FAILURE();
94 return -1.0;
95 }
96 return output_power / y_power;
97}
98
99std::string ProduceDebugText(size_t delay) {
100 std::ostringstream ss;
101 ss << "Delay: " << delay;
102 return ss.str();
103}
104
105} // namespace
106
107#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
108
109// Verifies that the check for non data dumper works.
110TEST(Subtractor, NullDataDumper) {
111 EXPECT_DEATH(Subtractor(nullptr, DetectOptimization()), "");
112}
113
114// Verifies the check for null subtractor output.
115// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
116// tests on test bots has been fixed.
117TEST(Subtractor, DISABLED_NullOutput) {
118 ApmDataDumper data_dumper(42);
119 Subtractor subtractor(&data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100120 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
121 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
peah522d71b2017-02-23 05:16:26 -0800122 RenderSignalAnalyzer render_signal_analyzer;
123 std::vector<float> y(kBlockSize, 0.f);
124
Per Åhgren8ba58612017-12-01 23:01:44 +0100125 EXPECT_DEATH(subtractor.Process(render_delay_buffer->GetRenderBuffer(), y,
126 render_signal_analyzer,
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200127 AecState(EchoCanceller3Config{}), nullptr),
128 "");
peah522d71b2017-02-23 05:16:26 -0800129}
130
131// Verifies the check for the capture signal size.
132TEST(Subtractor, WrongCaptureSize) {
133 ApmDataDumper data_dumper(42);
134 Subtractor subtractor(&data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +0100135 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
136 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
peah522d71b2017-02-23 05:16:26 -0800137 RenderSignalAnalyzer render_signal_analyzer;
138 std::vector<float> y(kBlockSize - 1, 0.f);
139 SubtractorOutput output;
140
Per Åhgren8ba58612017-12-01 23:01:44 +0100141 EXPECT_DEATH(subtractor.Process(render_delay_buffer->GetRenderBuffer(), y,
142 render_signal_analyzer,
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200143 AecState(EchoCanceller3Config{}), &output),
144 "");
peah522d71b2017-02-23 05:16:26 -0800145}
146
147#endif
148
149// Verifies that the subtractor is able to converge on correlated data.
150TEST(Subtractor, Convergence) {
151 std::vector<int> blocks_with_echo_path_changes;
152 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
153 SCOPED_TRACE(ProduceDebugText(delay_samples));
154
155 float echo_to_nearend_power = RunSubtractorTest(
156 100, delay_samples, false, blocks_with_echo_path_changes);
157 EXPECT_GT(0.1f, echo_to_nearend_power);
158 }
159}
160
161// Verifies that the subtractor does not converge on uncorrelated signals.
162TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) {
163 std::vector<int> blocks_with_echo_path_changes;
164 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
165 SCOPED_TRACE(ProduceDebugText(delay_samples));
166
167 float echo_to_nearend_power = RunSubtractorTest(
168 100, delay_samples, true, blocks_with_echo_path_changes);
169 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05);
170 }
171}
172
173// Verifies that the subtractor is properly reset when there is an echo path
174// change.
175TEST(Subtractor, EchoPathChangeReset) {
176 std::vector<int> blocks_with_echo_path_changes;
177 blocks_with_echo_path_changes.push_back(99);
178 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
179 SCOPED_TRACE(ProduceDebugText(delay_samples));
180
181 float echo_to_nearend_power = RunSubtractorTest(
182 100, delay_samples, false, blocks_with_echo_path_changes);
183 EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f);
184 }
185}
186
187} // namespace webrtc