henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/post_decode_vad.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 12 | |
| 13 | namespace webrtc { |
| 14 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 | [diff] [blame] | 15 | PostDecodeVad::~PostDecodeVad() { |
| 16 | if (vad_instance_) |
| 17 | WebRtcVad_Free(vad_instance_); |
| 18 | } |
| 19 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 20 | void PostDecodeVad::Enable() { |
| 21 | if (!vad_instance_) { |
| 22 | // Create the instance. |
Bjorn Volcker | de4703c | 2015-05-27 05:22:58 | [diff] [blame] | 23 | vad_instance_ = WebRtcVad_Create(); |
| 24 | if (vad_instance_ == nullptr) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 25 | // Failed to create instance. |
| 26 | Disable(); |
| 27 | return; |
| 28 | } |
| 29 | } |
| 30 | Init(); |
| 31 | enabled_ = true; |
| 32 | } |
| 33 | |
| 34 | void PostDecodeVad::Disable() { |
| 35 | enabled_ = false; |
| 36 | running_ = false; |
| 37 | } |
| 38 | |
| 39 | void PostDecodeVad::Init() { |
| 40 | running_ = false; |
| 41 | if (vad_instance_) { |
| 42 | WebRtcVad_Init(vad_instance_); |
| 43 | WebRtcVad_set_mode(vad_instance_, kVadMode); |
| 44 | running_ = true; |
| 45 | } |
| 46 | } |
| 47 | |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 48 | void PostDecodeVad::Update(int16_t* signal, size_t length, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 49 | AudioDecoder::SpeechType speech_type, |
| 50 | bool sid_frame, |
| 51 | int fs_hz) { |
| 52 | if (!vad_instance_ || !enabled_) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (speech_type == AudioDecoder::kComfortNoise || sid_frame || |
| 57 | fs_hz > 16000) { |
| 58 | // TODO(hlundin): Remove restriction on fs_hz. |
| 59 | running_ = false; |
| 60 | active_speech_ = true; |
| 61 | sid_interval_counter_ = 0; |
| 62 | } else if (!running_) { |
| 63 | ++sid_interval_counter_; |
| 64 | } |
| 65 | |
| 66 | if (sid_interval_counter_ >= kVadAutoEnable) { |
| 67 | Init(); |
| 68 | } |
| 69 | |
| 70 | if (length > 0 && running_) { |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 71 | size_t vad_sample_index = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 72 | active_speech_ = false; |
| 73 | // Loop through frame sizes 30, 20, and 10 ms. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 | [diff] [blame] | 74 | for (int vad_frame_size_ms = 30; vad_frame_size_ms >= 10; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 75 | vad_frame_size_ms -= 10) { |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 76 | size_t vad_frame_size_samples = |
| 77 | static_cast<size_t>(vad_frame_size_ms * fs_hz / 1000); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 78 | while (length - vad_sample_index >= vad_frame_size_samples) { |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 | [diff] [blame] | 79 | int vad_return = WebRtcVad_Process( |
| 80 | vad_instance_, fs_hz, &signal[vad_sample_index], |
| 81 | vad_frame_size_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 | [diff] [blame] | 82 | active_speech_ |= (vad_return == 1); |
| 83 | vad_sample_index += vad_frame_size_samples; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } // namespace webrtc |