bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +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 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame^] | 11 | #include "webrtc/common_audio/vad/vad_unittest.h" |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 12 | |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame^] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 16 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame^] | 17 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 18 | #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
| 19 | #include "webrtc/typedefs.h" |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 20 | |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 21 | VadTest::VadTest() {} |
bjornv@webrtc.org | c68f80a | 2011-12-20 14:08:34 +0000 | [diff] [blame] | 22 | |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 23 | void VadTest::SetUp() {} |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 24 | |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 25 | void VadTest::TearDown() {} |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 26 | |
| 27 | // Returns true if the rate and frame length combination is valid. |
bjornv@webrtc.org | b38fca1 | 2012-06-19 11:03:32 +0000 | [diff] [blame] | 28 | bool VadTest::ValidRatesAndFrameLengths(int rate, int frame_length) { |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 29 | if (rate == 8000) { |
| 30 | if (frame_length == 80 || frame_length == 160 || frame_length == 240) { |
| 31 | return true; |
| 32 | } |
| 33 | return false; |
| 34 | } else if (rate == 16000) { |
| 35 | if (frame_length == 160 || frame_length == 320 || frame_length == 480) { |
| 36 | return true; |
| 37 | } |
| 38 | return false; |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 39 | } else if (rate == 32000) { |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 40 | if (frame_length == 320 || frame_length == 640 || frame_length == 960) { |
| 41 | return true; |
| 42 | } |
| 43 | return false; |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 44 | } else if (rate == 48000) { |
| 45 | if (frame_length == 480 || frame_length == 960 || frame_length == 1440) { |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 54 | namespace { |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 55 | |
| 56 | TEST_F(VadTest, ApiTest) { |
| 57 | // This API test runs through the APIs for all possible valid and invalid |
| 58 | // combinations. |
| 59 | |
| 60 | VadInst* handle = NULL; |
| 61 | int16_t zeros[kMaxFrameLength] = { 0 }; |
| 62 | |
| 63 | // Construct a speech signal that will trigger the VAD in all modes. It is |
| 64 | // known that (i * i) will wrap around, but that doesn't matter in this case. |
| 65 | int16_t speech[kMaxFrameLength]; |
| 66 | for (int16_t i = 0; i < kMaxFrameLength; i++) { |
| 67 | speech[i] = (i * i); |
| 68 | } |
| 69 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 70 | // NULL instance tests |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 71 | EXPECT_EQ(-1, WebRtcVad_Create(NULL)); |
| 72 | EXPECT_EQ(-1, WebRtcVad_Init(NULL)); |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 73 | EXPECT_EQ(-1, WebRtcVad_Free(NULL)); |
| 74 | EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, kModes[0])); |
| 75 | EXPECT_EQ(-1, WebRtcVad_Process(NULL, kRates[0], speech, kFrameLengths[0])); |
| 76 | |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 77 | // WebRtcVad_Create() |
| 78 | ASSERT_EQ(0, WebRtcVad_Create(&handle)); |
| 79 | |
| 80 | // Not initialized tests |
| 81 | EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], speech, kFrameLengths[0])); |
| 82 | EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[0])); |
| 83 | |
| 84 | // WebRtcVad_Init() test |
| 85 | ASSERT_EQ(0, WebRtcVad_Init(handle)); |
| 86 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 87 | // WebRtcVad_set_mode() invalid modes tests. Tries smallest supported value |
| 88 | // minus one and largest supported value plus one. |
| 89 | EXPECT_EQ(-1, WebRtcVad_set_mode(handle, |
| 90 | WebRtcSpl_MinValueW32(kModes, |
| 91 | kModesSize) - 1)); |
| 92 | EXPECT_EQ(-1, WebRtcVad_set_mode(handle, |
| 93 | WebRtcSpl_MaxValueW32(kModes, |
| 94 | kModesSize) + 1)); |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 95 | |
| 96 | // WebRtcVad_Process() tests |
| 97 | // NULL speech pointer |
| 98 | EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], NULL, kFrameLengths[0])); |
| 99 | // Invalid sampling rate |
| 100 | EXPECT_EQ(-1, WebRtcVad_Process(handle, 9999, speech, kFrameLengths[0])); |
| 101 | // All zeros as input should work |
| 102 | EXPECT_EQ(0, WebRtcVad_Process(handle, kRates[0], zeros, kFrameLengths[0])); |
| 103 | for (size_t k = 0; k < kModesSize; k++) { |
| 104 | // Test valid modes |
| 105 | EXPECT_EQ(0, WebRtcVad_set_mode(handle, kModes[k])); |
| 106 | // Loop through sampling rate and frame length combinations |
| 107 | for (size_t i = 0; i < kRatesSize; i++) { |
| 108 | for (size_t j = 0; j < kFrameLengthsSize; j++) { |
| 109 | if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) { |
| 110 | EXPECT_EQ(1, WebRtcVad_Process(handle, |
| 111 | kRates[i], |
| 112 | speech, |
| 113 | kFrameLengths[j])); |
| 114 | } else { |
| 115 | EXPECT_EQ(-1, WebRtcVad_Process(handle, |
| 116 | kRates[i], |
| 117 | speech, |
| 118 | kFrameLengths[j])); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | EXPECT_EQ(0, WebRtcVad_Free(handle)); |
| 125 | } |
| 126 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 127 | TEST_F(VadTest, ValidRatesFrameLengths) { |
| 128 | // This test verifies valid and invalid rate/frame_length combinations. We |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 129 | // loop through some sampling rates and frame lengths from negative values to |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 130 | // values larger than possible. |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 131 | const int kNumRates = 12; |
| 132 | const int kRates[kNumRates] = { |
| 133 | -8000, -4000, 0, 4000, 8000, 8001, 15999, 16000, 32000, 48000, 48001, 96000 |
| 134 | }; |
| 135 | |
| 136 | const int kNumFrameLengths = 13; |
| 137 | const int kFrameLengths[kNumFrameLengths] = { |
| 138 | -10, 0, 80, 81, 159, 160, 240, 320, 480, 640, 960, 1440, 2000 |
| 139 | }; |
| 140 | |
| 141 | for (int i = 0; i < kNumRates; i++) { |
| 142 | for (int j = 0; j < kNumFrameLengths; j++) { |
| 143 | if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) { |
| 144 | EXPECT_EQ(0, WebRtcVad_ValidRateAndFrameLength(kRates[i], |
| 145 | kFrameLengths[j])); |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 146 | } else { |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 147 | EXPECT_EQ(-1, WebRtcVad_ValidRateAndFrameLength(kRates[i], |
| 148 | kFrameLengths[j])); |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
bjornv@webrtc.org | 250cd6f | 2011-10-28 12:45:58 +0000 | [diff] [blame] | 154 | // TODO(bjornv): Add a process test, run on file. |
| 155 | |
| 156 | } // namespace |