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 | |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 17 | #include "api/function_view.h" |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 18 | #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 | |
| 23 | namespace webrtc { |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 24 | namespace { |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 25 | constexpr 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 Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 29 | float RunClassifier(rtc::FunctionView<float()> sample_generator, |
| 30 | int sample_rate_hz) { |
| 31 | ApmDataDumper data_dumper(0); |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 32 | SignalClassifier classifier(&data_dumper); |
| 33 | std::array<float, 480> signal; |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 34 | classifier.Initialize(sample_rate_hz); |
| 35 | const size_t samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100); |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 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 | |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 48 | class SignalClassifierParametrization : public ::testing::TestWithParam<int> { |
| 49 | protected: |
| 50 | int sample_rate_hz() const { return GetParam(); } |
| 51 | }; |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 52 | |
| 53 | // White random noise is stationary, but does not trigger the detector |
| 54 | // every frame due to the randomness. |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 55 | TEST_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 Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // Sine curves are (very) stationary. They trigger the detector all |
| 63 | // the time. Except for a few initial frames. |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 64 | TEST_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 Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Pulses are transient if they are far enough apart. They shouldn't |
| 72 | // trigger the noise detector. |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 73 | TEST_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 Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 79 | } |
Alessio Bazzica | 70b775d | 2021-04-07 12:03:11 +0200 | [diff] [blame] | 80 | |
| 81 | INSTANTIATE_TEST_SUITE_P(GainController2SignalClassifier, |
| 82 | SignalClassifierParametrization, |
| 83 | ::testing::Values(8000, 16000, 32000, 48000)); |
| 84 | |
| 85 | } // namespace |
Alex Loiko | 4ed47d0 | 2018-04-04 15:05:57 +0200 | [diff] [blame] | 86 | } // namespace webrtc |