File: adaptive_mode_level_estimator_agc.cc

package info (click to toggle)
webrtc-audio-processing 1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,112 kB
  • sloc: cpp: 50,766; ansic: 19,793; asm: 236; makefile: 4
file content (65 lines) | stat: -rw-r--r-- 2,413 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_processing/agc2/adaptive_mode_level_estimator_agc.h"

#include <cmath>
#include <vector>

#include "modules/audio_processing/agc2/agc2_common.h"
#include "modules/audio_processing/include/audio_frame_view.h"

namespace webrtc {

AdaptiveModeLevelEstimatorAgc::AdaptiveModeLevelEstimatorAgc(
    ApmDataDumper* apm_data_dumper)
    : level_estimator_(apm_data_dumper) {
  set_target_level_dbfs(kDefaultAgc2LevelHeadroomDbfs);
}

// |audio| must be mono; in a multi-channel stream, provide the first (usually
// left) channel.
void AdaptiveModeLevelEstimatorAgc::Process(const int16_t* audio,
                                            size_t length,
                                            int sample_rate_hz) {
  std::vector<float> float_audio_frame(audio, audio + length);
  const float* const first_channel = &float_audio_frame[0];
  AudioFrameView<const float> frame_view(&first_channel, 1 /* num channels */,
                                         length);
  const auto vad_prob = agc2_vad_.AnalyzeFrame(frame_view);
  latest_voice_probability_ = vad_prob.speech_probability;
  if (latest_voice_probability_ > kVadConfidenceThreshold) {
    time_in_ms_since_last_estimate_ += kFrameDurationMs;
  }
  level_estimator_.Update(vad_prob);
}

// Retrieves the difference between the target RMS level and the current
// signal RMS level in dB. Returns true if an update is available and false
// otherwise, in which case |error| should be ignored and no action taken.
bool AdaptiveModeLevelEstimatorAgc::GetRmsErrorDb(int* error) {
  if (time_in_ms_since_last_estimate_ <= kTimeUntilConfidentMs) {
    return false;
  }
  *error =
      std::floor(target_level_dbfs() - level_estimator_.level_dbfs() + 0.5f);
  time_in_ms_since_last_estimate_ = 0;
  return true;
}

void AdaptiveModeLevelEstimatorAgc::Reset() {
  level_estimator_.Reset();
}

float AdaptiveModeLevelEstimatorAgc::voice_probability() const {
  return latest_voice_probability_;
}

}  // namespace webrtc