blob: bed5b6e5bea5d6c663ca84612032eb713a0fa7cf [file] [log] [blame]
peah69221db2017-01-27 03:28:19 -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/echo_remover.h"
peah69221db2017-01-27 03:28:19 -080012
peah522d71b2017-02-23 05:16:26 -080013#include <algorithm>
peah69221db2017-01-27 03:28:19 -080014#include <memory>
peah522d71b2017-02-23 05:16:26 -080015#include <numeric>
peah69221db2017-01-27 03:28:19 -080016#include <sstream>
17#include <string>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/aec3/aec3_common.h"
20#include "modules/audio_processing/aec3/render_buffer.h"
21#include "modules/audio_processing/aec3/render_delay_buffer.h"
22#include "modules/audio_processing/logging/apm_data_dumper.h"
23#include "modules/audio_processing/test/echo_canceller_test_tools.h"
24#include "rtc_base/random.h"
25#include "test/gtest.h"
peah69221db2017-01-27 03:28:19 -080026
27namespace webrtc {
28namespace {
29
30std::string ProduceDebugText(int sample_rate_hz) {
31 std::ostringstream ss;
32 ss << "Sample rate: " << sample_rate_hz;
33 return ss.str();
34}
35
peah522d71b2017-02-23 05:16:26 -080036std::string ProduceDebugText(int sample_rate_hz, int delay) {
37 std::ostringstream ss(ProduceDebugText(sample_rate_hz));
38 ss << ", Delay: " << delay;
39 return ss.str();
40}
41
peah69221db2017-01-27 03:28:19 -080042} // namespace
43
44// Verifies the basic API call sequence
45TEST(EchoRemover, BasicApiCalls) {
46 for (auto rate : {8000, 16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -080047 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -070048 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020049 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +010050 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +010051 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -080052
53 std::vector<std::vector<float>> render(NumBandsForRate(rate),
54 std::vector<float>(kBlockSize, 0.f));
55 std::vector<std::vector<float>> capture(
56 NumBandsForRate(rate), std::vector<float>(kBlockSize, 0.f));
57 for (size_t k = 0; k < 100; ++k) {
Per Åhgren8ba58612017-12-01 23:01:44 +010058 EchoPathVariability echo_path_variability(
59 k % 3 == 0 ? true : false,
60 k % 5 == 0 ? EchoPathVariability::DelayAdjustment::kNewDetectedDelay
61 : EchoPathVariability::DelayAdjustment::kNone,
62 false);
peah69221db2017-01-27 03:28:19 -080063 rtc::Optional<size_t> echo_path_delay_samples =
64 (k % 6 == 0 ? rtc::Optional<size_t>(k * 10)
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +010065 : rtc::nullopt);
peahcf02cf12017-04-05 14:18:07 -070066 render_buffer->Insert(render);
Per Åhgrenc59a5762017-12-11 21:34:19 +010067 render_buffer->PrepareCaptureProcessing();
68
peahcf02cf12017-04-05 14:18:07 -070069 remover->ProcessCapture(echo_path_delay_samples, echo_path_variability,
70 k % 2 == 0 ? true : false,
71 render_buffer->GetRenderBuffer(), &capture);
peah69221db2017-01-27 03:28:19 -080072 }
73 }
74}
75
76#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
77
78// Verifies the check for the samplerate.
79// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
80// tests on test bots has been fixed.
81TEST(EchoRemover, DISABLED_WrongSampleRate) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020082 EXPECT_DEATH(std::unique_ptr<EchoRemover>(
83 EchoRemover::Create(EchoCanceller3Config(), 8001)),
peah697a5902017-06-30 07:06:10 -070084 "");
peah69221db2017-01-27 03:28:19 -080085}
86
peah69221db2017-01-27 03:28:19 -080087// Verifies the check for the capture block size.
88TEST(EchoRemover, WrongCaptureBlockSize) {
89 for (auto rate : {8000, 16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -080090 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -070091 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020092 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +010093 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +010094 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -080095 std::vector<std::vector<float>> capture(
96 NumBandsForRate(rate), std::vector<float>(kBlockSize - 1, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +010097 EchoPathVariability echo_path_variability(
98 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah69221db2017-01-27 03:28:19 -080099 rtc::Optional<size_t> echo_path_delay_samples;
peahcf02cf12017-04-05 14:18:07 -0700100 EXPECT_DEATH(remover->ProcessCapture(
101 echo_path_delay_samples, echo_path_variability, false,
102 render_buffer->GetRenderBuffer(), &capture),
103 "");
peah69221db2017-01-27 03:28:19 -0800104 }
105}
106
107// Verifies the check for the number of capture bands.
peah522d71b2017-02-23 05:16:26 -0800108// TODO(peah): Re-enable the test once the issue with memory leaks during DEATH
109// tests on test bots has been fixed.c
110TEST(EchoRemover, DISABLED_WrongCaptureNumBands) {
peah69221db2017-01-27 03:28:19 -0800111 for (auto rate : {16000, 32000, 48000}) {
peah522d71b2017-02-23 05:16:26 -0800112 SCOPED_TRACE(ProduceDebugText(rate));
peah697a5902017-06-30 07:06:10 -0700113 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200114 EchoRemover::Create(EchoCanceller3Config(), rate));
Per Åhgren38e2d952017-11-17 14:54:28 +0100115 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create(
Per Åhgren8ba58612017-12-01 23:01:44 +0100116 EchoCanceller3Config(), NumBandsForRate(rate)));
peah69221db2017-01-27 03:28:19 -0800117 std::vector<std::vector<float>> capture(
118 NumBandsForRate(rate == 48000 ? 16000 : rate + 16000),
119 std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +0100120 EchoPathVariability echo_path_variability(
121 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah69221db2017-01-27 03:28:19 -0800122 rtc::Optional<size_t> echo_path_delay_samples;
peahcf02cf12017-04-05 14:18:07 -0700123 EXPECT_DEATH(remover->ProcessCapture(
124 echo_path_delay_samples, echo_path_variability, false,
125 render_buffer->GetRenderBuffer(), &capture),
126 "");
peah69221db2017-01-27 03:28:19 -0800127 }
128}
129
130// Verifies the check for non-null capture block.
131TEST(EchoRemover, NullCapture) {
peah697a5902017-06-30 07:06:10 -0700132 std::unique_ptr<EchoRemover> remover(
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200133 EchoRemover::Create(EchoCanceller3Config(), 8000));
Per Åhgren8ba58612017-12-01 23:01:44 +0100134 std::unique_ptr<RenderDelayBuffer> render_buffer(
135 RenderDelayBuffer::Create(EchoCanceller3Config(), 3));
136 EchoPathVariability echo_path_variability(
137 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah69221db2017-01-27 03:28:19 -0800138 rtc::Optional<size_t> echo_path_delay_samples;
139 EXPECT_DEATH(
peahcf02cf12017-04-05 14:18:07 -0700140 remover->ProcessCapture(echo_path_delay_samples, echo_path_variability,
141 false, render_buffer->GetRenderBuffer(), nullptr),
peah69221db2017-01-27 03:28:19 -0800142 "");
143}
144
145#endif
146
peah522d71b2017-02-23 05:16:26 -0800147// Performs a sanity check that the echo_remover is able to properly
148// remove echoes.
149TEST(EchoRemover, BasicEchoRemoval) {
150 constexpr int kNumBlocksToProcess = 500;
151 Random random_generator(42U);
152 for (auto rate : {8000, 16000, 32000, 48000}) {
153 std::vector<std::vector<float>> x(NumBandsForRate(rate),
154 std::vector<float>(kBlockSize, 0.f));
155 std::vector<std::vector<float>> y(NumBandsForRate(rate),
156 std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +0100157 EchoPathVariability echo_path_variability(
158 false, EchoPathVariability::DelayAdjustment::kNone, false);
peah522d71b2017-02-23 05:16:26 -0800159 for (size_t delay_samples : {0, 64, 150, 200, 301}) {
160 SCOPED_TRACE(ProduceDebugText(rate, delay_samples));
Per Åhgren8ba58612017-12-01 23:01:44 +0100161 EchoCanceller3Config config;
162 config.delay.min_echo_path_delay_blocks = 0;
163 std::unique_ptr<EchoRemover> remover(EchoRemover::Create(config, rate));
peahcf02cf12017-04-05 14:18:07 -0700164 std::unique_ptr<RenderDelayBuffer> render_buffer(
Per Åhgren8ba58612017-12-01 23:01:44 +0100165 RenderDelayBuffer::Create(config, NumBandsForRate(rate)));
Per Åhgrenc59a5762017-12-11 21:34:19 +0100166 render_buffer->SetDelay(delay_samples / kBlockSize);
167
peah522d71b2017-02-23 05:16:26 -0800168 std::vector<std::unique_ptr<DelayBuffer<float>>> delay_buffers(x.size());
169 for (size_t j = 0; j < x.size(); ++j) {
170 delay_buffers[j].reset(new DelayBuffer<float>(delay_samples));
171 }
172
173 float input_energy = 0.f;
174 float output_energy = 0.f;
175 for (int k = 0; k < kNumBlocksToProcess; ++k) {
176 const bool silence = k < 100 || (k % 100 >= 10);
177
178 for (size_t j = 0; j < x.size(); ++j) {
179 if (silence) {
180 std::fill(x[j].begin(), x[j].end(), 0.f);
181 } else {
182 RandomizeSampleVector(&random_generator, x[j]);
183 }
184 delay_buffers[j]->Delay(x[j], y[j]);
185 }
186
187 if (k > kNumBlocksToProcess / 2) {
188 for (size_t j = 0; j < x.size(); ++j) {
189 input_energy = std::inner_product(y[j].begin(), y[j].end(),
190 y[j].begin(), input_energy);
191 }
192 }
193
peahcf02cf12017-04-05 14:18:07 -0700194 render_buffer->Insert(x);
Per Åhgrenc59a5762017-12-11 21:34:19 +0100195 render_buffer->PrepareCaptureProcessing();
peahcf02cf12017-04-05 14:18:07 -0700196
Oskar Sundbomaa8b67d2017-11-17 14:34:48 +0100197 remover->ProcessCapture(delay_samples, echo_path_variability, false,
peahcf02cf12017-04-05 14:18:07 -0700198 render_buffer->GetRenderBuffer(), &y);
peah522d71b2017-02-23 05:16:26 -0800199
200 if (k > kNumBlocksToProcess / 2) {
201 for (size_t j = 0; j < x.size(); ++j) {
202 output_energy = std::inner_product(y[j].begin(), y[j].end(),
203 y[j].begin(), output_energy);
204 }
205 }
206 }
207 EXPECT_GT(input_energy, 10.f * output_energy);
208 }
209 }
210}
211
peah69221db2017-01-27 03:28:19 -0800212} // namespace webrtc