1 /*
2 * Copyright (c) 2012 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 "webrtc/common_audio/vad/vad_unittest.h"
12
13 #include <stdlib.h>
14
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 #include "webrtc/base/arraysize.h"
18 #include "webrtc/base/checks.h"
19 #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
20 #include "webrtc/common_audio/vad/include/webrtc_vad.h"
21 #include "webrtc/typedefs.h"
22
VadTest()23 VadTest::VadTest() {}
24
SetUp()25 void VadTest::SetUp() {}
26
TearDown()27 void VadTest::TearDown() {}
28
29 // Returns true if the rate and frame length combination is valid.
ValidRatesAndFrameLengths(int rate,size_t frame_length)30 bool VadTest::ValidRatesAndFrameLengths(int rate, size_t frame_length) {
31 if (rate == 8000) {
32 if (frame_length == 80 || frame_length == 160 || frame_length == 240) {
33 return true;
34 }
35 return false;
36 } else if (rate == 16000) {
37 if (frame_length == 160 || frame_length == 320 || frame_length == 480) {
38 return true;
39 }
40 return false;
41 } else if (rate == 32000) {
42 if (frame_length == 320 || frame_length == 640 || frame_length == 960) {
43 return true;
44 }
45 return false;
46 } else if (rate == 48000) {
47 if (frame_length == 480 || frame_length == 960 || frame_length == 1440) {
48 return true;
49 }
50 return false;
51 }
52
53 return false;
54 }
55
56 namespace {
57
TEST_F(VadTest,ApiTest)58 TEST_F(VadTest, ApiTest) {
59 // This API test runs through the APIs for all possible valid and invalid
60 // combinations.
61
62 VadInst* handle = WebRtcVad_Create();
63 int16_t zeros[kMaxFrameLength] = { 0 };
64
65 // Construct a speech signal that will trigger the VAD in all modes. It is
66 // known that (i * i) will wrap around, but that doesn't matter in this case.
67 int16_t speech[kMaxFrameLength];
68 for (size_t i = 0; i < kMaxFrameLength; i++) {
69 speech[i] = static_cast<int16_t>(i * i);
70 }
71
72 // nullptr instance tests
73 EXPECT_EQ(-1, WebRtcVad_Init(nullptr));
74 EXPECT_EQ(-1, WebRtcVad_set_mode(nullptr, kModes[0]));
75 EXPECT_EQ(-1,
76 WebRtcVad_Process(nullptr, kRates[0], speech, kFrameLengths[0]));
77
78 // WebRtcVad_Create()
79 RTC_CHECK(handle);
80
81 // Not initialized tests
82 EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], speech, kFrameLengths[0]));
83 EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[0]));
84
85 // WebRtcVad_Init() test
86 ASSERT_EQ(0, WebRtcVad_Init(handle));
87
88 // WebRtcVad_set_mode() invalid modes tests. Tries smallest supported value
89 // minus one and largest supported value plus one.
90 EXPECT_EQ(-1, WebRtcVad_set_mode(handle,
91 WebRtcSpl_MinValueW32(kModes,
92 kModesSize) - 1));
93 EXPECT_EQ(-1, WebRtcVad_set_mode(handle,
94 WebRtcSpl_MaxValueW32(kModes,
95 kModesSize) + 1));
96
97 // WebRtcVad_Process() tests
98 // nullptr as speech pointer
99 EXPECT_EQ(-1,
100 WebRtcVad_Process(handle, kRates[0], nullptr, kFrameLengths[0]));
101 // Invalid sampling rate
102 EXPECT_EQ(-1, WebRtcVad_Process(handle, 9999, speech, kFrameLengths[0]));
103 // All zeros as input should work
104 EXPECT_EQ(0, WebRtcVad_Process(handle, kRates[0], zeros, kFrameLengths[0]));
105 for (size_t k = 0; k < kModesSize; k++) {
106 // Test valid modes
107 EXPECT_EQ(0, WebRtcVad_set_mode(handle, kModes[k]));
108 // Loop through sampling rate and frame length combinations
109 for (size_t i = 0; i < kRatesSize; i++) {
110 for (size_t j = 0; j < kFrameLengthsSize; j++) {
111 if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) {
112 EXPECT_EQ(1, WebRtcVad_Process(handle,
113 kRates[i],
114 speech,
115 kFrameLengths[j]));
116 } else {
117 EXPECT_EQ(-1, WebRtcVad_Process(handle,
118 kRates[i],
119 speech,
120 kFrameLengths[j]));
121 }
122 }
123 }
124 }
125
126 WebRtcVad_Free(handle);
127 }
128
TEST_F(VadTest,ValidRatesFrameLengths)129 TEST_F(VadTest, ValidRatesFrameLengths) {
130 // This test verifies valid and invalid rate/frame_length combinations. We
131 // loop through some sampling rates and frame lengths from negative values to
132 // values larger than possible.
133 const int kRates[] = {
134 -8000, -4000, 0, 4000, 8000, 8001, 15999, 16000, 32000, 48000, 48001, 96000
135 };
136
137 const size_t kFrameLengths[] = {
138 0, 80, 81, 159, 160, 240, 320, 480, 640, 960, 1440, 2000
139 };
140
141 for (size_t i = 0; i < arraysize(kRates); i++) {
142 for (size_t j = 0; j < arraysize(kFrameLengths); j++) {
143 if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) {
144 EXPECT_EQ(0, WebRtcVad_ValidRateAndFrameLength(kRates[i],
145 kFrameLengths[j]));
146 } else {
147 EXPECT_EQ(-1, WebRtcVad_ValidRateAndFrameLength(kRates[i],
148 kFrameLengths[j]));
149 }
150 }
151 }
152 }
153
154 // TODO(bjornv): Add a process test, run on file.
155
156 } // namespace
157