blob: 0e48102897d44be41bb1b16496e91bdeca4f342f [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"
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{});
Per Åhgren971de072018-03-14 23:23:47 +010037 EXPECT_DEATH(
38 SuppressionGain(EchoCanceller3Config{}, DetectOptimization())
39 .GetGain(E2, R2, N2, RenderSignalAnalyzer((EchoCanceller3Config{})),
40 aec_state,
41 std::vector<std::vector<float>>(
42 3, std::vector<float>(kBlockSize, 0.f)),
43 &high_bands_gain, nullptr),
44 "");
peah522d71b2017-02-23 05:16:26 -080045}
46
47#endif
48
peah522d71b2017-02-23 05:16:26 -080049// Does a sanity check that the gains are correctly computed.
50TEST(SuppressionGain, BasicGainComputation) {
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020051 SuppressionGain suppression_gain(EchoCanceller3Config(),
peah8cee56f2017-08-24 22:36:53 -070052 DetectOptimization());
Per Åhgren971de072018-03-14 23:23:47 +010053 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070054 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -080055 std::array<float, kFftLengthBy2Plus1> E2;
Per Åhgren7ddd4632017-10-25 02:59:45 +020056 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -080057 std::array<float, kFftLengthBy2Plus1> R2;
58 std::array<float, kFftLengthBy2Plus1> N2;
59 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgren7ddd4632017-10-25 02:59:45 +020060 std::array<float, kBlockSize> s;
peah86afe9d2017-04-06 15:45:32 -070061 std::vector<std::vector<float>> x(1, std::vector<float>(kBlockSize, 0.f));
Per Åhgren8ba58612017-12-01 23:01:44 +010062 EchoCanceller3Config config;
63 AecState aec_state(config);
Per Åhgren7ddd4632017-10-25 02:59:45 +020064 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +010065 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010066 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
67 RenderDelayBuffer::Create(config, 3));
Per Åhgren3ab308f2018-02-21 08:46:03 +010068 rtc::Optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020069
peah522d71b2017-02-23 05:16:26 -080070 // Ensure that a strong noise is detected to mask any echoes.
71 E2.fill(10.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020072 Y2.fill(10.f);
peah522d71b2017-02-23 05:16:26 -080073 R2.fill(0.1f);
74 N2.fill(100.f);
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010075 s.fill(10.f);
76
Per Åhgren7ddd4632017-10-25 02:59:45 +020077 // Ensure that the gain is no longer forced to zero.
78 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +010079 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +010080 subtractor.FilterImpulseResponse(),
81 subtractor.ConvergedFilter(),
82 *render_delay_buffer->GetRenderBuffer(), E2, Y2, s, false);
Per Åhgren7ddd4632017-10-25 02:59:45 +020083 }
84
85 for (int k = 0; k < 100; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +010086 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +010087 subtractor.FilterImpulseResponse(),
88 subtractor.ConvergedFilter(),
89 *render_delay_buffer->GetRenderBuffer(), E2, Y2, s, false);
Per Åhgren7ddd4632017-10-25 02:59:45 +020090 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -070091 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -080092 }
93 std::for_each(g.begin(), g.end(),
94 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
95
96 // Ensure that a strong nearend is detected to mask any echoes.
97 E2.fill(100.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020098 Y2.fill(100.f);
peah522d71b2017-02-23 05:16:26 -080099 R2.fill(0.1f);
100 N2.fill(0.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200101 for (int k = 0; k < 100; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +0100102 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100103 subtractor.FilterImpulseResponse(),
104 subtractor.ConvergedFilter(),
105 *render_delay_buffer->GetRenderBuffer(), E2, Y2, s, false);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200106 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700107 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800108 }
109 std::for_each(g.begin(), g.end(),
110 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
111
112 // Ensure that a strong echo is suppressed.
peah1d680892017-05-23 04:07:10 -0700113 E2.fill(1000000000.f);
114 R2.fill(10000000000000.f);
peah522d71b2017-02-23 05:16:26 -0800115 N2.fill(0.f);
116 for (int k = 0; k < 10; ++k) {
Per Åhgren7ddd4632017-10-25 02:59:45 +0200117 suppression_gain.GetGain(E2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700118 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800119 }
120 std::for_each(g.begin(), g.end(),
121 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah6d822ad2017-04-10 13:52:14 -0700122
peah522d71b2017-02-23 05:16:26 -0800123}
124
125} // namespace aec3
126} // namespace webrtc