blob: 55b12793dad75d7a4cb286844928906c9c4cba06 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "common_audio/vad/vad_unittest.h"
14#include "test/gtest.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020015#include "typedefs.h" // NOLINT(build/include)
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000016
17extern "C" {
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/vad/vad_core.h"
19#include "common_audio/vad/vad_filterbank.h"
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000020}
21
oprypin67fdb802017-03-09 06:25:06 -080022namespace webrtc {
23namespace test {
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000024
pbos@webrtc.org3004c792013-05-07 12:36:21 +000025const int kNumValidFrameLengths = 3;
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000026
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 };
bjornv@webrtc.orga496b032012-03-20 12:53:06 +000030 static const int16_t kFeatures[kNumValidFrameLengths * kNumChannels] = {
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 };
bjornv@webrtc.orga496b032012-03-20 12:53:06 +000035 static const int16_t kOffsetVector[kNumChannels] = {
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000036 368, 368, 272, 176, 176, 176 };
bjornv@webrtc.orga496b032012-03-20 12:53:06 +000037 int16_t features[kNumChannels];
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];
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 for (size_t i = 0; i < kMaxFrameLength; ++i) {
pkastingb297c5a2015-07-22 15:17:22 -070043 speech[i] = static_cast<int16_t>(i * i);
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000044 }
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.orga496b032012-03-20 12:53:06 +000053 for (int k = 0; k < kNumChannels; ++k) {
54 EXPECT_EQ(kFeatures[k + frame_length_index * kNumChannels],
bjornv@webrtc.org40ea5102012-01-12 12:47:42 +000055 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.orga496b032012-03-20 12:53:06 +000069 for (int k = 0; k < kNumChannels; ++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.
Peter Kastingdce40cf2015-08-24 14:52:23 -070077 for (size_t i = 0; i < kMaxFrameLength; ++i) {
bjornv@webrtc.orge6471ba2012-01-09 09:54:07 +000078 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.orga496b032012-03-20 12:53:06 +000085 for (int k = 0; k < kNumChannels; ++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}
oprypin67fdb802017-03-09 06:25:06 -080093} // namespace test
94} // namespace webrtc