blob: d068328772dd6527ca60de87896bd5b24e68b2dd [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.
28TEST(SuppressionGain, NullOutputGains) {
29 std::array<float, kFftLengthBy2Plus1> E2;
30 std::array<float, kFftLengthBy2Plus1> R2;
Per Åhgrenfde4aa92018-08-27 14:19:35 +020031 std::array<float, kFftLengthBy2Plus1> S2;
peah522d71b2017-02-23 05:16:26 -080032 std::array<float, kFftLengthBy2Plus1> N2;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020033 FftData E;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020034 FftData Y;
peah86afe9d2017-04-06 15:45:32 -070035 E2.fill(0.f);
36 R2.fill(0.f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +020037 S2.fill(0.1f);
peah86afe9d2017-04-06 15:45:32 -070038 N2.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020039 E.re.fill(0.f);
40 E.im.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020041 Y.re.fill(0.f);
42 Y.im.fill(0.f);
43
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(
Per Åhgren47d7fbd2018-04-24 12:44:29 +020047 SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000)
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +020048 .GetGain(E2, S2, R2, 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>>(
52 1, std::vector<float>(kBlockSize, 0.f))),
Per Åhgren971de072018-03-14 23:23:47 +010053 &high_bands_gain, nullptr),
54 "");
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;
62 constexpr size_t kNumCaptureChannels = 1;
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(),
Per Åhgrence202a02019-09-02 17:01:19 +020066 kSampleRateHz);
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);
Per Åhgrenfde4aa92018-08-27 14:19:35 +020070 std::array<float, kFftLengthBy2Plus1> S2;
Per Åhgrenf9807252019-10-09 13:57:07 +020071 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -080072 std::array<float, kFftLengthBy2Plus1> R2;
73 std::array<float, kFftLengthBy2Plus1> N2;
74 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenf9807252019-10-09 13:57:07 +020075 std::vector<SubtractorOutput> output(kNumCaptureChannels);
Per Åhgrenb20b9372018-07-13 00:22:54 +020076 std::array<float, kBlockSize> y;
Per Åhgrence202a02019-09-02 17:01:19 +020077 std::vector<std::vector<std::vector<float>>> x(
78 kNumBands, std::vector<std::vector<float>>(
Per Åhgrenf9807252019-10-09 13:57:07 +020079 kNumRenderChannels, std::vector<float>(kBlockSize, 0.f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010080 EchoCanceller3Config config;
Per Åhgrenf9807252019-10-09 13:57:07 +020081 AecState aec_state(config, kNumCaptureChannels);
Per Åhgren7ddd4632017-10-25 02:59:45 +020082 ApmDataDumper data_dumper(42);
Per Åhgrena33dc012019-09-03 23:59:52 +020083 Subtractor subtractor(config, 1, 1, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010084 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrenf9807252019-10-09 13:57:07 +020085 RenderDelayBuffer::Create(config, kSampleRateHz, kNumRenderChannels));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020086 absl::optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020087
peah522d71b2017-02-23 05:16:26 -080088 // Ensure that a strong noise is detected to mask any echoes.
Per Åhgrenf9807252019-10-09 13:57:07 +020089 for (auto& E2_k : E2) {
90 E2_k.fill(10.f);
91 }
92 for (auto& Y2_k : Y2) {
93 Y2_k.fill(10.f);
94 }
peah522d71b2017-02-23 05:16:26 -080095 R2.fill(0.1f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +020096 S2.fill(0.1f);
peah522d71b2017-02-23 05:16:26 -080097 N2.fill(100.f);
Sam Zackrisson8f736c02019-10-01 12:47:53 +020098 for (auto& subtractor_output : output) {
99 subtractor_output.Reset();
100 }
Per Åhgrenb20b9372018-07-13 00:22:54 +0200101 y.fill(0.f);
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) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200105 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
106 subtractor.FilterImpulseResponse(),
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) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200111 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
112 subtractor.FilterImpulseResponse(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200113 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Per Åhgrenf9807252019-10-09 13:57:07 +0200114 suppression_gain.GetGain(E2[0], S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700115 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800116 }
117 std::for_each(g.begin(), g.end(),
118 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
119
120 // Ensure that a strong nearend is detected to mask any echoes.
Per Åhgrenf9807252019-10-09 13:57:07 +0200121 for (auto& E2_k : E2) {
122 E2_k.fill(100.f);
123 }
124 for (auto& Y2_k : Y2) {
125 Y2_k.fill(100.f);
126 }
peah522d71b2017-02-23 05:16:26 -0800127 R2.fill(0.1f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200128 S2.fill(0.1f);
peah522d71b2017-02-23 05:16:26 -0800129 N2.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200130
Per Åhgren7ddd4632017-10-25 02:59:45 +0200131 for (int k = 0; k < 100; ++k) {
Sam Zackrisson46b01402019-10-08 16:17:48 +0200132 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
133 subtractor.FilterImpulseResponse(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200134 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Per Åhgrenf9807252019-10-09 13:57:07 +0200135 suppression_gain.GetGain(E2[0], S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700136 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800137 }
138 std::for_each(g.begin(), g.end(),
139 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
140
141 // Ensure that a strong echo is suppressed.
Per Åhgrenf9807252019-10-09 13:57:07 +0200142 for (auto& E2_k : E2) {
143 E2_k.fill(1000000000.f);
144 }
peah1d680892017-05-23 04:07:10 -0700145 R2.fill(10000000000000.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200146
peah522d71b2017-02-23 05:16:26 -0800147 for (int k = 0; k < 10; ++k) {
Per Åhgrenf9807252019-10-09 13:57:07 +0200148 suppression_gain.GetGain(E2[0], S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700149 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800150 }
151 std::for_each(g.begin(), g.end(),
152 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah522d71b2017-02-23 05:16:26 -0800153}
154
155} // namespace aec3
156} // namespace webrtc