bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 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 "gtest/gtest.h" |
| 14 | #include "typedefs.h" |
| 15 | #include "vad_unittest.h" |
| 16 | |
| 17 | extern "C" { |
| 18 | #include "vad_core.h" |
| 19 | #include "vad_defines.h" |
| 20 | #include "vad_filterbank.h" |
| 21 | } |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | enum { kNumValidFrameLengths = 3 }; |
| 26 | |
| 27 | TEST_F(VadTest, vad_filterbank) { |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 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 * NUM_CHANNELS] = { |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 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[NUM_CHANNELS] = { |
| 36 | 368, 368, 272, 176, 176, 176 }; |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 37 | int16_t features[NUM_CHANNELS]; |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 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 (int16_t i = 0; i < kMaxFrameLength; ++i) { |
| 43 | speech[i] = (i * i); |
| 44 | } |
| 45 | |
| 46 | int frame_length_index = 0; |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame^] | 47 | ASSERT_EQ(0, WebRtcVad_InitCore(self)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 48 | for (size_t j = 0; j < kFrameLengthsSize; ++j) { |
| 49 | if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) { |
| 50 | EXPECT_EQ(kReference[frame_length_index], |
bjornv@webrtc.org | d1f148d | 2012-01-10 13:48:09 +0000 | [diff] [blame] | 51 | WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j], |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 52 | features)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 53 | for (int k = 0; k < NUM_CHANNELS; ++k) { |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 54 | EXPECT_EQ(kFeatures[k + frame_length_index * NUM_CHANNELS], |
| 55 | features[k]); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 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)); |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame^] | 64 | ASSERT_EQ(0, WebRtcVad_InitCore(self)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 65 | for (size_t j = 0; j < kFrameLengthsSize; ++j) { |
| 66 | if (ValidRatesAndFrameLengths(8000, kFrameLengths[j])) { |
bjornv@webrtc.org | d1f148d | 2012-01-10 13:48:09 +0000 | [diff] [blame] | 67 | EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j], |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 68 | features)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 69 | for (int k = 0; k < NUM_CHANNELS; ++k) { |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 70 | EXPECT_EQ(kOffsetVector[k], features[k]); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 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 (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.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame^] | 82 | ASSERT_EQ(0, WebRtcVad_InitCore(self)); |
bjornv@webrtc.org | d1f148d | 2012-01-10 13:48:09 +0000 | [diff] [blame] | 83 | EXPECT_EQ(0, WebRtcVad_CalculateFeatures(self, speech, kFrameLengths[j], |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 84 | features)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 85 | for (int k = 0; k < NUM_CHANNELS; ++k) { |
bjornv@webrtc.org | 40ea510 | 2012-01-12 12:47:42 +0000 | [diff] [blame] | 86 | EXPECT_EQ(kOffsetVector[k], features[k]); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | free(self); |
| 92 | } |
| 93 | } // namespace |