summaryrefslogtreecommitdiff
path: root/common_audio/vad
diff options
context:
space:
mode:
authorhenrik.lundin@webrtc.org <henrik.lundin@webrtc.org>2014-10-30 13:23:25 +0000
committerhenrik.lundin@webrtc.org <henrik.lundin@webrtc.org>2014-10-30 13:23:25 +0000
commitd7062cc078dbb80d775336a4870dbbc750fc9b32 (patch)
tree6be83968fda2812d288be056f56f87ce67632961 /common_audio/vad
parent78f89f1bfb31a3cb73b70c43315a3d83e524d7a6 (diff)
downloadwebrtc-d7062cc078dbb80d775336a4870dbbc750fc9b32.tar.gz
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/webrtc@7570 4adac7df-926f-26a2-2b94-8c16560cd09d
Diffstat (limited to 'common_audio/vad')
-rw-r--r--common_audio/vad/include/vad.h45
-rw-r--r--common_audio/vad/mock/mock_vad.h34
-rw-r--r--common_audio/vad/vad.cc43
3 files changed, 122 insertions, 0 deletions
diff --git a/common_audio/vad/include/vad.h b/common_audio/vad/include/vad.h
new file mode 100644
index 00000000..f1d12123
--- /dev/null
+++ b/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/common_audio/vad/mock/mock_vad.h b/common_audio/vad/mock/mock_vad.h
new file mode 100644
index 00000000..f1d8c226
--- /dev/null
+++ b/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/common_audio/vad/vad.cc b/common_audio/vad/vad.cc
new file mode 100644
index 00000000..9cc0c198
--- /dev/null
+++ b/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<int>(num_samples));
+ switch (ret) {
+ case 0:
+ return kPassive;
+ case 1:
+ return kActive;
+ default:
+ DCHECK(false) << "WebRtcVad_Process returned an error.";
+ return kError;
+ }
+}
+
+} // namespace webrtc