niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | f4b77fd | 2012-01-25 12:40:00 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/voice_detection_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 15 | #include "webrtc/base/criticalsection.h" |
| 16 | #include "webrtc/base/thread_checker.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
pbos@webrtc.org | 7fad4b8 | 2013-05-28 08:11:59 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | typedef VadInst Handle; |
| 23 | |
| 24 | namespace { |
bjornv@webrtc.org | f4b77fd | 2012-01-25 12:40:00 +0000 | [diff] [blame] | 25 | int MapSetting(VoiceDetection::Likelihood likelihood) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | switch (likelihood) { |
| 27 | case VoiceDetection::kVeryLowLikelihood: |
| 28 | return 3; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | case VoiceDetection::kLowLikelihood: |
| 30 | return 2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | case VoiceDetection::kModerateLikelihood: |
| 32 | return 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | case VoiceDetection::kHighLikelihood: |
| 34 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 36 | assert(false); |
mflodman@webrtc.org | ec31bc1 | 2012-02-06 12:42:45 +0000 | [diff] [blame] | 37 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | } // namespace |
| 40 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 41 | VoiceDetectionImpl::VoiceDetectionImpl(const AudioProcessing* apm, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 42 | rtc::CriticalSection* crit) |
| 43 | : ProcessingComponent(), |
| 44 | apm_(apm), |
| 45 | crit_(crit), |
| 46 | stream_has_voice_(false), |
| 47 | using_external_vad_(false), |
| 48 | likelihood_(kLowLikelihood), |
| 49 | frame_size_ms_(10), |
| 50 | frame_size_samples_(0) { |
| 51 | RTC_DCHECK(apm); |
| 52 | RTC_DCHECK(crit); |
| 53 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
| 55 | VoiceDetectionImpl::~VoiceDetectionImpl() {} |
| 56 | |
| 57 | int VoiceDetectionImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 58 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | if (!is_component_enabled()) { |
| 60 | return apm_->kNoError; |
| 61 | } |
| 62 | |
| 63 | if (using_external_vad_) { |
| 64 | using_external_vad_ = false; |
| 65 | return apm_->kNoError; |
| 66 | } |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 67 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | // TODO(ajm): concatenate data in frame buffer here. |
| 70 | |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 71 | int vad_ret = WebRtcVad_Process(static_cast<Handle*>(handle(0)), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 72 | apm_->proc_split_sample_rate_hz(), |
aluebs@webrtc.org | 2561d52 | 2014-07-17 08:27:39 +0000 | [diff] [blame] | 73 | audio->mixed_low_pass_data(), |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 74 | frame_size_samples_); |
| 75 | if (vad_ret == 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | stream_has_voice_ = false; |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 77 | audio->set_activity(AudioFrame::kVadPassive); |
| 78 | } else if (vad_ret == 1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | stream_has_voice_ = true; |
andrew@webrtc.org | ed083d4 | 2011-09-19 15:28:51 +0000 | [diff] [blame] | 80 | audio->set_activity(AudioFrame::kVadActive); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | } else { |
| 82 | return apm_->kUnspecifiedError; |
| 83 | } |
| 84 | |
| 85 | return apm_->kNoError; |
| 86 | } |
| 87 | |
| 88 | int VoiceDetectionImpl::Enable(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 89 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | return EnableComponent(enable); |
| 91 | } |
| 92 | |
| 93 | bool VoiceDetectionImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 94 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | return is_component_enabled(); |
| 96 | } |
| 97 | |
| 98 | int VoiceDetectionImpl::set_stream_has_voice(bool has_voice) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 99 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | using_external_vad_ = true; |
| 101 | stream_has_voice_ = has_voice; |
| 102 | return apm_->kNoError; |
| 103 | } |
| 104 | |
| 105 | bool VoiceDetectionImpl::stream_has_voice() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 106 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | // TODO(ajm): enable this assertion? |
| 108 | //assert(using_external_vad_ || is_component_enabled()); |
| 109 | return stream_has_voice_; |
| 110 | } |
| 111 | |
| 112 | int VoiceDetectionImpl::set_likelihood(VoiceDetection::Likelihood likelihood) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 113 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | if (MapSetting(likelihood) == -1) { |
| 115 | return apm_->kBadParameterError; |
| 116 | } |
| 117 | |
| 118 | likelihood_ = likelihood; |
| 119 | return Configure(); |
| 120 | } |
| 121 | |
| 122 | VoiceDetection::Likelihood VoiceDetectionImpl::likelihood() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 123 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | return likelihood_; |
| 125 | } |
| 126 | |
| 127 | int VoiceDetectionImpl::set_frame_size_ms(int size) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 128 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | assert(size == 10); // TODO(ajm): remove when supported. |
| 130 | if (size != 10 && |
| 131 | size != 20 && |
| 132 | size != 30) { |
| 133 | return apm_->kBadParameterError; |
| 134 | } |
| 135 | |
| 136 | frame_size_ms_ = size; |
| 137 | |
| 138 | return Initialize(); |
| 139 | } |
| 140 | |
| 141 | int VoiceDetectionImpl::frame_size_ms() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 142 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | return frame_size_ms_; |
| 144 | } |
| 145 | |
| 146 | int VoiceDetectionImpl::Initialize() { |
| 147 | int err = ProcessingComponent::Initialize(); |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 148 | |
| 149 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | if (err != apm_->kNoError || !is_component_enabled()) { |
| 151 | return err; |
| 152 | } |
| 153 | |
| 154 | using_external_vad_ = false; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 155 | frame_size_samples_ = static_cast<size_t>( |
| 156 | frame_size_ms_ * apm_->proc_split_sample_rate_hz() / 1000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | // TODO(ajm): intialize frame buffer here. |
| 158 | |
| 159 | return apm_->kNoError; |
| 160 | } |
| 161 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | void* VoiceDetectionImpl::CreateHandle() const { |
Bjorn Volcker | de4703c | 2015-05-27 07:22:58 +0200 | [diff] [blame] | 163 | return WebRtcVad_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 166 | void VoiceDetectionImpl::DestroyHandle(void* handle) const { |
bjornv@webrtc.org | 2a79672 | 2014-04-22 04:45:35 +0000 | [diff] [blame] | 167 | WebRtcVad_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int VoiceDetectionImpl::InitializeHandle(void* handle) const { |
| 171 | return WebRtcVad_Init(static_cast<Handle*>(handle)); |
| 172 | } |
| 173 | |
| 174 | int VoiceDetectionImpl::ConfigureHandle(void* handle) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame^] | 175 | rtc::CritScope cs(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | return WebRtcVad_set_mode(static_cast<Handle*>(handle), |
| 177 | MapSetting(likelihood_)); |
| 178 | } |
| 179 | |
| 180 | int VoiceDetectionImpl::num_handles_required() const { |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | int VoiceDetectionImpl::GetHandleError(void* handle) const { |
| 185 | // The VAD has no get_error() function. |
| 186 | assert(handle != NULL); |
| 187 | return apm_->kUnspecifiedError; |
| 188 | } |
| 189 | } // namespace webrtc |