blob: 02de706c776478d17f365fe82ac263995716e421 [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,
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +020050 Block(3, 1), false, &high_bands_gain, nullptr),
Per Åhgren971de072018-03-14 23:23:47 +010051 "");
peah522d71b2017-02-23 05:16:26 -080052}
53
54#endif
55
peah522d71b2017-02-23 05:16:26 -080056// Does a sanity check that the gains are correctly computed.
57TEST(SuppressionGain, BasicGainComputation) {
Per Åhgrenf9807252019-10-09 13:57:07 +020058 constexpr size_t kNumRenderChannels = 1;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010059 constexpr size_t kNumCaptureChannels = 2;
Per Åhgrence202a02019-09-02 17:01:19 +020060 constexpr int kSampleRateHz = 16000;
61 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020062 SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010063 kSampleRateHz, kNumCaptureChannels);
Per Åhgren971de072018-03-14 23:23:47 +010064 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070065 float high_bands_gain;
Per Åhgrenf9807252019-10-09 13:57:07 +020066 std::vector<std::array<float, kFftLengthBy2Plus1>> E2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010067 std::vector<std::array<float, kFftLengthBy2Plus1>> S2(kNumCaptureChannels,
Gustaf Ullberga63d1522021-06-11 14:02:53 +020068 {0.0f});
Per Åhgrenf9807252019-10-09 13:57:07 +020069 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010070 std::vector<std::array<float, kFftLengthBy2Plus1>> R2(kNumCaptureChannels);
Gustaf Ullberga63d1522021-06-11 14:02:53 +020071 std::vector<std::array<float, kFftLengthBy2Plus1>> R2_unbounded(
72 kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010073 std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -080074 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenf9807252019-10-09 13:57:07 +020075 std::vector<SubtractorOutput> output(kNumCaptureChannels);
Gustaf Ullbergd3ead1a2022-05-23 10:39:53 +020076 Block x(kNumBands, kNumRenderChannels);
Per Åhgren8ba58612017-12-01 23:01:44 +010077 EchoCanceller3Config config;
Per Åhgrenf9807252019-10-09 13:57:07 +020078 AecState aec_state(config, kNumCaptureChannels);
Per Åhgren7ddd4632017-10-25 02:59:45 +020079 ApmDataDumper data_dumper(42);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010080 Subtractor subtractor(config, kNumRenderChannels, kNumCaptureChannels,
81 &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010082 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrenf9807252019-10-09 13:57:07 +020083 RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020084 absl::optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020085
peah522d71b2017-02-23 05:16:26 -080086 // Ensure that a strong noise is detected to mask any echoes.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010087 for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
88 E2[ch].fill(10.f);
89 Y2[ch].fill(10.f);
Gustaf Ullberga63d1522021-06-11 14:02:53 +020090 R2[ch].fill(0.1f);
91 R2_unbounded[ch].fill(0.1f);
92 N2[ch].fill(100.0f);
Per Åhgrenf9807252019-10-09 13:57:07 +020093 }
Sam Zackrisson8f736c02019-10-01 12:47:53 +020094 for (auto& subtractor_output : output) {
95 subtractor_output.Reset();
96 }
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010097
Per Åhgren7ddd4632017-10-25 02:59:45 +020098 // Ensure that the gain is no longer forced to zero.
99 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200100 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
101 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200102 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200103 }
104
105 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200106 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
107 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200108 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200109 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
110 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800111 }
112 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200113 [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800114
115 // Ensure that a strong nearend is detected to mask any echoes.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100116 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 Ullberga63d1522021-06-11 14:02:53 +0200120 R2_unbounded[ch].fill(0.1f);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100121 S2[ch].fill(0.1f);
122 N2[ch].fill(0.f);
Per Åhgrenf9807252019-10-09 13:57:07 +0200123 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200124
Per Åhgren7ddd4632017-10-25 02:59:45 +0200125 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200126 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
127 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200128 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200129 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
130 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800131 }
132 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200133 [](float a) { EXPECT_NEAR(1.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800134
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100135 // Add a strong echo to one of the channels and ensure that it is suppressed.
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200136 E2[1].fill(1000000000.0f);
137 R2[1].fill(10000000000000.0f);
138 R2_unbounded[1].fill(10000000000000.0f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200139
peah522d71b2017-02-23 05:16:26 -0800140 for (int k = 0; k < 10; ++k) {
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200141 suppression_gain.GetGain(E2, S2, R2, R2_unbounded, N2, analyzer, aec_state,
142 x, false, &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800143 }
144 std::for_each(g.begin(), g.end(),
Gustaf Ullberga63d1522021-06-11 14:02:53 +0200145 [](float a) { EXPECT_NEAR(0.0f, a, 0.001f); });
peah522d71b2017-02-23 05:16:26 -0800146}
147
148} // namespace aec3
149} // namespace webrtc