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" |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 14 | #include "modules/audio_processing/aec3/render_delay_buffer.h" |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/aec3/subtractor.h" |
Per Åhgren | b20b937 | 2018-07-13 00:22:54 +0200 | [diff] [blame] | 16 | #include "modules/audio_processing/aec3/subtractor_output.h" |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
| 19 | #include "system_wrappers/include/cpu_features_wrapper.h" |
| 20 | #include "test/gtest.h" |
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. |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 28 | TEST(SuppressionGainDeathTest, NullOutputGains) { |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 29 | std::vector<std::array<float, kFftLengthBy2Plus1>> E2(1, {0.0f}); |
| 30 | std::vector<std::array<float, kFftLengthBy2Plus1>> R2(1, {0.0f}); |
| 31 | std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(1, {0.0f}); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 32 | std::vector<std::array<float, kFftLengthBy2Plus1>> S2(1); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 33 | std::vector<std::array<float, kFftLengthBy2Plus1>> N2(1, {0.0f}); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 34 | for (auto& S2_k : S2) { |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 35 | S2_k.fill(0.1f); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 36 | } |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 37 | FftData E; |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 38 | FftData Y; |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 39 | E.re.fill(0.0f); |
| 40 | E.im.fill(0.0f); |
| 41 | Y.re.fill(0.0f); |
| 42 | Y.im.fill(0.0f); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 43 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 44 | float high_bands_gain; |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 45 | AecState aec_state(EchoCanceller3Config{}, 1); |
Per Åhgren | 971de07 | 2018-03-14 23:23:47 +0100 | [diff] [blame] | 46 | EXPECT_DEATH( |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 47 | SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000, 1) |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 48 | .GetGain(E2, S2, R2, R2_unbounded, N2, |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 49 | RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state, |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 50 | Block(3, 1), false, &high_bands_gain, nullptr), |
Per Åhgren | 971de07 | 2018-03-14 23:23:47 +0100 | [diff] [blame] | 51 | ""); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | #endif |
| 55 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 56 | // Does a sanity check that the gains are correctly computed. |
| 57 | TEST(SuppressionGain, BasicGainComputation) { |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 58 | constexpr size_t kNumRenderChannels = 1; |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 59 | constexpr size_t kNumCaptureChannels = 2; |
Per Åhgren | ce202a0 | 2019-09-02 17:01:19 +0200 | [diff] [blame] | 60 | constexpr int kSampleRateHz = 16000; |
| 61 | constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 62 | SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(), |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 63 | kSampleRateHz, kNumCaptureChannels); |
Per Åhgren | 971de07 | 2018-03-14 23:23:47 +0100 | [diff] [blame] | 64 | RenderSignalAnalyzer analyzer(EchoCanceller3Config{}); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 65 | float high_bands_gain; |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 66 | std::vector<std::array<float, kFftLengthBy2Plus1>> E2(kNumCaptureChannels); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 67 | std::vector<std::array<float, kFftLengthBy2Plus1>> S2(kNumCaptureChannels, |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 68 | {0.0f}); |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 69 | std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 70 | std::vector<std::array<float, kFftLengthBy2Plus1>> R2(kNumCaptureChannels); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 71 | std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded( |
| 72 | kNumCaptureChannels); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 73 | std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumCaptureChannels); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 74 | std::array<float, kFftLengthBy2Plus1> g; |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 75 | std::vector<SubtractorOutput> output(kNumCaptureChannels); |
Gustaf Ullberg | d3ead1a | 2022-05-23 10:39:53 +0200 | [diff] [blame] | 76 | Block x(kNumBands, kNumRenderChannels); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 77 | EchoCanceller3Config config; |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 78 | AecState aec_state(config, kNumCaptureChannels); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 79 | ApmDataDumper data_dumper(42); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 80 | Subtractor subtractor(config, kNumRenderChannels, kNumCaptureChannels, |
| 81 | &data_dumper, DetectOptimization()); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 82 | std::unique_ptr<RenderDelayBuffer> render_delay_buffer( |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 83 | RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels)); |
Danil Chapovalov | db9f7ab | 2018-06-19 10:50:11 +0200 | [diff] [blame] | 84 | absl::optional<DelayEstimate> delay_estimate; |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 85 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 86 | // Ensure that a strong noise is detected to mask any echoes. |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 87 | for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) { |
| 88 | E2[ch].fill(10.f); |
| 89 | Y2[ch].fill(10.f); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 90 | R2[ch].fill(0.1f); |
| 91 | R2_unbounded[ch].fill(0.1f); |
| 92 | N2[ch].fill(100.0f); |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 93 | } |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 94 | for (auto& subtractor_output : output) { |
| 95 | subtractor_output.Reset(); |
| 96 | } |
Per Åhgren | b6b00dc | 2018-02-20 22:18:27 +0100 | [diff] [blame] | 97 | |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 98 | // Ensure that the gain is no longer forced to zero. |
| 99 | for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) { |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 100 | aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(), |
| 101 | subtractor.FilterImpulseResponses(), |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 102 | *render_delay_buffer->GetRenderBuffer(), E2, Y2, output); |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | for (int k = 0; k < 100; ++k) { |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 106 | aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(), |
| 107 | subtractor.FilterImpulseResponses(), |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 108 | *render_delay_buffer->GetRenderBuffer(), E2, Y2, output); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 109 | suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state, |
| 110 | x, false, &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 111 | } |
| 112 | std::for_each(g.begin(), g.end(), |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 113 | [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); }); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 114 | |
| 115 | // Ensure that a strong nearend is detected to mask any echoes. |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 116 | for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) { |
| 117 | E2[ch].fill(100.f); |
| 118 | Y2[ch].fill(100.f); |
| 119 | R2[ch].fill(0.1f); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 120 | R2_unbounded[ch].fill(0.1f); |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 121 | S2[ch].fill(0.1f); |
| 122 | N2[ch].fill(0.f); |
Per Åhgren | f980725 | 2019-10-09 13:57:07 +0200 | [diff] [blame] | 123 | } |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 124 | |
Per Åhgren | 7ddd463 | 2017-10-25 02:59:45 +0200 | [diff] [blame] | 125 | for (int k = 0; k < 100; ++k) { |
Per Åhgren | 119e219 | 2019-10-18 08:50:50 +0200 | [diff] [blame] | 126 | aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(), |
| 127 | subtractor.FilterImpulseResponses(), |
Sam Zackrisson | 8f736c0 | 2019-10-01 12:47:53 +0200 | [diff] [blame] | 128 | *render_delay_buffer->GetRenderBuffer(), E2, Y2, output); |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 129 | suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state, |
| 130 | x, false, &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 131 | } |
| 132 | std::for_each(g.begin(), g.end(), |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 133 | [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); }); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 134 | |
Gustaf Ullberg | 5ea5749 | 2019-11-05 15:19:02 +0100 | [diff] [blame] | 135 | // Add a strong echo to one of the channels and ensure that it is suppressed. |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 136 | E2[1].fill(1000000000.0f); |
| 137 | R2[1].fill(10000000000000.0f); |
| 138 | R2_unbounded[1].fill(10000000000000.0f); |
Per Åhgren | 47d7fbd | 2018-04-24 12:44:29 +0200 | [diff] [blame] | 139 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 140 | for (int k = 0; k < 10; ++k) { |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 141 | suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state, |
| 142 | x, false, &high_bands_gain, &g); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 143 | } |
| 144 | std::for_each(g.begin(), g.end(), |
Gustaf Ullberg | a63d152 | 2021-06-11 14:02:53 +0200 | [diff] [blame] | 145 | [](float a) { EXPECT_NEAR(0.0f, a, 0.001f); }); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace aec3 |
| 149 | } // namespace webrtc |