blob: 298cfd9d88a196c9b22c9694c5f8e5f4cfda4121 [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>
17
18#include "webrtc/common_audio/resampler/include/resampler.h"
19#include "webrtc/modules/audio_processing/agc/agc_audio_proc.h"
20#include "webrtc/modules/audio_processing/agc/common.h"
21#include "webrtc/modules/audio_processing/agc/histogram.h"
22#include "webrtc/modules/audio_processing/agc/pitch_based_vad.h"
23#include "webrtc/modules/audio_processing/agc/standalone_vad.h"
24#include "webrtc/modules/audio_processing/agc/utility.h"
25#include "webrtc/modules/interface/module_common_types.h"
26#include "webrtc/system_wrappers/interface/compile_assert.h"
27
28namespace webrtc {
29namespace {
30
31const int kDefaultLevelDbfs = -18;
32const double kDefaultVoiceValue = 1.0;
33const int kNumAnalysisFrames = 100;
34const double kActivityThreshold = 0.3;
35
36} // namespace
37
38Agc::Agc()
39 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
40 last_voice_probability_(kDefaultVoiceValue),
41 target_level_dbfs_(kDefaultLevelDbfs),
42 standalone_vad_enabled_(true),
43 histogram_(Histogram::Create(kNumAnalysisFrames)),
44 inactive_histogram_(Histogram::Create()),
45 audio_processing_(new AgcAudioProc()),
46 pitch_based_vad_(new PitchBasedVad()),
47 standalone_vad_(StandaloneVad::Create()),
48 // Initialize to the most common resampling situation.
49 resampler_(new Resampler(32000, kSampleRateHz, kResamplerSynchronous)) {
50 }
51
52Agc::~Agc() {}
53
54float Agc::AnalyzePreproc(const int16_t* audio, int length) {
55 assert(length > 0);
56 int num_clipped = 0;
57 for (int i = 0; i < length; ++i) {
58 if (audio[i] == 32767 || audio[i] == -32768)
59 ++num_clipped;
60 }
61 return 1.0f * num_clipped / length;
62}
63
64int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) {
65 assert(length == sample_rate_hz / 100);
66 if (sample_rate_hz > 32000) {
67 return -1;
68 }
69 // Resample to the required rate.
70 int16_t resampled[kLength10Ms];
71 const int16_t* resampled_ptr = audio;
72 if (sample_rate_hz != kSampleRateHz) {
73 if (resampler_->ResetIfNeeded(sample_rate_hz,
74 kSampleRateHz,
75 kResamplerSynchronous) != 0) {
76 return -1;
77 }
78 resampler_->Push(audio, length, resampled, kLength10Ms, length);
79 resampled_ptr = resampled;
80 }
81 assert(length == kLength10Ms);
82
83 if (standalone_vad_enabled_) {
84 if (standalone_vad_->AddAudio(resampled_ptr, length) != 0)
85 return -1;
86 }
87
88 AudioFeatures features;
89 audio_processing_->ExtractFeatures(resampled_ptr, length, &features);
90 if (features.num_frames > 0) {
91 if (features.silence) {
92 // The other features are invalid, so update the histogram with an
93 // arbitrary low value.
94 for (int n = 0; n < features.num_frames; ++n)
95 histogram_->Update(features.rms[n], 0.01);
96 return 0;
97 }
98
99 // Initialize to 0.5 which is a neutral value for combining probabilities,
100 // in case the standalone-VAD is not enabled.
101 double p_combined[] = {0.5, 0.5, 0.5, 0.5};
102 COMPILE_ASSERT(sizeof(p_combined) / sizeof(p_combined[0]) == kMaxNumFrames,
103 combined_probability_incorrect_size);
104 if (standalone_vad_enabled_) {
105 if (standalone_vad_->GetActivity(p_combined, kMaxNumFrames) < 0)
106 return -1;
107 }
108 // If any other VAD is enabled it must be combined before calling the
109 // pitch-based VAD.
110 if (pitch_based_vad_->VoicingProbability(features, p_combined) < 0)
111 return -1;
112 for (int n = 0; n < features.num_frames; n++) {
113 histogram_->Update(features.rms[n], p_combined[n]);
114 last_voice_probability_ = p_combined[n];
115 }
116 }
117 return 0;
118}
119
120bool Agc::GetRmsErrorDb(int* error) {
121 if (!error) {
122 assert(false);
123 return false;
124 }
125
126 if (histogram_->num_updates() < kNumAnalysisFrames) {
127 // We haven't yet received enough frames.
128 return false;
129 }
130
131 if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
132 // We are likely in an inactive segment.
133 return false;
134 }
135
136 double loudness = Linear2Loudness(histogram_->CurrentRms());
137 *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
138 histogram_->Reset();
139 return true;
140}
141
142void Agc::Reset() {
143 histogram_->Reset();
144}
145
146int Agc::set_target_level_dbfs(int level) {
147 // TODO(turajs): just some arbitrary sanity check. We can come up with better
148 // limits. The upper limit should be chosen such that the risk of clipping is
149 // low. The lower limit should not result in a too quiet signal.
150 if (level >= 0 || level <= -100)
151 return -1;
152 target_level_dbfs_ = level;
153 target_level_loudness_ = Dbfs2Loudness(level);
154 return 0;
155}
156
157void Agc::EnableStandaloneVad(bool enable) {
158 standalone_vad_enabled_ = enable;
159}
160
161} // namespace webrtc