1 /*
2  *  Copyright (c) 2016 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 #include <vector>
11 
12 #include "api/array_view.h"
13 #include "modules/audio_processing/audio_buffer.h"
14 #include "modules/audio_processing/gain_control_impl.h"
15 #include "modules/audio_processing/test/audio_buffer_tools.h"
16 #include "modules/audio_processing/test/bitexactness_tools.h"
17 #include "test/gtest.h"
18 
19 namespace webrtc {
20 namespace {
21 
22 const int kNumFramesToProcess = 100;
23 
ProcessOneFrame(int sample_rate_hz,AudioBuffer * render_audio_buffer,AudioBuffer * capture_audio_buffer,GainControlImpl * gain_controller)24 void ProcessOneFrame(int sample_rate_hz,
25                      AudioBuffer* render_audio_buffer,
26                      AudioBuffer* capture_audio_buffer,
27                      GainControlImpl* gain_controller) {
28   if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
29     render_audio_buffer->SplitIntoFrequencyBands();
30     capture_audio_buffer->SplitIntoFrequencyBands();
31   }
32 
33   std::vector<int16_t> render_audio;
34   GainControlImpl::PackRenderAudioBuffer(*render_audio_buffer, &render_audio);
35   gain_controller->ProcessRenderAudio(render_audio);
36   gain_controller->AnalyzeCaptureAudio(*capture_audio_buffer);
37   gain_controller->ProcessCaptureAudio(capture_audio_buffer, false);
38 
39   if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
40     capture_audio_buffer->MergeFrequencyBands();
41   }
42 }
43 
SetupComponent(int sample_rate_hz,GainControl::Mode mode,int target_level_dbfs,int stream_analog_level,int compression_gain_db,bool enable_limiter,int analog_level_min,int analog_level_max,GainControlImpl * gain_controller)44 void SetupComponent(int sample_rate_hz,
45                     GainControl::Mode mode,
46                     int target_level_dbfs,
47                     int stream_analog_level,
48                     int compression_gain_db,
49                     bool enable_limiter,
50                     int analog_level_min,
51                     int analog_level_max,
52                     GainControlImpl* gain_controller) {
53   gain_controller->Initialize(1, sample_rate_hz);
54   GainControl* gc = static_cast<GainControl*>(gain_controller);
55   gc->set_mode(mode);
56   gc->set_stream_analog_level(stream_analog_level);
57   gc->set_target_level_dbfs(target_level_dbfs);
58   gc->set_compression_gain_db(compression_gain_db);
59   gc->enable_limiter(enable_limiter);
60   gc->set_analog_level_limits(analog_level_min, analog_level_max);
61 }
62 
RunBitExactnessTest(int sample_rate_hz,size_t num_channels,GainControl::Mode mode,int target_level_dbfs,int stream_analog_level,int compression_gain_db,bool enable_limiter,int analog_level_min,int analog_level_max,int achieved_stream_analog_level_reference,rtc::ArrayView<const float> output_reference)63 void RunBitExactnessTest(int sample_rate_hz,
64                          size_t num_channels,
65                          GainControl::Mode mode,
66                          int target_level_dbfs,
67                          int stream_analog_level,
68                          int compression_gain_db,
69                          bool enable_limiter,
70                          int analog_level_min,
71                          int analog_level_max,
72                          int achieved_stream_analog_level_reference,
73                          rtc::ArrayView<const float> output_reference) {
74   GainControlImpl gain_controller;
75   SetupComponent(sample_rate_hz, mode, target_level_dbfs, stream_analog_level,
76                  compression_gain_db, enable_limiter, analog_level_min,
77                  analog_level_max, &gain_controller);
78 
79   const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
80   const StreamConfig render_config(sample_rate_hz, num_channels, false);
81   AudioBuffer render_buffer(
82       render_config.sample_rate_hz(), render_config.num_channels(),
83       render_config.sample_rate_hz(), 1, render_config.sample_rate_hz(), 1);
84   test::InputAudioFile render_file(
85       test::GetApmRenderTestVectorFileName(sample_rate_hz));
86   std::vector<float> render_input(samples_per_channel * num_channels);
87 
88   const StreamConfig capture_config(sample_rate_hz, num_channels, false);
89   AudioBuffer capture_buffer(
90       capture_config.sample_rate_hz(), capture_config.num_channels(),
91       capture_config.sample_rate_hz(), 1, capture_config.sample_rate_hz(), 1);
92   test::InputAudioFile capture_file(
93       test::GetApmCaptureTestVectorFileName(sample_rate_hz));
94   std::vector<float> capture_input(samples_per_channel * num_channels);
95 
96   for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
97     ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
98                                    &render_file, render_input);
99     ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
100                                    &capture_file, capture_input);
101 
102     test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer);
103     test::CopyVectorToAudioBuffer(capture_config, capture_input,
104                                   &capture_buffer);
105 
106     ProcessOneFrame(sample_rate_hz, &render_buffer, &capture_buffer,
107                     &gain_controller);
108   }
109 
110   // Extract and verify the test results.
111   std::vector<float> capture_output;
112   test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
113                                      &capture_output);
114 
115   EXPECT_EQ(achieved_stream_analog_level_reference,
116             gain_controller.stream_analog_level());
117 
118   // Compare the output with the reference. Only the first values of the output
119   // from last frame processed are compared in order not having to specify all
120   // preceeding frames as testvectors. As the algorithm being tested has a
121   // memory, testing only the last frame implicitly also tests the preceeding
122   // frames.
123   const float kElementErrorBound = 1.0f / 32768.0f;
124   EXPECT_TRUE(test::VerifyDeinterleavedArray(
125       capture_config.num_frames(), capture_config.num_channels(),
126       output_reference, capture_output, kElementErrorBound));
127 }
128 
129 }  // namespace
130 
131 // TODO(peah): Activate all these tests for ARM and ARM64 once the issue on the
132 // Chromium ARM and ARM64 boths have been identified. This is tracked in the
133 // issue https://bugs.chromium.org/p/webrtc/issues/detail?id=5711.
134 
135 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
136       defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100)137 TEST(GainControlBitExactnessTest,
138      Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
139 #else
140 TEST(GainControlBitExactnessTest,
141      DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
142 #endif
143   const int kStreamAnalogLevelReference = 50;
144   const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f};
145   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
146                       true, 0, 100, kStreamAnalogLevelReference,
147                       kOutputReference);
148 }
149 
150 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
151       defined(WEBRTC_ANDROID))
152 TEST(GainControlBitExactnessTest,
153      Stereo16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
154 #else
155 TEST(GainControlBitExactnessTest,
156      DISABLED_Stereo16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
157 #endif
158   const int kStreamAnalogLevelReference = 50;
159   const float kOutputReference[] = {-0.027313f, -0.015900f, -0.028107f,
160                                     -0.027313f, -0.015900f, -0.028107f};
161   RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
162                       true, 0, 100, kStreamAnalogLevelReference,
163                       kOutputReference);
164 }
165 
166 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
167       defined(WEBRTC_ANDROID))
168 TEST(GainControlBitExactnessTest,
169      Mono32kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
170 #else
171 TEST(GainControlBitExactnessTest,
172      DISABLED_Mono32kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
173 #endif
174   const int kStreamAnalogLevelReference = 50;
175   const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f};
176   RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
177                       true, 0, 100, kStreamAnalogLevelReference,
178                       kOutputReference);
179 }
180 
181 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
182       defined(WEBRTC_ANDROID))
183 TEST(GainControlBitExactnessTest,
184      Mono48kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
185 #else
186 TEST(GainControlBitExactnessTest,
187      DISABLED_Mono48kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
188 #endif
189   const int kStreamAnalogLevelReference = 50;
190   const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f};
191   RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
192                       true, 0, 100, kStreamAnalogLevelReference,
193                       kOutputReference);
194 }
195 
196 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
197       defined(WEBRTC_ANDROID))
198 TEST(GainControlBitExactnessTest,
199      Mono16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
200 #else
201 TEST(GainControlBitExactnessTest,
202      DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
203 #endif
204   const int kStreamAnalogLevelReference = 50;
205   const float kOutputReference[] = {-0.003967f, -0.002777f, -0.001770f};
206   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
207                       true, 0, 100, kStreamAnalogLevelReference,
208                       kOutputReference);
209 }
210 
211 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
212       defined(WEBRTC_ANDROID))
213 TEST(GainControlBitExactnessTest,
214      Stereo16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
215 #else
216 TEST(GainControlBitExactnessTest,
217      DISABLED_Stereo16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
218 #endif
219   const int kStreamAnalogLevelReference = 50;
220   const float kOutputReference[] = {-0.015411f, -0.008972f, -0.015839f,
221                                     -0.015411f, -0.008972f, -0.015839f};
222   RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
223                       true, 0, 100, kStreamAnalogLevelReference,
224                       kOutputReference);
225 }
226 
227 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
228       defined(WEBRTC_ANDROID))
229 TEST(GainControlBitExactnessTest,
230      Mono32kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
231 #else
232 TEST(GainControlBitExactnessTest,
233      DISABLED_Mono32kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
234 #endif
235   const int kStreamAnalogLevelReference = 50;
236   const float kOutputReference[] = {-0.006134f, -0.005524f, -0.005005f};
237   RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
238                       true, 0, 100, kStreamAnalogLevelReference,
239                       kOutputReference);
240 }
241 
242 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
243       defined(WEBRTC_ANDROID))
244 TEST(GainControlBitExactnessTest,
245      Mono48kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
246 #else
247 TEST(GainControlBitExactnessTest,
248      DISABLED_Mono48kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
249 #endif
250   const int kStreamAnalogLevelReference = 50;
251   const float kOutputReference[] = {-0.006134f, -0.005524f, -0.005005};
252   RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
253                       true, 0, 100, kStreamAnalogLevelReference,
254                       kOutputReference);
255 }
256 
257 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
258       defined(WEBRTC_ANDROID))
259 TEST(GainControlBitExactnessTest,
260      Mono16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
261 #else
262 TEST(GainControlBitExactnessTest,
263      DISABLED_Mono16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
264 #endif
265   const int kStreamAnalogLevelReference = 50;
266   const float kOutputReference[] = {-0.011749f, -0.008270f, -0.005219f};
267   RunBitExactnessTest(16000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
268                       true, 0, 100, kStreamAnalogLevelReference,
269                       kOutputReference);
270 }
271 
272 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
273       defined(WEBRTC_ANDROID))
274 TEST(GainControlBitExactnessTest,
275      Stereo16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
276 #else
277 TEST(GainControlBitExactnessTest,
278      DISABLED_Stereo16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
279 #endif
280   const int kStreamAnalogLevelReference = 50;
281   const float kOutputReference[] = {-0.048896f, -0.028479f, -0.050345f,
282                                     -0.048896f, -0.028479f, -0.050345f};
283   RunBitExactnessTest(16000, 2, GainControl::Mode::kFixedDigital, 10, 50, 5,
284                       true, 0, 100, kStreamAnalogLevelReference,
285                       kOutputReference);
286 }
287 
288 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
289       defined(WEBRTC_ANDROID))
290 TEST(GainControlBitExactnessTest,
291      Mono32kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
292 #else
293 TEST(GainControlBitExactnessTest,
294      DISABLED_Mono32kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
295 #endif
296   const int kStreamAnalogLevelReference = 50;
297   const float kOutputReference[] = {-0.018158f, -0.016357f, -0.014832f};
298   RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
299                       true, 0, 100, kStreamAnalogLevelReference,
300                       kOutputReference);
301 }
302 
303 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
304       defined(WEBRTC_ANDROID))
305 TEST(GainControlBitExactnessTest,
306      Mono48kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
307 #else
308 TEST(GainControlBitExactnessTest,
309      DISABLED_Mono48kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
310 #endif
311   const int kStreamAnalogLevelReference = 50;
312   const float kOutputReference[] = {-0.018158f, -0.016357f, -0.014832f};
313   RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
314                       true, 0, 100, kStreamAnalogLevelReference,
315                       kOutputReference);
316 }
317 
318 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
319       defined(WEBRTC_ANDROID))
320 TEST(GainControlBitExactnessTest,
321      Mono16kHz_AdaptiveAnalog_Tl10_SL10_CG5_Lim_AL0_100) {
322 #else
323 TEST(GainControlBitExactnessTest,
324      DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL10_CG5_Lim_AL0_100) {
325 #endif
326   const int kStreamAnalogLevelReference = 12;
327   const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f};
328   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 10, 5,
329                       true, 0, 100, kStreamAnalogLevelReference,
330                       kOutputReference);
331 }
332 
333 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
334       defined(WEBRTC_ANDROID))
335 TEST(GainControlBitExactnessTest,
336      Mono16kHz_AdaptiveAnalog_Tl10_SL100_CG5_Lim_AL70_80) {
337 #else
338 TEST(GainControlBitExactnessTest,
339      DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL100_CG5_Lim_AL70_80) {
340 #endif
341   const int kStreamAnalogLevelReference = 100;
342   const float kOutputReference[] = {-0.003998f, -0.002808f, -0.001770f};
343   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 100, 5,
344                       true, 70, 80, kStreamAnalogLevelReference,
345                       kOutputReference);
346 }
347 
348 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
349       defined(WEBRTC_ANDROID))
350 TEST(GainControlBitExactnessTest,
351      Mono16kHz_AdaptiveDigital_Tl10_SL100_CG5_NoLim_AL0_100) {
352 #else
353 TEST(GainControlBitExactnessTest,
354      DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL100_CG5_NoLim_AL0_100) {
355 #endif
356   const int kStreamAnalogLevelReference = 100;
357   const float kOutputReference[] = {-0.004028f, -0.002838f, -0.001770f};
358   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100, 5,
359                       false, 0, 100, kStreamAnalogLevelReference,
360                       kOutputReference);
361 }
362 
363 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
364       defined(WEBRTC_ANDROID))
365 TEST(GainControlBitExactnessTest,
366      Mono16kHz_AdaptiveDigital_Tl40_SL100_CG5_Lim_AL0_100) {
367 #else
368 TEST(GainControlBitExactnessTest,
369      DISABLED_Mono16kHz_AdaptiveDigital_Tl40_SL100_CG5_Lim_AL0_100) {
370 #endif
371   const int kStreamAnalogLevelReference = 100;
372   const float kOutputReference[] = {-0.008728f, -0.006134f, -0.003845f};
373   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 40, 100, 5,
374                       true, 0, 100, kStreamAnalogLevelReference,
375                       kOutputReference);
376 }
377 
378 #if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
379       defined(WEBRTC_ANDROID))
380 TEST(GainControlBitExactnessTest,
381      Mono16kHz_AdaptiveDigital_Tl10_SL100_CG30_Lim_AL0_100) {
382 #else
383 TEST(GainControlBitExactnessTest,
384      DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL100_CG30_Lim_AL0_100) {
385 #endif
386   const int kStreamAnalogLevelReference = 100;
387   const float kOutputReference[] = {-0.005859f, -0.004120f, -0.002594f};
388   RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100,
389                       30, true, 0, 100, kStreamAnalogLevelReference,
390                       kOutputReference);
391 }
392 
393 }  // namespace webrtc
394