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 <stdlib.h>
12 
13 #include "common_audio/vad/vad_unittest.h"
14 #include "test/gtest.h"
15 #include "typedefs.h"  // NOLINT(build/include)
16 
17 extern "C" {
18 #include "common_audio/vad/vad_core.h"
19 }
20 
21 namespace webrtc {
22 namespace test {
23 
TEST_F(VadTest,InitCore)24 TEST_F(VadTest, InitCore) {
25   // Test WebRtcVad_InitCore().
26   VadInstT* self = reinterpret_cast<VadInstT*>(malloc(sizeof(VadInstT)));
27 
28   // null pointer test.
29   EXPECT_EQ(-1, WebRtcVad_InitCore(nullptr));
30 
31   // Verify return = 0 for non-null pointer.
32   EXPECT_EQ(0, WebRtcVad_InitCore(self));
33   // Verify init_flag is set.
34   EXPECT_EQ(42, self->init_flag);
35 
36   free(self);
37 }
38 
TEST_F(VadTest,set_mode_core)39 TEST_F(VadTest, set_mode_core) {
40   VadInstT* self = reinterpret_cast<VadInstT*>(malloc(sizeof(VadInstT)));
41 
42   // TODO(bjornv): Add null pointer check if we take care of it in
43   // vad_core.c
44 
45   ASSERT_EQ(0, WebRtcVad_InitCore(self));
46   // Test WebRtcVad_set_mode_core().
47   // Invalid modes should return -1.
48   EXPECT_EQ(-1, WebRtcVad_set_mode_core(self, -1));
49   EXPECT_EQ(-1, WebRtcVad_set_mode_core(self, 1000));
50   // Valid modes should return 0.
51   for (size_t j = 0; j < kModesSize; ++j) {
52     EXPECT_EQ(0, WebRtcVad_set_mode_core(self, kModes[j]));
53   }
54 
55   free(self);
56 }
57 
TEST_F(VadTest,CalcVad)58 TEST_F(VadTest, CalcVad) {
59   VadInstT* self = reinterpret_cast<VadInstT*>(malloc(sizeof(VadInstT)));
60   int16_t speech[kMaxFrameLength];
61 
62   // TODO(bjornv): Add null pointer check if we take care of it in
63   // vad_core.c
64 
65   // Test WebRtcVad_CalcVadXXkhz()
66   // Verify that all zeros in gives VAD = 0 out.
67   memset(speech, 0, sizeof(speech));
68   ASSERT_EQ(0, WebRtcVad_InitCore(self));
69   for (size_t j = 0; j < kFrameLengthsSize; ++j) {
70     if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
71       EXPECT_EQ(0, WebRtcVad_CalcVad8khz(self, speech, kFrameLengths[j]));
72     }
73     if (ValidRatesAndFrameLengths(16000, kFrameLengths[j])) {
74       EXPECT_EQ(0, WebRtcVad_CalcVad16khz(self, speech, kFrameLengths[j]));
75     }
76     if (ValidRatesAndFrameLengths(32000, kFrameLengths[j])) {
77       EXPECT_EQ(0, WebRtcVad_CalcVad32khz(self, speech, kFrameLengths[j]));
78     }
79     if (ValidRatesAndFrameLengths(48000, kFrameLengths[j])) {
80       EXPECT_EQ(0, WebRtcVad_CalcVad48khz(self, speech, kFrameLengths[j]));
81     }
82   }
83 
84   // Construct a speech signal that will trigger the VAD in all modes. It is
85   // known that (i * i) will wrap around, but that doesn't matter in this case.
86   for (size_t i = 0; i < kMaxFrameLength; ++i) {
87     speech[i] = static_cast<int16_t>(i * i);
88   }
89   for (size_t j = 0; j < kFrameLengthsSize; ++j) {
90     if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
91       EXPECT_EQ(1, WebRtcVad_CalcVad8khz(self, speech, kFrameLengths[j]));
92     }
93     if (ValidRatesAndFrameLengths(16000, kFrameLengths[j])) {
94       EXPECT_EQ(1, WebRtcVad_CalcVad16khz(self, speech, kFrameLengths[j]));
95     }
96     if (ValidRatesAndFrameLengths(32000, kFrameLengths[j])) {
97       EXPECT_EQ(1, WebRtcVad_CalcVad32khz(self, speech, kFrameLengths[j]));
98     }
99     if (ValidRatesAndFrameLengths(48000, kFrameLengths[j])) {
100       EXPECT_EQ(1, WebRtcVad_CalcVad48khz(self, speech, kFrameLengths[j]));
101     }
102   }
103 
104   free(self);
105 }
106 }  // namespace test
107 }  // namespace webrtc
108