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> |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 19 | #include "modules/audio_processing/agc2/agc2_testing_common.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 21 | #include "modules/audio_processing/test/audio_buffer_tools.h" |
| 22 | #include "modules/audio_processing/test/bitexactness_tools.h" |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "test/gtest.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 28 | namespace { |
| 29 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 30 | // Sets all the samples in `ab` to `value`. |
| 31 | void SetAudioBufferSamples(float value, AudioBuffer& ab) { |
| 32 | for (size_t k = 0; k < ab.num_channels(); ++k) { |
| 33 | std::fill(ab.channels()[k], ab.channels()[k] + ab.num_frames(), value); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 37 | float RunAgc2WithConstantInput(GainController2& agc2, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 38 | float input_level, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 39 | int num_frames, |
| 40 | int sample_rate_hz) { |
| 41 | const int num_samples = rtc::CheckedDivExact(sample_rate_hz, 100); |
| 42 | 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] | 43 | |
| 44 | // Give time to the level estimator to converge. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 45 | for (int i = 0; i < num_frames + 1; ++i) { |
| 46 | SetAudioBufferSamples(input_level, ab); |
| 47 | agc2.Process(&ab); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Return the last sample from the last processed frame. |
Per Ã…hgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 51 | return ab.channels()[0][num_samples - 1]; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | AudioProcessing::Config::GainController2 CreateAgc2FixedDigitalModeConfig( |
| 55 | float fixed_gain_db) { |
| 56 | AudioProcessing::Config::GainController2 config; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 57 | config.adaptive_digital.enabled = false; |
| 58 | config.fixed_digital.gain_db = fixed_gain_db; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 59 | EXPECT_TRUE(GainController2::Validate(config)); |
| 60 | return config; |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<GainController2> CreateAgc2FixedDigitalMode( |
| 64 | float fixed_gain_db, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 65 | int sample_rate_hz) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 66 | auto agc2 = std::make_unique<GainController2>(); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 67 | agc2->ApplyConfig(CreateAgc2FixedDigitalModeConfig(fixed_gain_db)); |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 68 | agc2->Initialize(sample_rate_hz, /*num_channels=*/1); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 69 | return agc2; |
| 70 | } |
| 71 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 72 | } // namespace |
| 73 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 74 | TEST(GainController2, CheckDefaultConfig) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 75 | AudioProcessing::Config::GainController2 config; |
| 76 | EXPECT_TRUE(GainController2::Validate(config)); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 79 | TEST(GainController2, CheckFixedDigitalConfig) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 80 | AudioProcessing::Config::GainController2 config; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 81 | // Attenuation is not allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 82 | config.fixed_digital.gain_db = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 83 | EXPECT_FALSE(GainController2::Validate(config)); |
| 84 | // No gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 85 | config.fixed_digital.gain_db = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 86 | EXPECT_TRUE(GainController2::Validate(config)); |
| 87 | // Positive gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 88 | config.fixed_digital.gain_db = 15.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 89 | EXPECT_TRUE(GainController2::Validate(config)); |
| 90 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 91 | |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame^] | 92 | TEST(GainController2, CheckHeadroomDb) { |
| 93 | AudioProcessing::Config::GainController2 config; |
| 94 | config.adaptive_digital.headroom_db = -1.0f; |
| 95 | EXPECT_FALSE(GainController2::Validate(config)); |
| 96 | config.adaptive_digital.headroom_db = 0.0f; |
| 97 | EXPECT_TRUE(GainController2::Validate(config)); |
| 98 | config.adaptive_digital.headroom_db = 5.0f; |
| 99 | EXPECT_TRUE(GainController2::Validate(config)); |
| 100 | } |
| 101 | |
| 102 | TEST(GainController2, CheckMaxGainDb) { |
| 103 | AudioProcessing::Config::GainController2 config; |
| 104 | config.adaptive_digital.max_gain_db = -1.0f; |
| 105 | EXPECT_FALSE(GainController2::Validate(config)); |
| 106 | config.adaptive_digital.max_gain_db = 0.0f; |
| 107 | EXPECT_FALSE(GainController2::Validate(config)); |
| 108 | config.adaptive_digital.max_gain_db = 5.0f; |
| 109 | EXPECT_TRUE(GainController2::Validate(config)); |
| 110 | } |
| 111 | |
| 112 | TEST(GainController2, CheckInitialGainDb) { |
| 113 | AudioProcessing::Config::GainController2 config; |
| 114 | config.adaptive_digital.initial_gain_db = -1.0f; |
| 115 | EXPECT_FALSE(GainController2::Validate(config)); |
| 116 | config.adaptive_digital.initial_gain_db = 0.0f; |
| 117 | EXPECT_TRUE(GainController2::Validate(config)); |
| 118 | config.adaptive_digital.initial_gain_db = 5.0f; |
| 119 | EXPECT_TRUE(GainController2::Validate(config)); |
| 120 | } |
| 121 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 122 | TEST(GainController2, CheckAdaptiveDigitalMaxGainChangeSpeedConfig) { |
| 123 | AudioProcessing::Config::GainController2 config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 124 | config.adaptive_digital.max_gain_change_db_per_second = -1.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 125 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 126 | config.adaptive_digital.max_gain_change_db_per_second = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 127 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 128 | config.adaptive_digital.max_gain_change_db_per_second = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 129 | EXPECT_TRUE(GainController2::Validate(config)); |
| 130 | } |
| 131 | |
| 132 | TEST(GainController2, CheckAdaptiveDigitalMaxOutputNoiseLevelConfig) { |
| 133 | AudioProcessing::Config::GainController2 config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 134 | config.adaptive_digital.max_output_noise_level_dbfs = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 135 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 136 | config.adaptive_digital.max_output_noise_level_dbfs = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 137 | EXPECT_TRUE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 138 | config.adaptive_digital.max_output_noise_level_dbfs = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 139 | EXPECT_TRUE(GainController2::Validate(config)); |
| 140 | } |
| 141 | |
| 142 | // Checks that the default config is applied. |
| 143 | TEST(GainController2, ApplyDefaultConfig) { |
| 144 | auto gain_controller2 = std::make_unique<GainController2>(); |
| 145 | AudioProcessing::Config::GainController2 config; |
| 146 | gain_controller2->ApplyConfig(config); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 149 | TEST(GainController2FixedDigital, GainShouldChangeOnSetGain) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 150 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 151 | constexpr size_t kNumFrames = 5; |
| 152 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 153 | constexpr float kGain0Db = 0.0f; |
| 154 | constexpr float kGain20Db = 20.0f; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 155 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 156 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGain0Db, kSampleRateHz); |
| 157 | |
| 158 | // Signal level is unchanged with 0 db gain. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 159 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 160 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 161 | kInputLevel); |
| 162 | |
| 163 | // +20 db should increase signal by a factor of 10. |
| 164 | agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGain20Db)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 165 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 166 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 167 | kInputLevel * 10); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 170 | TEST(GainController2FixedDigital, ChangeFixedGainShouldBeFastAndTimeInvariant) { |
| 171 | // Number of frames required for the fixed gain controller to adapt on the |
| 172 | // input signal when the gain changes. |
| 173 | constexpr size_t kNumFrames = 5; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 174 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 175 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 176 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 177 | constexpr float kGainDbLow = 0.0f; |
| 178 | constexpr float kGainDbHigh = 25.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 179 | static_assert(kGainDbLow < kGainDbHigh, ""); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 180 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 181 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGainDbLow, kSampleRateHz); |
| 182 | |
| 183 | // Start with a lower gain. |
| 184 | const float output_level_pre = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 185 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 186 | |
| 187 | // Increase gain. |
| 188 | agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGainDbHigh)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 189 | static_cast<void>(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 190 | kNumFrames, kSampleRateHz)); |
| 191 | |
| 192 | // Back to the lower gain. |
| 193 | agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGainDbLow)); |
| 194 | const float output_level_post = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 195 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 196 | |
| 197 | EXPECT_EQ(output_level_pre, output_level_post); |
| 198 | } |
| 199 | |
| 200 | struct FixedDigitalTestParams { |
| 201 | FixedDigitalTestParams(float gain_db_min, |
| 202 | float gain_db_max, |
| 203 | size_t sample_rate, |
| 204 | bool saturation_expected) |
| 205 | : gain_db_min(gain_db_min), |
| 206 | gain_db_max(gain_db_max), |
| 207 | sample_rate(sample_rate), |
| 208 | saturation_expected(saturation_expected) {} |
| 209 | float gain_db_min; |
| 210 | float gain_db_max; |
| 211 | size_t sample_rate; |
| 212 | bool saturation_expected; |
| 213 | }; |
| 214 | |
| 215 | class FixedDigitalTest |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 216 | : public ::testing::Test, |
| 217 | public ::testing::WithParamInterface<FixedDigitalTestParams> {}; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 218 | |
| 219 | TEST_P(FixedDigitalTest, CheckSaturationBehaviorWithLimiter) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 220 | const float kInputLevel = 32767.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 221 | const size_t kNumFrames = 5; |
| 222 | |
| 223 | const auto params = GetParam(); |
| 224 | |
| 225 | const auto gains_db = |
| 226 | test::LinSpace(params.gain_db_min, params.gain_db_max, 10); |
| 227 | for (const auto gain_db : gains_db) { |
| 228 | SCOPED_TRACE(std::to_string(gain_db)); |
| 229 | auto agc2_fixed = CreateAgc2FixedDigitalMode(gain_db, params.sample_rate); |
| 230 | const float processed_sample = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 231 | *agc2_fixed, kInputLevel, kNumFrames, params.sample_rate); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 232 | if (params.saturation_expected) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 233 | EXPECT_FLOAT_EQ(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 234 | } else { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 235 | EXPECT_LT(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 236 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 237 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 240 | static_assert(test::kLimiterMaxInputLevelDbFs < 10, ""); |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 241 | INSTANTIATE_TEST_SUITE_P( |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 242 | GainController2, |
| 243 | FixedDigitalTest, |
| 244 | ::testing::Values( |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 245 | // When gain < `test::kLimiterMaxInputLevelDbFs`, the limiter will not |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 246 | // saturate the signal (at any sample rate). |
| 247 | FixedDigitalTestParams(0.1f, |
| 248 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 249 | 8000, |
| 250 | false), |
| 251 | FixedDigitalTestParams(0.1, |
| 252 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 253 | 48000, |
| 254 | false), |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 255 | // When gain > `test::kLimiterMaxInputLevelDbFs`, the limiter will |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 256 | // saturate the signal (at any sample rate). |
| 257 | FixedDigitalTestParams(test::kLimiterMaxInputLevelDbFs + 0.01f, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 258 | 10.0f, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 259 | 8000, |
| 260 | true), |
| 261 | FixedDigitalTestParams(test::kLimiterMaxInputLevelDbFs + 0.01f, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 262 | 10.0f, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 263 | 48000, |
| 264 | true))); |
| 265 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 266 | // Processes a test audio file and checks that the gain applied at the end of |
| 267 | // the recording is close to the expected value. |
| 268 | TEST(GainController2, CheckFinalGainWithAdaptiveDigitalController) { |
| 269 | // Create AGC2 enabling only the adaptive digital controller. |
| 270 | GainController2 agc2; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 271 | AudioProcessing::Config::GainController2 config; |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 272 | config.fixed_digital.gain_db = 0.0f; |
Alessio Bazzica | 8da7b35 | 2018-11-21 10:50:58 +0100 | [diff] [blame] | 273 | config.adaptive_digital.enabled = true; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 274 | agc2.ApplyConfig(config); |
| 275 | |
| 276 | // The input audio is a 48k stereo recording. |
| 277 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 278 | constexpr int kStereo = 2; |
| 279 | test::InputAudioFile input_file( |
| 280 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 281 | /*loop_at_end=*/true); |
| 282 | const StreamConfig stream_config(kSampleRateHz, kStereo, |
| 283 | /*has_keyboard=*/false); |
| 284 | |
| 285 | // Initialize AGC2 for the used input. |
| 286 | agc2.Initialize(kSampleRateHz, kStereo); |
| 287 | |
| 288 | // Init buffers. |
| 289 | constexpr int kFrameDurationMs = 10; |
| 290 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 291 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 292 | kSampleRateHz, kStereo); |
| 293 | |
| 294 | // Simulate. |
| 295 | constexpr float kGainDb = -6.0f; |
| 296 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 297 | constexpr int kDurationMs = 10000; |
| 298 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 299 | for (int i = 0; i < kNumFramesToProcess; ++i) { |
| 300 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 301 | stream_config.num_channels(), &input_file, |
| 302 | frame); |
| 303 | // Apply a fixed gain to the input audio. |
| 304 | for (float& x : frame) |
| 305 | x *= gain; |
| 306 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
| 307 | // Process. |
| 308 | agc2.Process(&audio_buffer); |
| 309 | } |
| 310 | |
| 311 | // Estimate the applied gain by processing a probing frame. |
| 312 | SetAudioBufferSamples(/*value=*/1.0f, audio_buffer); |
| 313 | agc2.Process(&audio_buffer); |
| 314 | const float applied_gain_db = |
| 315 | 20.0f * std::log10(audio_buffer.channels_const()[0][0]); |
| 316 | |
| 317 | constexpr float kExpectedGainDb = 5.6f; |
| 318 | constexpr float kToleranceDb = 0.3f; |
| 319 | EXPECT_NEAR(applied_gain_db, kExpectedGainDb, kToleranceDb); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 320 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 321 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 322 | } // namespace test |
| 323 | } // namespace webrtc |