blob: 9999d6764bde7dd35c1ff4305f350096528bb301 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/post_decode_vad.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
13namespace webrtc {
14
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000015PostDecodeVad::~PostDecodeVad() {
16 if (vad_instance_)
17 WebRtcVad_Free(vad_instance_);
18}
19
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020void PostDecodeVad::Enable() {
21 if (!vad_instance_) {
22 // Create the instance.
Bjorn Volckerde4703c2015-05-27 07:22:58 +020023 vad_instance_ = WebRtcVad_Create();
24 if (vad_instance_ == nullptr) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025 // 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
Yves Gerey665174f2018-06-19 15:03:05 +020048void PostDecodeVad::Update(int16_t* signal,
49 size_t length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050 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_) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070072 size_t vad_sample_index = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073 active_speech_ = false;
74 // Loop through frame sizes 30, 20, and 10 ms.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000075 for (int vad_frame_size_ms = 30; vad_frame_size_ms >= 10;
Yves Gerey665174f2018-06-19 15:03:05 +020076 vad_frame_size_ms -= 10) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070077 size_t vad_frame_size_samples =
78 static_cast<size_t>(vad_frame_size_ms * fs_hz / 1000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079 while (length - vad_sample_index >= vad_frame_size_samples) {
Yves Gerey665174f2018-06-19 15:03:05 +020080 int vad_return =
81 WebRtcVad_Process(vad_instance_, fs_hz, &signal[vad_sample_index],
82 vad_frame_size_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 active_speech_ |= (vad_return == 1);
84 vad_sample_index += vad_frame_size_samples;
85 }
86 }
87 }
88}
89
90} // namespace webrtc