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