blob: a89ae111ea19fed38e412708f1bc4667a737f32a [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/agc/agc.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
13#include <cmath>
14#include <cstdlib>
aluebsecf6b812015-06-25 12:28:48 -070015#include <vector>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/agc/loudness_histogram.h"
18#include "modules/audio_processing/agc/utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
21namespace webrtc {
22namespace {
23
24const int kDefaultLevelDbfs = -18;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000025const int kNumAnalysisFrames = 100;
26const double kActivityThreshold = 0.3;
27
28} // namespace
29
30Agc::Agc()
31 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
pbos@webrtc.org788acd12014-12-15 09:41:24 +000032 target_level_dbfs_(kDefaultLevelDbfs),
peahbbe42332016-06-08 06:42:02 -070033 histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)),
34 inactive_histogram_(LoudnessHistogram::Create()) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000035
Per Ã…hgren361d1c32019-11-06 22:17:14 +010036Agc::~Agc() = default;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037
Jonas Olsson645b0272018-02-15 15:16:27 +010038void Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
aluebsecf6b812015-06-25 12:28:48 -070039 vad_.ProcessChunk(audio, length, sample_rate_hz);
40 const std::vector<double>& rms = vad_.chunkwise_rms();
41 const std::vector<double>& probabilities =
42 vad_.chunkwise_voice_probabilities();
henrikg91d6ede2015-09-17 00:24:34 -070043 RTC_DCHECK_EQ(rms.size(), probabilities.size());
aluebsecf6b812015-06-25 12:28:48 -070044 for (size_t i = 0; i < rms.size(); ++i) {
45 histogram_->Update(rms[i], probabilities[i]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000046 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +000047}
48
49bool Agc::GetRmsErrorDb(int* error) {
50 if (!error) {
kwiberg9e2be5f2016-09-14 05:23:22 -070051 RTC_NOTREACHED();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000052 return false;
53 }
54
55 if (histogram_->num_updates() < kNumAnalysisFrames) {
56 // We haven't yet received enough frames.
57 return false;
58 }
59
60 if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
61 // We are likely in an inactive segment.
62 return false;
63 }
64
65 double loudness = Linear2Loudness(histogram_->CurrentRms());
66 *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
67 histogram_->Reset();
68 return true;
69}
70
71void Agc::Reset() {
72 histogram_->Reset();
73}
74
75int Agc::set_target_level_dbfs(int level) {
76 // TODO(turajs): just some arbitrary sanity check. We can come up with better
77 // limits. The upper limit should be chosen such that the risk of clipping is
78 // low. The lower limit should not result in a too quiet signal.
79 if (level >= 0 || level <= -100)
80 return -1;
81 target_level_dbfs_ = level;
82 target_level_loudness_ = Dbfs2Loudness(level);
83 return 0;
84}
85
kwiberg8cf88982016-08-26 14:50:38 -070086int Agc::target_level_dbfs() const {
87 return target_level_dbfs_;
88}
89
90float Agc::voice_probability() const {
91 return vad_.last_voice_probability();
92}
93
pbos@webrtc.org788acd12014-12-15 09:41:24 +000094} // namespace webrtc