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 <memory>
15
16 #include "api/array_view.h"
17 #include "modules/audio_processing/agc2/agc2_testing_common.h"
18 #include "modules/audio_processing/audio_buffer.h"
19 #include "modules/audio_processing/test/audio_buffer_tools.h"
20 #include "modules/audio_processing/test/bitexactness_tools.h"
21 #include "rtc_base/checks.h"
22 #include "test/gtest.h"
23
24 namespace webrtc {
25 namespace test {
26 namespace {
27
SetAudioBufferSamples(float value,AudioBuffer * ab)28 void SetAudioBufferSamples(float value, AudioBuffer* ab) {
29 // Sets all the samples in |ab| to |value|.
30 for (size_t k = 0; k < ab->num_channels(); ++k) {
31 std::fill(ab->channels()[k], ab->channels()[k] + ab->num_frames(), value);
32 }
33 }
34
RunAgc2WithConstantInput(GainController2 * agc2,float input_level,size_t num_frames,int sample_rate)35 float RunAgc2WithConstantInput(GainController2* agc2,
36 float input_level,
37 size_t num_frames,
38 int sample_rate) {
39 const int num_samples = rtc::CheckedDivExact(sample_rate, 100);
40 AudioBuffer ab(sample_rate, 1, sample_rate, 1, sample_rate, 1);
41
42 // Give time to the level estimator to converge.
43 for (size_t i = 0; i < num_frames + 1; ++i) {
44 SetAudioBufferSamples(input_level, &ab);
45 agc2->Process(&ab);
46 }
47
48 // Return the last sample from the last processed frame.
49 return ab.channels()[0][num_samples - 1];
50 }
51
CreateAgc2FixedDigitalModeConfig(float fixed_gain_db)52 AudioProcessing::Config::GainController2 CreateAgc2FixedDigitalModeConfig(
53 float fixed_gain_db) {
54 AudioProcessing::Config::GainController2 config;
55 config.adaptive_digital.enabled = false;
56 config.fixed_digital.gain_db = fixed_gain_db;
57 // TODO(alessiob): Check why ASSERT_TRUE() below does not compile.
58 EXPECT_TRUE(GainController2::Validate(config));
59 return config;
60 }
61
CreateAgc2FixedDigitalMode(float fixed_gain_db,size_t sample_rate_hz)62 std::unique_ptr<GainController2> CreateAgc2FixedDigitalMode(
63 float fixed_gain_db,
64 size_t sample_rate_hz) {
65 auto agc2 = std::make_unique<GainController2>();
66 agc2->ApplyConfig(CreateAgc2FixedDigitalModeConfig(fixed_gain_db));
67 agc2->Initialize(sample_rate_hz);
68 return agc2;
69 }
70
GainAfterProcessingFile(GainController2 * gain_controller)71 float GainAfterProcessingFile(GainController2* gain_controller) {
72 // Set up an AudioBuffer to be filled from the speech file.
73 constexpr size_t kStereo = 2u;
74 const StreamConfig capture_config(AudioProcessing::kSampleRate48kHz, kStereo,
75 false);
76 AudioBuffer ab(capture_config.sample_rate_hz(), capture_config.num_channels(),
77 capture_config.sample_rate_hz(), capture_config.num_channels(),
78 capture_config.sample_rate_hz(),
79 capture_config.num_channels());
80 test::InputAudioFile capture_file(
81 test::GetApmCaptureTestVectorFileName(AudioProcessing::kSampleRate48kHz));
82 std::vector<float> capture_input(capture_config.num_frames() *
83 capture_config.num_channels());
84
85 // The file should contain at least this many frames. Every iteration, we put
86 // a frame through the gain controller.
87 const int kNumFramesToProcess = 100;
88 for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
89 ReadFloatSamplesFromStereoFile(capture_config.num_frames(),
90 capture_config.num_channels(), &capture_file,
91 capture_input);
92
93 test::CopyVectorToAudioBuffer(capture_config, capture_input, &ab);
94 gain_controller->Process(&ab);
95 }
96
97 // Send in a last frame with values constant 1 (It's low enough to detect high
98 // gain, and for ease of computation). The applied gain is the result.
99 constexpr float sample_value = 1.f;
100 SetAudioBufferSamples(sample_value, &ab);
101 gain_controller->Process(&ab);
102 return ab.channels()[0][0];
103 }
104
105 } // namespace
106
TEST(GainController2,CreateApplyConfig)107 TEST(GainController2, CreateApplyConfig) {
108 // Instances GainController2 and applies different configurations.
109 std::unique_ptr<GainController2> gain_controller2(new GainController2());
110
111 // Check that the default config is valid.
112 AudioProcessing::Config::GainController2 config;
113 EXPECT_TRUE(GainController2::Validate(config));
114 gain_controller2->ApplyConfig(config);
115
116 // Check that attenuation is not allowed.
117 config.fixed_digital.gain_db = -5.f;
118 EXPECT_FALSE(GainController2::Validate(config));
119
120 // Check that valid configurations are applied.
121 for (const float& fixed_gain_db : {0.f, 5.f, 10.f, 40.f}) {
122 config.fixed_digital.gain_db = fixed_gain_db;
123 EXPECT_TRUE(GainController2::Validate(config));
124 gain_controller2->ApplyConfig(config);
125 }
126 }
127
TEST(GainController2,ToString)128 TEST(GainController2, ToString) {
129 // Tests GainController2::ToString(). Only test the enabled property.
130 AudioProcessing::Config::GainController2 config;
131
132 config.enabled = false;
133 EXPECT_EQ("{enabled: false", GainController2::ToString(config).substr(0, 15));
134
135 config.enabled = true;
136 EXPECT_EQ("{enabled: true", GainController2::ToString(config).substr(0, 14));
137 }
138
TEST(GainController2FixedDigital,GainShouldChangeOnSetGain)139 TEST(GainController2FixedDigital, GainShouldChangeOnSetGain) {
140 constexpr float kInputLevel = 1000.f;
141 constexpr size_t kNumFrames = 5;
142 constexpr size_t kSampleRateHz = 8000;
143 constexpr float kGain0Db = 0.f;
144 constexpr float kGain20Db = 20.f;
145
146 auto agc2_fixed = CreateAgc2FixedDigitalMode(kGain0Db, kSampleRateHz);
147
148 // Signal level is unchanged with 0 db gain.
149 EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(agc2_fixed.get(), kInputLevel,
150 kNumFrames, kSampleRateHz),
151 kInputLevel);
152
153 // +20 db should increase signal by a factor of 10.
154 agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGain20Db));
155 EXPECT_FLOAT_EQ(RunAgc2WithConstantInput(agc2_fixed.get(), kInputLevel,
156 kNumFrames, kSampleRateHz),
157 kInputLevel * 10);
158 }
159
TEST(GainController2FixedDigital,ChangeFixedGainShouldBeFastAndTimeInvariant)160 TEST(GainController2FixedDigital, ChangeFixedGainShouldBeFastAndTimeInvariant) {
161 // Number of frames required for the fixed gain controller to adapt on the
162 // input signal when the gain changes.
163 constexpr size_t kNumFrames = 5;
164
165 constexpr float kInputLevel = 1000.f;
166 constexpr size_t kSampleRateHz = 8000;
167 constexpr float kGainDbLow = 0.f;
168 constexpr float kGainDbHigh = 25.f;
169 static_assert(kGainDbLow < kGainDbHigh, "");
170
171 auto agc2_fixed = CreateAgc2FixedDigitalMode(kGainDbLow, kSampleRateHz);
172
173 // Start with a lower gain.
174 const float output_level_pre = RunAgc2WithConstantInput(
175 agc2_fixed.get(), kInputLevel, kNumFrames, kSampleRateHz);
176
177 // Increase gain.
178 agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGainDbHigh));
179 static_cast<void>(RunAgc2WithConstantInput(agc2_fixed.get(), kInputLevel,
180 kNumFrames, kSampleRateHz));
181
182 // Back to the lower gain.
183 agc2_fixed->ApplyConfig(CreateAgc2FixedDigitalModeConfig(kGainDbLow));
184 const float output_level_post = RunAgc2WithConstantInput(
185 agc2_fixed.get(), kInputLevel, kNumFrames, kSampleRateHz);
186
187 EXPECT_EQ(output_level_pre, output_level_post);
188 }
189
190 struct FixedDigitalTestParams {
FixedDigitalTestParamswebrtc::test::FixedDigitalTestParams191 FixedDigitalTestParams(float gain_db_min,
192 float gain_db_max,
193 size_t sample_rate,
194 bool saturation_expected)
195 : gain_db_min(gain_db_min),
196 gain_db_max(gain_db_max),
197 sample_rate(sample_rate),
198 saturation_expected(saturation_expected) {}
199 float gain_db_min;
200 float gain_db_max;
201 size_t sample_rate;
202 bool saturation_expected;
203 };
204
205 class FixedDigitalTest
206 : public ::testing::Test,
207 public ::testing::WithParamInterface<FixedDigitalTestParams> {};
208
TEST_P(FixedDigitalTest,CheckSaturationBehaviorWithLimiter)209 TEST_P(FixedDigitalTest, CheckSaturationBehaviorWithLimiter) {
210 const float kInputLevel = 32767.f;
211 const size_t kNumFrames = 5;
212
213 const auto params = GetParam();
214
215 const auto gains_db =
216 test::LinSpace(params.gain_db_min, params.gain_db_max, 10);
217 for (const auto gain_db : gains_db) {
218 SCOPED_TRACE(std::to_string(gain_db));
219 auto agc2_fixed = CreateAgc2FixedDigitalMode(gain_db, params.sample_rate);
220 const float processed_sample = RunAgc2WithConstantInput(
221 agc2_fixed.get(), kInputLevel, kNumFrames, params.sample_rate);
222 if (params.saturation_expected) {
223 EXPECT_FLOAT_EQ(processed_sample, 32767.f);
224 } else {
225 EXPECT_LT(processed_sample, 32767.f);
226 }
227 }
228 }
229
230 static_assert(test::kLimiterMaxInputLevelDbFs < 10, "");
231 INSTANTIATE_TEST_SUITE_P(
232 GainController2,
233 FixedDigitalTest,
234 ::testing::Values(
235 // When gain < |test::kLimiterMaxInputLevelDbFs|, the limiter will not
236 // saturate the signal (at any sample rate).
237 FixedDigitalTestParams(0.1f,
238 test::kLimiterMaxInputLevelDbFs - 0.01f,
239 8000,
240 false),
241 FixedDigitalTestParams(0.1,
242 test::kLimiterMaxInputLevelDbFs - 0.01f,
243 48000,
244 false),
245 // When gain > |test::kLimiterMaxInputLevelDbFs|, the limiter will
246 // saturate the signal (at any sample rate).
247 FixedDigitalTestParams(test::kLimiterMaxInputLevelDbFs + 0.01f,
248 10.f,
249 8000,
250 true),
251 FixedDigitalTestParams(test::kLimiterMaxInputLevelDbFs + 0.01f,
252 10.f,
253 48000,
254 true)));
255
TEST(GainController2,UsageSaturationMargin)256 TEST(GainController2, UsageSaturationMargin) {
257 GainController2 gain_controller2;
258 gain_controller2.Initialize(AudioProcessing::kSampleRate48kHz);
259
260 AudioProcessing::Config::GainController2 config;
261 // Check that samples are not amplified as much when extra margin is
262 // high. They should not be amplified at all, but only after convergence. GC2
263 // starts with a gain, and it takes time until it's down to 0 dB.
264 config.fixed_digital.gain_db = 0.f;
265 config.adaptive_digital.enabled = true;
266 config.adaptive_digital.extra_saturation_margin_db = 50.f;
267 gain_controller2.ApplyConfig(config);
268
269 EXPECT_LT(GainAfterProcessingFile(&gain_controller2), 2.f);
270 }
271
TEST(GainController2,UsageNoSaturationMargin)272 TEST(GainController2, UsageNoSaturationMargin) {
273 GainController2 gain_controller2;
274 gain_controller2.Initialize(AudioProcessing::kSampleRate48kHz);
275
276 AudioProcessing::Config::GainController2 config;
277 // Check that some gain is applied if there is no margin.
278 config.fixed_digital.gain_db = 0.f;
279 config.adaptive_digital.enabled = true;
280 config.adaptive_digital.extra_saturation_margin_db = 0.f;
281 gain_controller2.ApplyConfig(config);
282
283 EXPECT_GT(GainAfterProcessingFile(&gain_controller2), 2.f);
284 }
285
286 } // namespace test
287 } // namespace webrtc
288