Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 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/signal_classifier.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/logging/apm_data_dumper.h" |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "rtc_base/random.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | namespace { |
| 25 | Random rand_gen(42); |
| 26 | ApmDataDumper data_dumper(0); |
| 27 | constexpr int kNumIterations = 100; |
| 28 | |
| 29 | // Runs the signal classifier on audio generated by 'sample_generator' |
| 30 | // for kNumIterations. Returns the number of frames classified as noise. |
| 31 | int RunClassifier(std::function<float()> sample_generator, int rate) { |
| 32 | SignalClassifier classifier(&data_dumper); |
| 33 | std::array<float, 480> signal; |
| 34 | classifier.Initialize(rate); |
| 35 | const size_t samples_per_channel = rtc::CheckedDivExact(rate, 100); |
| 36 | int number_of_noise_frames = 0; |
| 37 | for (int i = 0; i < kNumIterations; ++i) { |
| 38 | for (size_t j = 0; j < samples_per_channel; ++j) { |
| 39 | signal[j] = sample_generator(); |
| 40 | } |
| 41 | number_of_noise_frames += |
| 42 | classifier.Analyze({&signal[0], samples_per_channel}) == |
| 43 | SignalClassifier::SignalType::kStationary; |
| 44 | } |
| 45 | return number_of_noise_frames; |
| 46 | } |
| 47 | |
| 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. |
| 56 | TEST(AutomaticGainController2SignalClassifier, WhiteNoise) { |
| 57 | for (const auto rate : {8000, 16000, 32000, 48000}) { |
| 58 | const int number_of_noise_frames = RunClassifier(WhiteNoiseGenerator, rate); |
| 59 | EXPECT_GT(number_of_noise_frames, kNumIterations / 2); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Sine curves are (very) stationary. They trigger the detector all |
| 64 | // the time. Except for a few initial frames. |
| 65 | TEST(AutomaticGainController2SignalClassifier, SineTone) { |
| 66 | for (const auto rate : {8000, 16000, 32000, 48000}) { |
| 67 | test::SineGenerator gen(600.f, rate); |
| 68 | const int number_of_noise_frames = RunClassifier(gen, rate); |
| 69 | EXPECT_GE(number_of_noise_frames, kNumIterations - 5); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Pulses are transient if they are far enough apart. They shouldn't |
| 74 | // trigger the noise detector. |
| 75 | TEST(AutomaticGainController2SignalClassifier, PulseTone) { |
| 76 | for (const auto rate : {8000, 16000, 32000, 48000}) { |
| 77 | test::PulseGenerator gen(30.f, rate); |
| 78 | const int number_of_noise_frames = RunClassifier(gen, rate); |
| 79 | EXPECT_EQ(number_of_noise_frames, 0); |
| 80 | } |
| 81 | } |
| 82 | } // namespace webrtc |