summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/vad/standalone_vad.h
blob: b08463374ec40a5938f78bf1b3af133af7633cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef MODULES_AUDIO_PROCESSING_AGC_STANDALONE_VAD_H_
#define MODULES_AUDIO_PROCESSING_AGC_STANDALONE_VAD_H_

#include <stddef.h>
#include <stdint.h>

#include "common_audio/vad/include/webrtc_vad.h"
#include "modules/audio_processing/vad/common.h"

namespace webrtc {

class StandaloneVad {
 public:
  static StandaloneVad* Create();
  ~StandaloneVad();

  // Outputs
  //   p: a buffer where probabilities are written to.
  //   length_p: number of elements of `p`.
  //
  // return value:
  //    -1: if no audio is stored or VAD returns error.
  //     0: in success.
  // In case of error the content of `activity` is unchanged.
  //
  // Note that due to a high false-positive (VAD decision is active while the
  // processed audio is just background noise) rate, stand-alone VAD is used as
  // a one-sided indicator. The activity probability is 0.5 if the frame is
  // classified as active, and the probability is 0.01 if the audio is
  // classified as passive. In this way, when probabilities are combined, the
  // effect of the stand-alone VAD is neutral if the input is classified as
  // active.
  int GetActivity(double* p, size_t length_p);

  // Expecting 10 ms of 16 kHz audio to be pushed in.
  int AddAudio(const int16_t* data, size_t length);

  // Set aggressiveness of VAD, 0 is the least aggressive and 3 is the most
  // aggressive mode. Returns -1 if the input is less than 0 or larger than 3,
  // otherwise 0 is returned.
  int set_mode(int mode);
  // Get the agressiveness of the current VAD.
  int mode() const { return mode_; }

 private:
  explicit StandaloneVad(VadInst* vad);

  static const size_t kMaxNum10msFrames = 3;

  // TODO(turajs): Is there a way to use scoped-pointer here?
  VadInst* vad_;
  int16_t buffer_[kMaxNum10msFrames * kLength10Ms];
  size_t index_;
  int mode_;
};

}  // namespace webrtc

#endif  // MODULES_AUDIO_PROCESSING_AGC_STANDALONE_VAD_H_