summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/gain_control_unittest.cc
blob: 1662dc506f1f849fcaf4c7dac30d70f82cdf218a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include <vector>

#include "api/array_view.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_processing/gain_control_impl.h"
#include "modules/audio_processing/test/audio_buffer_tools.h"
#include "modules/audio_processing/test/bitexactness_tools.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

const int kNumFramesToProcess = 100;

void ProcessOneFrame(int sample_rate_hz,
                     AudioBuffer* render_audio_buffer,
                     AudioBuffer* capture_audio_buffer,
                     GainControlImpl* gain_controller) {
  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
    render_audio_buffer->SplitIntoFrequencyBands();
    capture_audio_buffer->SplitIntoFrequencyBands();
  }

  std::vector<int16_t> render_audio;
  GainControlImpl::PackRenderAudioBuffer(*render_audio_buffer, &render_audio);
  gain_controller->ProcessRenderAudio(render_audio);
  gain_controller->AnalyzeCaptureAudio(*capture_audio_buffer);
  gain_controller->ProcessCaptureAudio(capture_audio_buffer, false);

  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
    capture_audio_buffer->MergeFrequencyBands();
  }
}

void 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) {
  gain_controller->Initialize(1, sample_rate_hz);
  GainControl* gc = static_cast<GainControl*>(gain_controller);
  gc->set_mode(mode);
  gc->set_stream_analog_level(stream_analog_level);
  gc->set_target_level_dbfs(target_level_dbfs);
  gc->set_compression_gain_db(compression_gain_db);
  gc->enable_limiter(enable_limiter);
  gc->set_analog_level_limits(analog_level_min, analog_level_max);
}

void 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) {
  GainControlImpl gain_controller;
  SetupComponent(sample_rate_hz, mode, target_level_dbfs, stream_analog_level,
                 compression_gain_db, enable_limiter, analog_level_min,
                 analog_level_max, &gain_controller);

  const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
  const StreamConfig render_config(sample_rate_hz, num_channels);
  AudioBuffer render_buffer(
      render_config.sample_rate_hz(), render_config.num_channels(),
      render_config.sample_rate_hz(), 1, render_config.sample_rate_hz(), 1);
  test::InputAudioFile render_file(
      test::GetApmRenderTestVectorFileName(sample_rate_hz));
  std::vector<float> render_input(samples_per_channel * num_channels);

  const StreamConfig capture_config(sample_rate_hz, num_channels);
  AudioBuffer capture_buffer(
      capture_config.sample_rate_hz(), capture_config.num_channels(),
      capture_config.sample_rate_hz(), 1, capture_config.sample_rate_hz(), 1);
  test::InputAudioFile capture_file(
      test::GetApmCaptureTestVectorFileName(sample_rate_hz));
  std::vector<float> capture_input(samples_per_channel * num_channels);

  for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
                                   &render_file, render_input);
    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
                                   &capture_file, capture_input);

    test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer);
    test::CopyVectorToAudioBuffer(capture_config, capture_input,
                                  &capture_buffer);

    ProcessOneFrame(sample_rate_hz, &render_buffer, &capture_buffer,
                    &gain_controller);
  }

  // Extract and verify the test results.
  std::vector<float> capture_output;
  test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
                                     &capture_output);

  EXPECT_EQ(achieved_stream_analog_level_reference,
            gain_controller.stream_analog_level());

  // Compare the output with the reference. Only the first values of the output
  // from last frame processed are compared in order not having to specify all
  // preceeding frames as testvectors. As the algorithm being tested has a
  // memory, testing only the last frame implicitly also tests the preceeding
  // frames.
  const float kElementErrorBound = 1.0f / 32768.0f;
  EXPECT_TRUE(test::VerifyDeinterleavedArray(
      capture_config.num_frames(), capture_config.num_channels(),
      output_reference, capture_output, kElementErrorBound));
}

}  // namespace

// TODO(peah): Activate all these tests for ARM and ARM64 once the issue on the
// Chromium ARM and ARM64 boths have been identified. This is tracked in the
// issue https://bugs.chromium.org/p/webrtc/issues/detail?id=5711.

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Stereo16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Stereo16kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.027313f, -0.015900f, -0.028107f,
                                    -0.027313f, -0.015900f, -0.028107f};
  RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono32kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono32kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono48kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono48kHz_AdaptiveAnalog_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.010162f, -0.009155f, -0.008301f};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.003967f, -0.002777f, -0.001770f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Stereo16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Stereo16kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.015411f, -0.008972f, -0.015839f,
                                    -0.015411f, -0.008972f, -0.015839f};
  RunBitExactnessTest(16000, 2, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono32kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono32kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.006134f, -0.005524f, -0.005005f};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono48kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono48kHz_AdaptiveDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.006134f, -0.005524f, -0.005005};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kAdaptiveDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.011749f, -0.008270f, -0.005219f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Stereo16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Stereo16kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.048896f, -0.028479f, -0.050345f,
                                    -0.048896f, -0.028479f, -0.050345f};
  RunBitExactnessTest(16000, 2, GainControl::Mode::kFixedDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono32kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono32kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.018158f, -0.016357f, -0.014832f};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono48kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono48kHz_FixedDigital_Tl10_SL50_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 50;
  const float kOutputReference[] = {-0.018158f, -0.016357f, -0.014832f};
  RunBitExactnessTest(32000, 1, GainControl::Mode::kFixedDigital, 10, 50, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveAnalog_Tl10_SL10_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL10_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 12;
  const float kOutputReference[] = {-0.006561f, -0.004608f, -0.002899f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 10, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveAnalog_Tl10_SL100_CG5_Lim_AL70_80) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveAnalog_Tl10_SL100_CG5_Lim_AL70_80) {
#endif
  const int kStreamAnalogLevelReference = 100;
  const float kOutputReference[] = {-0.003998f, -0.002808f, -0.001770f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveAnalog, 10, 100, 5,
                      true, 70, 80, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveDigital_Tl10_SL100_CG5_NoLim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL100_CG5_NoLim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 100;
  const float kOutputReference[] = {-0.004028f, -0.002838f, -0.001770f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100, 5,
                      false, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveDigital_Tl40_SL100_CG5_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveDigital_Tl40_SL100_CG5_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 100;
  const float kOutputReference[] = {-0.008728f, -0.006134f, -0.003845f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 40, 100, 5,
                      true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

#if !(defined(WEBRTC_ARCH_ARM64) || defined(WEBRTC_ARCH_ARM) || \
      defined(WEBRTC_ANDROID))
TEST(GainControlBitExactnessTest,
     Mono16kHz_AdaptiveDigital_Tl10_SL100_CG30_Lim_AL0_100) {
#else
TEST(GainControlBitExactnessTest,
     DISABLED_Mono16kHz_AdaptiveDigital_Tl10_SL100_CG30_Lim_AL0_100) {
#endif
  const int kStreamAnalogLevelReference = 100;
  const float kOutputReference[] = {-0.005859f, -0.004120f, -0.002594f};
  RunBitExactnessTest(16000, 1, GainControl::Mode::kAdaptiveDigital, 10, 100,
                      30, true, 0, 100, kStreamAnalogLevelReference,
                      kOutputReference);
}

}  // namespace webrtc