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

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

Issue 1192863006: Revert "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>
18 17
19 #include "webrtc/base/checks.h" 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"
20 #include "webrtc/modules/audio_processing/agc/histogram.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"
21 #include "webrtc/modules/audio_processing/agc/utility.h" 24 #include "webrtc/modules/audio_processing/agc/utility.h"
22 #include "webrtc/modules/interface/module_common_types.h" 25 #include "webrtc/modules/interface/module_common_types.h"
23 26
24 namespace webrtc { 27 namespace webrtc {
25 namespace { 28 namespace {
26 29
27 const int kDefaultLevelDbfs = -18; 30 const int kDefaultLevelDbfs = -18;
31 const double kDefaultVoiceValue = 1.0;
28 const int kNumAnalysisFrames = 100; 32 const int kNumAnalysisFrames = 100;
29 const double kActivityThreshold = 0.3; 33 const double kActivityThreshold = 0.3;
30 34
31 } // namespace 35 } // namespace
32 36
33 Agc::Agc() 37 Agc::Agc()
34 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)), 38 : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
39 last_voice_probability_(kDefaultVoiceValue),
35 target_level_dbfs_(kDefaultLevelDbfs), 40 target_level_dbfs_(kDefaultLevelDbfs),
41 standalone_vad_enabled_(true),
36 histogram_(Histogram::Create(kNumAnalysisFrames)), 42 histogram_(Histogram::Create(kNumAnalysisFrames)),
37 inactive_histogram_(Histogram::Create()) { 43 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)) {
38 } 49 }
39 50
40 Agc::~Agc() {} 51 Agc::~Agc() {}
41 52
42 float Agc::AnalyzePreproc(const int16_t* audio, int length) { 53 float Agc::AnalyzePreproc(const int16_t* audio, int length) {
43 assert(length > 0); 54 assert(length > 0);
44 int num_clipped = 0; 55 int num_clipped = 0;
45 for (int i = 0; i < length; ++i) { 56 for (int i = 0; i < length; ++i) {
46 if (audio[i] == 32767 || audio[i] == -32768) 57 if (audio[i] == 32767 || audio[i] == -32768)
47 ++num_clipped; 58 ++num_clipped;
48 } 59 }
49 return 1.0f * num_clipped / length; 60 return 1.0f * num_clipped / length;
50 } 61 }
51 62
52 int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) { 63 int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) {
53 vad_.ProcessChunk(audio, length, sample_rate_hz); 64 assert(length == sample_rate_hz / 100);
54 const std::vector<double>& rms = vad_.chunkwise_rms(); 65 if (sample_rate_hz > 32000) {
55 const std::vector<double>& probabilities = 66 return -1;
56 vad_.chunkwise_voice_probabilities(); 67 }
57 DCHECK_EQ(rms.size(), probabilities.size()); 68 // Resample to the required rate.
58 for (size_t i = 0; i < rms.size(); ++i) { 69 int16_t resampled[kLength10Ms];
59 histogram_->Update(rms[i], probabilities[i]); 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 }
60 } 113 }
61 return 0; 114 return 0;
62 } 115 }
63 116
64 bool Agc::GetRmsErrorDb(int* error) { 117 bool Agc::GetRmsErrorDb(int* error) {
65 if (!error) { 118 if (!error) {
66 assert(false); 119 assert(false);
67 return false; 120 return false;
68 } 121 }
69 122
(...skipping 21 matching lines...) Expand all
91 // TODO(turajs): just some arbitrary sanity check. We can come up with better 144 // 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 145 // 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. 146 // low. The lower limit should not result in a too quiet signal.
94 if (level >= 0 || level <= -100) 147 if (level >= 0 || level <= -100)
95 return -1; 148 return -1;
96 target_level_dbfs_ = level; 149 target_level_dbfs_ = level;
97 target_level_loudness_ = Dbfs2Loudness(level); 150 target_level_loudness_ = Dbfs2Loudness(level);
98 return 0; 151 return 0;
99 } 152 }
100 153
154 void Agc::EnableStandaloneVad(bool enable) {
155 standalone_vad_enabled_ = enable;
156 }
157
101 } // namespace webrtc 158 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/agc/agc.h ('k') | webrtc/modules/audio_processing/agc/agc_audio_proc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698