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/comfort_noise_generator.h"
12
13 #include <algorithm>
14 #include <numeric>
15
16 #include "rtc_base/random.h"
17 #include "system_wrappers/include/cpu_features_wrapper.h"
18 #include "test/gtest.h"
19 #include "typedefs.h" // NOLINT(build/include)
20
21 namespace webrtc {
22 namespace aec3 {
23 namespace {
24
Power(const FftData & N)25 float Power(const FftData& N) {
26 std::array<float, kFftLengthBy2Plus1> N2;
27 N.Spectrum(Aec3Optimization::kNone, &N2);
28 return std::accumulate(N2.begin(), N2.end(), 0.f) / N2.size();
29 }
30
31 } // namespace
32
33 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
34
TEST(ComfortNoiseGenerator,NullLowerBandNoise)35 TEST(ComfortNoiseGenerator, NullLowerBandNoise) {
36 std::array<float, kFftLengthBy2Plus1> N2;
37 FftData noise;
38 EXPECT_DEATH(
39 ComfortNoiseGenerator(DetectOptimization())
40 .Compute(AecState(EchoCanceller3Config{}), N2, nullptr, &noise),
41 "");
42 }
43
TEST(ComfortNoiseGenerator,NullUpperBandNoise)44 TEST(ComfortNoiseGenerator, NullUpperBandNoise) {
45 std::array<float, kFftLengthBy2Plus1> N2;
46 FftData noise;
47 EXPECT_DEATH(
48 ComfortNoiseGenerator(DetectOptimization())
49 .Compute(AecState(EchoCanceller3Config{}), N2, &noise, nullptr),
50 "");
51 }
52
53 #endif
54
55 #if defined(WEBRTC_ARCH_X86_FAMILY)
56 // Verifies that the optimized methods are bitexact to their reference
57 // counterparts.
TEST(ComfortNoiseGenerator,TestOptimizations)58 TEST(ComfortNoiseGenerator, TestOptimizations) {
59 if (WebRtc_GetCPUInfo(kSSE2) != 0) {
60 Random random_generator(42U);
61 uint32_t seed = 42;
62 uint32_t seed_SSE2 = 42;
63 std::array<float, kFftLengthBy2Plus1> N2;
64 FftData lower_band_noise;
65 FftData upper_band_noise;
66 FftData lower_band_noise_SSE2;
67 FftData upper_band_noise_SSE2;
68 for (int k = 0; k < 10; ++k) {
69 for (size_t j = 0; j < N2.size(); ++j) {
70 N2[j] = random_generator.Rand<float>() * 1000.f;
71 }
72
73 EstimateComfortNoise(N2, &seed, &lower_band_noise, &upper_band_noise);
74 EstimateComfortNoise_SSE2(N2, &seed_SSE2, &lower_band_noise_SSE2,
75 &upper_band_noise_SSE2);
76 for (size_t j = 0; j < lower_band_noise.re.size(); ++j) {
77 EXPECT_NEAR(lower_band_noise.re[j], lower_band_noise_SSE2.re[j],
78 0.00001f);
79 EXPECT_NEAR(upper_band_noise.re[j], upper_band_noise_SSE2.re[j],
80 0.00001f);
81 }
82 for (size_t j = 1; j < lower_band_noise.re.size() - 1; ++j) {
83 EXPECT_NEAR(lower_band_noise.im[j], lower_band_noise_SSE2.im[j],
84 0.00001f);
85 EXPECT_NEAR(upper_band_noise.im[j], upper_band_noise_SSE2.im[j],
86 0.00001f);
87 }
88 }
89 }
90 }
91
92 #endif
93
TEST(ComfortNoiseGenerator,CorrectLevel)94 TEST(ComfortNoiseGenerator, CorrectLevel) {
95 ComfortNoiseGenerator cng(DetectOptimization());
96 AecState aec_state(EchoCanceller3Config{});
97
98 std::array<float, kFftLengthBy2Plus1> N2;
99 N2.fill(1000.f * 1000.f);
100
101 FftData n_lower;
102 FftData n_upper;
103 n_lower.re.fill(0.f);
104 n_lower.im.fill(0.f);
105 n_upper.re.fill(0.f);
106 n_upper.im.fill(0.f);
107
108 // Ensure instantaneous updata to nonzero noise.
109 cng.Compute(aec_state, N2, &n_lower, &n_upper);
110 EXPECT_LT(0.f, Power(n_lower));
111 EXPECT_LT(0.f, Power(n_upper));
112
113 for (int k = 0; k < 10000; ++k) {
114 cng.Compute(aec_state, N2, &n_lower, &n_upper);
115 }
116 EXPECT_NEAR(N2[0], Power(n_lower), N2[0] / 10.f);
117 EXPECT_NEAR(N2[0], Power(n_upper), N2[0] / 10.f);
118 }
119
120 } // namespace aec3
121 } // namespace webrtc
122