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 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 13 | #include "webrtc/common_audio/vad/vad_unittest.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 14 | #include "webrtc/test/gtest.h" |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 15 | #include "webrtc/typedefs.h" |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 16 | |
| 17 | extern "C" { |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/vad/vad_core.h" |
| 19 | #include "webrtc/common_audio/vad/vad_sp.h" |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 20 | } |
| 21 | |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | namespace test { |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 24 | |
| 25 | TEST_F(VadTest, vad_sp) { |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame] | 26 | VadInstT* self = reinterpret_cast<VadInstT*>(malloc(sizeof(VadInstT))); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 27 | const size_t kMaxFrameLenSp = 960; // Maximum frame length in this unittest. |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 28 | int16_t zeros[kMaxFrameLenSp] = { 0 }; |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 29 | int32_t state[2] = { 0 }; |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 30 | int16_t data_in[kMaxFrameLenSp]; |
| 31 | int16_t data_out[kMaxFrameLenSp]; |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 32 | |
| 33 | // We expect the first value to be 1600 as long as |frame_counter| is zero, |
| 34 | // which is true for the first iteration. |
| 35 | static const int16_t kReferenceMin[32] = { |
| 36 | 1600, 720, 509, 512, 532, 552, 570, 588, |
| 37 | 606, 624, 642, 659, 675, 691, 707, 723, |
| 38 | 1600, 544, 502, 522, 542, 561, 579, 597, |
| 39 | 615, 633, 651, 667, 683, 699, 715, 731 |
| 40 | }; |
| 41 | |
| 42 | // Construct a speech signal that will trigger the VAD in all modes. It is |
| 43 | // known that (i * i) will wrap around, but that doesn't matter in this case. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 44 | for (size_t i = 0; i < kMaxFrameLenSp; ++i) { |
pkasting | b297c5a | 2015-07-22 15:17:22 -0700 | [diff] [blame] | 45 | data_in[i] = static_cast<int16_t>(i * i); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 46 | } |
| 47 | // Input values all zeros, expect all zeros out. |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 48 | WebRtcVad_Downsampling(zeros, data_out, state, kMaxFrameLenSp); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 49 | EXPECT_EQ(0, state[0]); |
| 50 | EXPECT_EQ(0, state[1]); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | for (size_t i = 0; i < kMaxFrameLenSp / 2; ++i) { |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 52 | EXPECT_EQ(0, data_out[i]); |
| 53 | } |
| 54 | // Make a simple non-zero data test. |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 55 | WebRtcVad_Downsampling(data_in, data_out, state, kMaxFrameLenSp); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 56 | EXPECT_EQ(207, state[0]); |
| 57 | EXPECT_EQ(2270, state[1]); |
| 58 | |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame] | 59 | ASSERT_EQ(0, WebRtcVad_InitCore(self)); |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 60 | // TODO(bjornv): Replace this part of the test with taking values from an |
| 61 | // array and calculate the reference value here. Make sure the values are not |
| 62 | // ordered. |
| 63 | for (int16_t i = 0; i < 16; ++i) { |
| 64 | int16_t value = 500 * (i + 1); |
bjornv@webrtc.org | a496b03 | 2012-03-20 12:53:06 +0000 | [diff] [blame] | 65 | for (int j = 0; j < kNumChannels; ++j) { |
bjornv@webrtc.org | e6471ba | 2012-01-09 09:54:07 +0000 | [diff] [blame] | 66 | // Use values both above and below initialized value. |
| 67 | EXPECT_EQ(kReferenceMin[i], WebRtcVad_FindMinimum(self, value, j)); |
| 68 | EXPECT_EQ(kReferenceMin[i + 16], WebRtcVad_FindMinimum(self, 12000, j)); |
| 69 | } |
| 70 | self->frame_counter++; |
| 71 | } |
| 72 | |
| 73 | free(self); |
| 74 | } |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 75 | } // namespace test |
| 76 | } // namespace webrtc |