webrtc/modules/audio_processing/agc2/signal_classifier_unittest.cc
Alex Loiko 4ed47d0190 Noise level estimation for AGC2.
We put back the old noise estimator from LevelController. We add a few
new unit tests. We also re-arrange the code so that it fits with how
it is used in AGC2. The differences are:

1. The NoiseLevelEstimator is now fully self-contained.
2. The NoiseLevelEstimator is responsible for calling SignalClassifier
   and computing the signal energy. Previously the signal type and
   energy were used in several places. It made sense to compute the
   values independently of the noise calculation.
3. Re-initialization doesn't have to be done by the caller.
4. The interface is AudioFrameView instead of AudioBuffer.

# Bots are green, nothing should break internal stuff
NOTRY=True

Bug: webrtc:7494
Change-Id: I442bdbbeb3796eb2518e96000aec9dc5a039ae6d
Reviewed-on: https://webrtc-review.googlesource.com/66380
Commit-Queue: Alex Loiko <aleloi@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22738}
2018-04-04 18:23:55 +00:00

82 lines
2.9 KiB
C++

/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_processing/agc2/signal_classifier.h"
#include <array>
#include <functional>
#include <limits>
#include "modules/audio_processing/agc2/agc2_testing_common.h"
#include "modules/audio_processing/logging/apm_data_dumper.h"
#include "rtc_base/gunit.h"
#include "rtc_base/random.h"
namespace webrtc {
namespace {
Random rand_gen(42);
ApmDataDumper data_dumper(0);
constexpr int kNumIterations = 100;
// Runs the signal classifier on audio generated by 'sample_generator'
// for kNumIterations. Returns the number of frames classified as noise.
int RunClassifier(std::function<float()> sample_generator, int rate) {
SignalClassifier classifier(&data_dumper);
std::array<float, 480> signal;
classifier.Initialize(rate);
const size_t samples_per_channel = rtc::CheckedDivExact(rate, 100);
int number_of_noise_frames = 0;
for (int i = 0; i < kNumIterations; ++i) {
for (size_t j = 0; j < samples_per_channel; ++j) {
signal[j] = sample_generator();
}
number_of_noise_frames +=
classifier.Analyze({&signal[0], samples_per_channel}) ==
SignalClassifier::SignalType::kStationary;
}
return number_of_noise_frames;
}
float WhiteNoiseGenerator() {
return static_cast<float>(rand_gen.Rand(std::numeric_limits<int16_t>::min(),
std::numeric_limits<int16_t>::max()));
}
} // namespace
// White random noise is stationary, but does not trigger the detector
// every frame due to the randomness.
TEST(AutomaticGainController2SignalClassifier, WhiteNoise) {
for (const auto rate : {8000, 16000, 32000, 48000}) {
const int number_of_noise_frames = RunClassifier(WhiteNoiseGenerator, rate);
EXPECT_GT(number_of_noise_frames, kNumIterations / 2);
}
}
// Sine curves are (very) stationary. They trigger the detector all
// the time. Except for a few initial frames.
TEST(AutomaticGainController2SignalClassifier, SineTone) {
for (const auto rate : {8000, 16000, 32000, 48000}) {
test::SineGenerator gen(600.f, rate);
const int number_of_noise_frames = RunClassifier(gen, rate);
EXPECT_GE(number_of_noise_frames, kNumIterations - 5);
}
}
// Pulses are transient if they are far enough apart. They shouldn't
// trigger the noise detector.
TEST(AutomaticGainController2SignalClassifier, PulseTone) {
for (const auto rate : {8000, 16000, 32000, 48000}) {
test::PulseGenerator gen(30.f, rate);
const int number_of_noise_frames = RunClassifier(gen, rate);
EXPECT_EQ(number_of_noise_frames, 0);
}
}
} // namespace webrtc