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 #ifndef MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_APPLIER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_APPLIER_H_
13 
14 #include "modules/audio_processing/agc2/agc2_common.h"
15 #include "modules/audio_processing/agc2/gain_applier.h"
16 #include "modules/audio_processing/agc2/vad_with_level.h"
17 #include "modules/audio_processing/include/audio_frame_view.h"
18 
19 namespace webrtc {
20 
21 class ApmDataDumper;
22 
23 struct SignalWithLevels {
24   SignalWithLevels(AudioFrameView<float> float_frame);
25   SignalWithLevels(const SignalWithLevels&);
26 
27   float input_level_dbfs = -1.f;
28   float input_noise_level_dbfs = -1.f;
29   VadWithLevel::LevelAndProbability vad_result;
30   float limiter_audio_level_dbfs = -1.f;
31   bool estimate_is_confident = false;
32   AudioFrameView<float> float_frame;
33 };
34 
35 class AdaptiveDigitalGainApplier {
36  public:
37   explicit AdaptiveDigitalGainApplier(ApmDataDumper* apm_data_dumper);
38   // Decide what gain to apply.
39   void Process(SignalWithLevels signal_with_levels);
40 
41  private:
42   float last_gain_db_ = kInitialAdaptiveDigitalGainDb;
43   GainApplier gain_applier_;
44   int calls_since_last_gain_log_ = 0;
45 
46   // For some combinations of noise and speech probability, increasing
47   // the level is not allowed. Since we may get VAD results in bursts,
48   // we keep track of this variable until the next VAD results come
49   // in.
50   bool gain_increase_allowed_ = true;
51   ApmDataDumper* apm_data_dumper_ = nullptr;
52 };
53 }  // namespace webrtc
54 
55 #endif  // MODULES_AUDIO_PROCESSING_AGC2_ADAPTIVE_DIGITAL_GAIN_APPLIER_H_
56