• 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/adaptive_mode_level_estimator_agc.h"
12 
13 #include <cmath>
14 #include <vector>
15 
16 #include "modules/audio_processing/agc2/agc2_common.h"
17 #include "modules/audio_processing/include/audio_frame_view.h"
18 
19 namespace webrtc {
20 
AdaptiveModeLevelEstimatorAgc(ApmDataDumper * apm_data_dumper)21 AdaptiveModeLevelEstimatorAgc::AdaptiveModeLevelEstimatorAgc(
22     ApmDataDumper* apm_data_dumper)
23     : level_estimator_(apm_data_dumper) {
24   set_target_level_dbfs(kDefaultAgc2LevelHeadroomDbfs);
25 }
26 
27 // |audio| must be mono; in a multi-channel stream, provide the first (usually
28 // left) channel.
Process(const int16_t * audio,size_t length,int sample_rate_hz)29 void AdaptiveModeLevelEstimatorAgc::Process(const int16_t* audio,
30                                             size_t length,
31                                             int sample_rate_hz) {
32   std::vector<float> float_audio_frame(audio, audio + length);
33   const float* const first_channel = &float_audio_frame[0];
34   AudioFrameView<const float> frame_view(&first_channel, 1 /* num channels */,
35                                          length);
36   const auto vad_prob = agc2_vad_.AnalyzeFrame(frame_view);
37   latest_voice_probability_ = vad_prob.speech_probability;
38   if (latest_voice_probability_ > kVadConfidenceThreshold) {
39     time_in_ms_since_last_estimate_ += kFrameDurationMs;
40   }
41   level_estimator_.UpdateEstimation(vad_prob);
42 }
43 
44 // Retrieves the difference between the target RMS level and the current
45 // signal RMS level in dB. Returns true if an update is available and false
46 // otherwise, in which case |error| should be ignored and no action taken.
GetRmsErrorDb(int * error)47 bool AdaptiveModeLevelEstimatorAgc::GetRmsErrorDb(int* error) {
48   if (time_in_ms_since_last_estimate_ <= kTimeUntilConfidentMs) {
49     return false;
50   }
51   *error = std::floor(target_level_dbfs() -
52                       level_estimator_.LatestLevelEstimate() + 0.5f);
53   time_in_ms_since_last_estimate_ = 0;
54   return true;
55 }
56 
Reset()57 void AdaptiveModeLevelEstimatorAgc::Reset() {
58   level_estimator_.Reset();
59 }
60 
voice_probability() const61 float AdaptiveModeLevelEstimatorAgc::voice_probability() const {
62   return latest_voice_probability_;
63 }
64 
65 }  // namespace webrtc
66