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/adaptive_digital_gain_applier.h"
12 
13 #include <algorithm>
14 
15 #include "common_audio/include/audio_util.h"
16 #include "modules/audio_processing/agc2/agc2_common.h"
17 #include "modules/audio_processing/logging/apm_data_dumper.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/numerics/safe_minmax.h"
20 #include "system_wrappers/include/metrics.h"
21 
22 namespace webrtc {
23 namespace {
24 
25 // This function maps input level to desired applied gain. We want to
26 // boost the signal so that peaks are at -kHeadroomDbfs. We can't
27 // apply more than kMaxGainDb gain.
ComputeGainDb(float input_level_dbfs)28 float ComputeGainDb(float input_level_dbfs) {
29   // If the level is very low, boost it as much as we can.
30   if (input_level_dbfs < -(kHeadroomDbfs + kMaxGainDb)) {
31     return kMaxGainDb;
32   }
33 
34   // We expect to end up here most of the time: the level is below
35   // -headroom, but we can boost it to -headroom.
36   if (input_level_dbfs < -kHeadroomDbfs) {
37     return -kHeadroomDbfs - input_level_dbfs;
38   }
39 
40   // Otherwise, the level is too high and we can't boost. The
41   // LevelEstimator is responsible for not reporting bogus gain
42   // values.
43   RTC_DCHECK_LE(input_level_dbfs, 0.f);
44   return 0.f;
45 }
46 
47 // We require 'gain + noise_level <= kMaxNoiseLevelDbfs'.
LimitGainByNoise(float target_gain,float input_noise_level_dbfs,ApmDataDumper * apm_data_dumper)48 float LimitGainByNoise(float target_gain,
49                        float input_noise_level_dbfs,
50                        ApmDataDumper* apm_data_dumper) {
51   const float noise_headroom_db = kMaxNoiseLevelDbfs - input_noise_level_dbfs;
52   apm_data_dumper->DumpRaw("agc2_noise_headroom_db", noise_headroom_db);
53   return std::min(target_gain, std::max(noise_headroom_db, 0.f));
54 }
55 
LimitGainByLowConfidence(float target_gain,float last_gain,float limiter_audio_level_dbfs,bool estimate_is_confident)56 float LimitGainByLowConfidence(float target_gain,
57                                float last_gain,
58                                float limiter_audio_level_dbfs,
59                                bool estimate_is_confident) {
60   if (estimate_is_confident ||
61       limiter_audio_level_dbfs <= kLimiterThresholdForAgcGainDbfs) {
62     return target_gain;
63   }
64   const float limiter_level_before_gain = limiter_audio_level_dbfs - last_gain;
65 
66   // Compute a new gain so that limiter_level_before_gain + new_gain <=
67   // kLimiterThreshold.
68   const float new_target_gain = std::max(
69       kLimiterThresholdForAgcGainDbfs - limiter_level_before_gain, 0.f);
70   return std::min(new_target_gain, target_gain);
71 }
72 
73 // Computes how the gain should change during this frame.
74 // Return the gain difference in db to 'last_gain_db'.
ComputeGainChangeThisFrameDb(float target_gain_db,float last_gain_db,bool gain_increase_allowed)75 float ComputeGainChangeThisFrameDb(float target_gain_db,
76                                    float last_gain_db,
77                                    bool gain_increase_allowed) {
78   float target_gain_difference_db = target_gain_db - last_gain_db;
79   if (!gain_increase_allowed) {
80     target_gain_difference_db = std::min(target_gain_difference_db, 0.f);
81   }
82 
83   return rtc::SafeClamp(target_gain_difference_db, -kMaxGainChangePerFrameDb,
84                         kMaxGainChangePerFrameDb);
85 }
86 }  // namespace
87 
SignalWithLevels(AudioFrameView<float> float_frame)88 SignalWithLevels::SignalWithLevels(AudioFrameView<float> float_frame)
89     : float_frame(float_frame) {}
90 SignalWithLevels::SignalWithLevels(const SignalWithLevels&) = default;
91 
AdaptiveDigitalGainApplier(ApmDataDumper * apm_data_dumper)92 AdaptiveDigitalGainApplier::AdaptiveDigitalGainApplier(
93     ApmDataDumper* apm_data_dumper)
94     : gain_applier_(false, DbToRatio(last_gain_db_)),
95       apm_data_dumper_(apm_data_dumper) {}
96 
Process(SignalWithLevels signal_with_levels)97 void AdaptiveDigitalGainApplier::Process(SignalWithLevels signal_with_levels) {
98   calls_since_last_gain_log_++;
99   if (calls_since_last_gain_log_ == 100) {
100     calls_since_last_gain_log_ = 0;
101     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc2.DigitalGainApplied",
102                                 last_gain_db_, 0, kMaxGainDb, kMaxGainDb + 1);
103     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.Agc2.EstimatedNoiseLevel",
104                                 -signal_with_levels.input_noise_level_dbfs, 0,
105                                 100, 101);
106   }
107 
108   signal_with_levels.input_level_dbfs =
109       std::min(signal_with_levels.input_level_dbfs, 0.f);
110 
111   RTC_DCHECK_GE(signal_with_levels.input_level_dbfs, -150.f);
112   RTC_DCHECK_GE(signal_with_levels.float_frame.num_channels(), 1);
113   RTC_DCHECK_GE(signal_with_levels.float_frame.samples_per_channel(), 1);
114 
115   const float target_gain_db = LimitGainByLowConfidence(
116       LimitGainByNoise(ComputeGainDb(signal_with_levels.input_level_dbfs),
117                        signal_with_levels.input_noise_level_dbfs,
118                        apm_data_dumper_),
119       last_gain_db_, signal_with_levels.limiter_audio_level_dbfs,
120       signal_with_levels.estimate_is_confident);
121 
122   // Forbid increasing the gain when there is no speech.
123   gain_increase_allowed_ = signal_with_levels.vad_result.speech_probability >
124                            kVadConfidenceThreshold;
125 
126   const float gain_change_this_frame_db = ComputeGainChangeThisFrameDb(
127       target_gain_db, last_gain_db_, gain_increase_allowed_);
128 
129   apm_data_dumper_->DumpRaw("agc2_want_to_change_by_db",
130                             target_gain_db - last_gain_db_);
131   apm_data_dumper_->DumpRaw("agc2_will_change_by_db",
132                             gain_change_this_frame_db);
133 
134   // Optimization: avoid calling math functions if gain does not
135   // change.
136   if (gain_change_this_frame_db != 0.f) {
137     gain_applier_.SetGainFactor(
138         DbToRatio(last_gain_db_ + gain_change_this_frame_db));
139   }
140   gain_applier_.ApplyGain(signal_with_levels.float_frame);
141 
142   // Remember that the gain has changed for the next iteration.
143   last_gain_db_ = last_gain_db_ + gain_change_this_frame_db;
144   apm_data_dumper_->DumpRaw("agc2_applied_gain_db", last_gain_db_);
145 }
146 }  // namespace webrtc
147