• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 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/agc2/gain_applier.h"
12 
13 #include <math.h>
14 
15 #include <algorithm>
16 #include <limits>
17 
18 #include "modules/audio_processing/agc2/vector_float_frame.h"
19 #include "rtc_base/gunit.h"
20 
21 namespace webrtc {
TEST(AutomaticGainController2GainApplier,InitialGainIsRespected)22 TEST(AutomaticGainController2GainApplier, InitialGainIsRespected) {
23   constexpr float initial_signal_level = 123.f;
24   constexpr float gain_factor = 10.f;
25   VectorFloatFrame fake_audio(1, 1, initial_signal_level);
26   GainApplier gain_applier(true, gain_factor);
27 
28   gain_applier.ApplyGain(fake_audio.float_frame_view());
29   EXPECT_NEAR(fake_audio.float_frame_view().channel(0)[0],
30               initial_signal_level * gain_factor, 0.1f);
31 }
32 
TEST(AutomaticGainController2GainApplier,ClippingIsDone)33 TEST(AutomaticGainController2GainApplier, ClippingIsDone) {
34   constexpr float initial_signal_level = 30000.f;
35   constexpr float gain_factor = 10.f;
36   VectorFloatFrame fake_audio(1, 1, initial_signal_level);
37   GainApplier gain_applier(true, gain_factor);
38 
39   gain_applier.ApplyGain(fake_audio.float_frame_view());
40   EXPECT_NEAR(fake_audio.float_frame_view().channel(0)[0],
41               std::numeric_limits<int16_t>::max(), 0.1f);
42 }
43 
TEST(AutomaticGainController2GainApplier,ClippingIsNotDone)44 TEST(AutomaticGainController2GainApplier, ClippingIsNotDone) {
45   constexpr float initial_signal_level = 30000.f;
46   constexpr float gain_factor = 10.f;
47   VectorFloatFrame fake_audio(1, 1, initial_signal_level);
48   GainApplier gain_applier(false, gain_factor);
49 
50   gain_applier.ApplyGain(fake_audio.float_frame_view());
51 
52   EXPECT_NEAR(fake_audio.float_frame_view().channel(0)[0],
53               initial_signal_level * gain_factor, 0.1f);
54 }
55 
TEST(AutomaticGainController2GainApplier,RampingIsDone)56 TEST(AutomaticGainController2GainApplier, RampingIsDone) {
57   constexpr float initial_signal_level = 30000.f;
58   constexpr float initial_gain_factor = 1.f;
59   constexpr float target_gain_factor = 0.5f;
60   constexpr int num_channels = 3;
61   constexpr int samples_per_channel = 4;
62   VectorFloatFrame fake_audio(num_channels, samples_per_channel,
63                               initial_signal_level);
64   GainApplier gain_applier(false, initial_gain_factor);
65 
66   gain_applier.SetGainFactor(target_gain_factor);
67   gain_applier.ApplyGain(fake_audio.float_frame_view());
68 
69   // The maximal gain change should be close to that in linear interpolation.
70   for (size_t channel = 0; channel < num_channels; ++channel) {
71     float max_signal_change = 0.f;
72     float last_signal_level = initial_signal_level;
73     for (const auto sample : fake_audio.float_frame_view().channel(channel)) {
74       const float current_change = fabs(last_signal_level - sample);
75       max_signal_change = std::max(max_signal_change, current_change);
76       last_signal_level = sample;
77     }
78     const float total_gain_change =
79         fabs((initial_gain_factor - target_gain_factor) * initial_signal_level);
80     EXPECT_NEAR(max_signal_change, total_gain_change / samples_per_channel,
81                 0.1f);
82   }
83 
84   // Next frame should have the desired level.
85   VectorFloatFrame next_fake_audio_frame(num_channels, samples_per_channel,
86                                          initial_signal_level);
87   gain_applier.ApplyGain(next_fake_audio_frame.float_frame_view());
88 
89   // The last sample should have the new gain.
90   EXPECT_NEAR(next_fake_audio_frame.float_frame_view().channel(0)[0],
91               initial_signal_level * target_gain_factor, 0.1f);
92 }
93 }  // namespace webrtc
94