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