blob: 12c8cfb7fbd1731d782ffc4d9312d4e488d976d2 [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>
15
16#include <algorithm>
aluebsecf6b812015-06-25 12:28:48 -070017#include <vector>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/agc/loudness_histogram.h"
20#include "modules/audio_processing/agc/utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000022
23namespace webrtc {
24namespace {
25
26const int kDefaultLevelDbfs = -18;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000027const int kNumAnalysisFrames = 100;
28const double kActivityThreshold = 0.3;
29
30} // namespace
31
32Agc::Agc()
33 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
pbos@webrtc.org788acd12014-12-15 09:41:24 +000034 target_level_dbfs_(kDefaultLevelDbfs),
peahbbe42332016-06-08 06:42:02 -070035 histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)),
36 inactive_histogram_(LoudnessHistogram::Create()) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037
38Agc::~Agc() {}
39
Peter Kastingdce40cf2015-08-24 14:52:23 -070040float Agc::AnalyzePreproc(const int16_t* audio, size_t length) {
kwibergaf476c72016-11-28 15:21:39 -080041 RTC_DCHECK_GT(length, 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 size_t num_clipped = 0;
43 for (size_t i = 0; i < length; ++i) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000044 if (audio[i] == 32767 || audio[i] == -32768)
45 ++num_clipped;
46 }
47 return 1.0f * num_clipped / length;
48}
49
Jonas Olsson645b0272018-02-15 15:16:27 +010050void Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
aluebsecf6b812015-06-25 12:28:48 -070051 vad_.ProcessChunk(audio, length, sample_rate_hz);
52 const std::vector<double>& rms = vad_.chunkwise_rms();
53 const std::vector<double>& probabilities =
54 vad_.chunkwise_voice_probabilities();
henrikg91d6ede2015-09-17 00:24:34 -070055 RTC_DCHECK_EQ(rms.size(), probabilities.size());
aluebsecf6b812015-06-25 12:28:48 -070056 for (size_t i = 0; i < rms.size(); ++i) {
57 histogram_->Update(rms[i], probabilities[i]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000058 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059}
60
61bool Agc::GetRmsErrorDb(int* error) {
62 if (!error) {
kwiberg9e2be5f2016-09-14 05:23:22 -070063 RTC_NOTREACHED();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000064 return false;
65 }
66
67 if (histogram_->num_updates() < kNumAnalysisFrames) {
68 // We haven't yet received enough frames.
69 return false;
70 }
71
72 if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
73 // We are likely in an inactive segment.
74 return false;
75 }
76
77 double loudness = Linear2Loudness(histogram_->CurrentRms());
78 *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
79 histogram_->Reset();
80 return true;
81}
82
83void Agc::Reset() {
84 histogram_->Reset();
85}
86
87int Agc::set_target_level_dbfs(int level) {
88 // TODO(turajs): just some arbitrary sanity check. We can come up with better
89 // limits. The upper limit should be chosen such that the risk of clipping is
90 // low. The lower limit should not result in a too quiet signal.
91 if (level >= 0 || level <= -100)
92 return -1;
93 target_level_dbfs_ = level;
94 target_level_loudness_ = Dbfs2Loudness(level);
95 return 0;
96}
97
kwiberg8cf88982016-08-26 14:50:38 -070098int Agc::target_level_dbfs() const {
99 return target_level_dbfs_;
100}
101
102float Agc::voice_probability() const {
103 return vad_.last_voice_probability();
104}
105
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000106} // namespace webrtc