blob: ef31371fc8fcf0a5a0f3302bfb510254e738e393 [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;
Per Åhgren7ddd4632017-10-25 02:59:45 +020045 AecState aec_state(EchoCanceller3Config{});
Per Åhgren971de072018-03-14 23:23:47 +010046 EXPECT_DEATH(
Per Åhgren47d7fbd2018-04-24 12:44:29 +020047 SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000)
Per Åhgrenfde4aa92018-08-27 14:19:35 +020048 .GetGain(E2, S2, R2, N2, E, Y,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020049 RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state,
Per Åhgren971de072018-03-14 23:23:47 +010050 std::vector<std::vector<float>>(
51 3, std::vector<float>(kBlockSize, 0.f)),
52 &high_bands_gain, nullptr),
53 "");
peah522d71b2017-02-23 05:16:26 -080054}
55
56#endif
57
peah522d71b2017-02-23 05:16:26 -080058// Does a sanity check that the gains are correctly computed.
59TEST(SuppressionGain, BasicGainComputation) {
Per Åhgren47d7fbd2018-04-24 12:44:29 +020060 SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
61 16000);
Per Åhgren971de072018-03-14 23:23:47 +010062 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070063 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -080064 std::array<float, kFftLengthBy2Plus1> E2;
Per Åhgrenfde4aa92018-08-27 14:19:35 +020065 std::array<float, kFftLengthBy2Plus1> S2;
Per Åhgren7ddd4632017-10-25 02:59:45 +020066 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -080067 std::array<float, kFftLengthBy2Plus1> R2;
68 std::array<float, kFftLengthBy2Plus1> N2;
69 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenb20b9372018-07-13 00:22:54 +020070 SubtractorOutput output;
71 std::array<float, kBlockSize> y;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020072 FftData E;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020073 FftData Y;
peah86afe9d2017-04-06 15:45:32 -070074 std::vector<std::vector<float>> x(1, std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +010075 EchoCanceller3Config config;
76 AecState aec_state(config);
Per Åhgren7ddd4632017-10-25 02:59:45 +020077 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +010078 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010079 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
80 RenderDelayBuffer::Create(config, 3));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020081 absl::optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020082
peah522d71b2017-02-23 05:16:26 -080083 // Ensure that a strong noise is detected to mask any echoes.
84 E2.fill(10.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020085 Y2.fill(10.f);
peah522d71b2017-02-23 05:16:26 -080086 R2.fill(0.1f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +020087 S2.fill(0.1f);
peah522d71b2017-02-23 05:16:26 -080088 N2.fill(100.f);
Per Åhgrenb20b9372018-07-13 00:22:54 +020089 output.Reset();
90 y.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020091 E.re.fill(sqrtf(E2[0]));
92 E.im.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020093 Y.re.fill(sqrtf(Y2[0]));
94 Y.im.fill(0.f);
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010095
Per Åhgren7ddd4632017-10-25 02:59:45 +020096 // Ensure that the gain is no longer forced to zero.
97 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +010098 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +010099 subtractor.FilterImpulseResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200100 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
101 y);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200102 }
103
104 for (int k = 0; k < 100; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +0100105 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100106 subtractor.FilterImpulseResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200107 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
108 y);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200109 suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700110 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800111 }
112 std::for_each(g.begin(), g.end(),
113 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
114
115 // Ensure that a strong nearend is detected to mask any echoes.
116 E2.fill(100.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200117 Y2.fill(100.f);
peah522d71b2017-02-23 05:16:26 -0800118 R2.fill(0.1f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200119 S2.fill(0.1f);
peah522d71b2017-02-23 05:16:26 -0800120 N2.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200121 E.re.fill(sqrtf(E2[0]));
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200122 Y.re.fill(sqrtf(Y2[0]));
123
Per Åhgren7ddd4632017-10-25 02:59:45 +0200124 for (int k = 0; k < 100; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +0100125 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100126 subtractor.FilterImpulseResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200127 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
128 y);
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200129 suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700130 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800131 }
132 std::for_each(g.begin(), g.end(),
133 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
134
135 // Ensure that a strong echo is suppressed.
peah1d680892017-05-23 04:07:10 -0700136 E2.fill(1000000000.f);
137 R2.fill(10000000000000.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200138 E.re.fill(sqrtf(E2[0]));
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200139
peah522d71b2017-02-23 05:16:26 -0800140 for (int k = 0; k < 10; ++k) {
Per Åhgrenfde4aa92018-08-27 14:19:35 +0200141 suppression_gain.GetGain(E2, S2, R2, N2, E, Y, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700142 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800143 }
144 std::for_each(g.begin(), g.end(),
145 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah522d71b2017-02-23 05:16:26 -0800146}
147
148} // namespace aec3
149} // namespace webrtc