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/voice_detection_impl.h" | 11 #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
15 #include "webrtc/common_audio/vad/include/webrtc_vad.h" | 15 #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
16 #include "webrtc/modules/audio_processing/audio_buffer.h" | 16 #include "webrtc/modules/audio_processing/audio_buffer.h" |
17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
18 | 17 |
19 namespace webrtc { | 18 namespace webrtc { |
20 | 19 |
21 typedef VadInst Handle; | 20 typedef VadInst Handle; |
22 | 21 |
23 namespace { | 22 namespace { |
24 int MapSetting(VoiceDetection::Likelihood likelihood) { | 23 int MapSetting(VoiceDetection::Likelihood likelihood) { |
25 switch (likelihood) { | 24 switch (likelihood) { |
26 case VoiceDetection::kVeryLowLikelihood: | 25 case VoiceDetection::kVeryLowLikelihood: |
27 return 3; | 26 return 3; |
28 case VoiceDetection::kLowLikelihood: | 27 case VoiceDetection::kLowLikelihood: |
29 return 2; | 28 return 2; |
30 case VoiceDetection::kModerateLikelihood: | 29 case VoiceDetection::kModerateLikelihood: |
31 return 1; | 30 return 1; |
32 case VoiceDetection::kHighLikelihood: | 31 case VoiceDetection::kHighLikelihood: |
33 return 0; | 32 return 0; |
34 } | 33 } |
35 assert(false); | 34 assert(false); |
36 return -1; | 35 return -1; |
37 } | 36 } |
38 } // namespace | 37 } // namespace |
39 | 38 |
40 VoiceDetectionImpl::VoiceDetectionImpl(const AudioProcessing* apm, | 39 VoiceDetectionImpl::VoiceDetectionImpl(const AudioProcessing* apm, |
41 CriticalSectionWrapper* crit) | 40 rtc::CriticalSection* crit) |
42 : ProcessingComponent(), | 41 : ProcessingComponent(), |
43 apm_(apm), | 42 apm_(apm), |
44 crit_(crit), | 43 crit_(crit), |
45 stream_has_voice_(false), | 44 stream_has_voice_(false), |
46 using_external_vad_(false), | 45 using_external_vad_(false), |
47 likelihood_(kLowLikelihood), | 46 likelihood_(kLowLikelihood), |
48 frame_size_ms_(10), | 47 frame_size_ms_(10), |
49 frame_size_samples_(0) {} | 48 frame_size_samples_(0) {} |
50 | 49 |
51 VoiceDetectionImpl::~VoiceDetectionImpl() {} | 50 VoiceDetectionImpl::~VoiceDetectionImpl() {} |
52 | 51 |
53 int VoiceDetectionImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 52 int VoiceDetectionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
54 if (!is_component_enabled()) { | 53 if (!is_component_enabled()) { |
55 return apm_->kNoError; | 54 return apm_->kNoError; |
56 } | 55 } |
57 | 56 |
58 if (using_external_vad_) { | 57 if (using_external_vad_) { |
59 using_external_vad_ = false; | 58 using_external_vad_ = false; |
(...skipping 14 matching lines...) Expand all Loading... |
74 stream_has_voice_ = true; | 73 stream_has_voice_ = true; |
75 audio->set_activity(AudioFrame::kVadActive); | 74 audio->set_activity(AudioFrame::kVadActive); |
76 } else { | 75 } else { |
77 return apm_->kUnspecifiedError; | 76 return apm_->kUnspecifiedError; |
78 } | 77 } |
79 | 78 |
80 return apm_->kNoError; | 79 return apm_->kNoError; |
81 } | 80 } |
82 | 81 |
83 int VoiceDetectionImpl::Enable(bool enable) { | 82 int VoiceDetectionImpl::Enable(bool enable) { |
84 CriticalSectionScoped crit_scoped(crit_); | 83 rtc::CritScope cs(crit_); |
85 return EnableComponent(enable); | 84 return EnableComponent(enable); |
86 } | 85 } |
87 | 86 |
88 bool VoiceDetectionImpl::is_enabled() const { | 87 bool VoiceDetectionImpl::is_enabled() const { |
| 88 rtc::CritScope cs(crit_); |
89 return is_component_enabled(); | 89 return is_component_enabled(); |
90 } | 90 } |
91 | 91 |
92 int VoiceDetectionImpl::set_stream_has_voice(bool has_voice) { | 92 int VoiceDetectionImpl::set_stream_has_voice(bool has_voice) { |
| 93 rtc::CritScope cs(crit_); |
93 using_external_vad_ = true; | 94 using_external_vad_ = true; |
94 stream_has_voice_ = has_voice; | 95 stream_has_voice_ = has_voice; |
95 return apm_->kNoError; | 96 return apm_->kNoError; |
96 } | 97 } |
97 | 98 |
98 bool VoiceDetectionImpl::stream_has_voice() const { | 99 bool VoiceDetectionImpl::stream_has_voice() const { |
| 100 rtc::CritScope cs(crit_); |
99 // TODO(ajm): enable this assertion? | 101 // TODO(ajm): enable this assertion? |
100 //assert(using_external_vad_ || is_component_enabled()); | 102 //assert(using_external_vad_ || is_component_enabled()); |
101 return stream_has_voice_; | 103 return stream_has_voice_; |
102 } | 104 } |
103 | 105 |
104 int VoiceDetectionImpl::set_likelihood(VoiceDetection::Likelihood likelihood) { | 106 int VoiceDetectionImpl::set_likelihood(VoiceDetection::Likelihood likelihood) { |
105 CriticalSectionScoped crit_scoped(crit_); | 107 rtc::CritScope cs(crit_); |
106 if (MapSetting(likelihood) == -1) { | 108 if (MapSetting(likelihood) == -1) { |
107 return apm_->kBadParameterError; | 109 return apm_->kBadParameterError; |
108 } | 110 } |
109 | 111 |
110 likelihood_ = likelihood; | 112 likelihood_ = likelihood; |
111 return Configure(); | 113 return Configure(); |
112 } | 114 } |
113 | 115 |
114 VoiceDetection::Likelihood VoiceDetectionImpl::likelihood() const { | 116 VoiceDetection::Likelihood VoiceDetectionImpl::likelihood() const { |
| 117 rtc::CritScope cs(crit_); |
115 return likelihood_; | 118 return likelihood_; |
116 } | 119 } |
117 | 120 |
118 int VoiceDetectionImpl::set_frame_size_ms(int size) { | 121 int VoiceDetectionImpl::set_frame_size_ms(int size) { |
119 CriticalSectionScoped crit_scoped(crit_); | 122 rtc::CritScope cs(crit_); |
120 assert(size == 10); // TODO(ajm): remove when supported. | 123 assert(size == 10); // TODO(ajm): remove when supported. |
121 if (size != 10 && | 124 if (size != 10 && |
122 size != 20 && | 125 size != 20 && |
123 size != 30) { | 126 size != 30) { |
124 return apm_->kBadParameterError; | 127 return apm_->kBadParameterError; |
125 } | 128 } |
126 | 129 |
127 frame_size_ms_ = size; | 130 frame_size_ms_ = size; |
128 | 131 |
129 return Initialize(); | 132 return Initialize(); |
130 } | 133 } |
131 | 134 |
132 int VoiceDetectionImpl::frame_size_ms() const { | 135 int VoiceDetectionImpl::frame_size_ms() const { |
| 136 rtc::CritScope cs(crit_); |
133 return frame_size_ms_; | 137 return frame_size_ms_; |
134 } | 138 } |
135 | 139 |
136 int VoiceDetectionImpl::Initialize() { | 140 int VoiceDetectionImpl::Initialize() { |
137 int err = ProcessingComponent::Initialize(); | 141 int err = ProcessingComponent::Initialize(); |
138 if (err != apm_->kNoError || !is_component_enabled()) { | 142 if (err != apm_->kNoError || !is_component_enabled()) { |
139 return err; | 143 return err; |
140 } | 144 } |
141 | 145 |
142 using_external_vad_ = false; | 146 using_external_vad_ = false; |
(...skipping 24 matching lines...) Expand all Loading... |
167 int VoiceDetectionImpl::num_handles_required() const { | 171 int VoiceDetectionImpl::num_handles_required() const { |
168 return 1; | 172 return 1; |
169 } | 173 } |
170 | 174 |
171 int VoiceDetectionImpl::GetHandleError(void* handle) const { | 175 int VoiceDetectionImpl::GetHandleError(void* handle) const { |
172 // The VAD has no get_error() function. | 176 // The VAD has no get_error() function. |
173 assert(handle != NULL); | 177 assert(handle != NULL); |
174 return apm_->kUnspecifiedError; | 178 return apm_->kUnspecifiedError; |
175 } | 179 } |
176 } // namespace webrtc | 180 } // namespace webrtc |
OLD | NEW |