File: gain_applier_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 3,563 bytes parent folder | download | duplicates (8)
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
/*
 *  Copyright (c) 2018 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 "modules/audio_processing/agc2/gain_applier.h"

#include <math.h>

#include <algorithm>
#include <limits>

#include "api/audio/audio_view.h"
#include "modules/audio_processing/agc2/vector_float_frame.h"
#include "rtc_base/gunit.h"

namespace webrtc {
TEST(AutomaticGainController2GainApplier, InitialGainIsRespected) {
  constexpr float initial_signal_level = 123.f;
  constexpr float gain_factor = 10.f;
  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
  GainApplier gain_applier(true, gain_factor);

  auto fake_view = fake_audio.view();
  gain_applier.ApplyGain(fake_audio.view());
  EXPECT_NEAR(fake_view[0][0], initial_signal_level * gain_factor, 0.1f);
}

TEST(AutomaticGainController2GainApplier, ClippingIsDone) {
  constexpr float initial_signal_level = 30000.f;
  constexpr float gain_factor = 10.f;
  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
  GainApplier gain_applier(true, gain_factor);

  gain_applier.ApplyGain(fake_audio.view());
  EXPECT_NEAR(fake_audio.view()[0][0], std::numeric_limits<int16_t>::max(),
              0.1f);
}

TEST(AutomaticGainController2GainApplier, ClippingIsNotDone) {
  constexpr float initial_signal_level = 30000.f;
  constexpr float gain_factor = 10.f;
  VectorFloatFrame fake_audio(1, 1, initial_signal_level);
  GainApplier gain_applier(false, gain_factor);

  gain_applier.ApplyGain(fake_audio.view());

  EXPECT_NEAR(fake_audio.view()[0][0], initial_signal_level * gain_factor,
              0.1f);
}

TEST(AutomaticGainController2GainApplier, RampingIsDone) {
  constexpr float initial_signal_level = 30000.f;
  constexpr float initial_gain_factor = 1.f;
  constexpr float target_gain_factor = 0.5f;
  constexpr int num_channels = 3;
  constexpr int samples_per_channel = 4;
  VectorFloatFrame fake_audio(num_channels, samples_per_channel,
                              initial_signal_level);
  GainApplier gain_applier(false, initial_gain_factor);

  gain_applier.SetGainFactor(target_gain_factor);
  gain_applier.ApplyGain(fake_audio.view());

  // The maximal gain change should be close to that in linear interpolation.
  for (size_t channel = 0; channel < num_channels; ++channel) {
    float max_signal_change = 0.f;
    float last_signal_level = initial_signal_level;
    for (const auto sample : fake_audio.view()[channel]) {
      const float current_change = fabs(last_signal_level - sample);
      max_signal_change = std::max(max_signal_change, current_change);
      last_signal_level = sample;
    }
    const float total_gain_change =
        fabs((initial_gain_factor - target_gain_factor) * initial_signal_level);
    EXPECT_NEAR(max_signal_change, total_gain_change / samples_per_channel,
                0.1f);
  }

  // Next frame should have the desired level.
  VectorFloatFrame next_fake_audio_frame(num_channels, samples_per_channel,
                                         initial_signal_level);
  gain_applier.ApplyGain(next_fake_audio_frame.view());

  // The last sample should have the new gain.
  EXPECT_NEAR(next_fake_audio_frame.view()[0][0],
              initial_signal_level * target_gain_factor, 0.1f);
}
}  // namespace webrtc