1 /*
2 * Copyright (c) 2012 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_processing/vad/standalone_vad.h"
12
13 #include <string.h>
14
15 #include "common_audio/vad/include/webrtc_vad.h"
16 #include "rtc_base/checks.h"
17
18 namespace webrtc {
19
20 static const int kDefaultStandaloneVadMode = 3;
21
StandaloneVad(VadInst * vad)22 StandaloneVad::StandaloneVad(VadInst* vad)
23 : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) {}
24
~StandaloneVad()25 StandaloneVad::~StandaloneVad() {
26 WebRtcVad_Free(vad_);
27 }
28
Create()29 StandaloneVad* StandaloneVad::Create() {
30 VadInst* vad = WebRtcVad_Create();
31 if (!vad)
32 return nullptr;
33
34 int err = WebRtcVad_Init(vad);
35 err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
36 if (err != 0) {
37 WebRtcVad_Free(vad);
38 return nullptr;
39 }
40 return new StandaloneVad(vad);
41 }
42
AddAudio(const int16_t * data,size_t length)43 int StandaloneVad::AddAudio(const int16_t* data, size_t length) {
44 if (length != kLength10Ms)
45 return -1;
46
47 if (index_ + length > kLength10Ms * kMaxNum10msFrames)
48 // Reset the buffer if it's full.
49 // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we
50 // can forgo the buffering.
51 index_ = 0;
52
53 memcpy(&buffer_[index_], data, sizeof(int16_t) * length);
54 index_ += length;
55 return 0;
56 }
57
GetActivity(double * p,size_t length_p)58 int StandaloneVad::GetActivity(double* p, size_t length_p) {
59 if (index_ == 0)
60 return -1;
61
62 const size_t num_frames = index_ / kLength10Ms;
63 if (num_frames > length_p)
64 return -1;
65 RTC_DCHECK_EQ(0, WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_));
66
67 int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_);
68 if (activity < 0)
69 return -1;
70 else if (activity == 0)
71 p[0] = 0.01; // Arbitrary but small and non-zero.
72 else
73 p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities.
74 for (size_t n = 1; n < num_frames; n++)
75 p[n] = p[0];
76 // Reset the buffer to start from the beginning.
77 index_ = 0;
78 return activity;
79 }
80
set_mode(int mode)81 int StandaloneVad::set_mode(int mode) {
82 if (mode < 0 || mode > 3)
83 return -1;
84 if (WebRtcVad_set_mode(vad_, mode) != 0)
85 return -1;
86
87 mode_ = mode;
88 return 0;
89 }
90
91 } // namespace webrtc
92