Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: webrtc/modules/audio_processing/agc/agc.cc

Issue 1181933002: Pull the Voice Activity Detector out from the AGC (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_processing/agc/agc.h" 11 #include "webrtc/modules/audio_processing/agc/agc.h"
12 12
13 #include <cmath> 13 #include <cmath>
14 #include <cstdlib> 14 #include <cstdlib>
15 15
16 #include <algorithm> 16 #include <algorithm>
17 #include <vector>
17 18
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" 19 #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" 20 #include "webrtc/modules/audio_processing/agc/utility.h"
25 #include "webrtc/modules/interface/module_common_types.h" 21 #include "webrtc/modules/interface/module_common_types.h"
26 22
27 namespace webrtc { 23 namespace webrtc {
28 namespace { 24 namespace {
29 25
30 const int kDefaultLevelDbfs = -18; 26 const int kDefaultLevelDbfs = -18;
31 const double kDefaultVoiceValue = 1.0;
32 const int kNumAnalysisFrames = 100; 27 const int kNumAnalysisFrames = 100;
33 const double kActivityThreshold = 0.3; 28 const double kActivityThreshold = 0.3;
34 29
35 } // namespace 30 } // namespace
36 31
37 Agc::Agc() 32 Agc::Agc()
38 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)), 33 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
39 last_voice_probability_(kDefaultVoiceValue),
40 target_level_dbfs_(kDefaultLevelDbfs), 34 target_level_dbfs_(kDefaultLevelDbfs),
41 standalone_vad_enabled_(true),
42 histogram_(Histogram::Create(kNumAnalysisFrames)), 35 histogram_(Histogram::Create(kNumAnalysisFrames)),
43 inactive_histogram_(Histogram::Create()), 36 inactive_histogram_(Histogram::Create()) {
44 audio_processing_(new AgcAudioProc()),
45 pitch_based_vad_(new PitchBasedVad()),
46 standalone_vad_(StandaloneVad::Create()),
47 // Initialize to the most common resampling situation.
48 resampler_(new Resampler(32000, kSampleRateHz, 1)) {
49 } 37 }
50 38
51 Agc::~Agc() {} 39 Agc::~Agc() {}
52 40
53 float Agc::AnalyzePreproc(const int16_t* audio, int length) { 41 float Agc::AnalyzePreproc(const int16_t* audio, int length) {
54 assert(length > 0); 42 assert(length > 0);
55 int num_clipped = 0; 43 int num_clipped = 0;
56 for (int i = 0; i < length; ++i) { 44 for (int i = 0; i < length; ++i) {
57 if (audio[i] == 32767 || audio[i] == -32768) 45 if (audio[i] == 32767 || audio[i] == -32768)
58 ++num_clipped; 46 ++num_clipped;
59 } 47 }
60 return 1.0f * num_clipped / length; 48 return 1.0f * num_clipped / length;
61 } 49 }
62 50
63 int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) { 51 int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) {
64 assert(length == sample_rate_hz / 100); 52 vad_.ProcessCaptureAudio(audio, length);
65 if (sample_rate_hz > 32000) { 53 std::vector<double> rms = vad_.chunkwise_rms();
66 return -1; 54 std::vector<double> p = vad_.chunkwise_voice_probabilities();
Andrew MacDonald 2015/06/15 04:32:25 Could use const auto for these. Perhaps probabilit
aluebs-webrtc 2015/06/16 01:17:52 I find the explicit types add readability here, bu
67 } 55 for (size_t i = 0; i < rms.size(); ++i) {
Andrew MacDonald 2015/06/15 04:32:25 DCHECK_EQ(rms.size(), p.size()) ?
aluebs-webrtc 2015/06/16 01:17:52 Done.
68 // Resample to the required rate. 56 histogram_->Update(rms[i], p[i]);
69 int16_t resampled[kLength10Ms];
70 const int16_t* resampled_ptr = audio;
71 if (sample_rate_hz != kSampleRateHz) {
72 if (resampler_->ResetIfNeeded(sample_rate_hz, kSampleRateHz, 1) != 0) {
73 return -1;
74 }
75 resampler_->Push(audio, length, resampled, kLength10Ms, length);
76 resampled_ptr = resampled;
77 }
78 assert(length == kLength10Ms);
79
80 if (standalone_vad_enabled_) {
81 if (standalone_vad_->AddAudio(resampled_ptr, length) != 0)
82 return -1;
83 }
84
85 AudioFeatures features;
86 audio_processing_->ExtractFeatures(resampled_ptr, length, &features);
87 if (features.num_frames > 0) {
88 if (features.silence) {
89 // The other features are invalid, so update the histogram with an
90 // arbitrary low value.
91 for (int n = 0; n < features.num_frames; ++n)
92 histogram_->Update(features.rms[n], 0.01);
93 return 0;
94 }
95
96 // Initialize to 0.5 which is a neutral value for combining probabilities,
97 // in case the standalone-VAD is not enabled.
98 double p_combined[] = {0.5, 0.5, 0.5, 0.5};
99 static_assert(sizeof(p_combined) / sizeof(p_combined[0]) == kMaxNumFrames,
100 "combined probability incorrect size");
101 if (standalone_vad_enabled_) {
102 if (standalone_vad_->GetActivity(p_combined, kMaxNumFrames) < 0)
103 return -1;
104 }
105 // If any other VAD is enabled it must be combined before calling the
106 // pitch-based VAD.
107 if (pitch_based_vad_->VoicingProbability(features, p_combined) < 0)
108 return -1;
109 for (int n = 0; n < features.num_frames; n++) {
110 histogram_->Update(features.rms[n], p_combined[n]);
111 last_voice_probability_ = p_combined[n];
112 }
113 } 57 }
114 return 0; 58 return 0;
115 } 59 }
116 60
117 bool Agc::GetRmsErrorDb(int* error) { 61 bool Agc::GetRmsErrorDb(int* error) {
118 if (!error) { 62 if (!error) {
119 assert(false); 63 assert(false);
120 return false; 64 return false;
121 } 65 }
122 66
(...skipping 21 matching lines...) Expand all
144 // TODO(turajs): just some arbitrary sanity check. We can come up with better 88 // TODO(turajs): just some arbitrary sanity check. We can come up with better
145 // limits. The upper limit should be chosen such that the risk of clipping is 89 // limits. The upper limit should be chosen such that the risk of clipping is
146 // low. The lower limit should not result in a too quiet signal. 90 // low. The lower limit should not result in a too quiet signal.
147 if (level >= 0 || level <= -100) 91 if (level >= 0 || level <= -100)
148 return -1; 92 return -1;
149 target_level_dbfs_ = level; 93 target_level_dbfs_ = level;
150 target_level_loudness_ = Dbfs2Loudness(level); 94 target_level_loudness_ = Dbfs2Loudness(level);
151 return 0; 95 return 0;
152 } 96 }
153 97
154 void Agc::EnableStandaloneVad(bool enable) {
155 standalone_vad_enabled_ = enable;
156 }
157
158 } // namespace webrtc 98 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698