• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <assert.h>
14 
15 #include "webrtc/modules/include/module_common_types.h"
16 #include "webrtc/modules/utility/include/audio_frame_operations.h"
17 #include "webrtc/typedefs.h"
18 
19 namespace webrtc {
20 
21 static const int kDefaultStandaloneVadMode = 3;
22 
StandaloneVad(VadInst * vad)23 StandaloneVad::StandaloneVad(VadInst* vad)
24     : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) {
25 }
26 
~StandaloneVad()27 StandaloneVad::~StandaloneVad() {
28   WebRtcVad_Free(vad_);
29 }
30 
Create()31 StandaloneVad* StandaloneVad::Create() {
32   VadInst* vad = WebRtcVad_Create();
33   if (!vad)
34     return nullptr;
35 
36   int err = WebRtcVad_Init(vad);
37   err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode);
38   if (err != 0) {
39     WebRtcVad_Free(vad);
40     return nullptr;
41   }
42   return new StandaloneVad(vad);
43 }
44 
AddAudio(const int16_t * data,size_t length)45 int StandaloneVad::AddAudio(const int16_t* data, size_t length) {
46   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 
GetActivity(double * p,size_t length_p)60 int StandaloneVad::GetActivity(double* p, size_t length_p) {
61   if (index_ == 0)
62     return -1;
63 
64   const size_t num_frames = index_ / kLength10Ms;
65   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.
76   for (size_t n = 1; n < num_frames; n++)
77     p[n] = p[0];
78   // Reset the buffer to start from the beginning.
79   index_ = 0;
80   return activity;
81 }
82 
set_mode(int mode)83 int 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
94