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_agc.h"
12 
13 #include "common_audio/include/audio_util.h"
14 #include "modules/audio_processing/agc2/vad_with_level.h"
15 #include "modules/audio_processing/logging/apm_data_dumper.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
AdaptiveAgc(ApmDataDumper * apm_data_dumper)20 AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper)
21     : speech_level_estimator_(apm_data_dumper),
22       gain_applier_(apm_data_dumper),
23       apm_data_dumper_(apm_data_dumper),
24       noise_level_estimator_(apm_data_dumper) {
25   RTC_DCHECK(apm_data_dumper);
26 }
27 
AdaptiveAgc(ApmDataDumper * apm_data_dumper,const AudioProcessing::Config::GainController2 & config)28 AdaptiveAgc::AdaptiveAgc(ApmDataDumper* apm_data_dumper,
29                          const AudioProcessing::Config::GainController2& config)
30     : speech_level_estimator_(
31           apm_data_dumper,
32           config.adaptive_digital.level_estimator,
33           config.adaptive_digital.use_saturation_protector,
34           config.adaptive_digital.extra_saturation_margin_db),
35       gain_applier_(apm_data_dumper),
36       apm_data_dumper_(apm_data_dumper),
37       noise_level_estimator_(apm_data_dumper) {
38   RTC_DCHECK(apm_data_dumper);
39 }
40 
41 AdaptiveAgc::~AdaptiveAgc() = default;
42 
Process(AudioFrameView<float> float_frame,float last_audio_level)43 void AdaptiveAgc::Process(AudioFrameView<float> float_frame,
44                           float last_audio_level) {
45   auto signal_with_levels = SignalWithLevels(float_frame);
46   signal_with_levels.vad_result = vad_.AnalyzeFrame(float_frame);
47   apm_data_dumper_->DumpRaw("agc2_vad_probability",
48                             signal_with_levels.vad_result.speech_probability);
49   apm_data_dumper_->DumpRaw("agc2_vad_rms_dbfs",
50                             signal_with_levels.vad_result.speech_rms_dbfs);
51   apm_data_dumper_->DumpRaw("agc2_vad_peak_dbfs",
52                             signal_with_levels.vad_result.speech_peak_dbfs);
53 
54   speech_level_estimator_.UpdateEstimation(signal_with_levels.vad_result);
55 
56   signal_with_levels.input_level_dbfs =
57       speech_level_estimator_.LatestLevelEstimate();
58 
59   signal_with_levels.input_noise_level_dbfs =
60       noise_level_estimator_.Analyze(float_frame);
61 
62   apm_data_dumper_->DumpRaw("agc2_noise_estimate_dbfs",
63                             signal_with_levels.input_noise_level_dbfs);
64 
65   signal_with_levels.limiter_audio_level_dbfs =
66       last_audio_level > 0 ? FloatS16ToDbfs(last_audio_level) : -90.f;
67   apm_data_dumper_->DumpRaw("agc2_last_limiter_audio_level",
68                             signal_with_levels.limiter_audio_level_dbfs);
69 
70   signal_with_levels.estimate_is_confident =
71       speech_level_estimator_.LevelEstimationIsConfident();
72 
73   // The gain applier applies the gain.
74   gain_applier_.Process(signal_with_levels);
75 }
76 
Reset()77 void AdaptiveAgc::Reset() {
78   speech_level_estimator_.Reset();
79 }
80 
81 }  // namespace webrtc
82