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