blob: 999b0f27ab0daaa6b5192a3dad238e225f7af0b4 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -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/suppression_gain.h"
peah522d71b2017-02-23 05:16:26 -080012
Per Åhgren7ddd4632017-10-25 02:59:45 +020013#include "modules/audio_processing/aec3/aec_state.h"
Per Åhgren8ba58612017-12-01 23:01:44 +010014#include "modules/audio_processing/aec3/render_delay_buffer.h"
Per Åhgren7ddd4632017-10-25 02:59:45 +020015#include "modules/audio_processing/aec3/subtractor.h"
Per Åhgrenb20b9372018-07-13 00:22:54 +020016#include "modules/audio_processing/aec3/subtractor_output.h"
Per Åhgren7ddd4632017-10-25 02:59:45 +020017#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "system_wrappers/include/cpu_features_wrapper.h"
20#include "test/gtest.h"
peah522d71b2017-02-23 05:16:26 -080021
22namespace webrtc {
23namespace 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.
Tommia5e07cc2020-05-26 21:40:37 +020028TEST(SuppressionGainDeathTest, NullOutputGains) {
Gustaf Ullberga63d1522021-06-11 14:02:53 +020029 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 Ullberg5ea57492019-11-05 15:19:02 +010032 std::vector<std::array<float, kFftLengthBy2Plus1>> S2(1);
Gustaf Ullberga63d1522021-06-11 14:02:53 +020033 std::vector<std::array<float, kFftLengthBy2Plus1>> N2(1, {0.0f});
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010034 for (auto& S2_k : S2) {
Gustaf Ullberga63d1522021-06-11 14:02:53 +020035 S2_k.fill(0.1f);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010036 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +020037 FftData E;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020038 FftData Y;
Gustaf Ullberga63d1522021-06-11 14:02:53 +020039 E.re.fill(0.0f);
40 E.im.fill(0.0f);
41 Y.re.fill(0.0f);
42 Y.im.fill(0.0f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020043
peah86afe9d2017-04-06 15:45:32 -070044 float high_bands_gain;
Sam Zackrisson8f736c02019-10-01 12:47:53 +020045 AecState aec_state(EchoCanceller3Config{}, 1);
Per Åhgren971de072018-03-14 23:23:47 +010046 EXPECT_DEATH(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010047 SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000, 1)
Gustaf Ullberga63d1522021-06-11 14:02:53 +020048 .GetGain(E2, S2, R2, R2_unbounded, N2,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020049 RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state,
Per Åhgrence202a02019-09-02 17:01:19 +020050 std::vector<std::vector<std::vector<float>>>(
51 3, std::vector<std::vector<float>>(
Gustaf Ullberga63d1522021-06-11 14:02:53 +020052 1, std::vector<float>(kBlockSize, 0.0f))),
Gustaf Ullberg7e4ad822020-10-22 14:36:37 +020053 false, &high_bands_gain, nullptr),
Per Åhgren971de072018-03-14 23:23:47 +010054 "");
peah522d71b2017-02-23 05:16:26 -080055}
56
57#endif
58
peah522d71b2017-02-23 05:16:26 -080059// Does a sanity check that the gains are correctly computed.
60TEST(SuppressionGain, BasicGainComputation) {
Per Åhgrenf9807252019-10-09 13:57:07 +020061 constexpr size_t kNumRenderChannels = 1;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010062 constexpr size_t kNumCaptureChannels = 2;
Per Åhgrence202a02019-09-02 17:01:19 +020063 constexpr int kSampleRateHz = 16000;
64 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020065 SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010066 kSampleRateHz, kNumCaptureChannels);
Per Åhgren971de072018-03-14 23:23:47 +010067 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070068 float high_bands_gain;
Per Åhgrenf9807252019-10-09 13:57:07 +020069 std::vector<std::array<float, kFftLengthBy2Plus1>> E2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010070 std::vector<std::array<float, kFftLengthBy2Plus1>> S2(kNumCaptureChannels,
Gustaf Ullberga63d1522021-06-11 14:02:53 +020071 {0.0f});
Per Åhgrenf9807252019-10-09 13:57:07 +020072 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010073 std::vector<std::array<float, kFftLengthBy2Plus1>> R2(kNumCaptureChannels);
Gustaf Ullberga63d1522021-06-11 14:02:53 +020074 std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(
75 kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010076 std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -080077 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenf9807252019-10-09 13:57:07 +020078 std::vector<SubtractorOutput> output(kNumCaptureChannels);
Per Åhgrence202a02019-09-02 17:01:19 +020079 std::vector<std::vector<std::vector<float>>> x(
80 kNumBands, std::vector<std::vector<float>>(
Gustaf Ullberga63d1522021-06-11 14:02:53 +020081 kNumRenderChannels, std::vector<float>(kBlockSize, 0.0f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010082 EchoCanceller3Config config;
Per Åhgrenf9807252019-10-09 13:57:07 +020083 AecState aec_state(config, kNumCaptureChannels);
Per Åhgren7ddd4632017-10-25 02:59:45 +020084 ApmDataDumper data_dumper(42);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010085 Subtractor subtractor(config, kNumRenderChannels, kNumCaptureChannels,
86 &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010087 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrenf9807252019-10-09 13:57:07 +020088 RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020089 absl::optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020090
peah522d71b2017-02-23 05:16:26 -080091 // Ensure that a strong noise is detected to mask any echoes.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010092 for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
93 E2[ch].fill(10.f);
94 Y2[ch].fill(10.f);
Gustaf Ullberga63d1522021-06-11 14:02:53 +020095 R2[ch].fill(0.1f);
96 R2_unbounded[ch].fill(0.1f);
97 N2[ch].fill(100.0f);
Per Åhgrenf9807252019-10-09 13:57:07 +020098 }
Sam Zackrisson8f736c02019-10-01 12:47:53 +020099 for (auto& subtractor_output : output) {
100 subtractor_output.Reset();
101 }
Per Åhgrenb6b00dc2018-02-20 22:18:27 +0100102
Per Åhgren7ddd4632017-10-25 02:59:45 +0200103 // Ensure that the gain is no longer forced to zero.
104 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200105 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
106 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200107 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200108 }
109
110 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200111 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
112 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200113 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200114 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
115 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800116 }
117 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200118 [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800119
120 // Ensure that a strong nearend is detected to mask any echoes.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100121 for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
122 E2[ch].fill(100.f);
123 Y2[ch].fill(100.f);
124 R2[ch].fill(0.1f);
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200125 R2_unbounded[ch].fill(0.1f);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100126 S2[ch].fill(0.1f);
127 N2[ch].fill(0.f);
Per Åhgrenf9807252019-10-09 13:57:07 +0200128 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200129
Per Åhgren7ddd4632017-10-25 02:59:45 +0200130 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200131 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
132 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200133 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200134 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
135 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800136 }
137 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200138 [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800139
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100140 // Add a strong echo to one of the channels and ensure that it is suppressed.
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200141 E2[1].fill(1000000000.0f);
142 R2[1].fill(10000000000000.0f);
143 R2_unbounded[1].fill(10000000000000.0f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200144
peah522d71b2017-02-23 05:16:26 -0800145 for (int k = 0; k < 10; ++k) {
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200146 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
147 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800148 }
149 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200150 [](float a) { EXPECT_NEAR(0.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800151}
152
153} // namespace aec3
154} // namespace webrtc