alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/gain_controller2.h" |
| 12 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 13 | #include <algorithm> |
Alessio Bazzica | 980c460 | 2021-04-14 19:09:17 +0200 | [diff] [blame] | 14 | #include <cmath> |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 15 | #include <memory> |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 16 | #include <numeric> |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 17 | #include <tuple> |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/array_view.h" |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 20 | #include "modules/audio_processing/agc2/agc2_testing_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 22 | #include "modules/audio_processing/test/audio_buffer_tools.h" |
| 23 | #include "modules/audio_processing/test/bitexactness_tools.h" |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 31 | using Agc2Config = AudioProcessing::Config::GainController2; |
| 32 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 33 | // Sets all the samples in `ab` to `value`. |
| 34 | void SetAudioBufferSamples(float value, AudioBuffer& ab) { |
| 35 | for (size_t k = 0; k < ab.num_channels(); ++k) { |
| 36 | std::fill(ab.channels()[k], ab.channels()[k] + ab.num_frames(), value); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 40 | float RunAgc2WithConstantInput(GainController2& agc2, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 41 | float input_level, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 42 | int num_frames, |
| 43 | int sample_rate_hz) { |
| 44 | const int num_samples = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 45 | AudioBuffer ab(sample_rate_hz, 1, sample_rate_hz, 1, sample_rate_hz, 1); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 46 | |
| 47 | // Give time to the level estimator to converge. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 48 | for (int i = 0; i < num_frames + 1; ++i) { |
| 49 | SetAudioBufferSamples(input_level, ab); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 50 | agc2.Process(/*speech_probability=*/absl::nullopt, &ab); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Return the last sample from the last processed frame. |
Per Ã…hgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 54 | return ab.channels()[0][num_samples - 1]; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 57 | std::unique_ptr<GainController2> CreateAgc2FixedDigitalMode( |
| 58 | float fixed_gain_db, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 59 | int sample_rate_hz) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 60 | Agc2Config config; |
| 61 | config.adaptive_digital.enabled = false; |
| 62 | config.fixed_digital.gain_db = fixed_gain_db; |
| 63 | EXPECT_TRUE(GainController2::Validate(config)); |
| 64 | return std::make_unique<GainController2>(config, sample_rate_hz, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 65 | /*num_channels=*/1, |
| 66 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 67 | } |
| 68 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 69 | } // namespace |
| 70 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 71 | TEST(GainController2, CheckDefaultConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 72 | Agc2Config config; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 73 | EXPECT_TRUE(GainController2::Validate(config)); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 76 | TEST(GainController2, CheckFixedDigitalConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 77 | Agc2Config config; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 78 | // Attenuation is not allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 79 | config.fixed_digital.gain_db = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 80 | EXPECT_FALSE(GainController2::Validate(config)); |
| 81 | // No gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 82 | config.fixed_digital.gain_db = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 83 | EXPECT_TRUE(GainController2::Validate(config)); |
| 84 | // Positive gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 85 | config.fixed_digital.gain_db = 15.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 86 | EXPECT_TRUE(GainController2::Validate(config)); |
| 87 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 88 | |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 89 | TEST(GainController2, CheckHeadroomDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 90 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 91 | config.adaptive_digital.headroom_db = -1.0f; |
| 92 | EXPECT_FALSE(GainController2::Validate(config)); |
| 93 | config.adaptive_digital.headroom_db = 0.0f; |
| 94 | EXPECT_TRUE(GainController2::Validate(config)); |
| 95 | config.adaptive_digital.headroom_db = 5.0f; |
| 96 | EXPECT_TRUE(GainController2::Validate(config)); |
| 97 | } |
| 98 | |
| 99 | TEST(GainController2, CheckMaxGainDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 100 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 101 | config.adaptive_digital.max_gain_db = -1.0f; |
| 102 | EXPECT_FALSE(GainController2::Validate(config)); |
| 103 | config.adaptive_digital.max_gain_db = 0.0f; |
| 104 | EXPECT_FALSE(GainController2::Validate(config)); |
| 105 | config.adaptive_digital.max_gain_db = 5.0f; |
| 106 | EXPECT_TRUE(GainController2::Validate(config)); |
| 107 | } |
| 108 | |
| 109 | TEST(GainController2, CheckInitialGainDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 110 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 111 | config.adaptive_digital.initial_gain_db = -1.0f; |
| 112 | EXPECT_FALSE(GainController2::Validate(config)); |
| 113 | config.adaptive_digital.initial_gain_db = 0.0f; |
| 114 | EXPECT_TRUE(GainController2::Validate(config)); |
| 115 | config.adaptive_digital.initial_gain_db = 5.0f; |
| 116 | EXPECT_TRUE(GainController2::Validate(config)); |
| 117 | } |
| 118 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 119 | TEST(GainController2, CheckAdaptiveDigitalMaxGainChangeSpeedConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 120 | Agc2Config config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 121 | config.adaptive_digital.max_gain_change_db_per_second = -1.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 122 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 123 | config.adaptive_digital.max_gain_change_db_per_second = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 124 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 125 | config.adaptive_digital.max_gain_change_db_per_second = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 126 | EXPECT_TRUE(GainController2::Validate(config)); |
| 127 | } |
| 128 | |
| 129 | TEST(GainController2, CheckAdaptiveDigitalMaxOutputNoiseLevelConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 130 | Agc2Config config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 131 | config.adaptive_digital.max_output_noise_level_dbfs = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 132 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 133 | config.adaptive_digital.max_output_noise_level_dbfs = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 134 | EXPECT_TRUE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 135 | config.adaptive_digital.max_output_noise_level_dbfs = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 136 | EXPECT_TRUE(GainController2::Validate(config)); |
| 137 | } |
| 138 | |
| 139 | // Checks that the default config is applied. |
| 140 | TEST(GainController2, ApplyDefaultConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 141 | auto gain_controller2 = std::make_unique<GainController2>( |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 142 | Agc2Config{}, /*sample_rate_hz=*/16000, /*num_channels=*/2, |
| 143 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 144 | EXPECT_TRUE(gain_controller2.get()); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 147 | TEST(GainController2FixedDigital, GainShouldChangeOnSetGain) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 148 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 149 | constexpr size_t kNumFrames = 5; |
| 150 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 151 | constexpr float kGain0Db = 0.0f; |
| 152 | constexpr float kGain20Db = 20.0f; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 153 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 154 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGain0Db, kSampleRateHz); |
| 155 | |
| 156 | // Signal level is unchanged with 0 db gain. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 157 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 158 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 159 | kInputLevel); |
| 160 | |
| 161 | // +20 db should increase signal by a factor of 10. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 162 | agc2_fixed->SetFixedGainDb(kGain20Db); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 163 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 164 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 165 | kInputLevel * 10); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 168 | TEST(GainController2FixedDigital, ChangeFixedGainShouldBeFastAndTimeInvariant) { |
| 169 | // Number of frames required for the fixed gain controller to adapt on the |
| 170 | // input signal when the gain changes. |
| 171 | constexpr size_t kNumFrames = 5; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 172 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 173 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 174 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 175 | constexpr float kGainDbLow = 0.0f; |
| 176 | constexpr float kGainDbHigh = 25.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 177 | static_assert(kGainDbLow < kGainDbHigh, ""); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 178 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 179 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGainDbLow, kSampleRateHz); |
| 180 | |
| 181 | // Start with a lower gain. |
| 182 | const float output_level_pre = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 183 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 184 | |
| 185 | // Increase gain. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 186 | agc2_fixed->SetFixedGainDb(kGainDbHigh); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 187 | static_cast<void>(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 188 | kNumFrames, kSampleRateHz)); |
| 189 | |
| 190 | // Back to the lower gain. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 191 | agc2_fixed->SetFixedGainDb(kGainDbLow); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 192 | const float output_level_post = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 193 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 194 | |
| 195 | EXPECT_EQ(output_level_pre, output_level_post); |
| 196 | } |
| 197 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 198 | class FixedDigitalTest |
| 199 | : public ::testing::TestWithParam<std::tuple<float, float, int, bool>> { |
| 200 | protected: |
| 201 | float gain_db_min() const { return std::get<0>(GetParam()); } |
| 202 | float gain_db_max() const { return std::get<1>(GetParam()); } |
| 203 | int sample_rate_hz() const { return std::get<2>(GetParam()); } |
| 204 | bool saturation_expected() const { return std::get<3>(GetParam()); } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 205 | }; |
| 206 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 207 | TEST_P(FixedDigitalTest, CheckSaturationBehaviorWithLimiter) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 208 | for (const float gain_db : test::LinSpace(gain_db_min(), gain_db_max(), 10)) { |
| 209 | SCOPED_TRACE(gain_db); |
| 210 | auto agc2_fixed = CreateAgc2FixedDigitalMode(gain_db, sample_rate_hz()); |
| 211 | const float processed_sample = |
| 212 | RunAgc2WithConstantInput(*agc2_fixed, /*input_level=*/32767.0f, |
| 213 | /*num_frames=*/5, sample_rate_hz()); |
| 214 | if (saturation_expected()) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 215 | EXPECT_FLOAT_EQ(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 216 | } else { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 217 | EXPECT_LT(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 218 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 219 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 222 | static_assert(test::kLimiterMaxInputLevelDbFs < 10, ""); |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 223 | INSTANTIATE_TEST_SUITE_P( |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 224 | GainController2, |
| 225 | FixedDigitalTest, |
| 226 | ::testing::Values( |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 227 | // When gain < `test::kLimiterMaxInputLevelDbFs`, the limiter will not |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 228 | // saturate the signal (at any sample rate). |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 229 | std::make_tuple(0.1f, |
| 230 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 231 | 8000, |
| 232 | false), |
| 233 | std::make_tuple(0.1, |
| 234 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 235 | 48000, |
| 236 | false), |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 237 | // When gain > `test::kLimiterMaxInputLevelDbFs`, the limiter will |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 238 | // saturate the signal (at any sample rate). |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 239 | std::make_tuple(test::kLimiterMaxInputLevelDbFs + 0.01f, |
| 240 | 10.0f, |
| 241 | 8000, |
| 242 | true), |
| 243 | std::make_tuple(test::kLimiterMaxInputLevelDbFs + 0.01f, |
| 244 | 10.0f, |
| 245 | 48000, |
| 246 | true))); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 247 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 248 | // Processes a test audio file and checks that the gain applied at the end of |
| 249 | // the recording is close to the expected value. |
| 250 | TEST(GainController2, CheckFinalGainWithAdaptiveDigitalController) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 251 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 252 | constexpr int kStereo = 2; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 253 | |
| 254 | // Create AGC2 enabling only the adaptive digital controller. |
| 255 | Agc2Config config; |
| 256 | config.fixed_digital.gain_db = 0.0f; |
| 257 | config.adaptive_digital.enabled = true; |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 258 | GainController2 agc2(config, kSampleRateHz, kStereo, |
| 259 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 260 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 261 | test::InputAudioFile input_file( |
| 262 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 263 | /*loop_at_end=*/true); |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 264 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 265 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 266 | // Init buffers. |
| 267 | constexpr int kFrameDurationMs = 10; |
| 268 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 269 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 270 | kSampleRateHz, kStereo); |
| 271 | |
| 272 | // Simulate. |
| 273 | constexpr float kGainDb = -6.0f; |
| 274 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 275 | constexpr int kDurationMs = 10000; |
| 276 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 277 | for (int i = 0; i < kNumFramesToProcess; ++i) { |
| 278 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 279 | stream_config.num_channels(), &input_file, |
| 280 | frame); |
| 281 | // Apply a fixed gain to the input audio. |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 282 | for (float& x : frame) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 283 | x *= gain; |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 284 | } |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 285 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 286 | agc2.Process(/*speech_probability=*/absl::nullopt, &audio_buffer); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Estimate the applied gain by processing a probing frame. |
| 290 | SetAudioBufferSamples(/*value=*/1.0f, audio_buffer); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 291 | agc2.Process(/*speech_probability=*/absl::nullopt, &audio_buffer); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 292 | const float applied_gain_db = |
| 293 | 20.0f * std::log10(audio_buffer.channels_const()[0][0]); |
| 294 | |
| 295 | constexpr float kExpectedGainDb = 5.6f; |
| 296 | constexpr float kToleranceDb = 0.3f; |
| 297 | EXPECT_NEAR(applied_gain_db, kExpectedGainDb, kToleranceDb); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 298 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 299 | |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame^] | 300 | // Processes a test audio file and checks that the injected speech probability |
| 301 | // is ignored when the internal VAD is used. |
| 302 | TEST(GainController2, |
| 303 | CheckInjectedVadProbabilityNotUsedWithAdaptiveDigitalController) { |
| 304 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 305 | constexpr int kStereo = 2; |
| 306 | |
| 307 | // Create AGC2 enabling only the adaptive digital controller. |
| 308 | Agc2Config config; |
| 309 | config.fixed_digital.gain_db = 0.0f; |
| 310 | config.adaptive_digital.enabled = true; |
| 311 | GainController2 agc2(config, kSampleRateHz, kStereo, |
| 312 | /*use_internal_vad=*/true); |
| 313 | GainController2 agc2_reference(config, kSampleRateHz, kStereo, |
| 314 | /*use_internal_vad=*/true); |
| 315 | |
| 316 | test::InputAudioFile input_file( |
| 317 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 318 | /*loop_at_end=*/true); |
| 319 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 320 | |
| 321 | // Init buffers. |
| 322 | constexpr int kFrameDurationMs = 10; |
| 323 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 324 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 325 | kSampleRateHz, kStereo); |
| 326 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 327 | kStereo, kSampleRateHz, kStereo); |
| 328 | |
| 329 | // Simulate. |
| 330 | constexpr float kGainDb = -6.0f; |
| 331 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 332 | constexpr int kDurationMs = 10000; |
| 333 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 334 | constexpr float kSpeechProbabilities[] = {1.0f, 0.3f}; |
| 335 | constexpr float kEpsilon = 0.0001f; |
| 336 | bool all_samples_zero = true; |
| 337 | for (int i = 0, j = 0; i < kNumFramesToProcess; ++i, j = 1 - j) { |
| 338 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 339 | stream_config.num_channels(), &input_file, |
| 340 | frame); |
| 341 | // Apply a fixed gain to the input audio. |
| 342 | for (float& x : frame) { |
| 343 | x *= gain; |
| 344 | } |
| 345 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
| 346 | agc2.Process(kSpeechProbabilities[j], &audio_buffer); |
| 347 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 348 | &audio_buffer_reference); |
| 349 | agc2_reference.Process(absl::nullopt, &audio_buffer_reference); |
| 350 | |
| 351 | // Check the output buffers. |
| 352 | for (int i = 0; i < kStereo; ++i) { |
| 353 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 354 | all_samples_zero &= |
| 355 | fabs(audio_buffer.channels_const()[i][j]) < kEpsilon; |
| 356 | EXPECT_FLOAT_EQ(audio_buffer.channels_const()[i][j], |
| 357 | audio_buffer_reference.channels_const()[i][j]); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | EXPECT_FALSE(all_samples_zero); |
| 362 | } |
| 363 | |
| 364 | // Processes a test audio file and checks that the injected speech probability |
| 365 | // is not ignored when the internal VAD is not used. |
| 366 | TEST(GainController2, |
| 367 | CheckInjectedVadProbabilityUsedWithAdaptiveDigitalController) { |
| 368 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 369 | constexpr int kStereo = 2; |
| 370 | |
| 371 | // Create AGC2 enabling only the adaptive digital controller. |
| 372 | Agc2Config config; |
| 373 | config.fixed_digital.gain_db = 0.0f; |
| 374 | config.adaptive_digital.enabled = true; |
| 375 | GainController2 agc2(config, kSampleRateHz, kStereo, |
| 376 | /*use_internal_vad=*/false); |
| 377 | GainController2 agc2_reference(config, kSampleRateHz, kStereo, |
| 378 | /*use_internal_vad=*/true); |
| 379 | |
| 380 | test::InputAudioFile input_file( |
| 381 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 382 | /*loop_at_end=*/true); |
| 383 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 384 | |
| 385 | // Init buffers. |
| 386 | constexpr int kFrameDurationMs = 10; |
| 387 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 388 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 389 | kSampleRateHz, kStereo); |
| 390 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 391 | kStereo, kSampleRateHz, kStereo); |
| 392 | // Simulate. |
| 393 | constexpr float kGainDb = -6.0f; |
| 394 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 395 | constexpr int kDurationMs = 10000; |
| 396 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 397 | constexpr float kSpeechProbabilities[] = {1.0f, 0.3f}; |
| 398 | constexpr float kEpsilon = 0.0001f; |
| 399 | bool all_samples_zero = true; |
| 400 | bool all_samples_equal = true; |
| 401 | for (int i = 0, j = 0; i < kNumFramesToProcess; ++i, j = 1 - j) { |
| 402 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 403 | stream_config.num_channels(), &input_file, |
| 404 | frame); |
| 405 | // Apply a fixed gain to the input audio. |
| 406 | for (float& x : frame) { |
| 407 | x *= gain; |
| 408 | } |
| 409 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
| 410 | agc2.Process(kSpeechProbabilities[j], &audio_buffer); |
| 411 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 412 | &audio_buffer_reference); |
| 413 | agc2_reference.Process(absl::nullopt, &audio_buffer_reference); |
| 414 | // Check the output buffers. |
| 415 | for (int i = 0; i < kStereo; ++i) { |
| 416 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 417 | all_samples_zero &= |
| 418 | fabs(audio_buffer.channels_const()[i][j]) < kEpsilon; |
| 419 | all_samples_equal &= |
| 420 | fabs(audio_buffer.channels_const()[i][j] - |
| 421 | audio_buffer_reference.channels_const()[i][j]) < kEpsilon; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | EXPECT_FALSE(all_samples_zero); |
| 426 | EXPECT_FALSE(all_samples_equal); |
| 427 | } |
| 428 | |
| 429 | // Processes a test audio file and checks that the output is equal when |
| 430 | // an injected speech probability from `VoiceActivityDetectorWrapper` and |
| 431 | // the speech probability computed by the internal VAD are the same. |
| 432 | TEST(GainController2, |
| 433 | CheckEqualResultFromInjectedVadProbabilityWithAdaptiveDigitalController) { |
| 434 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 435 | constexpr int kStereo = 2; |
| 436 | |
| 437 | // Create AGC2 enabling only the adaptive digital controller. |
| 438 | Agc2Config config; |
| 439 | config.fixed_digital.gain_db = 0.0f; |
| 440 | config.adaptive_digital.enabled = true; |
| 441 | GainController2 agc2(config, kSampleRateHz, kStereo, |
| 442 | /*use_internal_vad=*/false); |
| 443 | GainController2 agc2_reference(config, kSampleRateHz, kStereo, |
| 444 | /*use_internal_vad=*/true); |
| 445 | VoiceActivityDetectorWrapper vad(config.adaptive_digital.vad_reset_period_ms, |
| 446 | GetAvailableCpuFeatures(), kSampleRateHz); |
| 447 | test::InputAudioFile input_file( |
| 448 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 449 | /*loop_at_end=*/true); |
| 450 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 451 | |
| 452 | // Init buffers. |
| 453 | constexpr int kFrameDurationMs = 10; |
| 454 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 455 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 456 | kSampleRateHz, kStereo); |
| 457 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 458 | kStereo, kSampleRateHz, kStereo); |
| 459 | |
| 460 | // Simulate. |
| 461 | constexpr float kGainDb = -6.0f; |
| 462 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 463 | constexpr int kDurationMs = 10000; |
| 464 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 465 | for (int i = 0; i < kNumFramesToProcess; ++i) { |
| 466 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 467 | stream_config.num_channels(), &input_file, |
| 468 | frame); |
| 469 | // Apply a fixed gain to the input audio. |
| 470 | for (float& x : frame) { |
| 471 | x *= gain; |
| 472 | } |
| 473 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 474 | &audio_buffer_reference); |
| 475 | agc2_reference.Process(absl::nullopt, &audio_buffer_reference); |
| 476 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
| 477 | agc2.Process(vad.Analyze(AudioFrameView<const float>( |
| 478 | audio_buffer.channels(), audio_buffer.num_channels(), |
| 479 | audio_buffer.num_frames())), |
| 480 | &audio_buffer); |
| 481 | // Check the output buffer. |
| 482 | for (int i = 0; i < kStereo; ++i) { |
| 483 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 484 | EXPECT_FLOAT_EQ(audio_buffer.channels_const()[i][j], |
| 485 | audio_buffer_reference.channels_const()[i][j]); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 491 | } // namespace test |
| 492 | } // namespace webrtc |