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 #include "common_audio/vad/vad_filterbank.h"
20 }
21 
22 namespace webrtc {
23 namespace test {
24 
25 const int kNumValidFrameLengths = 3;
26 
TEST_F(VadTest,vad_filterbank)27 TEST_F(VadTest, vad_filterbank) {
28   VadInstT* self = reinterpret_cast<VadInstT*>(malloc(sizeof(VadInstT)));
29   static const int16_t kReference[kNumValidFrameLengths] = { 48, 11, 11 };
30   static const int16_t kFeatures[kNumValidFrameLengths * kNumChannels] = {
31       1213, 759, 587, 462, 434, 272,
32       1479, 1385, 1291, 1200, 1103, 1099,
33       1732, 1692, 1681, 1629, 1436, 1436
34   };
35   static const int16_t kOffsetVector[kNumChannels] = {
36       368, 368, 272, 176, 176, 176 };
37   int16_t features[kNumChannels];
38 
39   // Construct a speech signal that will trigger the VAD in all modes. It is
40   // known that (i * i) will wrap around, but that doesn't matter in this case.
41   int16_t speech[kMaxFrameLength];
42   for (size_t i = 0; i < kMaxFrameLength; ++i) {
43     speech[i] = static_cast<int16_t>(i * i);
44   }
45 
46   int frame_length_index = 0;
47   ASSERT_EQ(0, WebRtcVad_InitCore(self));
48   for (size_t j = 0; j < kFrameLengthsSize; ++j) {
49     if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
50       EXPECT_EQ(kReference[frame_length_index],
51                 WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
52                                             features));
53       for (int k = 0; k < kNumChannels; ++k) {
54         EXPECT_EQ(kFeatures[k + frame_length_index * kNumChannels],
55                   features[k]);
56       }
57       frame_length_index++;
58     }
59   }
60   EXPECT_EQ(kNumValidFrameLengths, frame_length_index);
61 
62   // Verify that all zeros in gives kOffsetVector out.
63   memset(speech, 0, sizeof(speech));
64   ASSERT_EQ(0, WebRtcVad_InitCore(self));
65   for (size_t j = 0; j < kFrameLengthsSize; ++j) {
66     if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
67       EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
68                                                features));
69       for (int k = 0; k < kNumChannels; ++k) {
70         EXPECT_EQ(kOffsetVector[k], features[k]);
71       }
72     }
73   }
74 
75   // Verify that all ones in gives kOffsetVector out. Any other constant input
76   // will have a small impact in the sub bands.
77   for (size_t i = 0; i < kMaxFrameLength; ++i) {
78     speech[i] = 1;
79   }
80   for (size_t j = 0; j < kFrameLengthsSize; ++j) {
81     if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
82       ASSERT_EQ(0, WebRtcVad_InitCore(self));
83       EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
84                                                features));
85       for (int k = 0; k < kNumChannels; ++k) {
86         EXPECT_EQ(kOffsetVector[k], features[k]);
87       }
88     }
89   }
90 
91   free(self);
92 }
93 }  // namespace test
94 }  // namespace webrtc
95