blob: 69d6a7b345d51230f6f7132f92a259c87b6633d1 [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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
16#include "webrtc/common_audio/vad/include/webrtc_vad.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/arraysize.h"
18#include "webrtc/rtc_base/checks.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
oprypin67fdb802017-03-09 06:25:06 -080055namespace webrtc {
56namespace test {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000057
58TEST_F(VadTest, ApiTest) {
59 // This API test runs through the APIs for all possible valid and invalid
60 // combinations.
61
Bjorn Volckerde4703c2015-05-27 07:22:58 +020062 VadInst* handle = WebRtcVad_Create();
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000063 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];
Peter Kastingdce40cf2015-08-24 14:52:23 -070068 for (size_t i = 0; i < kMaxFrameLength; i++) {
pkastingb297c5a2015-07-22 15:17:22 -070069 speech[i] = static_cast<int16_t>(i * i);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000070 }
71
Bjorn Volckerde4703c2015-05-27 07:22:58 +020072 // 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]));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000077
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000078 // WebRtcVad_Create()
henrikg91d6ede2015-09-17 00:24:34 -070079 RTC_CHECK(handle);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000080
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
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000088 // 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));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000096
97 // WebRtcVad_Process() tests
Bjorn Volckerde4703c2015-05-27 07:22:58 +020098 // nullptr as speech pointer
99 EXPECT_EQ(-1,
100 WebRtcVad_Process(handle, kRates[0], nullptr, kFrameLengths[0]));
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000101 // 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
bjornv@webrtc.org2a796722014-04-22 04:45:35 +0000126 WebRtcVad_Free(handle);
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000127}
128
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000129TEST_F(VadTest, ValidRatesFrameLengths) {
130 // This test verifies valid and invalid rate/frame_length combinations. We
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000131 // loop through some sampling rates and frame lengths from negative values to
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000132 // values larger than possible.
Peter Kasting728d9032015-06-11 14:31:38 -0700133 const int kRates[] = {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000134 -8000, -4000, 0, 4000, 8000, 8001, 15999, 16000, 32000, 48000, 48001, 96000
135 };
136
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 const size_t kFrameLengths[] = {
138 0, 80, 81, 159, 160, 240, 320, 480, 640, 960, 1440, 2000
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000139 };
140
Peter Kasting728d9032015-06-11 14:31:38 -0700141 for (size_t i = 0; i < arraysize(kRates); i++) {
142 for (size_t j = 0; j < arraysize(kFrameLengths); j++) {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000143 if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) {
144 EXPECT_EQ(0, WebRtcVad_ValidRateAndFrameLength(kRates[i],
145 kFrameLengths[j]));
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000146 } else {
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +0000147 EXPECT_EQ(-1, WebRtcVad_ValidRateAndFrameLength(kRates[i],
148 kFrameLengths[j]));
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000149 }
150 }
151 }
152}
153
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +0000154// TODO(bjornv): Add a process test, run on file.
155
oprypin67fdb802017-03-09 06:25:06 -0800156} // namespace test
157} // namespace webrtc