blob: 1209526a92e6268e43ce6c66c8214b3be66137d9 [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
13#include <assert.h>
14
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010015#include "webrtc/modules/include/module_common_types.h"
16#include "webrtc/modules/utility/include/audio_frame_operations.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21static const int kDefaultStandaloneVadMode = 3;
22
23StandaloneVad::StandaloneVad(VadInst* vad)
aluebsecf6b812015-06-25 12:28:48 -070024 : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) {
25}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000026
27StandaloneVad::~StandaloneVad() {
28 WebRtcVad_Free(vad_);
29}
30
31StandaloneVad* StandaloneVad::Create() {
Bjorn Volckerde4703c2015-05-27 07:22:58 +020032 VadInst* vad = WebRtcVad_Create();
33 if (!vad)
34 return nullptr;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000035
36 int err = WebRtcVad_Init(vad);
37 err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
38 if (err != 0) {
39 WebRtcVad_Free(vad);
Bjorn Volckerde4703c2015-05-27 07:22:58 +020040 return nullptr;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000041 }
42 return new StandaloneVad(vad);
43}
44
Peter Kastingdce40cf2015-08-24 14:52:23 -070045int StandaloneVad::AddAudio(const int16_t* data, size_t length) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000046 if (length != kLength10Ms)
47 return -1;
48
49 if (index_ + length > kLength10Ms * kMaxNum10msFrames)
50 // Reset the buffer if it's full.
51 // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we
52 // can forgo the buffering.
53 index_ = 0;
54
55 memcpy(&buffer_[index_], data, sizeof(int16_t) * length);
56 index_ += length;
57 return 0;
58}
59
Peter Kastingdce40cf2015-08-24 14:52:23 -070060int StandaloneVad::GetActivity(double* p, size_t length_p) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000061 if (index_ == 0)
62 return -1;
63
Peter Kastingdce40cf2015-08-24 14:52:23 -070064 const size_t num_frames = index_ / kLength10Ms;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000065 if (num_frames > length_p)
66 return -1;
67 assert(WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_) == 0);
68
69 int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_);
70 if (activity < 0)
71 return -1;
72 else if (activity == 0)
73 p[0] = 0.01; // Arbitrary but small and non-zero.
74 else
75 p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities.
Peter Kastingdce40cf2015-08-24 14:52:23 -070076 for (size_t n = 1; n < num_frames; n++)
pbos@webrtc.org788acd12014-12-15 09:41:24 +000077 p[n] = p[0];
78 // Reset the buffer to start from the beginning.
79 index_ = 0;
80 return activity;
81}
82
83int StandaloneVad::set_mode(int mode) {
84 if (mode < 0 || mode > 3)
85 return -1;
86 if (WebRtcVad_set_mode(vad_, mode) != 0)
87 return -1;
88
89 mode_ = mode;
90 return 0;
91}
92
93} // namespace webrtc