blob: f1a3a664f0ac0d1ea0cdcb606b56c992f97e3d9c [file] [log] [blame]
Alex Loiko4ed47d02018-04-04 15:05:57 +02001/*
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
Alessio Bazzica70b775d2021-04-07 12:03:11 +020017#include "api/function_view.h"
Alex Loiko4ed47d02018-04-04 15:05:57 +020018#include "modules/audio_processing/agc2/agc2_testing_common.h"
19#include "modules/audio_processing/logging/apm_data_dumper.h"
20#include "rtc_base/gunit.h"
21#include "rtc_base/random.h"
22
23namespace webrtc {
Alex Loiko4ed47d02018-04-04 15:05:57 +020024namespace {
Alex Loiko4ed47d02018-04-04 15:05:57 +020025constexpr int kNumIterations = 100;
26
27// Runs the signal classifier on audio generated by 'sample_generator'
28// for kNumIterations. Returns the number of frames classified as noise.
Alessio Bazzica70b775d2021-04-07 12:03:11 +020029float RunClassifier(rtc::FunctionView<float()> sample_generator,
30 int sample_rate_hz) {
31 ApmDataDumper data_dumper(0);
Alex Loiko4ed47d02018-04-04 15:05:57 +020032 SignalClassifier classifier(&data_dumper);
33 std::array<float, 480> signal;
Alessio Bazzica70b775d2021-04-07 12:03:11 +020034 classifier.Initialize(sample_rate_hz);
35 const size_t samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
Alex Loiko4ed47d02018-04-04 15:05:57 +020036 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
Alessio Bazzica70b775d2021-04-07 12:03:11 +020048class SignalClassifierParametrization : public ::testing::TestWithParam<int> {
49 protected:
50 int sample_rate_hz() const { return GetParam(); }
51};
Alex Loiko4ed47d02018-04-04 15:05:57 +020052
53// White random noise is stationary, but does not trigger the detector
54// every frame due to the randomness.
Alessio Bazzica70b775d2021-04-07 12:03:11 +020055TEST_P(SignalClassifierParametrization, WhiteNoise) {
56 test::WhiteNoiseGenerator gen(/*min_amplitude=*/test::kMinS16,
57 /*max_amplitude=*/test::kMaxS16);
58 const int number_of_noise_frames = RunClassifier(gen, sample_rate_hz());
59 EXPECT_GT(number_of_noise_frames, kNumIterations / 2);
Alex Loiko4ed47d02018-04-04 15:05:57 +020060}
61
62// Sine curves are (very) stationary. They trigger the detector all
63// the time. Except for a few initial frames.
Alessio Bazzica70b775d2021-04-07 12:03:11 +020064TEST_P(SignalClassifierParametrization, SineTone) {
65 test::SineGenerator gen(/*amplitude=*/test::kMaxS16, /*frequency_hz=*/600.0f,
66 sample_rate_hz());
67 const int number_of_noise_frames = RunClassifier(gen, sample_rate_hz());
68 EXPECT_GE(number_of_noise_frames, kNumIterations - 5);
Alex Loiko4ed47d02018-04-04 15:05:57 +020069}
70
71// Pulses are transient if they are far enough apart. They shouldn't
72// trigger the noise detector.
Alessio Bazzica70b775d2021-04-07 12:03:11 +020073TEST_P(SignalClassifierParametrization, PulseTone) {
74 test::PulseGenerator gen(/*pulse_amplitude=*/test::kMaxS16,
75 /*no_pulse_amplitude=*/10.0f, /*frequency_hz=*/20.0f,
76 sample_rate_hz());
77 const int number_of_noise_frames = RunClassifier(gen, sample_rate_hz());
78 EXPECT_EQ(number_of_noise_frames, 0);
Alex Loiko4ed47d02018-04-04 15:05:57 +020079}
Alessio Bazzica70b775d2021-04-07 12:03:11 +020080
81INSTANTIATE_TEST_SUITE_P(GainController2SignalClassifier,
82 SignalClassifierParametrization,
83 ::testing::Values(8000, 16000, 32000, 48000));
84
85} // namespace
Alex Loiko4ed47d02018-04-04 15:05:57 +020086} // namespace webrtc