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 "api/array_view.h"
14 #include "modules/audio_processing/agc2/agc2_common.h"
15 #include "rtc_base/numerics/safe_minmax.h"
16 
17 namespace webrtc {
18 namespace {
19 
20 // Returns true when the gain factor is so close to 1 that it would
21 // not affect int16 samples.
GainCloseToOne(float gain_factor)22 bool GainCloseToOne(float gain_factor) {
23   return 1.f - 1.f / kMaxFloatS16Value <= gain_factor &&
24          gain_factor <= 1.f + 1.f / kMaxFloatS16Value;
25 }
26 
ClipSignal(AudioFrameView<float> signal)27 void ClipSignal(AudioFrameView<float> signal) {
28   for (size_t k = 0; k < signal.num_channels(); ++k) {
29     rtc::ArrayView<float> channel_view = signal.channel(k);
30     for (auto& sample : channel_view) {
31       sample = rtc::SafeClamp(sample, kMinFloatS16Value, kMaxFloatS16Value);
32     }
33   }
34 }
35 
ApplyGainWithRamping(float last_gain_linear,float gain_at_end_of_frame_linear,float inverse_samples_per_channel,AudioFrameView<float> float_frame)36 void ApplyGainWithRamping(float last_gain_linear,
37                           float gain_at_end_of_frame_linear,
38                           float inverse_samples_per_channel,
39                           AudioFrameView<float> float_frame) {
40   // Do not modify the signal.
41   if (last_gain_linear == gain_at_end_of_frame_linear &&
42       GainCloseToOne(gain_at_end_of_frame_linear)) {
43     return;
44   }
45 
46   // Gain is constant and different from 1.
47   if (last_gain_linear == gain_at_end_of_frame_linear) {
48     for (size_t k = 0; k < float_frame.num_channels(); ++k) {
49       rtc::ArrayView<float> channel_view = float_frame.channel(k);
50       for (auto& sample : channel_view) {
51         sample *= gain_at_end_of_frame_linear;
52       }
53     }
54     return;
55   }
56 
57   // The gain changes. We have to change slowly to avoid discontinuities.
58   const float increment = (gain_at_end_of_frame_linear - last_gain_linear) *
59                           inverse_samples_per_channel;
60   float gain = last_gain_linear;
61   for (size_t i = 0; i < float_frame.samples_per_channel(); ++i) {
62     for (size_t ch = 0; ch < float_frame.num_channels(); ++ch) {
63       float_frame.channel(ch)[i] *= gain;
64     }
65     gain += increment;
66   }
67 }
68 
69 }  // namespace
70 
GainApplier(bool hard_clip_samples,float initial_gain_factor)71 GainApplier::GainApplier(bool hard_clip_samples, float initial_gain_factor)
72     : hard_clip_samples_(hard_clip_samples),
73       last_gain_factor_(initial_gain_factor),
74       current_gain_factor_(initial_gain_factor) {}
75 
ApplyGain(AudioFrameView<float> signal)76 void GainApplier::ApplyGain(AudioFrameView<float> signal) {
77   if (static_cast<int>(signal.samples_per_channel()) != samples_per_channel_) {
78     Initialize(signal.samples_per_channel());
79   }
80 
81   ApplyGainWithRamping(last_gain_factor_, current_gain_factor_,
82                        inverse_samples_per_channel_, signal);
83 
84   last_gain_factor_ = current_gain_factor_;
85 
86   if (hard_clip_samples_) {
87     ClipSignal(signal);
88   }
89 }
90 
SetGainFactor(float gain_factor)91 void GainApplier::SetGainFactor(float gain_factor) {
92   RTC_DCHECK_GT(gain_factor, 0.f);
93   current_gain_factor_ = gain_factor;
94 }
95 
Initialize(size_t samples_per_channel)96 void GainApplier::Initialize(size_t samples_per_channel) {
97   RTC_DCHECK_GT(samples_per_channel, 0);
98   samples_per_channel_ = static_cast<int>(samples_per_channel);
99   inverse_samples_per_channel_ = 1.f / samples_per_channel_;
100 }
101 
102 }  // namespace webrtc
103