• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 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 "common_audio/vad/include/vad.h"
12 
13 #include <memory>
14 
15 #include "common_audio/vad/include/webrtc_vad.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
20 namespace {
21 
22 class VadImpl final : public Vad {
23  public:
VadImpl(Aggressiveness aggressiveness)24   explicit VadImpl(Aggressiveness aggressiveness)
25       : handle_(nullptr), aggressiveness_(aggressiveness) {
26     Reset();
27   }
28 
~VadImpl()29   ~VadImpl() override { WebRtcVad_Free(handle_); }
30 
VoiceActivity(const int16_t * audio,size_t num_samples,int sample_rate_hz)31   Activity VoiceActivity(const int16_t* audio,
32                          size_t num_samples,
33                          int sample_rate_hz) override {
34     int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples);
35     switch (ret) {
36       case 0:
37         return kPassive;
38       case 1:
39         return kActive;
40       default:
41         RTC_NOTREACHED() << "WebRtcVad_Process returned an error.";
42         return kError;
43     }
44   }
45 
Reset()46   void Reset() override {
47     if (handle_)
48       WebRtcVad_Free(handle_);
49     handle_ = WebRtcVad_Create();
50     RTC_CHECK(handle_);
51     RTC_CHECK_EQ(WebRtcVad_Init(handle_), 0);
52     RTC_CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0);
53   }
54 
55  private:
56   VadInst* handle_;
57   Aggressiveness aggressiveness_;
58 };
59 
60 }  // namespace
61 
CreateVad(Vad::Aggressiveness aggressiveness)62 std::unique_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness) {
63   return std::unique_ptr<Vad>(new VadImpl(aggressiveness));
64 }
65 
66 }  // namespace webrtc
67