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
11 #include "modules/audio_processing/gain_controller2.h"
12
13 #include <algorithm>
14 #include <cmath>
15 #include <memory>
16 #include <numeric>
17 #include <tuple>
18
19 #include "api/array_view.h"
20 #include "modules/audio_processing/agc2/agc2_testing_common.h"
21 #include "modules/audio_processing/audio_buffer.h"
22 #include "modules/audio_processing/test/audio_buffer_tools.h"
23 #include "modules/audio_processing/test/bitexactness_tools.h"
24 #include "rtc_base/checks.h"
25 #include "test/gmock.h"
26 #include "test/gtest.h"
27
28 namespace webrtc {
29 namespace test {
30 namespace {
31
32 using ::testing::Eq;
33 using ::testing::Optional;
34
35 using Agc2Config = AudioProcessing::Config::GainController2;
36 using InputVolumeControllerConfig = InputVolumeController::Config;
37
38 // Sets all the samples in `ab` to `value`.
SetAudioBufferSamples(float value,AudioBuffer & ab)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);
42 }
43 }
44
RunAgc2WithConstantInput(GainController2 & agc2,float input_level,int num_frames,int sample_rate_hz,int num_channels=1,int applied_initial_volume=0)45 float RunAgc2WithConstantInput(GainController2& agc2,
46 float input_level,
47 int num_frames,
48 int sample_rate_hz,
49 int num_channels = 1,
50 int applied_initial_volume = 0) {
51 const int num_samples = rtc::CheckedDivExact(sample_rate_hz, 100);
52 AudioBuffer ab(sample_rate_hz, num_channels, sample_rate_hz, num_channels,
53 sample_rate_hz, num_channels);
54
55 // Give time to the level estimator to converge.
56 for (int i = 0; i < num_frames + 1; ++i) {
57 SetAudioBufferSamples(input_level, ab);
58 const auto applied_volume = agc2.GetRecommendedInputVolume();
59 agc2.Analyze(i > 0 && applied_volume.has_value() ? *applied_volume
60 : applied_initial_volume,
61 ab);
62 agc2.Process(/*speech_probability=*/absl::nullopt,
63 /*input_volume_changed=*/false, &ab);
64 }
65
66 // Return the last sample from the last processed frame.
67 return ab.channels()[0][num_samples - 1];
68 }
69
CreateAgc2FixedDigitalMode(float fixed_gain_db,int sample_rate_hz)70 std::unique_ptr<GainController2> CreateAgc2FixedDigitalMode(
71 float fixed_gain_db,
72 int sample_rate_hz) {
73 Agc2Config config;
74 config.adaptive_digital.enabled = false;
75 config.fixed_digital.gain_db = fixed_gain_db;
76 EXPECT_TRUE(GainController2::Validate(config));
77 return std::make_unique<GainController2>(
78 config, InputVolumeControllerConfig{}, sample_rate_hz,
79 /*num_channels=*/1,
80 /*use_internal_vad=*/true);
81 }
82
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
96 } // namespace
97
TEST(GainController2,CheckDefaultConfig)98 TEST(GainController2, CheckDefaultConfig) {
99 Agc2Config config;
100 EXPECT_TRUE(GainController2::Validate(config));
101 }
102
TEST(GainController2,CheckFixedDigitalConfig)103 TEST(GainController2, CheckFixedDigitalConfig) {
104 Agc2Config config;
105 // Attenuation is not allowed.
106 config.fixed_digital.gain_db = -5.0f;
107 EXPECT_FALSE(GainController2::Validate(config));
108 // No gain is allowed.
109 config.fixed_digital.gain_db = 0.0f;
110 EXPECT_TRUE(GainController2::Validate(config));
111 // Positive gain is allowed.
112 config.fixed_digital.gain_db = 15.0f;
113 EXPECT_TRUE(GainController2::Validate(config));
114 }
115
TEST(GainController2,CheckHeadroomDb)116 TEST(GainController2, CheckHeadroomDb) {
117 Agc2Config config;
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
TEST(GainController2,CheckMaxGainDb)126 TEST(GainController2, CheckMaxGainDb) {
127 Agc2Config config;
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
TEST(GainController2,CheckInitialGainDb)136 TEST(GainController2, CheckInitialGainDb) {
137 Agc2Config config;
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
TEST(GainController2,CheckAdaptiveDigitalMaxGainChangeSpeedConfig)146 TEST(GainController2, CheckAdaptiveDigitalMaxGainChangeSpeedConfig) {
147 Agc2Config config;
148 config.adaptive_digital.max_gain_change_db_per_second = -1.0f;
149 EXPECT_FALSE(GainController2::Validate(config));
150 config.adaptive_digital.max_gain_change_db_per_second = 0.0f;
151 EXPECT_FALSE(GainController2::Validate(config));
152 config.adaptive_digital.max_gain_change_db_per_second = 5.0f;
153 EXPECT_TRUE(GainController2::Validate(config));
154 }
155
TEST(GainController2,CheckAdaptiveDigitalMaxOutputNoiseLevelConfig)156 TEST(GainController2, CheckAdaptiveDigitalMaxOutputNoiseLevelConfig) {
157 Agc2Config config;
158 config.adaptive_digital.max_output_noise_level_dbfs = 5.0f;
159 EXPECT_FALSE(GainController2::Validate(config));
160 config.adaptive_digital.max_output_noise_level_dbfs = 0.0f;
161 EXPECT_TRUE(GainController2::Validate(config));
162 config.adaptive_digital.max_output_noise_level_dbfs = -5.0f;
163 EXPECT_TRUE(GainController2::Validate(config));
164 }
165
TEST(GainController2,CheckGetRecommendedInputVolumeWhenInputVolumeControllerNotEnabled)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;
177
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
TEST(GainController2,CheckGetRecommendedInputVolumeWhenInputVolumeControllerNotEnabledAndSpecificConfigUsed)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);
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
TEST(GainController2,CheckGetRecommendedInputVolumeWhenInputVolumeControllerEnabled)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;
240 config.adaptive_digital.enabled = true;
241
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
TEST(GainController2,CheckGetRecommendedInputVolumeWhenInputVolumeControllerEnabledAndSpecificConfigUsed)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);
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
294 // Checks that the default config is applied.
TEST(GainController2,ApplyDefaultConfig)295 TEST(GainController2, ApplyDefaultConfig) {
296 auto gain_controller2 = std::make_unique<GainController2>(
297 Agc2Config{}, InputVolumeControllerConfig{},
298 /*sample_rate_hz=*/16000, /*num_channels=*/2,
299 /*use_internal_vad=*/true);
300 EXPECT_TRUE(gain_controller2.get());
301 }
302
TEST(GainController2FixedDigital,GainShouldChangeOnSetGain)303 TEST(GainController2FixedDigital, GainShouldChangeOnSetGain) {
304 constexpr float kInputLevel = 1000.0f;
305 constexpr size_t kNumFrames = 5;
306 constexpr size_t kSampleRateHz = 8000;
307 constexpr float kGain0Db = 0.0f;
308 constexpr float kGain20Db = 20.0f;
309
310 auto agc2_fixed = CreateAgc2FixedDigitalMode(kGain0Db, kSampleRateHz);
311
312 // Signal level is unchanged with 0 db gain.
313 EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames,
314 kSampleRateHz),
315 kInputLevel);
316
317 // +20 db should increase signal by a factor of 10.
318 agc2_fixed->SetFixedGainDb(kGain20Db);
319 EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel, kNumFrames,
320 kSampleRateHz),
321 kInputLevel * 10);
322 }
323
TEST(GainController2FixedDigital,ChangeFixedGainShouldBeFastAndTimeInvariant)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;
328
329 constexpr float kInputLevel = 1000.0f;
330 constexpr size_t kSampleRateHz = 8000;
331 constexpr float kGainDbLow = 0.0f;
332 constexpr float kGainDbHigh = 25.0f;
333 static_assert(kGainDbLow < kGainDbHigh, "");
334
335 auto agc2_fixed = CreateAgc2FixedDigitalMode(kGainDbLow, kSampleRateHz);
336
337 // Start with a lower gain.
338 const float output_level_pre = RunAgc2WithConstantInput(
339 *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz);
340
341 // Increase gain.
342 agc2_fixed->SetFixedGainDb(kGainDbHigh);
343 static_cast<void>(RunAgc2WithConstantInput(*agc2_fixed, kInputLevel,
344 kNumFrames, kSampleRateHz));
345
346 // Back to the lower gain.
347 agc2_fixed->SetFixedGainDb(kGainDbLow);
348 const float output_level_post = RunAgc2WithConstantInput(
349 *agc2_fixed, kInputLevel, kNumFrames, kSampleRateHz);
350
351 EXPECT_EQ(output_level_pre, output_level_post);
352 }
353
354 class FixedDigitalTest
355 : public ::testing::TestWithParam<std::tuple<float, float, int, bool>> {
356 protected:
gain_db_min() const357 float gain_db_min() const { return std::get<0>(GetParam()); }
gain_db_max() const358 float gain_db_max() const { return std::get<1>(GetParam()); }
sample_rate_hz() const359 int sample_rate_hz() const { return std::get<2>(GetParam()); }
saturation_expected() const360 bool saturation_expected() const { return std::get<3>(GetParam()); }
361 };
362
TEST_P(FixedDigitalTest,CheckSaturationBehaviorWithLimiter)363 TEST_P(FixedDigitalTest, CheckSaturationBehaviorWithLimiter) {
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()) {
371 EXPECT_FLOAT_EQ(processed_sample, 32767.0f);
372 } else {
373 EXPECT_LT(processed_sample, 32767.0f);
374 }
375 }
376 }
377
378 static_assert(test::kLimiterMaxInputLevelDbFs < 10, "");
379 INSTANTIATE_TEST_SUITE_P(
380 GainController2,
381 FixedDigitalTest,
382 ::testing::Values(
383 // When gain < `test::kLimiterMaxInputLevelDbFs`, the limiter will not
384 // saturate the signal (at any sample rate).
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),
393 // When gain > `test::kLimiterMaxInputLevelDbFs`, the limiter will
394 // saturate the signal (at any sample rate).
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)));
403
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.
TEST(GainController2,CheckFinalGainWithAdaptiveDigitalController)406 TEST(GainController2, CheckFinalGainWithAdaptiveDigitalController) {
407 constexpr int kSampleRateHz = AudioProcessing::kSampleRate48kHz;
408 constexpr int kStereo = 2;
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;
414 GainController2 agc2(config, /*input_volume_controller_config=*/{},
415 kSampleRateHz, kStereo,
416 /*use_internal_vad=*/true);
417
418 test::InputAudioFile input_file(
419 test::GetApmCaptureTestVectorFileName(kSampleRateHz),
420 /*loop_at_end=*/true);
421 const StreamConfig stream_config(kSampleRateHz, kStereo);
422
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.
439 for (float& x : frame) {
440 x *= gain;
441 }
442 test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer);
443 agc2.Process(/*speech_probability=*/absl::nullopt,
444 /*input_volume_changed=*/false, &audio_buffer);
445 }
446
447 // Estimate the applied gain by processing a probing frame.
448 SetAudioBufferSamples(/*value=*/1.0f, audio_buffer);
449 agc2.Process(/*speech_probability=*/absl::nullopt,
450 /*input_volume_changed=*/false, &audio_buffer);
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);
457 }
458
459 // Processes a test audio file and checks that the injected speech probability
460 // is ignored when the internal VAD is used.
TEST(GainController2,CheckInjectedVadProbabilityNotUsedWithAdaptiveDigitalController)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;
470 GainController2 agc2(config, /*input_volume_controller_config=*/{},
471 kSampleRateHz, kStereo,
472 /*use_internal_vad=*/true);
473 GainController2 agc2_reference(config, /*input_volume_controller_config=*/{},
474 kSampleRateHz, kStereo,
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);
507 agc2.Process(kSpeechProbabilities[j], /*input_volume_changed=*/false,
508 &audio_buffer);
509 test::CopyVectorToAudioBuffer(stream_config, frame,
510 &audio_buffer_reference);
511 agc2_reference.Process(/*speech_probability=*/absl::nullopt,
512 /*input_volume_changed=*/false,
513 &audio_buffer_reference);
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.
TEST(GainController2,CheckInjectedVadProbabilityUsedWithAdaptiveDigitalController)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;
539 GainController2 agc2(config, /*input_volume_controller_config=*/{},
540 kSampleRateHz, kStereo,
541 /*use_internal_vad=*/false);
542 GainController2 agc2_reference(config, /*input_volume_controller_config=*/{},
543 kSampleRateHz, kStereo,
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);
576 agc2.Process(kSpeechProbabilities[j], /*input_volume_changed=*/false,
577 &audio_buffer);
578 test::CopyVectorToAudioBuffer(stream_config, frame,
579 &audio_buffer_reference);
580 agc2_reference.Process(/*speech_probability=*/absl::nullopt,
581 /*input_volume_changed=*/false,
582 &audio_buffer_reference);
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.
TEST(GainController2,CheckEqualResultFromInjectedVadProbabilityWithAdaptiveDigitalController)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;
610 GainController2 agc2(config, /*input_volume_controller_config=*/{},
611 kSampleRateHz, kStereo,
612 /*use_internal_vad=*/false);
613 GainController2 agc2_reference(config, /*input_volume_controller_config=*/{},
614 kSampleRateHz, kStereo,
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);
646 agc2_reference.Process(absl::nullopt, /*input_volume_changed=*/false,
647 &audio_buffer_reference);
648 test::CopyVectorToAudioBuffer(stream_config, frame, &audio_buffer);
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,
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
664 } // namespace test
665 } // namespace webrtc
666