blob: 320fda96f1994ceb3b476db372df15b07b5611d8 [file] [log] [blame]
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +00001/*
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +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
11#include <stdlib.h>
12
13#include "gtest/gtest.h"
14#include "typedefs.h"
15#include "vad_unittest.h"
16
17extern "C" {
18#include "vad_core.h"
19#include "vad_defines.h"
20#include "vad_filterbank.h"
21}
22
23namespace {
24
25enum { kNumValidFrameLengths = 3 };
26
27TEST_F(VadTest, vad_filterbank) {
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000028 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 * NUM_CHANNELS] = {
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000031 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[NUM_CHANNELS] = {
36 368, 368, 272, 176, 176, 176 };
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000037 int16_t features[NUM_CHANNELS];
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000038
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 (int16_t i = 0; i < kMaxFrameLength; ++i) {
43 speech[i] = (i * i);
44 }
45
46 int frame_length_index = 0;
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +000047 ASSERT_EQ(0, WebRtcVad_InitCore(self));
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000048 for (size_t j = 0; j < kFrameLengthsSize; ++j) {
49 if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
50 EXPECT_EQ(kReference[frame_length_index],
bjornv@webrtc.orgd1f148d2012-01-10 13:48:09 +000051 WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000052 features));
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000053 for (int k = 0; k < NUM_CHANNELS; ++k) {
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000054 EXPECT_EQ(kFeatures[k + frame_length_index * NUM_CHANNELS],
55 features[k]);
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000056 }
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));
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +000064 ASSERT_EQ(0, WebRtcVad_InitCore(self));
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000065 for (size_t j = 0; j < kFrameLengthsSize; ++j) {
66 if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) {
bjornv@webrtc.orgd1f148d2012-01-10 13:48:09 +000067 EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000068 features));
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000069 for (int k = 0; k < NUM_CHANNELS; ++k) {
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000070 EXPECT_EQ(kOffsetVector[k], features[k]);
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000071 }
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 (int16_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])) {
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +000082 ASSERT_EQ(0, WebRtcVad_InitCore(self));
bjornv@webrtc.orgd1f148d2012-01-10 13:48:09 +000083 EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j],
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000084 features));
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000085 for (int k = 0; k < NUM_CHANNELS; ++k) {
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000086 EXPECT_EQ(kOffsetVector[k], features[k]);
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000087 }
88 }
89 }
90
91 free(self);
92}
93} // namespace