blob: 8636eb487f10f4a0e999ebd1f529900f241d07d4 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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
aluebsecf6b812015-06-25 12:28:48 -070011#include "webrtc/modules/audio_processing/vad/standalone_vad.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
kwiberg9e2be5f2016-09-14 05:23:22 -070013#include "webrtc/base/checks.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/include/module_common_types.h"
15#include "webrtc/modules/utility/include/audio_frame_operations.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000016#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20static const int kDefaultStandaloneVadMode = 3;
21
22StandaloneVad::StandaloneVad(VadInst* vad)
aluebsecf6b812015-06-25 12:28:48 -070023 : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) {
24}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000025
26StandaloneVad::~StandaloneVad() {
27 WebRtcVad_Free(vad_);
28}
29
30StandaloneVad* StandaloneVad::Create() {
Bjorn Volckerde4703c2015-05-27 07:22:58 +020031 VadInst* vad = WebRtcVad_Create();
32 if (!vad)
33 return nullptr;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000034
35 int err = WebRtcVad_Init(vad);
36 err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
37 if (err != 0) {
38 WebRtcVad_Free(vad);
Bjorn Volckerde4703c2015-05-27 07:22:58 +020039 return nullptr;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000040 }
41 return new StandaloneVad(vad);
42}
43
Peter Kastingdce40cf2015-08-24 14:52:23 -070044int StandaloneVad::AddAudio(const int16_t* data, size_t length) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045 if (length != kLength10Ms)
46 return -1;
47
48 if (index_ + length > kLength10Ms * kMaxNum10msFrames)
49 // Reset the buffer if it's full.
50 // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we
51 // can forgo the buffering.
52 index_ = 0;
53
54 memcpy(&buffer_[index_], data, sizeof(int16_t) * length);
55 index_ += length;
56 return 0;
57}
58
Peter Kastingdce40cf2015-08-24 14:52:23 -070059int StandaloneVad::GetActivity(double* p, size_t length_p) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060 if (index_ == 0)
61 return -1;
62
Peter Kastingdce40cf2015-08-24 14:52:23 -070063 const size_t num_frames = index_ / kLength10Ms;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000064 if (num_frames > length_p)
65 return -1;
kwiberg9e2be5f2016-09-14 05:23:22 -070066 RTC_DCHECK_EQ(0, WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +000067
68 int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_);
69 if (activity < 0)
70 return -1;
71 else if (activity == 0)
72 p[0] = 0.01; // Arbitrary but small and non-zero.
73 else
74 p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities.
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 for (size_t n = 1; n < num_frames; n++)
pbos@webrtc.org788acd12014-12-15 09:41:24 +000076 p[n] = p[0];
77 // Reset the buffer to start from the beginning.
78 index_ = 0;
79 return activity;
80}
81
82int StandaloneVad::set_mode(int mode) {
83 if (mode < 0 || mode > 3)
84 return -1;
85 if (WebRtcVad_set_mode(vad_, mode) != 0)
86 return -1;
87
88 mode_ = mode;
89 return 0;
90}
91
92} // namespace webrtc