xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/comfort_noise_generator_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "api/audio/echo_canceller3_config.h"
17 #include "modules/audio_processing/aec3/aec_state.h"
18 #include "rtc_base/random.h"
19 #include "rtc_base/system/arch.h"
20 #include "system_wrappers/include/cpu_features_wrapper.h"
21 #include "test/gtest.h"
22 
23 namespace webrtc {
24 namespace aec3 {
25 namespace {
26 
Power(const FftData & N)27 float Power(const FftData& N) {
28   std::array<float, kFftLengthBy2Plus1> N2;
29   N.Spectrum(Aec3Optimization::kNone, N2);
30   return std::accumulate(N2.begin(), N2.end(), 0.f) / N2.size();
31 }
32 
33 }  // namespace
34 
TEST(ComfortNoiseGenerator,CorrectLevel)35 TEST(ComfortNoiseGenerator, CorrectLevel) {
36   constexpr size_t kNumChannels = 5;
37   EchoCanceller3Config config;
38   ComfortNoiseGenerator cng(config, DetectOptimization(), kNumChannels);
39   AecState aec_state(config, kNumChannels);
40 
41   std::vector<std::array<float, kFftLengthBy2Plus1>> N2(kNumChannels);
42   std::vector<FftData> n_lower(kNumChannels);
43   std::vector<FftData> n_upper(kNumChannels);
44 
45   for (size_t ch = 0; ch < kNumChannels; ++ch) {
46     N2[ch].fill(1000.f * 1000.f / (ch + 1));
47     n_lower[ch].re.fill(0.f);
48     n_lower[ch].im.fill(0.f);
49     n_upper[ch].re.fill(0.f);
50     n_upper[ch].im.fill(0.f);
51   }
52 
53   // Ensure instantaneous updata to nonzero noise.
54   cng.Compute(false, N2, n_lower, n_upper);
55 
56   for (size_t ch = 0; ch < kNumChannels; ++ch) {
57     EXPECT_LT(0.f, Power(n_lower[ch]));
58     EXPECT_LT(0.f, Power(n_upper[ch]));
59   }
60 
61   for (int k = 0; k < 10000; ++k) {
62     cng.Compute(false, N2, n_lower, n_upper);
63   }
64 
65   for (size_t ch = 0; ch < kNumChannels; ++ch) {
66     EXPECT_NEAR(2.f * N2[ch][0], Power(n_lower[ch]), N2[ch][0] / 10.f);
67     EXPECT_NEAR(2.f * N2[ch][0], Power(n_upper[ch]), N2[ch][0] / 10.f);
68   }
69 }
70 
71 }  // namespace aec3
72 }  // namespace webrtc
73