blob: fc78f07ebb0a2c813b5b8fc28b9a5a67d8bb028e [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"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020#include "webrtc/modules/audio_processing/agc/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),
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036 histogram_(Histogram::Create(kNumAnalysisFrames)),
aluebsecf6b812015-06-25 12:28:48 -070037 inactive_histogram_(Histogram::Create()) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000038 }
39
40Agc::~Agc() {}
41
Peter Kastingdce40cf2015-08-24 14:52:23 -070042float Agc::AnalyzePreproc(const int16_t* audio, size_t length) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000043 assert(length > 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -070044 size_t num_clipped = 0;
45 for (size_t i = 0; i < length; ++i) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000046 if (audio[i] == 32767 || audio[i] == -32768)
47 ++num_clipped;
48 }
49 return 1.0f * num_clipped / length;
50}
51
Peter Kastingdce40cf2015-08-24 14:52:23 -070052int Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
aluebsecf6b812015-06-25 12:28:48 -070053 vad_.ProcessChunk(audio, length, sample_rate_hz);
54 const std::vector<double>& rms = vad_.chunkwise_rms();
55 const std::vector<double>& probabilities =
56 vad_.chunkwise_voice_probabilities();
henrikg91d6ede2015-09-17 00:24:34 -070057 RTC_DCHECK_EQ(rms.size(), probabilities.size());
aluebsecf6b812015-06-25 12:28:48 -070058 for (size_t i = 0; i < rms.size(); ++i) {
59 histogram_->Update(rms[i], probabilities[i]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060 }
61 return 0;
62}
63
64bool Agc::GetRmsErrorDb(int* error) {
65 if (!error) {
66 assert(false);
67 return false;
68 }
69
70 if (histogram_->num_updates() < kNumAnalysisFrames) {
71 // We haven't yet received enough frames.
72 return false;
73 }
74
75 if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
76 // We are likely in an inactive segment.
77 return false;
78 }
79
80 double loudness = Linear2Loudness(histogram_->CurrentRms());
81 *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
82 histogram_->Reset();
83 return true;
84}
85
86void Agc::Reset() {
87 histogram_->Reset();
88}
89
90int Agc::set_target_level_dbfs(int level) {
91 // TODO(turajs): just some arbitrary sanity check. We can come up with better
92 // limits. The upper limit should be chosen such that the risk of clipping is
93 // low. The lower limit should not result in a too quiet signal.
94 if (level >= 0 || level <= -100)
95 return -1;
96 target_level_dbfs_ = level;
97 target_level_loudness_ = Dbfs2Loudness(level);
98 return 0;
99}
100
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000101} // namespace webrtc