blob: 714073ad10410d6aa3f252b7961568dc06dcbbcd [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:211/*
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.org9c55f0f2014-06-09 08:10:2811#include "webrtc/modules/audio_coding/neteq/post_decode_vad.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2112
13namespace webrtc {
14
pbos@webrtc.org2d1a55c2013-07-31 15:54:0015PostDecodeVad::~PostDecodeVad() {
16 if (vad_instance_)
17 WebRtcVad_Free(vad_instance_);
18}
19
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2120void PostDecodeVad::Enable() {
21 if (!vad_instance_) {
22 // Create the instance.
Bjorn Volckerde4703c2015-05-27 05:22:5823 vad_instance_ = WebRtcVad_Create();
24 if (vad_instance_ == nullptr) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2125 // Failed to create instance.
26 Disable();
27 return;
28 }
29 }
30 Init();
31 enabled_ = true;
32}
33
34void PostDecodeVad::Disable() {
35 enabled_ = false;
36 running_ = false;
37}
38
39void 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 Kastingdce40cf2015-08-24 21:52:2348void PostDecodeVad::Update(int16_t* signal, size_t length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2149 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 Kastingdce40cf2015-08-24 21:52:2371 size_t vad_sample_index = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2172 active_speech_ = false;
73 // Loop through frame sizes 30, 20, and 10 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:2874 for (int vad_frame_size_ms = 30; vad_frame_size_ms >= 10;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2175 vad_frame_size_ms -= 10) {
Peter Kastingdce40cf2015-08-24 21:52:2376 size_t vad_frame_size_samples =
77 static_cast<size_t>(vad_frame_size_ms * fs_hz / 1000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2178 while (length - vad_sample_index >= vad_frame_size_samples) {
turaj@webrtc.org362a55e2013-09-20 16:25:2879 int vad_return = WebRtcVad_Process(
80 vad_instance_, fs_hz, &signal[vad_sample_index],
81 vad_frame_size_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:2182 active_speech_ |= (vad_return == 1);
83 vad_sample_index += vad_frame_size_samples;
84 }
85 }
86 }
87}
88
89} // namespace webrtc