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