From 8aa4d2d2cd46bca6da7e071583482dd7ed0e2d0c Mon Sep 17 00:00:00 2001 From: "henrik.lundin@webrtc.org" Date: Thu, 30 Oct 2014 13:23:25 +0000 Subject: [PATCH] Creating a C++ wrapper class for VAD Also adding a mock. This work is part of an ongoing effort to encapsulate encoders in AudioEncoder classes. The CNG encoder will also be implemented as an AudioEncoder class, and will also contain a VAD C++ wrapper. BUG=3926 R=bjornv@webrtc.org Review URL: https://webrtc-codereview.appspot.com/27839004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7570 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/common_audio/BUILD.gn | 2 ++ webrtc/common_audio/common_audio.gyp | 2 ++ webrtc/common_audio/vad/include/vad.h | 45 +++++++++++++++++++++++++ webrtc/common_audio/vad/mock/mock_vad.h | 34 +++++++++++++++++++ webrtc/common_audio/vad/vad.cc | 43 +++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 webrtc/common_audio/vad/include/vad.h create mode 100644 webrtc/common_audio/vad/mock/mock_vad.h create mode 100644 webrtc/common_audio/vad/vad.cc diff --git a/webrtc/common_audio/BUILD.gn b/webrtc/common_audio/BUILD.gn index ad49d17fd6..1338a75647 100644 --- a/webrtc/common_audio/BUILD.gn +++ b/webrtc/common_audio/BUILD.gn @@ -71,7 +71,9 @@ source_set("common_audio") { "signal_processing/splitting_filter.c", "signal_processing/sqrt_of_one_minus_x_squared.c", "signal_processing/vector_scaling_operations.c", + "vad/include/vad.h", "vad/include/webrtc_vad.h", + "vad/vad.cc", "vad/webrtc_vad.c", "vad/vad_core.c", "vad/vad_core.h", diff --git a/webrtc/common_audio/common_audio.gyp b/webrtc/common_audio/common_audio.gyp index 6c1b79608a..70d6909c55 100644 --- a/webrtc/common_audio/common_audio.gyp +++ b/webrtc/common_audio/common_audio.gyp @@ -85,7 +85,9 @@ 'signal_processing/splitting_filter.c', 'signal_processing/sqrt_of_one_minus_x_squared.c', 'signal_processing/vector_scaling_operations.c', + 'vad/include/vad.h', 'vad/include/webrtc_vad.h', + 'vad/vad.cc', 'vad/webrtc_vad.c', 'vad/vad_core.c', 'vad/vad_core.h', diff --git a/webrtc/common_audio/vad/include/vad.h b/webrtc/common_audio/vad/include/vad.h new file mode 100644 index 0000000000..f1d12123f4 --- /dev/null +++ b/webrtc/common_audio/vad/include/vad.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2014 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 WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_ +#define WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_ + +#include "webrtc/base/checks.h" +#include "webrtc/common_audio/vad/include/webrtc_vad.h" +#include "webrtc/typedefs.h" + +namespace webrtc { + +// This is a C++ wrapper class for WebRtcVad. +class Vad { + public: + enum Aggressiveness { + kVadNormal = 0, + kVadLowBitrate = 1, + kVadAggressive = 2, + kVadVeryAggressive = 3 + }; + + enum Activity { kPassive = 0, kActive = 1, kError = -1 }; + + explicit Vad(enum Aggressiveness mode); + + virtual ~Vad(); + + enum Activity VoiceActivity(const int16_t* audio, + size_t num_samples, + int sample_rate_hz); + + private: + VadInst* handle_; +}; + +} // namespace webrtc +#endif // WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_ diff --git a/webrtc/common_audio/vad/mock/mock_vad.h b/webrtc/common_audio/vad/mock/mock_vad.h new file mode 100644 index 0000000000..f1d8c226b0 --- /dev/null +++ b/webrtc/common_audio/vad/mock/mock_vad.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2014 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 WEBRTC_COMMON_AUDIO_VAD_MOCK_MOCK_VAD_H_ +#define WEBRTC_COMMON_AUDIO_VAD_MOCK_MOCK_VAD_H_ + +#include "webrtc/common_audio/vad/include/vad.h" + +#include "testing/gmock/include/gmock/gmock.h" + +namespace webrtc { + +class MockVad : public Vad { + public: + explicit MockVad(enum Aggressiveness mode) {} + virtual ~MockVad() { Die(); } + MOCK_METHOD0(Die, void()); + + MOCK_METHOD3(VoiceActivity, + enum Activity(const int16_t* audio, + size_t num_samples, + int sample_rate_hz)); +}; + +} // namespace webrtc + +#endif // WEBRTC_COMMON_AUDIO_VAD_MOCK_MOCK_VAD_H_ diff --git a/webrtc/common_audio/vad/vad.cc b/webrtc/common_audio/vad/vad.cc new file mode 100644 index 0000000000..9cc0c19877 --- /dev/null +++ b/webrtc/common_audio/vad/vad.cc @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014 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. + */ + +#include "webrtc/common_audio/vad/include/vad.h" + +#include "webrtc/base/checks.h" + +namespace webrtc { + +Vad::Vad(enum Aggressiveness mode) { + CHECK_EQ(WebRtcVad_Create(&handle_), 0); + CHECK_EQ(WebRtcVad_Init(handle_), 0); + CHECK_EQ(WebRtcVad_set_mode(handle_, mode), 0); +} + +Vad::~Vad() { + WebRtcVad_Free(handle_); +} + +enum Vad::Activity Vad::VoiceActivity(const int16_t* audio, + size_t num_samples, + int sample_rate_hz) { + int ret = WebRtcVad_Process( + handle_, sample_rate_hz, audio, static_cast(num_samples)); + switch (ret) { + case 0: + return kPassive; + case 1: + return kActive; + default: + DCHECK(false) << "WebRtcVad_Process returned an error."; + return kError; + } +} + +} // namespace webrtc