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