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