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" |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 25 | #include "test/gmock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "test/gtest.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | namespace test { |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 32 | using ::testing::Eq; |
| 33 | using ::testing::Optional; |
| 34 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 35 | using Agc2Config = AudioProcessing::Config::GainController2; |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 36 | using InputVolumeControllerConfig = InputVolumeController::Config; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 37 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 38 | // Sets all the samples in `ab` to `value`. |
| 39 | void SetAudioBufferSamples(float value, AudioBuffer& ab) { |
| 40 | for (size_t k = 0; k < ab.num_channels(); ++k) { |
| 41 | std::fill(ab.channels()[k], ab.channels()[k] + ab.num_frames(), value); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 45 | float RunAgc2WithConstantInput(GainController2& agc2, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 46 | float input_level, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 47 | int num_frames, |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 48 | int sample_rate_hz, |
| 49 | int num_channels = 1, |
| 50 | int applied_initial_volume = 0) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 51 | const int num_samples = rtc::CheckedDivExact(sample_rate_hz, 100); |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 52 | AudioBuffer ab(sample_rate_hz, num_channels, sample_rate_hz, num_channels, |
| 53 | sample_rate_hz, num_channels); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 54 | |
| 55 | // Give time to the level estimator to converge. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 56 | for (int i = 0; i < num_frames + 1; ++i) { |
| 57 | SetAudioBufferSamples(input_level, ab); |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 58 | const auto applied_volume = agc2.GetRecommendedInputVolume(); |
| 59 | agc2.Analyze(i > 0 && applied_volume.has_value() ? *applied_volume |
| 60 | : applied_initial_volume, |
| 61 | ab); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 62 | agc2.Process(/*speech_probability=*/absl::nullopt, |
| 63 | /*input_volume_changed=*/false, &ab); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Return the last sample from the last processed frame. |
Per Ã…hgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 67 | return ab.channels()[0][num_samples - 1]; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 70 | std::unique_ptr<GainController2> CreateAgc2FixedDigitalMode( |
| 71 | float fixed_gain_db, |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 72 | int sample_rate_hz) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 73 | Agc2Config config; |
| 74 | config.adaptive_digital.enabled = false; |
| 75 | config.fixed_digital.gain_db = fixed_gain_db; |
| 76 | EXPECT_TRUE(GainController2::Validate(config)); |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 77 | return std::make_unique<GainController2>( |
| 78 | config, InputVolumeControllerConfig{}, sample_rate_hz, |
| 79 | /*num_channels=*/1, |
| 80 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 83 | constexpr InputVolumeControllerConfig kTestInputVolumeControllerConfig{ |
| 84 | .clipped_level_min = 20, |
| 85 | .clipped_level_step = 30, |
| 86 | .clipped_ratio_threshold = 0.4, |
| 87 | .clipped_wait_frames = 50, |
| 88 | .enable_clipping_predictor = true, |
| 89 | .target_range_max_dbfs = -6, |
| 90 | .target_range_min_dbfs = -70, |
| 91 | .update_input_volume_wait_frames = 100, |
| 92 | .speech_probability_threshold = 0.9, |
| 93 | .speech_ratio_threshold = 1, |
| 94 | }; |
| 95 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 96 | } // namespace |
| 97 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 98 | TEST(GainController2, CheckDefaultConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 99 | Agc2Config config; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 100 | EXPECT_TRUE(GainController2::Validate(config)); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 103 | TEST(GainController2, CheckFixedDigitalConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 104 | Agc2Config config; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 105 | // Attenuation is not allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 106 | config.fixed_digital.gain_db = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 107 | EXPECT_FALSE(GainController2::Validate(config)); |
| 108 | // No gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 109 | config.fixed_digital.gain_db = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 110 | EXPECT_TRUE(GainController2::Validate(config)); |
| 111 | // Positive gain is allowed. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 112 | config.fixed_digital.gain_db = 15.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 113 | EXPECT_TRUE(GainController2::Validate(config)); |
| 114 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 115 | |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 116 | TEST(GainController2, CheckHeadroomDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 117 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 118 | config.adaptive_digital.headroom_db = -1.0f; |
| 119 | EXPECT_FALSE(GainController2::Validate(config)); |
| 120 | config.adaptive_digital.headroom_db = 0.0f; |
| 121 | EXPECT_TRUE(GainController2::Validate(config)); |
| 122 | config.adaptive_digital.headroom_db = 5.0f; |
| 123 | EXPECT_TRUE(GainController2::Validate(config)); |
| 124 | } |
| 125 | |
| 126 | TEST(GainController2, CheckMaxGainDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 127 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 128 | config.adaptive_digital.max_gain_db = -1.0f; |
| 129 | EXPECT_FALSE(GainController2::Validate(config)); |
| 130 | config.adaptive_digital.max_gain_db = 0.0f; |
| 131 | EXPECT_FALSE(GainController2::Validate(config)); |
| 132 | config.adaptive_digital.max_gain_db = 5.0f; |
| 133 | EXPECT_TRUE(GainController2::Validate(config)); |
| 134 | } |
| 135 | |
| 136 | TEST(GainController2, CheckInitialGainDb) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 137 | Agc2Config config; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 138 | config.adaptive_digital.initial_gain_db = -1.0f; |
| 139 | EXPECT_FALSE(GainController2::Validate(config)); |
| 140 | config.adaptive_digital.initial_gain_db = 0.0f; |
| 141 | EXPECT_TRUE(GainController2::Validate(config)); |
| 142 | config.adaptive_digital.initial_gain_db = 5.0f; |
| 143 | EXPECT_TRUE(GainController2::Validate(config)); |
| 144 | } |
| 145 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 146 | TEST(GainController2, CheckAdaptiveDigitalMaxGainChangeSpeedConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 147 | Agc2Config config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 148 | config.adaptive_digital.max_gain_change_db_per_second = -1.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 149 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 150 | config.adaptive_digital.max_gain_change_db_per_second = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 151 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 152 | config.adaptive_digital.max_gain_change_db_per_second = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 153 | EXPECT_TRUE(GainController2::Validate(config)); |
| 154 | } |
| 155 | |
| 156 | TEST(GainController2, CheckAdaptiveDigitalMaxOutputNoiseLevelConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 157 | Agc2Config config; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 158 | config.adaptive_digital.max_output_noise_level_dbfs = 5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 159 | EXPECT_FALSE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 160 | config.adaptive_digital.max_output_noise_level_dbfs = 0.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 161 | EXPECT_TRUE(GainController2::Validate(config)); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 162 | config.adaptive_digital.max_output_noise_level_dbfs = -5.0f; |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 163 | EXPECT_TRUE(GainController2::Validate(config)); |
| 164 | } |
| 165 | |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 166 | TEST(GainController2, |
| 167 | CheckGetRecommendedInputVolumeWhenInputVolumeControllerNotEnabled) { |
| 168 | constexpr float kHighInputLevel = 32767.0f; |
| 169 | constexpr float kLowInputLevel = 1000.0f; |
| 170 | constexpr int kInitialInputVolume = 100; |
| 171 | constexpr int kNumChannels = 2; |
| 172 | constexpr int kNumFrames = 5; |
| 173 | constexpr int kSampleRateHz = 16000; |
| 174 | |
| 175 | Agc2Config config; |
| 176 | config.input_volume_controller.enabled = false; |
Hanna Silen | 27fed45 | 2022-11-22 15:00:58 +0100 | [diff] [blame] | 177 | |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 178 | auto gain_controller = std::make_unique<GainController2>( |
| 179 | config, InputVolumeControllerConfig{}, kSampleRateHz, kNumChannels, |
| 180 | /*use_internal_vad=*/true); |
| 181 | |
| 182 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 183 | |
| 184 | // Run AGC for a signal with no clipping or detected speech. |
| 185 | RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames, |
| 186 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 187 | |
| 188 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 189 | |
| 190 | // Run AGC for a signal with clipping. |
| 191 | RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames, |
| 192 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 193 | |
| 194 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 195 | } |
| 196 | |
| 197 | TEST( |
| 198 | GainController2, |
| 199 | CheckGetRecommendedInputVolumeWhenInputVolumeControllerNotEnabledAndSpecificConfigUsed) { |
| 200 | constexpr float kHighInputLevel = 32767.0f; |
| 201 | constexpr float kLowInputLevel = 1000.0f; |
| 202 | constexpr int kInitialInputVolume = 100; |
| 203 | constexpr int kNumChannels = 2; |
| 204 | constexpr int kNumFrames = 5; |
| 205 | constexpr int kSampleRateHz = 16000; |
| 206 | |
| 207 | Agc2Config config; |
| 208 | config.input_volume_controller.enabled = false; |
| 209 | |
| 210 | auto gain_controller = std::make_unique<GainController2>( |
| 211 | config, kTestInputVolumeControllerConfig, kSampleRateHz, kNumChannels, |
| 212 | /*use_internal_vad=*/true); |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 213 | |
| 214 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 215 | |
| 216 | // Run AGC for a signal with no clipping or detected speech. |
| 217 | RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames, |
| 218 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 219 | |
| 220 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 221 | |
| 222 | // Run AGC for a signal with clipping. |
| 223 | RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames, |
| 224 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 225 | |
| 226 | EXPECT_FALSE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 227 | } |
| 228 | |
| 229 | TEST(GainController2, |
| 230 | CheckGetRecommendedInputVolumeWhenInputVolumeControllerEnabled) { |
| 231 | constexpr float kHighInputLevel = 32767.0f; |
| 232 | constexpr float kLowInputLevel = 1000.0f; |
| 233 | constexpr int kInitialInputVolume = 100; |
| 234 | constexpr int kNumChannels = 2; |
| 235 | constexpr int kNumFrames = 5; |
| 236 | constexpr int kSampleRateHz = 16000; |
| 237 | |
| 238 | Agc2Config config; |
| 239 | config.input_volume_controller.enabled = true; |
Hanna Silen | 27fed45 | 2022-11-22 15:00:58 +0100 | [diff] [blame] | 240 | config.adaptive_digital.enabled = true; |
| 241 | |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 242 | auto gain_controller = std::make_unique<GainController2>( |
| 243 | config, InputVolumeControllerConfig{}, kSampleRateHz, kNumChannels, |
| 244 | /*use_internal_vad=*/true); |
| 245 | |
| 246 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 247 | |
| 248 | // Run AGC for a signal with no clipping or detected speech. |
| 249 | RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames, |
| 250 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 251 | |
| 252 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 253 | |
| 254 | // Run AGC for a signal with clipping. |
| 255 | RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames, |
| 256 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 257 | |
| 258 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 259 | } |
| 260 | |
| 261 | TEST( |
| 262 | GainController2, |
| 263 | CheckGetRecommendedInputVolumeWhenInputVolumeControllerEnabledAndSpecificConfigUsed) { |
| 264 | constexpr float kHighInputLevel = 32767.0f; |
| 265 | constexpr float kLowInputLevel = 1000.0f; |
| 266 | constexpr int kInitialInputVolume = 100; |
| 267 | constexpr int kNumChannels = 2; |
| 268 | constexpr int kNumFrames = 5; |
| 269 | constexpr int kSampleRateHz = 16000; |
| 270 | |
| 271 | Agc2Config config; |
| 272 | config.input_volume_controller.enabled = true; |
| 273 | config.adaptive_digital.enabled = true; |
| 274 | |
| 275 | auto gain_controller = std::make_unique<GainController2>( |
| 276 | config, kTestInputVolumeControllerConfig, kSampleRateHz, kNumChannels, |
| 277 | /*use_internal_vad=*/true); |
Hanna Silen | d7cfbe3 | 2022-11-02 19:12:20 +0100 | [diff] [blame] | 278 | |
| 279 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 280 | |
| 281 | // Run AGC for a signal with no clipping or detected speech. |
| 282 | RunAgc2WithConstantInput(*gain_controller, kLowInputLevel, kNumFrames, |
| 283 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 284 | |
| 285 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 286 | |
| 287 | // Run AGC for a signal with clipping. |
| 288 | RunAgc2WithConstantInput(*gain_controller, kHighInputLevel, kNumFrames, |
| 289 | kSampleRateHz, kNumChannels, kInitialInputVolume); |
| 290 | |
| 291 | EXPECT_TRUE(gain_controller->GetRecommendedInputVolume().has_value()); |
| 292 | } |
| 293 | |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 294 | // Checks that the default config is applied. |
| 295 | TEST(GainController2, ApplyDefaultConfig) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 296 | auto gain_controller2 = std::make_unique<GainController2>( |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 297 | Agc2Config{}, InputVolumeControllerConfig{}, |
| 298 | /*sample_rate_hz=*/16000, /*num_channels=*/2, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 299 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 300 | EXPECT_TRUE(gain_controller2.get()); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 303 | TEST(GainController2FixedDigital, GainShouldChangeOnSetGain) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 304 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 305 | constexpr size_t kNumFrames = 5; |
| 306 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 307 | constexpr float kGain0Db = 0.0f; |
| 308 | constexpr float kGain20Db = 20.0f; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 309 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 310 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGain0Db, kSampleRateHz); |
| 311 | |
| 312 | // Signal level is unchanged with 0 db gain. |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 313 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 314 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 315 | kInputLevel); |
| 316 | |
| 317 | // +20 db should increase signal by a factor of 10. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 318 | agc2_fixed->SetFixedGainDb(kGain20Db); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 319 | EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames, |
| 320 | kSampleRateHz), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 321 | kInputLevel * 10); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 324 | TEST(GainController2FixedDigital, ChangeFixedGainShouldBeFastAndTimeInvariant) { |
| 325 | // Number of frames required for the fixed gain controller to adapt on the |
| 326 | // input signal when the gain changes. |
| 327 | constexpr size_t kNumFrames = 5; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 328 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 329 | constexpr float kInputLevel = 1000.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 330 | constexpr size_t kSampleRateHz = 8000; |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 331 | constexpr float kGainDbLow = 0.0f; |
| 332 | constexpr float kGainDbHigh = 25.0f; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 333 | static_assert(kGainDbLow < kGainDbHigh, ""); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 334 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 335 | auto agc2_fixed = CreateAgc2FixedDigitalMode(kGainDbLow, kSampleRateHz); |
| 336 | |
| 337 | // Start with a lower gain. |
| 338 | const float output_level_pre = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 339 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 340 | |
| 341 | // Increase gain. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 342 | agc2_fixed->SetFixedGainDb(kGainDbHigh); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 343 | static_cast<void>(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 344 | kNumFrames, kSampleRateHz)); |
| 345 | |
| 346 | // Back to the lower gain. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 347 | agc2_fixed->SetFixedGainDb(kGainDbLow); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 348 | const float output_level_post = RunAgc2WithConstantInput( |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 349 | *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 350 | |
| 351 | EXPECT_EQ(output_level_pre, output_level_post); |
| 352 | } |
| 353 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 354 | class FixedDigitalTest |
| 355 | : public ::testing::TestWithParam<std::tuple<float, float, int, bool>> { |
| 356 | protected: |
| 357 | float gain_db_min() const { return std::get<0>(GetParam()); } |
| 358 | float gain_db_max() const { return std::get<1>(GetParam()); } |
| 359 | int sample_rate_hz() const { return std::get<2>(GetParam()); } |
| 360 | bool saturation_expected() const { return std::get<3>(GetParam()); } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 361 | }; |
| 362 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 363 | TEST_P(FixedDigitalTest, CheckSaturationBehaviorWithLimiter) { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 364 | for (const float gain_db : test::LinSpace(gain_db_min(), gain_db_max(), 10)) { |
| 365 | SCOPED_TRACE(gain_db); |
| 366 | auto agc2_fixed = CreateAgc2FixedDigitalMode(gain_db, sample_rate_hz()); |
| 367 | const float processed_sample = |
| 368 | RunAgc2WithConstantInput(*agc2_fixed, /*input_level=*/32767.0f, |
| 369 | /*num_frames=*/5, sample_rate_hz()); |
| 370 | if (saturation_expected()) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 371 | EXPECT_FLOAT_EQ(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 372 | } else { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 373 | EXPECT_LT(processed_sample, 32767.0f); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 374 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 375 | } |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 378 | static_assert(test::kLimiterMaxInputLevelDbFs < 10, ""); |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 379 | INSTANTIATE_TEST_SUITE_P( |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 380 | GainController2, |
| 381 | FixedDigitalTest, |
| 382 | ::testing::Values( |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 383 | // When gain < `test::kLimiterMaxInputLevelDbFs`, the limiter will not |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 384 | // saturate the signal (at any sample rate). |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 385 | std::make_tuple(0.1f, |
| 386 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 387 | 8000, |
| 388 | false), |
| 389 | std::make_tuple(0.1, |
| 390 | test::kLimiterMaxInputLevelDbFs - 0.01f, |
| 391 | 48000, |
| 392 | false), |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 393 | // When gain > `test::kLimiterMaxInputLevelDbFs`, the limiter will |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 394 | // saturate the signal (at any sample rate). |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 395 | std::make_tuple(test::kLimiterMaxInputLevelDbFs + 0.01f, |
| 396 | 10.0f, |
| 397 | 8000, |
| 398 | true), |
| 399 | std::make_tuple(test::kLimiterMaxInputLevelDbFs + 0.01f, |
| 400 | 10.0f, |
| 401 | 48000, |
| 402 | true))); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 403 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 404 | // Processes a test audio file and checks that the gain applied at the end of |
| 405 | // the recording is close to the expected value. |
| 406 | TEST(GainController2, CheckFinalGainWithAdaptiveDigitalController) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 407 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 408 | constexpr int kStereo = 2; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 409 | |
| 410 | // Create AGC2 enabling only the adaptive digital controller. |
| 411 | Agc2Config config; |
| 412 | config.fixed_digital.gain_db = 0.0f; |
| 413 | config.adaptive_digital.enabled = true; |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 414 | GainController2 agc2(config, /*input_volume_controller_config=*/{}, |
| 415 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 416 | /*use_internal_vad=*/true); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 417 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 418 | test::InputAudioFile input_file( |
| 419 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 420 | /*loop_at_end=*/true); |
Henrik Lundin | 64253a9 | 2022-02-04 09:02:48 +0000 | [diff] [blame] | 421 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 422 | |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 423 | // Init buffers. |
| 424 | constexpr int kFrameDurationMs = 10; |
| 425 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 426 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 427 | kSampleRateHz, kStereo); |
| 428 | |
| 429 | // Simulate. |
| 430 | constexpr float kGainDb = -6.0f; |
| 431 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 432 | constexpr int kDurationMs = 10000; |
| 433 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 434 | for (int i = 0; i < kNumFramesToProcess; ++i) { |
| 435 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 436 | stream_config.num_channels(), &input_file, |
| 437 | frame); |
| 438 | // Apply a fixed gain to the input audio. |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 439 | for (float& x : frame) { |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 440 | x *= gain; |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 441 | } |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 442 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 443 | agc2.Process(/*speech_probability=*/absl::nullopt, |
| 444 | /*input_volume_changed=*/false, &audio_buffer); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Estimate the applied gain by processing a probing frame. |
| 448 | SetAudioBufferSamples(/*value=*/1.0f, audio_buffer); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 449 | agc2.Process(/*speech_probability=*/absl::nullopt, |
| 450 | /*input_volume_changed=*/false, &audio_buffer); |
Alessio Bazzica | 6ee9734 | 2021-09-27 17:06:40 +0200 | [diff] [blame] | 451 | const float applied_gain_db = |
| 452 | 20.0f * std::log10(audio_buffer.channels_const()[0][0]); |
| 453 | |
| 454 | constexpr float kExpectedGainDb = 5.6f; |
| 455 | constexpr float kToleranceDb = 0.3f; |
| 456 | EXPECT_NEAR(applied_gain_db, kExpectedGainDb, kToleranceDb); |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 457 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 458 | |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 459 | // Processes a test audio file and checks that the injected speech probability |
| 460 | // is ignored when the internal VAD is used. |
| 461 | TEST(GainController2, |
| 462 | CheckInjectedVadProbabilityNotUsedWithAdaptiveDigitalController) { |
| 463 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 464 | constexpr int kStereo = 2; |
| 465 | |
| 466 | // Create AGC2 enabling only the adaptive digital controller. |
| 467 | Agc2Config config; |
| 468 | config.fixed_digital.gain_db = 0.0f; |
| 469 | config.adaptive_digital.enabled = true; |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 470 | GainController2 agc2(config, /*input_volume_controller_config=*/{}, |
| 471 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 472 | /*use_internal_vad=*/true); |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 473 | GainController2 agc2_reference(config, /*input_volume_controller_config=*/{}, |
| 474 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 475 | /*use_internal_vad=*/true); |
| 476 | |
| 477 | test::InputAudioFile input_file( |
| 478 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 479 | /*loop_at_end=*/true); |
| 480 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 481 | |
| 482 | // Init buffers. |
| 483 | constexpr int kFrameDurationMs = 10; |
| 484 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 485 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 486 | kSampleRateHz, kStereo); |
| 487 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 488 | kStereo, kSampleRateHz, kStereo); |
| 489 | |
| 490 | // Simulate. |
| 491 | constexpr float kGainDb = -6.0f; |
| 492 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 493 | constexpr int kDurationMs = 10000; |
| 494 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 495 | constexpr float kSpeechProbabilities[] = {1.0f, 0.3f}; |
| 496 | constexpr float kEpsilon = 0.0001f; |
| 497 | bool all_samples_zero = true; |
| 498 | for (int i = 0, j = 0; i < kNumFramesToProcess; ++i, j = 1 - j) { |
| 499 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 500 | stream_config.num_channels(), &input_file, |
| 501 | frame); |
| 502 | // Apply a fixed gain to the input audio. |
| 503 | for (float& x : frame) { |
| 504 | x *= gain; |
| 505 | } |
| 506 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 507 | agc2.Process(kSpeechProbabilities[j], /*input_volume_changed=*/false, |
| 508 | &audio_buffer); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 509 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 510 | &audio_buffer_reference); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 511 | agc2_reference.Process(/*speech_probability=*/absl::nullopt, |
| 512 | /*input_volume_changed=*/false, |
| 513 | &audio_buffer_reference); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 514 | |
| 515 | // Check the output buffers. |
| 516 | for (int i = 0; i < kStereo; ++i) { |
| 517 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 518 | all_samples_zero &= |
| 519 | fabs(audio_buffer.channels_const()[i][j]) < kEpsilon; |
| 520 | EXPECT_FLOAT_EQ(audio_buffer.channels_const()[i][j], |
| 521 | audio_buffer_reference.channels_const()[i][j]); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | EXPECT_FALSE(all_samples_zero); |
| 526 | } |
| 527 | |
| 528 | // Processes a test audio file and checks that the injected speech probability |
| 529 | // is not ignored when the internal VAD is not used. |
| 530 | TEST(GainController2, |
| 531 | CheckInjectedVadProbabilityUsedWithAdaptiveDigitalController) { |
| 532 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 533 | constexpr int kStereo = 2; |
| 534 | |
| 535 | // Create AGC2 enabling only the adaptive digital controller. |
| 536 | Agc2Config config; |
| 537 | config.fixed_digital.gain_db = 0.0f; |
| 538 | config.adaptive_digital.enabled = true; |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 539 | GainController2 agc2(config, /*input_volume_controller_config=*/{}, |
| 540 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 541 | /*use_internal_vad=*/false); |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 542 | GainController2 agc2_reference(config, /*input_volume_controller_config=*/{}, |
| 543 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 544 | /*use_internal_vad=*/true); |
| 545 | |
| 546 | test::InputAudioFile input_file( |
| 547 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 548 | /*loop_at_end=*/true); |
| 549 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 550 | |
| 551 | // Init buffers. |
| 552 | constexpr int kFrameDurationMs = 10; |
| 553 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 554 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 555 | kSampleRateHz, kStereo); |
| 556 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 557 | kStereo, kSampleRateHz, kStereo); |
| 558 | // Simulate. |
| 559 | constexpr float kGainDb = -6.0f; |
| 560 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 561 | constexpr int kDurationMs = 10000; |
| 562 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 563 | constexpr float kSpeechProbabilities[] = {1.0f, 0.3f}; |
| 564 | constexpr float kEpsilon = 0.0001f; |
| 565 | bool all_samples_zero = true; |
| 566 | bool all_samples_equal = true; |
| 567 | for (int i = 0, j = 0; i < kNumFramesToProcess; ++i, j = 1 - j) { |
| 568 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 569 | stream_config.num_channels(), &input_file, |
| 570 | frame); |
| 571 | // Apply a fixed gain to the input audio. |
| 572 | for (float& x : frame) { |
| 573 | x *= gain; |
| 574 | } |
| 575 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 576 | agc2.Process(kSpeechProbabilities[j], /*input_volume_changed=*/false, |
| 577 | &audio_buffer); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 578 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 579 | &audio_buffer_reference); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 580 | agc2_reference.Process(/*speech_probability=*/absl::nullopt, |
| 581 | /*input_volume_changed=*/false, |
| 582 | &audio_buffer_reference); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 583 | // Check the output buffers. |
| 584 | for (int i = 0; i < kStereo; ++i) { |
| 585 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 586 | all_samples_zero &= |
| 587 | fabs(audio_buffer.channels_const()[i][j]) < kEpsilon; |
| 588 | all_samples_equal &= |
| 589 | fabs(audio_buffer.channels_const()[i][j] - |
| 590 | audio_buffer_reference.channels_const()[i][j]) < kEpsilon; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | EXPECT_FALSE(all_samples_zero); |
| 595 | EXPECT_FALSE(all_samples_equal); |
| 596 | } |
| 597 | |
| 598 | // Processes a test audio file and checks that the output is equal when |
| 599 | // an injected speech probability from `VoiceActivityDetectorWrapper` and |
| 600 | // the speech probability computed by the internal VAD are the same. |
| 601 | TEST(GainController2, |
| 602 | CheckEqualResultFromInjectedVadProbabilityWithAdaptiveDigitalController) { |
| 603 | constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz; |
| 604 | constexpr int kStereo = 2; |
| 605 | |
| 606 | // Create AGC2 enabling only the adaptive digital controller. |
| 607 | Agc2Config config; |
| 608 | config.fixed_digital.gain_db = 0.0f; |
| 609 | config.adaptive_digital.enabled = true; |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 610 | GainController2 agc2(config, /*input_volume_controller_config=*/{}, |
| 611 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 612 | /*use_internal_vad=*/false); |
Hanna Silen | a657490 | 2022-11-30 16:59:05 +0100 | [diff] [blame] | 613 | GainController2 agc2_reference(config, /*input_volume_controller_config=*/{}, |
| 614 | kSampleRateHz, kStereo, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 615 | /*use_internal_vad=*/true); |
| 616 | VoiceActivityDetectorWrapper vad(config.adaptive_digital.vad_reset_period_ms, |
| 617 | GetAvailableCpuFeatures(), kSampleRateHz); |
| 618 | test::InputAudioFile input_file( |
| 619 | test::GetApmCaptureTestVectorFileName(kSampleRateHz), |
| 620 | /*loop_at_end=*/true); |
| 621 | const StreamConfig stream_config(kSampleRateHz, kStereo); |
| 622 | |
| 623 | // Init buffers. |
| 624 | constexpr int kFrameDurationMs = 10; |
| 625 | std::vector<float> frame(kStereo * stream_config.num_frames()); |
| 626 | AudioBuffer audio_buffer(kSampleRateHz, kStereo, kSampleRateHz, kStereo, |
| 627 | kSampleRateHz, kStereo); |
| 628 | AudioBuffer audio_buffer_reference(kSampleRateHz, kStereo, kSampleRateHz, |
| 629 | kStereo, kSampleRateHz, kStereo); |
| 630 | |
| 631 | // Simulate. |
| 632 | constexpr float kGainDb = -6.0f; |
| 633 | const float gain = std::pow(10.0f, kGainDb / 20.0f); |
| 634 | constexpr int kDurationMs = 10000; |
| 635 | constexpr int kNumFramesToProcess = kDurationMs / kFrameDurationMs; |
| 636 | for (int i = 0; i < kNumFramesToProcess; ++i) { |
| 637 | ReadFloatSamplesFromStereoFile(stream_config.num_frames(), |
| 638 | stream_config.num_channels(), &input_file, |
| 639 | frame); |
| 640 | // Apply a fixed gain to the input audio. |
| 641 | for (float& x : frame) { |
| 642 | x *= gain; |
| 643 | } |
| 644 | test::CopyVectorToAudioBuffer(stream_config, frame, |
| 645 | &audio_buffer_reference); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 646 | agc2_reference.Process(absl::nullopt, /*input_volume_changed=*/false, |
| 647 | &audio_buffer_reference); |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 648 | test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer); |
Alessio Bazzica | fcf1af3 | 2022-09-07 17:14:26 +0200 | [diff] [blame] | 649 | float speech_probability = vad.Analyze(AudioFrameView<const float>( |
| 650 | audio_buffer.channels(), audio_buffer.num_channels(), |
| 651 | audio_buffer.num_frames())); |
| 652 | agc2.Process(speech_probability, /*input_volume_changed=*/false, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 653 | &audio_buffer); |
| 654 | // Check the output buffer. |
| 655 | for (int i = 0; i < kStereo; ++i) { |
| 656 | for (int j = 0; j < static_cast<int>(audio_buffer.num_frames()); ++j) { |
| 657 | EXPECT_FLOAT_EQ(audio_buffer.channels_const()[i][j], |
| 658 | audio_buffer_reference.channels_const()[i][j]); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 664 | } // namespace test |
| 665 | } // namespace webrtc |