1 /*
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 
11 #include "modules/audio_processing/aec3/residual_echo_estimator.h"
12 
13 #include "modules/audio_processing/aec3/aec3_fft.h"
14 #include "modules/audio_processing/aec3/aec_state.h"
15 #include "modules/audio_processing/include/audio_processing.h"
16 #include "modules/audio_processing/test/echo_canceller_test_tools.h"
17 #include "rtc_base/random.h"
18 #include "test/gtest.h"
19 
20 namespace webrtc {
21 
22 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
23 
24 // Verifies that the check for non-null output residual echo power works.
TEST(ResidualEchoEstimator,NullResidualEchoPowerOutput)25 TEST(ResidualEchoEstimator, NullResidualEchoPowerOutput) {
26   AecState aec_state(EchoCanceller3Config{});
27   RenderBuffer render_buffer(Aec3Optimization::kNone, 3, 10,
28                              std::vector<size_t>(1, 10));
29   std::vector<std::array<float, kFftLengthBy2Plus1>> H2;
30   std::array<float, kFftLengthBy2Plus1> S2_linear;
31   std::array<float, kFftLengthBy2Plus1> Y2;
32   EXPECT_DEATH(ResidualEchoEstimator(EchoCanceller3Config{})
33                    .Estimate(aec_state, render_buffer, S2_linear, Y2, nullptr),
34                "");
35 }
36 
37 #endif
38 
TEST(ResidualEchoEstimator,BasicTest)39 TEST(ResidualEchoEstimator, BasicTest) {
40   ResidualEchoEstimator estimator(EchoCanceller3Config{});
41   EchoCanceller3Config config;
42   config.ep_strength.default_len = 0.f;
43   AecState aec_state(config);
44   RenderBuffer render_buffer(Aec3Optimization::kNone, 3, 10,
45                              std::vector<size_t>(1, 10));
46   std::array<float, kFftLengthBy2Plus1> E2_main;
47   std::array<float, kFftLengthBy2Plus1> E2_shadow;
48   std::array<float, kFftLengthBy2Plus1> S2_linear;
49   std::array<float, kFftLengthBy2Plus1> S2_fallback;
50   std::array<float, kFftLengthBy2Plus1> Y2;
51   std::array<float, kFftLengthBy2Plus1> R2;
52   EchoPathVariability echo_path_variability(false, false);
53   std::vector<std::vector<float>> x(3, std::vector<float>(kBlockSize, 0.f));
54   std::vector<std::array<float, kFftLengthBy2Plus1>> H2(10);
55   Random random_generator(42U);
56   FftData X;
57   std::array<float, kBlockSize> x_old;
58   std::array<float, kBlockSize> s;
59   Aec3Fft fft;
60 
61   for (auto& H2_k : H2) {
62     H2_k.fill(0.01f);
63   }
64   H2[2].fill(10.f);
65   H2[2][0] = 0.1f;
66 
67   std::array<float, kAdaptiveFilterTimeDomainLength> h;
68   h.fill(0.f);
69 
70   s.fill(100.f);
71 
72   constexpr float kLevel = 10.f;
73   E2_shadow.fill(kLevel);
74   E2_main.fill(kLevel);
75   S2_linear.fill(kLevel);
76   S2_fallback.fill(kLevel);
77   Y2.fill(kLevel);
78 
79   for (int k = 0; k < 2000; ++k) {
80     RandomizeSampleVector(&random_generator, x[0]);
81     std::for_each(x[0].begin(), x[0].end(), [](float& a) { a /= 30.f; });
82     fft.PaddedFft(x[0], x_old, &X);
83     render_buffer.Insert(x);
84 
85     aec_state.HandleEchoPathChange(echo_path_variability);
86     aec_state.Update(H2, h, true, 2, render_buffer, E2_main, Y2, x[0], s,
87                      false);
88 
89     estimator.Estimate(aec_state, render_buffer, S2_linear, Y2, &R2);
90   }
91   std::for_each(R2.begin(), R2.end(),
92                 [&](float a) { EXPECT_NEAR(kLevel, a, 0.1f); });
93 }
94 
95 }  // namespace webrtc
96