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 11 #include "webrtc/modules/audio_coding/neteq/post_decode_vad.h" 12 13 namespace webrtc { 14 15 PostDecodeVad::~PostDecodeVad() { 16 if (vad_instance_) 17 WebRtcVad_Free(vad_instance_); 18 } 19 20 void PostDecodeVad::Enable() { 21 if (!vad_instance_) { 22 // Create the instance. 23 if (WebRtcVad_Create(&vad_instance_) != 0) { 24 // Failed to create instance. 25 Disable(); 26 return; 27 } 28 } 29 Init(); 30 enabled_ = true; 31 } 32 33 void PostDecodeVad::Disable() { 34 enabled_ = false; 35 running_ = false; 36 } 37 38 void PostDecodeVad::Init() { 39 running_ = false; 40 if (vad_instance_) { 41 WebRtcVad_Init(vad_instance_); 42 WebRtcVad_set_mode(vad_instance_, kVadMode); 43 running_ = true; 44 } 45 } 46 47 void PostDecodeVad::Update(int16_t* signal, int length, 48 AudioDecoder::SpeechType speech_type, 49 bool sid_frame, 50 int fs_hz) { 51 if (!vad_instance_ || !enabled_) { 52 return; 53 } 54 55 if (speech_type == AudioDecoder::kComfortNoise || sid_frame || 56 fs_hz > 16000) { 57 // TODO(hlundin): Remove restriction on fs_hz. 58 running_ = false; 59 active_speech_ = true; 60 sid_interval_counter_ = 0; 61 } else if (!running_) { 62 ++sid_interval_counter_; 63 } 64 65 if (sid_interval_counter_ >= kVadAutoEnable) { 66 Init(); 67 } 68 69 if (length > 0 && running_) { 70 int vad_sample_index = 0; 71 active_speech_ = false; 72 // Loop through frame sizes 30, 20, and 10 ms. 73 for (int vad_frame_size_ms = 30; vad_frame_size_ms >= 10; 74 vad_frame_size_ms -= 10) { 75 int vad_frame_size_samples = vad_frame_size_ms * fs_hz / 1000; 76 while (length - vad_sample_index >= vad_frame_size_samples) { 77 int vad_return = WebRtcVad_Process( 78 vad_instance_, fs_hz, &signal[vad_sample_index], 79 vad_frame_size_samples); 80 active_speech_ |= (vad_return == 1); 81 vad_sample_index += vad_frame_size_samples; 82 } 83 } 84 } 85 } 86 87 } // namespace webrtc 88