blob: a6256cbe46ab4a5c30d04c8421c66583fa07fce6 [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
11#include "webrtc/modules/audio_processing/agc/agc.h"
12
13#include <cmath>
14#include <cstdlib>
15
16#include <algorithm>
aluebsecf6b812015-06-25 12:28:48 -070017#include <vector>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018
aluebsecf6b812015-06-25 12:28:48 -070019#include "webrtc/base/checks.h"
peahbbe42332016-06-08 06:42:02 -070020#include "webrtc/modules/audio_processing/agc/loudness_histogram.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000021#include "webrtc/modules/audio_processing/agc/utility.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023
24namespace webrtc {
25namespace {
26
27const int kDefaultLevelDbfs = -18;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000028const int kNumAnalysisFrames = 100;
29const double kActivityThreshold = 0.3;
30
31} // namespace
32
33Agc::Agc()
34 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
pbos@webrtc.org788acd12014-12-15 09:41:24 +000035 target_level_dbfs_(kDefaultLevelDbfs),
peahbbe42332016-06-08 06:42:02 -070036 histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)),
37 inactive_histogram_(LoudnessHistogram::Create()) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000038
39Agc::~Agc() {}
40
Peter Kastingdce40cf2015-08-24 14:52:23 -070041float Agc::AnalyzePreproc(const int16_t* audio, size_t length) {
kwiberg9e2be5f2016-09-14 05:23:22 -070042 RTC_DCHECK_GT(length, 0u);
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t num_clipped = 0;
44 for (size_t i = 0; i < length; ++i) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045 if (audio[i] == 32767 || audio[i] == -32768)
46 ++num_clipped;
47 }
48 return 1.0f * num_clipped / length;
49}
50
Peter Kastingdce40cf2015-08-24 14:52:23 -070051int Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
aluebsecf6b812015-06-25 12:28:48 -070052 vad_.ProcessChunk(audio, length, sample_rate_hz);
53 const std::vector<double>& rms = vad_.chunkwise_rms();
54 const std::vector<double>& probabilities =
55 vad_.chunkwise_voice_probabilities();
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_DCHECK_EQ(rms.size(), probabilities.size());
aluebsecf6b812015-06-25 12:28:48 -070057 for (size_t i = 0; i < rms.size(); ++i) {
58 histogram_->Update(rms[i], probabilities[i]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059 }
60 return 0;
61}
62
63bool Agc::GetRmsErrorDb(int* error) {
64 if (!error) {
kwiberg9e2be5f2016-09-14 05:23:22 -070065 RTC_NOTREACHED();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000066 return false;
67 }
68
69 if (histogram_->num_updates() < kNumAnalysisFrames) {
70 // We haven't yet received enough frames.
71 return false;
72 }
73
74 if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
75 // We are likely in an inactive segment.
76 return false;
77 }
78
79 double loudness = Linear2Loudness(histogram_->CurrentRms());
80 *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
81 histogram_->Reset();
82 return true;
83}
84
85void Agc::Reset() {
86 histogram_->Reset();
87}
88
89int Agc::set_target_level_dbfs(int level) {
90 // TODO(turajs): just some arbitrary sanity check. We can come up with better
91 // limits. The upper limit should be chosen such that the risk of clipping is
92 // low. The lower limit should not result in a too quiet signal.
93 if (level >= 0 || level <= -100)
94 return -1;
95 target_level_dbfs_ = level;
96 target_level_loudness_ = Dbfs2Loudness(level);
97 return 0;
98}
99
kwiberg8cf88982016-08-26 14:50:38 -0700100int Agc::target_level_dbfs() const {
101 return target_level_dbfs_;
102}
103
104float Agc::voice_probability() const {
105 return vad_.last_voice_probability();
106}
107
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000108} // namespace webrtc