blob: 987ed526c00a9793ca97fca30b3248be66b06157 [file] [log] [blame]
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/vad/include/vad.h"
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000012
kwibergbfefb032016-05-01 14:53:46 -070013#include <memory>
14
Yves Gerey988cc082018-10-23 12:03:01 +020015#include "common_audio/vad/include/webrtc_vad.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000017
18namespace webrtc {
19
kwiberge9e78962015-09-08 23:04:51 -070020namespace {
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000021
kwiberge9e78962015-09-08 23:04:51 -070022class VadImpl final : public Vad {
23 public:
24 explicit VadImpl(Aggressiveness aggressiveness)
25 : handle_(nullptr), aggressiveness_(aggressiveness) {
26 Reset();
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000027 }
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000028
kwiberge9e78962015-09-08 23:04:51 -070029 ~VadImpl() override { WebRtcVad_Free(handle_); }
30
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:
nisseeb4ca4e2017-01-12 02:24:27 -080041 RTC_NOTREACHED() << "WebRtcVad_Process returned an error.";
kwiberge9e78962015-09-08 23:04:51 -070042 return kError;
43 }
44 }
45
46 void Reset() override {
47 if (handle_)
48 WebRtcVad_Free(handle_);
49 handle_ = WebRtcVad_Create();
henrikg91d6ede2015-09-17 00:24:34 -070050 RTC_CHECK(handle_);
51 RTC_CHECK_EQ(WebRtcVad_Init(handle_), 0);
52 RTC_CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0);
kwiberge9e78962015-09-08 23:04:51 -070053 }
54
55 private:
56 VadInst* handle_;
57 Aggressiveness aggressiveness_;
58};
59
60} // namespace
61
kwiberg91d97562016-02-14 01:10:03 -080062std::unique_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness) {
63 return std::unique_ptr<Vad>(new VadImpl(aggressiveness));
kwiberg12cfc9b2015-09-08 05:57:53 -070064}
65
henrik.lundin@webrtc.org8aa4d2d2014-10-30 13:23:25 +000066} // namespace webrtc