blob: 331b903ebefc5931cc7cba2c9941514026d4db0f [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)
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 Åhgrence202a02019-09-02 17:01:19 +020061 constexpr size_t kNumChannels = 1;
62 constexpr int kSampleRateHz = 16000;
63 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020064 SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
Per Åhgrence202a02019-09-02 17:01:19 +020065 kSampleRateHz);
Per Åhgren971de072018-03-14 23:23:47 +010066 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070067 float high_bands_gain;
peah522d71b2017-02-23 05:16:26 -080068 std::array<float, kFftLengthBy2Plus1> E2;
Per Åhgrenfde4aa92018-08-27 14:19:35 +020069 std::array<float, kFftLengthBy2Plus1> S2;
Per Åhgren7ddd4632017-10-25 02:59:45 +020070 std::array<float, kFftLengthBy2Plus1> Y2;
peah522d71b2017-02-23 05:16:26 -080071 std::array<float, kFftLengthBy2Plus1> R2;
72 std::array<float, kFftLengthBy2Plus1> N2;
73 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenb20b9372018-07-13 00:22:54 +020074 SubtractorOutput output;
75 std::array<float, kBlockSize> y;
Per Åhgrence202a02019-09-02 17:01:19 +020076 std::vector<std::vector<std::vector<float>>> x(
77 kNumBands, std::vector<std::vector<float>>(
78 kNumChannels, std::vector<float>(kBlockSize, 0.f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010079 EchoCanceller3Config config;
80 AecState aec_state(config);
Per Åhgren7ddd4632017-10-25 02:59:45 +020081 ApmDataDumper data_dumper(42);
Per Åhgren09a718a2017-12-11 22:28:45 +010082 Subtractor subtractor(config, &data_dumper, DetectOptimization());
Per Åhgren8ba58612017-12-01 23:01:44 +010083 std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
Per Åhgrence202a02019-09-02 17:01:19 +020084 RenderDelayBuffer::Create(config, kSampleRateHz, kNumChannels));
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020085 absl::optional<DelayEstimate> delay_estimate;
Per Åhgren7ddd4632017-10-25 02:59:45 +020086
peah522d71b2017-02-23 05:16:26 -080087 // Ensure that a strong noise is detected to mask any echoes.
88 E2.fill(10.f);
Per Åhgren7ddd4632017-10-25 02:59:45 +020089 Y2.fill(10.f);
peah522d71b2017-02-23 05:16:26 -080090 R2.fill(0.1f);
Per Åhgrenfde4aa92018-08-27 14:19:35 +020091 S2.fill(0.1f);
peah522d71b2017-02-23 05:16:26 -080092 N2.fill(100.f);
Per Åhgrenb20b9372018-07-13 00:22:54 +020093 output.Reset();
94 y.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);
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200109 suppression_gain.GetGain(E2, S2, R2, N2, 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
Per Åhgren7ddd4632017-10-25 02:59:45 +0200122 for (int k = 0; k < 100; ++k) {
Per Åhgren3ab308f2018-02-21 08:46:03 +0100123 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponse(),
Per Åhgren0e6d2f52017-12-20 22:19:56 +0100124 subtractor.FilterImpulseResponse(),
Per Åhgrenb20b9372018-07-13 00:22:54 +0200125 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output,
126 y);
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200127 suppression_gain.GetGain(E2, S2, 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(1.f, a, 0.001); });
132
133 // Ensure that a strong echo is suppressed.
peah1d680892017-05-23 04:07:10 -0700134 E2.fill(1000000000.f);
135 R2.fill(10000000000000.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200136
peah522d71b2017-02-23 05:16:26 -0800137 for (int k = 0; k < 10; ++k) {
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +0200138 suppression_gain.GetGain(E2, S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700139 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800140 }
141 std::for_each(g.begin(), g.end(),
142 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah522d71b2017-02-23 05:16:26 -0800143}
144
145} // namespace aec3
146} // namespace webrtc