peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/echo_remover.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 12 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | #include <algorithm> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 14 | #include <memory> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 15 | #include <numeric> |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/aec3/aec3_common.h" |
| 19 | #include "modules/audio_processing/aec3/render_buffer.h" |
| 20 | #include "modules/audio_processing/aec3/render_delay_buffer.h" |
| 21 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
| 22 | #include "modules/audio_processing/test/echo_canceller_test_tools.h" |
| 23 | #include "rtc_base/random.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 24 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace { |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 29 | std::string ProduceDebugText(int sample_rate_hz) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 30 | rtc::StringBuilder ss; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 31 | ss << "Sample rate: " << sample_rate_hz; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 32 | return ss.Release(); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 33 | } |
| 34 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 35 | std::string ProduceDebugText(int sample_rate_hz, int delay) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 36 | rtc::StringBuilder ss(ProduceDebugText(sample_rate_hz)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 37 | ss << ", Delay: " << delay; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 38 | return ss.Release(); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 39 | } |
| 40 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 41 | } // namespace |
| 42 | |
Sam Zackrisson | b18c4eb | 2020-01-24 12:55:17 +0100 | [diff] [blame] | 43 | class EchoRemoverMultiChannel |
| 44 | : public ::testing::Test, |
| 45 | public ::testing::WithParamInterface<std::tuple<size_t, size_t>> {}; |
| 46 | |
| 47 | INSTANTIATE_TEST_SUITE_P(MultiChannel, |
| 48 | EchoRemoverMultiChannel, |
| 49 | ::testing::Combine(::testing::Values(1, 2, 8), |
| 50 | ::testing::Values(1, 2, 8))); |
| 51 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 52 | // Verifies the basic API call sequence |
Sam Zackrisson | b18c4eb | 2020-01-24 12:55:17 +0100 | [diff] [blame] | 53 | TEST_P(EchoRemoverMultiChannel, BasicApiCalls) { |
| 54 | const size_t num_render_channels = std::get<0>(GetParam()); |
| 55 | const size_t num_capture_channels = std::get<1>(GetParam()); |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 56 | absl::optional<DelayEstimate> delay_estimate; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 57 | for (auto rate : {16000, 32000, 48000}) { |
Sam Zackrisson | b18c4eb | 2020-01-24 12:55:17 +0100 | [diff] [blame] | 58 | SCOPED_TRACE(ProduceDebugText(rate)); |
| 59 | std::unique_ptr<EchoRemover> remover( |
| 60 | EchoRemover::Create(EchoCanceller3Config(), rate, num_render_channels, |
| 61 | num_capture_channels)); |
| 62 | std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create( |
| 63 | EchoCanceller3Config(), rate, num_render_channels)); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 64 | |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 65 | Block render(NumBandsForRate(rate), num_render_channels); |
| 66 | Block capture(NumBandsForRate(rate), num_capture_channels); |
Sam Zackrisson | b18c4eb | 2020-01-24 12:55:17 +0100 | [diff] [blame] | 67 | for (size_t k = 0; k < 100; ++k) { |
| 68 | EchoPathVariability echo_path_variability( |
| 69 | k % 3 == 0 ? true : false, |
| 70 | k % 5 == 0 ? EchoPathVariability::DelayAdjustment::kNewDetectedDelay |
| 71 | : EchoPathVariability::DelayAdjustment::kNone, |
| 72 | false); |
| 73 | render_buffer->Insert(render); |
| 74 | render_buffer->PrepareCaptureProcessing(); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 75 | |
Sam Zackrisson | b18c4eb | 2020-01-24 12:55:17 +0100 | [diff] [blame] | 76 | remover->ProcessCapture(echo_path_variability, k % 2 == 0 ? true : false, |
| 77 | delay_estimate, render_buffer->GetRenderBuffer(), |
| 78 | nullptr, &capture); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 84 | |
| 85 | // Verifies the check for the samplerate. |
| 86 | // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH |
| 87 | // tests on test bots has been fixed. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 88 | TEST(EchoRemoverDeathTest, DISABLED_WrongSampleRate) { |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 89 | EXPECT_DEATH(std::unique_ptr<EchoRemover>( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 90 | EchoRemover::Create(EchoCanceller3Config(), 8001, 1, 1)), |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 91 | ""); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 92 | } |
| 93 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 94 | // Verifies the check for the number of capture bands. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 95 | // TODO(peah): Re-enable the test once the issue with memory leaks during DEATH |
| 96 | // tests on test bots has been fixed.c |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 97 | TEST(EchoRemoverDeathTest, DISABLED_WrongCaptureNumBands) { |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 98 | absl::optional<DelayEstimate> delay_estimate; |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 99 | for (auto rate : {16000, 32000, 48000}) { |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 100 | SCOPED_TRACE(ProduceDebugText(rate)); |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 101 | std::unique_ptr<EchoRemover> remover( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 102 | EchoRemover::Create(EchoCanceller3Config(), rate, 1, 1)); |
Gustaf Ullberg | cd277b8 | 2019-08-19 12:15:39 +0200 | [diff] [blame] | 103 | std::unique_ptr<RenderDelayBuffer> render_buffer( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 104 | RenderDelayBuffer::Create(EchoCanceller3Config(), rate, 1)); |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 105 | Block capture(NumBandsForRate(rate == 48000 ? 16000 : rate + 16000), 1); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 106 | EchoPathVariability echo_path_variability( |
| 107 | false, EchoPathVariability::DelayAdjustment::kNone, false); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 108 | EXPECT_DEATH(remover->ProcessCapture( |
| 109 | echo_path_variability, false, delay_estimate, |
| 110 | render_buffer->GetRenderBuffer(), nullptr, &capture), |
| 111 | ""); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | // Verifies the check for non-null capture block. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 116 | TEST(EchoRemoverDeathTest, NullCapture) { |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 117 | absl::optional<DelayEstimate> delay_estimate; |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 118 | std::unique_ptr<EchoRemover> remover( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 119 | EchoRemover::Create(EchoCanceller3Config(), 16000, 1, 1)); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 120 | std::unique_ptr<RenderDelayBuffer> render_buffer( |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 121 | RenderDelayBuffer::Create(EchoCanceller3Config(), 16000, 1)); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 122 | EchoPathVariability echo_path_variability( |
| 123 | false, EchoPathVariability::DelayAdjustment::kNone, false); |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 124 | EXPECT_DEATH(remover->ProcessCapture( |
| 125 | echo_path_variability, false, delay_estimate, |
| 126 | render_buffer->GetRenderBuffer(), nullptr, nullptr), |
| 127 | ""); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 132 | // Performs a sanity check that the echo_remover is able to properly |
| 133 | // remove echoes. |
| 134 | TEST(EchoRemover, BasicEchoRemoval) { |
| 135 | constexpr int kNumBlocksToProcess = 500; |
| 136 | Random random_generator(42U); |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 137 | absl::optional<DelayEstimate> delay_estimate; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 138 | for (size_t num_channels : {1, 2, 4}) { |
| 139 | for (auto rate : {16000, 32000, 48000}) { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 140 | Block x(NumBandsForRate(rate), num_channels); |
| 141 | Block y(NumBandsForRate(rate), num_channels); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 142 | EchoPathVariability echo_path_variability( |
| 143 | false, EchoPathVariability::DelayAdjustment::kNone, false); |
| 144 | for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
| 145 | SCOPED_TRACE(ProduceDebugText(rate, delay_samples)); |
| 146 | EchoCanceller3Config config; |
| 147 | std::unique_ptr<EchoRemover> remover( |
| 148 | EchoRemover::Create(config, rate, num_channels, num_channels)); |
| 149 | std::unique_ptr<RenderDelayBuffer> render_buffer( |
| 150 | RenderDelayBuffer::Create(config, rate, num_channels)); |
| 151 | render_buffer->AlignFromDelay(delay_samples / kBlockSize); |
Per Åhgren | c59a576 | 2017-12-11 21:34:19 +0100 | [diff] [blame] | 152 | |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 153 | std::vector<std::vector<std::unique_ptr<DelayBuffer<float>>>> |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 154 | delay_buffers(x.NumBands()); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 155 | for (size_t band = 0; band < delay_buffers.size(); ++band) { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 156 | delay_buffers[band].resize(x.NumChannels()); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 159 | for (int band = 0; band < x.NumBands(); ++band) { |
| 160 | for (int channel = 0; channel < x.NumChannels(); ++channel) { |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 161 | delay_buffers[band][channel].reset( |
| 162 | new DelayBuffer<float>(delay_samples)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | float input_energy = 0.f; |
| 167 | float output_energy = 0.f; |
| 168 | for (int k = 0; k < kNumBlocksToProcess; ++k) { |
| 169 | const bool silence = k < 100 || (k % 100 >= 10); |
| 170 | |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 171 | for (int band = 0; band < x.NumBands(); ++band) { |
| 172 | for (int channel = 0; channel < x.NumChannels(); ++channel) { |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 173 | if (silence) { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 174 | std::fill(x.begin(band, channel), x.end(band, channel), 0.f); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 175 | } else { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 176 | RandomizeSampleVector(&random_generator, x.View(band, channel)); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 177 | } |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 178 | delay_buffers[band][channel]->Delay(x.View(band, channel), |
| 179 | y.View(band, channel)); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | if (k > kNumBlocksToProcess / 2) { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 184 | input_energy = std::inner_product( |
| 185 | y.begin(/*band=*/0, /*channel=*/0), |
| 186 | y.end(/*band=*/0, /*channel=*/0), |
| 187 | y.begin(/*band=*/0, /*channel=*/0), input_energy); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | render_buffer->Insert(x); |
| 191 | render_buffer->PrepareCaptureProcessing(); |
| 192 | |
| 193 | remover->ProcessCapture(echo_path_variability, false, delay_estimate, |
Per Åhgren | c20a19c | 2019-11-13 11:12:29 +0100 | [diff] [blame] | 194 | render_buffer->GetRenderBuffer(), nullptr, |
| 195 | &y); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 196 | |
| 197 | if (k > kNumBlocksToProcess / 2) { |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 198 | output_energy = std::inner_product( |
| 199 | y.begin(/*band=*/0, /*channel=*/0), |
| 200 | y.end(/*band=*/0, /*channel=*/0), |
| 201 | y.begin(/*band=*/0, /*channel=*/0), output_energy); |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | EXPECT_GT(input_energy, 10.f * output_energy); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 205 | } |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 210 | } // namespace webrtc |