peah | 522d71b | 2017-02-23 05:16:26 -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 | |
| 11 | #include "webrtc/modules/audio_processing/aec3/subtractor.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <numeric> |
| 15 | #include <string> |
| 16 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | #include "webrtc/modules/audio_processing/aec3/aec_state.h" |
| 18 | #include "webrtc/modules/audio_processing/test/echo_canceller_test_tools.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 19 | #include "webrtc/rtc_base/random.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 20 | #include "webrtc/test/gtest.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | namespace { |
| 24 | |
| 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()); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 31 | std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 32 | std::vector<float> y(kBlockSize, 0.f); |
| 33 | std::array<float, kBlockSize> x_old; |
| 34 | SubtractorOutput output; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 35 | RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength, |
| 36 | std::vector<size_t>(1, kAdaptiveFilterLength)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 37 | RenderSignalAnalyzer render_signal_analyzer; |
| 38 | Random random_generator(42U); |
| 39 | Aec3Fft fft; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 40 | std::array<float, kFftLengthBy2Plus1> Y2; |
| 41 | std::array<float, kFftLengthBy2Plus1> E2_main; |
| 42 | std::array<float, kFftLengthBy2Plus1> E2_shadow; |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 43 | AecState aec_state(AudioProcessing::Config::EchoCanceller3{}); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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) { |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 51 | RandomizeSampleVector(&random_generator, x[0]); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 52 | if (uncorrelated_inputs) { |
| 53 | RandomizeSampleVector(&random_generator, y); |
| 54 | } else { |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 55 | delay_buffer.Delay(x[0], y); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 56 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 57 | render_buffer.Insert(x); |
| 58 | render_signal_analyzer.Update(render_buffer, aec_state.FilterDelay()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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 | } |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 66 | subtractor.Process(render_buffer, y, render_signal_analyzer, aec_state, |
| 67 | &output); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 68 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 69 | aec_state.HandleEchoPathChange(EchoPathVariability(false, false)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 70 | aec_state.Update(subtractor.FilterFrequencyResponse(), |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 71 | subtractor.FilterImpulseResponse(), |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 72 | rtc::Optional<size_t>(delay_samples / kBlockSize), |
peah | 2910357 | 2017-07-11 02:54:02 -0700 | [diff] [blame] | 73 | render_buffer, E2_main, Y2, x[0], output.s_main, false); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 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 | |
| 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. |
| 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. |
| 104 | TEST(Subtractor, DISABLED_NullOutput) { |
| 105 | ApmDataDumper data_dumper(42); |
| 106 | Subtractor subtractor(&data_dumper, DetectOptimization()); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 107 | RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength, |
| 108 | std::vector<size_t>(1, kAdaptiveFilterLength)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 109 | RenderSignalAnalyzer render_signal_analyzer; |
| 110 | std::vector<float> y(kBlockSize, 0.f); |
| 111 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 112 | EXPECT_DEATH( |
| 113 | subtractor.Process(render_buffer, y, render_signal_analyzer, |
| 114 | AecState(AudioProcessing::Config::EchoCanceller3{}), |
| 115 | nullptr), |
| 116 | ""); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Verifies the check for the capture signal size. |
| 120 | TEST(Subtractor, WrongCaptureSize) { |
| 121 | ApmDataDumper data_dumper(42); |
| 122 | Subtractor subtractor(&data_dumper, DetectOptimization()); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 123 | RenderBuffer render_buffer(Aec3Optimization::kNone, 3, kAdaptiveFilterLength, |
| 124 | std::vector<size_t>(1, kAdaptiveFilterLength)); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 125 | RenderSignalAnalyzer render_signal_analyzer; |
| 126 | std::vector<float> y(kBlockSize - 1, 0.f); |
| 127 | SubtractorOutput output; |
| 128 | |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 129 | EXPECT_DEATH( |
| 130 | subtractor.Process(render_buffer, y, render_signal_analyzer, |
| 131 | AecState(AudioProcessing::Config::EchoCanceller3{}), |
| 132 | &output), |
| 133 | ""); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #endif |
| 137 | |
| 138 | // Verifies that the subtractor is able to converge on correlated data. |
| 139 | TEST(Subtractor, Convergence) { |
| 140 | std::vector<int> blocks_with_echo_path_changes; |
| 141 | for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
| 142 | SCOPED_TRACE(ProduceDebugText(delay_samples)); |
| 143 | |
| 144 | float echo_to_nearend_power = RunSubtractorTest( |
| 145 | 100, delay_samples, false, blocks_with_echo_path_changes); |
| 146 | EXPECT_GT(0.1f, echo_to_nearend_power); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Verifies that the subtractor does not converge on uncorrelated signals. |
| 151 | TEST(Subtractor, NonConvergenceOnUncorrelatedSignals) { |
| 152 | std::vector<int> blocks_with_echo_path_changes; |
| 153 | for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
| 154 | SCOPED_TRACE(ProduceDebugText(delay_samples)); |
| 155 | |
| 156 | float echo_to_nearend_power = RunSubtractorTest( |
| 157 | 100, delay_samples, true, blocks_with_echo_path_changes); |
| 158 | EXPECT_NEAR(1.f, echo_to_nearend_power, 0.05); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Verifies that the subtractor is properly reset when there is an echo path |
| 163 | // change. |
| 164 | TEST(Subtractor, EchoPathChangeReset) { |
| 165 | std::vector<int> blocks_with_echo_path_changes; |
| 166 | blocks_with_echo_path_changes.push_back(99); |
| 167 | for (size_t delay_samples : {0, 64, 150, 200, 301}) { |
| 168 | SCOPED_TRACE(ProduceDebugText(delay_samples)); |
| 169 | |
| 170 | float echo_to_nearend_power = RunSubtractorTest( |
| 171 | 100, delay_samples, false, blocks_with_echo_path_changes); |
| 172 | EXPECT_NEAR(1.f, echo_to_nearend_power, 0.0000001f); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } // namespace webrtc |