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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aec3/suppression_gain.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 12 | |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 13 | #include "modules/audio_processing/aec3/aec_state.h" |
| 14 | #include "modules/audio_processing/aec3/render_buffer.h" |
| 15 | #include "modules/audio_processing/aec3/subtractor.h" |
| 16 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "system_wrappers/include/cpu_features_wrapper.h" |
| 19 | #include "test/gtest.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 20 | #include "typedefs.h" // NOLINT(build/include) |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace aec3 { |
| 24 | |
| 25 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 26 | |
| 27 | // Verifies that the check for non-null output gains works. |
| 28 | TEST(SuppressionGain, NullOutputGains) { |
| 29 | std::array<float, kFftLengthBy2Plus1> E2; |
| 30 | std::array<float, kFftLengthBy2Plus1> R2; |
| 31 | std::array<float, kFftLengthBy2Plus1> N2; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 32 | E2.fill(0.f); |
| 33 | R2.fill(0.f); |
| 34 | N2.fill(0.f); |
| 35 | float high_bands_gain; |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 36 | AecState aec_state(EchoCanceller3Config{}); |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 37 | EXPECT_DEATH(SuppressionGain(EchoCanceller3Config{}, DetectOptimization()) |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 38 | .GetGain(E2, R2, N2, RenderSignalAnalyzer(), aec_state, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 39 | std::vector<std::vector<float>>( |
| 40 | 3, std::vector<float>(kBlockSize, 0.f)), |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 41 | &high_bands_gain, nullptr), |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 42 | ""); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #endif |
| 46 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 47 | // Does a sanity check that the gains are correctly computed. |
| 48 | TEST(SuppressionGain, BasicGainComputation) { |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame] | 49 | SuppressionGain suppression_gain(EchoCanceller3Config(), |
peah | 8cee56f | 2017-08-24 22:36:53 -0700 | [diff] [blame] | 50 | DetectOptimization()); |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 51 | RenderSignalAnalyzer analyzer; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 52 | float high_bands_gain; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 53 | std::array<float, kFftLengthBy2Plus1> E2; |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 54 | std::array<float, kFftLengthBy2Plus1> Y2; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | std::array<float, kFftLengthBy2Plus1> R2; |
| 56 | std::array<float, kFftLengthBy2Plus1> N2; |
| 57 | std::array<float, kFftLengthBy2Plus1> g; |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 58 | std::array<float, kBlockSize> s; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 59 | std::vector<std::vector<float>> x(1, std::vector<float>(kBlockSize, 0.f)); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 60 | AecState aec_state(EchoCanceller3Config{}); |
| 61 | ApmDataDumper data_dumper(42); |
| 62 | Subtractor subtractor(&data_dumper, DetectOptimization()); |
| 63 | RenderBuffer render_buffer( |
| 64 | DetectOptimization(), 1, |
| 65 | std::max(kUnknownDelayRenderWindowSize, kAdaptiveFilterLength), |
| 66 | std::vector<size_t>(1, kAdaptiveFilterLength)); |
| 67 | |
| 68 | // Verify the functionality for forcing a zero gain. |
| 69 | E2.fill(1000000000.f); |
| 70 | R2.fill(10000000000000.f); |
| 71 | N2.fill(0.f); |
| 72 | s.fill(10.f); |
| 73 | aec_state.Update(subtractor.FilterFrequencyResponse(), |
| 74 | subtractor.FilterImpulseResponse(), |
| 75 | subtractor.ConvergedFilter(), rtc::Optional<size_t>(10), |
| 76 | render_buffer, E2, Y2, x[0], s, false); |
| 77 | suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x, &high_bands_gain, |
| 78 | &g); |
| 79 | std::for_each(g.begin(), g.end(), [](float a) { EXPECT_FLOAT_EQ(0.f, a); }); |
| 80 | EXPECT_FLOAT_EQ(0.f, high_bands_gain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 81 | |
| 82 | // Ensure that a strong noise is detected to mask any echoes. |
| 83 | E2.fill(10.f); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 84 | Y2.fill(10.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 85 | R2.fill(0.1f); |
| 86 | N2.fill(100.f); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 87 | // Ensure that the gain is no longer forced to zero. |
| 88 | for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) { |
| 89 | aec_state.Update(subtractor.FilterFrequencyResponse(), |
| 90 | subtractor.FilterImpulseResponse(), |
| 91 | subtractor.ConvergedFilter(), rtc::Optional<size_t>(10), |
| 92 | render_buffer, E2, Y2, x[0], s, false); |
| 93 | } |
| 94 | |
| 95 | for (int k = 0; k < 100; ++k) { |
| 96 | aec_state.Update(subtractor.FilterFrequencyResponse(), |
| 97 | subtractor.FilterImpulseResponse(), |
| 98 | subtractor.ConvergedFilter(), rtc::Optional<size_t>(10), |
| 99 | render_buffer, E2, Y2, x[0], s, false); |
| 100 | suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 101 | &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 102 | } |
| 103 | std::for_each(g.begin(), g.end(), |
| 104 | [](float a) { EXPECT_NEAR(1.f, a, 0.001); }); |
| 105 | |
| 106 | // Ensure that a strong nearend is detected to mask any echoes. |
| 107 | E2.fill(100.f); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 108 | Y2.fill(100.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 109 | R2.fill(0.1f); |
| 110 | N2.fill(0.f); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 111 | for (int k = 0; k < 100; ++k) { |
| 112 | aec_state.Update(subtractor.FilterFrequencyResponse(), |
| 113 | subtractor.FilterImpulseResponse(), |
| 114 | subtractor.ConvergedFilter(), rtc::Optional<size_t>(10), |
| 115 | render_buffer, E2, Y2, x[0], s, false); |
| 116 | suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 117 | &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 118 | } |
| 119 | std::for_each(g.begin(), g.end(), |
| 120 | [](float a) { EXPECT_NEAR(1.f, a, 0.001); }); |
| 121 | |
| 122 | // Ensure that a strong echo is suppressed. |
peah | 1d68089 | 2017-05-23 04:07:10 -0700 | [diff] [blame] | 123 | E2.fill(1000000000.f); |
| 124 | R2.fill(10000000000000.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 125 | N2.fill(0.f); |
| 126 | for (int k = 0; k < 10; ++k) { |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame^] | 127 | suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x, |
peah | 14c11a4 | 2017-07-11 06:13:43 -0700 | [diff] [blame] | 128 | &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 129 | } |
| 130 | std::for_each(g.begin(), g.end(), |
| 131 | [](float a) { EXPECT_NEAR(0.f, a, 0.001); }); |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 132 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace aec3 |
| 136 | } // namespace webrtc |