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
13namespace webrtc {
14
15PostDecodeVad::~PostDecodeVad() {
16  if (vad_instance_)
17    WebRtcVad_Free(vad_instance_);
18}
19
20void 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
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
48void PostDecodeVad::Update(int16_t* signal, size_t length,
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_) {
71    size_t vad_sample_index = 0;
72    active_speech_ = false;
73    // Loop through frame sizes 30, 20, and 10 ms.
74    for (int vad_frame_size_ms = 30; vad_frame_size_ms >= 10;
75        vad_frame_size_ms -= 10) {
76      size_t vad_frame_size_samples =
77          static_cast<size_t>(vad_frame_size_ms * fs_hz / 1000);
78      while (length - vad_sample_index >= vad_frame_size_samples) {
79        int vad_return = WebRtcVad_Process(
80            vad_instance_, fs_hz, &signal[vad_sample_index],
81            vad_frame_size_samples);
82        active_speech_ |= (vad_return == 1);
83        vad_sample_index += vad_frame_size_samples;
84      }
85    }
86  }
87}
88
89}  // namespace webrtc
90