• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 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/agc2/noise_level_estimator.h"
12 
13 #include <array>
14 #include <functional>
15 #include <limits>
16 
17 #include "modules/audio_processing/agc2/agc2_testing_common.h"
18 #include "modules/audio_processing/agc2/vector_float_frame.h"
19 #include "modules/audio_processing/logging/apm_data_dumper.h"
20 #include "rtc_base/gunit.h"
21 #include "rtc_base/random.h"
22 
23 namespace webrtc {
24 namespace {
25 Random rand_gen(42);
26 ApmDataDumper data_dumper(0);
27 constexpr int kNumIterations = 200;
28 constexpr int kFramesPerSecond = 100;
29 
30 // Runs the noise estimator on audio generated by 'sample_generator'
31 // for kNumIterations. Returns the last noise level estimate.
RunEstimator(std::function<float ()> sample_generator,int rate)32 float RunEstimator(std::function<float()> sample_generator, int rate) {
33   NoiseLevelEstimator estimator(&data_dumper);
34   const size_t samples_per_channel =
35       rtc::CheckedDivExact(rate, kFramesPerSecond);
36   VectorFloatFrame signal(1, static_cast<int>(samples_per_channel), 0.f);
37 
38   for (int i = 0; i < kNumIterations; ++i) {
39     AudioFrameView<float> frame_view = signal.float_frame_view();
40     for (size_t j = 0; j < samples_per_channel; ++j) {
41       frame_view.channel(0)[j] = sample_generator();
42     }
43     estimator.Analyze(frame_view);
44   }
45   return estimator.Analyze(signal.float_frame_view());
46 }
47 
WhiteNoiseGenerator()48 float WhiteNoiseGenerator() {
49   return static_cast<float>(rand_gen.Rand(std::numeric_limits<int16_t>::min(),
50                                           std::numeric_limits<int16_t>::max()));
51 }
52 }  // namespace
53 
54 // White random noise is stationary, but does not trigger the detector
55 // every frame due to the randomness.
TEST(AutomaticGainController2NoiseEstimator,RandomNoise)56 TEST(AutomaticGainController2NoiseEstimator, RandomNoise) {
57   for (const auto rate : {8000, 16000, 32000, 48000}) {
58     const float noise_level = RunEstimator(WhiteNoiseGenerator, rate);
59     EXPECT_NEAR(noise_level, -5.f, 1.f);
60   }
61 }
62 
63 // Sine curves are (very) stationary. They trigger the detector all
64 // the time. Except for a few initial frames.
TEST(AutomaticGainController2NoiseEstimator,SineTone)65 TEST(AutomaticGainController2NoiseEstimator, SineTone) {
66   for (const auto rate : {8000, 16000, 32000, 48000}) {
67     test::SineGenerator gen(600.f, rate);
68     const float noise_level = RunEstimator(gen, rate);
69     EXPECT_NEAR(noise_level, -33.f, 1.f);
70   }
71 }
72 
73 // Pulses are transient if they are far enough apart. They shouldn't
74 // trigger the noise detector.
TEST(AutomaticGainController2NoiseEstimator,PulseTone)75 TEST(AutomaticGainController2NoiseEstimator, PulseTone) {
76   for (const auto rate : {8000, 16000, 32000, 48000}) {
77     test::PulseGenerator gen(20.f, rate);
78     const int noise_level = RunEstimator(gen, rate);
79     EXPECT_NEAR(noise_level, -79.f, 1.f);
80   }
81 }
82 
83 }  // namespace webrtc
84