blob: 577407916b2637ba8e7f34bda13258788dc10a72 [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"
14#include "modules/audio_processing/aec3/render_buffer.h"
15#include "modules/audio_processing/aec3/subtractor.h"
16#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "system_wrappers/include/cpu_features_wrapper.h"
19#include "test/gtest.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "typedefs.h" // NOLINT(build/include)
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;
31 std::array<float, kFftLengthBy2Plus1> N2;
peah86afe9d2017-04-06 15:45:32 -070032 E2.fill(0.f);
33 R2.fill(0.f);
34 N2.fill(0.f);
35 float high_bands_gain;
Per Åhgren7ddd4632017-10-25 02:59:45 +020036 AecState aec_state(EchoCanceller3Config{});
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020037 EXPECT_DEATH(SuppressionGain(EchoCanceller3Config{}, DetectOptimization())
Per Åhgren7ddd4632017-10-25 02:59:45 +020038 .GetGain(E2, R2, N2, RenderSignalAnalyzer(), aec_state,
peah86afe9d2017-04-06 15:45:32 -070039 std::vector<std::vector<float>>(
40 3, std::vector<float>(kBlockSize, 0.f)),
Per Åhgren7ddd4632017-10-25 02:59:45 +020041 &high_bands_gain, nullptr),
peah86afe9d2017-04-06 15:45:32 -070042 "");
peah522d71b2017-02-23 05:16:26 -080043}
44
45#endif
46
peah522d71b2017-02-23 05:16:26 -080047// Does a sanity check that the gains are correctly computed.
48TEST(SuppressionGain, BasicGainComputation) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020049 SuppressionGain suppression_gain(EchoCanceller3Config(),
peah8cee56f2017-08-24 22:36:53 -070050 DetectOptimization());
peah14c11a42017-07-11 06:13:43 -070051 RenderSignalAnalyzer analyzer;
peah86afe9d2017-04-06 15:45:32 -070052 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -080053 std::array<float, kFftLengthBy2Plus1> E2;
Per Åhgren7ddd4632017-10-25 02:59:45 +020054 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -080055 std::array<float, kFftLengthBy2Plus1> R2;
56 std::array<float, kFftLengthBy2Plus1> N2;
57 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgren7ddd4632017-10-25 02:59:45 +020058 std::array<float, kBlockSize> s;
peah86afe9d2017-04-06 15:45:32 -070059 std::vector<std::vector<float>> x(1, std::vector<float>(kBlockSize, 0.f));
Per Åhgren7ddd4632017-10-25 02:59:45 +020060 AecState aec_state(EchoCanceller3Config{});
61 ApmDataDumper data_dumper(42);
62 Subtractor subtractor(&data_dumper, DetectOptimization());
63 RenderBuffer render_buffer(
64 DetectOptimization(), 1,
65 std::max(kUnknownDelayRenderWindowSize, kAdaptiveFilterLength),
66 std::vector<size_t>(1, kAdaptiveFilterLength));
67
68 // Verify the functionality for forcing a zero gain.
69 E2.fill(1000000000.f);
70 R2.fill(10000000000000.f);
71 N2.fill(0.f);
72 s.fill(10.f);
73 aec_state.Update(subtractor.FilterFrequencyResponse(),
74 subtractor.FilterImpulseResponse(),
75 subtractor.ConvergedFilter(), rtc::Optional<size_t>(10),
76 render_buffer, E2, Y2, x[0], s, false);
77 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x, &high_bands_gain,
78 &g);
79 std::for_each(g.begin(), g.end(), [](float a) { EXPECT_FLOAT_EQ(0.f, a); });
80 EXPECT_FLOAT_EQ(0.f, high_bands_gain);
peah522d71b2017-02-23 05:16:26 -080081
82 // Ensure that a strong noise is detected to mask any echoes.
83 E2.fill(10.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020084 Y2.fill(10.f);
peah522d71b2017-02-23 05:16:26 -080085 R2.fill(0.1f);
86 N2.fill(100.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020087 // Ensure that the gain is no longer forced to zero.
88 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
89 aec_state.Update(subtractor.FilterFrequencyResponse(),
90 subtractor.FilterImpulseResponse(),
91 subtractor.ConvergedFilter(), rtc::Optional<size_t>(10),
92 render_buffer, E2, Y2, x[0], s, false);
93 }
94
95 for (int k = 0; k < 100; ++k) {
96 aec_state.Update(subtractor.FilterFrequencyResponse(),
97 subtractor.FilterImpulseResponse(),
98 subtractor.ConvergedFilter(), rtc::Optional<size_t>(10),
99 render_buffer, E2, Y2, x[0], s, false);
100 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700101 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800102 }
103 std::for_each(g.begin(), g.end(),
104 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
105
106 // Ensure that a strong nearend is detected to mask any echoes.
107 E2.fill(100.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200108 Y2.fill(100.f);
peah522d71b2017-02-23 05:16:26 -0800109 R2.fill(0.1f);
110 N2.fill(0.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200111 for (int k = 0; k < 100; ++k) {
112 aec_state.Update(subtractor.FilterFrequencyResponse(),
113 subtractor.FilterImpulseResponse(),
114 subtractor.ConvergedFilter(), rtc::Optional<size_t>(10),
115 render_buffer, E2, Y2, x[0], s, false);
116 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700117 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800118 }
119 std::for_each(g.begin(), g.end(),
120 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
121
122 // Ensure that a strong echo is suppressed.
peah1d680892017-05-23 04:07:10 -0700123 E2.fill(1000000000.f);
124 R2.fill(10000000000000.f);
peah522d71b2017-02-23 05:16:26 -0800125 N2.fill(0.f);
126 for (int k = 0; k < 10; ++k) {
Per Åhgren7ddd4632017-10-25 02:59:45 +0200127 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700128 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800129 }
130 std::for_each(g.begin(), g.end(),
131 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah6d822ad2017-04-10 13:52:14 -0700132
peah522d71b2017-02-23 05:16:26 -0800133}
134
135} // namespace aec3
136} // namespace webrtc