blob: 0452f2e1fb79700035d42500a5bc38fc6aab6b7b [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) {
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010029 std::vector<std::array<float, kFftLengthBy2Plus1>> E2(1, {0.f});
30 std::vector<std::array<float, kFftLengthBy2Plus1>> R2(1, {0.f});
31 std::vector<std::array<float, kFftLengthBy2Plus1>> S2(1);
32 std::vector<std::array<float, kFftLengthBy2Plus1>> N2(1, {0.f});
33 for (auto& S2_k : S2) {
34 S2_k.fill(.1f);
35 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +020036 FftData E;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020037 FftData Y;
Per Åhgren47d7fbd2018-04-24 12:44:29 +020038 E.re.fill(0.f);
39 E.im.fill(0.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020040 Y.re.fill(0.f);
41 Y.im.fill(0.f);
42
peah86afe9d2017-04-06 15:45:32 -070043 float high_bands_gain;
Sam Zackrisson8f736c02019-10-01 12:47:53 +020044 AecState aec_state(EchoCanceller3Config{}, 1);
Per Åhgren971de072018-03-14 23:23:47 +010045 EXPECT_DEATH(
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010046 SuppressionGain(EchoCanceller3Config{}, DetectOptimization(), 16000, 1)
Gustaf Ullberg2bab5ad2019-04-15 17:15:37 +020047 .GetGain(E2, S2, R2, N2,
Per Åhgren47d7fbd2018-04-24 12:44:29 +020048 RenderSignalAnalyzer((EchoCanceller3Config{})), aec_state,
Per Åhgrence202a02019-09-02 17:01:19 +020049 std::vector<std::vector<std::vector<float>>>(
50 3, std::vector<std::vector<float>>(
51 1, std::vector<float>(kBlockSize, 0.f))),
Per Åhgren971de072018-03-14 23:23:47 +010052 &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 Åhgrenf9807252019-10-09 13:57:07 +020060 constexpr size_t kNumRenderChannels = 1;
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010061 constexpr size_t kNumCaptureChannels = 2;
Per Åhgrence202a02019-09-02 17:01:19 +020062 constexpr int kSampleRateHz = 16000;
63 constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
Per Åhgren47d7fbd2018-04-24 12:44:29 +020064 SuppressionGain suppression_gain(EchoCanceller3Config(), DetectOptimization(),
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010065 kSampleRateHz, kNumCaptureChannels);
Per Åhgren971de072018-03-14 23:23:47 +010066 RenderSignalAnalyzer analyzer(EchoCanceller3Config{});
peah86afe9d2017-04-06 15:45:32 -070067 float high_bands_gain;
Per Åhgrenf9807252019-10-09 13:57:07 +020068 std::vector<std::array<float, kFftLengthBy2Plus1>> E2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010069 std::vector<std::array<float, kFftLengthBy2Plus1>> S2(kNumCaptureChannels,
70 {0.f});
Per Åhgrenf9807252019-10-09 13:57:07 +020071 std::vector<std::array<float, kFftLengthBy2Plus1>> Y2(kNumCaptureChannels);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010072 std::vector<std::array<float, kFftLengthBy2Plus1>> R2(kNumCaptureChannels);
73 std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumCaptureChannels);
peah522d71b2017-02-23 05:16:26 -080074 std::array<float, kFftLengthBy2Plus1> g;
Per Åhgrenf9807252019-10-09 13:57:07 +020075 std::vector<SubtractorOutput> output(kNumCaptureChannels);
Per Åhgrence202a02019-09-02 17:01:19 +020076 std::vector<std::vector<std::vector<float>>> x(
77 kNumBands, std::vector<std::vector<float>>(
Per Åhgrenf9807252019-10-09 13:57:07 +020078 kNumRenderChannels, std::vector<float>(kBlockSize, 0.f)));
Per Åhgren8ba58612017-12-01 23:01:44 +010079 EchoCanceller3Config config;
Per Åhgrenf9807252019-10-09 13:57:07 +020080 AecState aec_state(config, kNumCaptureChannels);
Per Åhgren7ddd4632017-10-25 02:59:45 +020081 ApmDataDumper data_dumper(42);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010082 Subtractor subtractor(config, kNumRenderChannels, kNumCaptureChannels,
83 &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.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +010089 for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
90 E2[ch].fill(10.f);
91 Y2[ch].fill(10.f);
92 R2[ch].fill(.1f);
93 N2[ch].fill(100.f);
Per Åhgrenf9807252019-10-09 13:57:07 +020094 }
Sam Zackrisson8f736c02019-10-01 12:47:53 +020095 for (auto& subtractor_output : output) {
96 subtractor_output.Reset();
97 }
Per Åhgrenb6b00dc2018-02-20 22:18:27 +010098
Per Åhgren7ddd4632017-10-25 02:59:45 +020099 // Ensure that the gain is no longer forced to zero.
100 for (int k = 0; k <= kNumBlocksPerSecond / 5 + 1; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200101 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
102 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200103 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Per Åhgren7ddd4632017-10-25 02:59:45 +0200104 }
105
106 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200107 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
108 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200109 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100110 suppression_gain.GetGain(E2, S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700111 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800112 }
113 std::for_each(g.begin(), g.end(),
114 [](float a) { EXPECT_NEAR(1.f, a, 0.001); });
115
116 // Ensure that a strong nearend is detected to mask any echoes.
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100117 for (size_t ch = 0; ch < kNumCaptureChannels; ++ch) {
118 E2[ch].fill(100.f);
119 Y2[ch].fill(100.f);
120 R2[ch].fill(0.1f);
121 S2[ch].fill(0.1f);
122 N2[ch].fill(0.f);
Per Åhgrenf9807252019-10-09 13:57:07 +0200123 }
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200124
Per Åhgren7ddd4632017-10-25 02:59:45 +0200125 for (int k = 0; k < 100; ++k) {
Per Åhgren119e2192019-10-18 08:50:50 +0200126 aec_state.Update(delay_estimate, subtractor.FilterFrequencyResponses(),
127 subtractor.FilterImpulseResponses(),
Sam Zackrisson8f736c02019-10-01 12:47:53 +0200128 *render_delay_buffer->GetRenderBuffer(), E2, Y2, output);
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100129 suppression_gain.GetGain(E2, S2, R2, N2, 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
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100135 // Add a strong echo to one of the channels and ensure that it is suppressed.
136 E2[1].fill(1000000000.f);
137 R2[1].fill(10000000000000.f);
Per Åhgren47d7fbd2018-04-24 12:44:29 +0200138
peah522d71b2017-02-23 05:16:26 -0800139 for (int k = 0; k < 10; ++k) {
Gustaf Ullberg5ea57492019-11-05 15:19:02 +0100140 suppression_gain.GetGain(E2, S2, R2, N2, analyzer, aec_state, x,
peah14c11a42017-07-11 06:13:43 -0700141 &high_bands_gain, &g);
peah522d71b2017-02-23 05:16:26 -0800142 }
143 std::for_each(g.begin(), g.end(),
144 [](float a) { EXPECT_NEAR(0.f, a, 0.001); });
peah522d71b2017-02-23 05:16:26 -0800145}
146
147} // namespace aec3
148} // namespace webrtc