blob: a03f667b408defe4efaf64d287c40e5121802eab [file] [log] [blame]
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +00003 *
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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000011#include "webrtc/common_audio/vad/vad_unittest.h"
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000012
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000013#include <stdlib.h>
14
Peter Kasting728d9032015-06-11 14:31:38 -070015#include "webrtc/base/arraysize.h"
Bjorn Volckerde4703c2015-05-27 07:22:58 +020016#include "webrtc/base/checks.h"
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000017#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
18#include "webrtc/common_audio/vad/include/webrtc_vad.h"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/test/gtest.h"
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000020#include "webrtc/typedefs.h"
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000021
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000022VadTest::VadTest() {}
bjornv@webrtc.orgc68f80a2011-12-20 14:08:34 +000023
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000024void VadTest::SetUp() {}
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000025
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000026void VadTest::TearDown() {}
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000027
28// Returns true if the rate and frame length combination is valid.
Peter Kastingdce40cf2015-08-24 14:52:23 -070029bool VadTest::ValidRatesAndFrameLengths(int rate, size_t frame_length) {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000030 if (rate == 8000) {
31 if (frame_length == 80 || frame_length == 160 || frame_length == 240) {
32 return true;
33 }
34 return false;
35 } else if (rate == 16000) {
36 if (frame_length == 160 || frame_length == 320 || frame_length == 480) {
37 return true;
38 }
39 return false;
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000040 } else if (rate == 32000) {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000041 if (frame_length == 320 || frame_length == 640 || frame_length == 960) {
42 return true;
43 }
44 return false;
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000045 } else if (rate == 48000) {
46 if (frame_length == 480 || frame_length == 960 || frame_length == 1440) {
47 return true;
48 }
49 return false;
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000050 }
51
52 return false;
53}
54
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000055namespace {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000056
57TEST_F(VadTest, ApiTest) {
58 // This API test runs through the APIs for all possible valid and invalid
59 // combinations.
60
Bjorn Volckerde4703c2015-05-27 07:22:58 +020061 VadInst* handle = WebRtcVad_Create();
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000062 int16_t zeros[kMaxFrameLength] = { 0 };
63
64 // Construct a speech signal that will trigger the VAD in all modes. It is
65 // known that (i * i) will wrap around, but that doesn't matter in this case.
66 int16_t speech[kMaxFrameLength];
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 for (size_t i = 0; i < kMaxFrameLength; i++) {
pkastingb297c5a2015-07-22 15:17:22 -070068 speech[i] = static_cast<int16_t>(i * i);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000069 }
70
Bjorn Volckerde4703c2015-05-27 07:22:58 +020071 // nullptr instance tests
72 EXPECT_EQ(-1, WebRtcVad_Init(nullptr));
73 EXPECT_EQ(-1, WebRtcVad_set_mode(nullptr, kModes[0]));
74 EXPECT_EQ(-1,
75 WebRtcVad_Process(nullptr, kRates[0], speech, kFrameLengths[0]));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000076
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000077 // WebRtcVad_Create()
henrikg91d6ede2015-09-17 00:24:34 -070078 RTC_CHECK(handle);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000079
80 // Not initialized tests
81 EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], speech, kFrameLengths[0]));
82 EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[0]));
83
84 // WebRtcVad_Init() test
85 ASSERT_EQ(0, WebRtcVad_Init(handle));
86
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000087 // WebRtcVad_set_mode() invalid modes tests. Tries smallest supported value
88 // minus one and largest supported value plus one.
89 EXPECT_EQ(-1, WebRtcVad_set_mode(handle,
90 WebRtcSpl_MinValueW32(kModes,
91 kModesSize) - 1));
92 EXPECT_EQ(-1, WebRtcVad_set_mode(handle,
93 WebRtcSpl_MaxValueW32(kModes,
94 kModesSize) + 1));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000095
96 // WebRtcVad_Process() tests
Bjorn Volckerde4703c2015-05-27 07:22:58 +020097 // nullptr as speech pointer
98 EXPECT_EQ(-1,
99 WebRtcVad_Process(handle, kRates[0], nullptr, kFrameLengths[0]));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000100 // Invalid sampling rate
101 EXPECT_EQ(-1, WebRtcVad_Process(handle, 9999, speech, kFrameLengths[0]));
102 // All zeros as input should work
103 EXPECT_EQ(0, WebRtcVad_Process(handle, kRates[0], zeros, kFrameLengths[0]));
104 for (size_t k = 0; k < kModesSize; k++) {
105 // Test valid modes
106 EXPECT_EQ(0, WebRtcVad_set_mode(handle, kModes[k]));
107 // Loop through sampling rate and frame length combinations
108 for (size_t i = 0; i < kRatesSize; i++) {
109 for (size_t j = 0; j < kFrameLengthsSize; j++) {
110 if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) {
111 EXPECT_EQ(1, WebRtcVad_Process(handle,
112 kRates[i],
113 speech,
114 kFrameLengths[j]));
115 } else {
116 EXPECT_EQ(-1, WebRtcVad_Process(handle,
117 kRates[i],
118 speech,
119 kFrameLengths[j]));
120 }
121 }
122 }
123 }
124
bjornv@webrtc.org2a796722014-04-22 04:45:35 +0000125 WebRtcVad_Free(handle);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000126}
127
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000128TEST_F(VadTest, ValidRatesFrameLengths) {
129 // This test verifies valid and invalid rate/frame_length combinations. We
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000130 // loop through some sampling rates and frame lengths from negative values to
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000131 // values larger than possible.
Peter Kasting728d9032015-06-11 14:31:38 -0700132 const int kRates[] = {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000133 -8000, -4000, 0, 4000, 8000, 8001, 15999, 16000, 32000, 48000, 48001, 96000
134 };
135
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 const size_t kFrameLengths[] = {
137 0, 80, 81, 159, 160, 240, 320, 480, 640, 960, 1440, 2000
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000138 };
139
Peter Kasting728d9032015-06-11 14:31:38 -0700140 for (size_t i = 0; i < arraysize(kRates); i++) {
141 for (size_t j = 0; j < arraysize(kFrameLengths); j++) {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000142 if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) {
143 EXPECT_EQ(0, WebRtcVad_ValidRateAndFrameLength(kRates[i],
144 kFrameLengths[j]));
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000145 } else {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000146 EXPECT_EQ(-1, WebRtcVad_ValidRateAndFrameLength(kRates[i],
147 kFrameLengths[j]));
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000148 }
149 }
150 }
151}
152
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000153// TODO(bjornv): Add a process test, run on file.
154
155} // namespace