blob: ecc47342d0df1b3823964125d8692338df1dd42a [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 "testing/gtest/include/gtest/gtest.h"
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000016
Peter Kasting728d9032015-06-11 14:31:38 -070017#include "webrtc/base/arraysize.h"
Bjorn Volckerde4703c2015-05-27 07:22:58 +020018#include "webrtc/base/checks.h"
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000019#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"
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000022
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000023VadTest::VadTest() {}
bjornv@webrtc.orgc68f80a2011-12-20 14:08:34 +000024
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000025void VadTest::SetUp() {}
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000026
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000027void VadTest::TearDown() {}
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000028
29// Returns true if the rate and frame length combination is valid.
Peter Kastingdce40cf2015-08-24 14:52:23 -070030bool VadTest::ValidRatesAndFrameLengths(int rate, size_t frame_length) {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000031 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;
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000041 } else if (rate == 32000) {
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000042 if (frame_length == 320 || frame_length == 640 || frame_length == 960) {
43 return true;
44 }
45 return false;
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000046 } else if (rate == 48000) {
47 if (frame_length == 480 || frame_length == 960 || frame_length == 1440) {
48 return true;
49 }
50 return false;
bjornv@webrtc.org250cd6f2011-10-28 12:45:58 +000051 }
52
53 return false;
54}
55
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000056namespace {
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()
Bjorn Volckerde4703c2015-05-27 07:22:58 +020079 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
156} // namespace