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