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