From 250cd6f41b65bb2094e4aac8bd97a5a7cab96e7e Mon Sep 17 00:00:00 2001 From: "bjornv@webrtc.org" Date: Fri, 28 Oct 2011 12:45:58 +0000 Subject: [PATCH] Added a VAD unit test to common_audio. At this stage it runs through the API calls, but should later be complemented with tests on a file. Review URL: http://webrtc-codereview.appspot.com/243002 git-svn-id: http://webrtc.googlecode.com/svn/trunk@832 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/common_audio/common_audio.gyp | 2 + .../vad/main/test/unit_test/unit_test.cc | 123 -------------- .../vad/main/test/unit_test/unit_test.h | 28 ---- src/common_audio/vad/test/vad_unittest.cc | 158 ++++++++++++++++++ 4 files changed, 160 insertions(+), 151 deletions(-) delete mode 100644 src/common_audio/vad/main/test/unit_test/unit_test.cc delete mode 100644 src/common_audio/vad/main/test/unit_test/unit_test.h create mode 100644 src/common_audio/vad/test/vad_unittest.cc diff --git a/src/common_audio/common_audio.gyp b/src/common_audio/common_audio.gyp index 743e9daf9f..e9e23caa74 100644 --- a/src/common_audio/common_audio.gyp +++ b/src/common_audio/common_audio.gyp @@ -23,10 +23,12 @@ '<(webrtc_root)/../test/test.gyp:test_support', '<(webrtc_root)/../testing/gtest.gyp:gtest', 'resampler', + 'vad', ], 'sources': [ '<(webrtc_root)/../test/run_all_unittests.cc', 'resampler/main/source/resampler_unittest.cc', + 'vad/test/vad_unittest.cc', ], }, ], diff --git a/src/common_audio/vad/main/test/unit_test/unit_test.cc b/src/common_audio/vad/main/test/unit_test/unit_test.cc deleted file mode 100644 index 8ac793e44e..0000000000 --- a/src/common_audio/vad/main/test/unit_test/unit_test.cc +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - - -/* - * This file includes the implementation of the VAD unit tests. - */ - -#include -#include "unit_test.h" -#include "webrtc_vad.h" - - -class VadEnvironment : public ::testing::Environment { - public: - virtual void SetUp() { - } - - virtual void TearDown() { - } -}; - -VadTest::VadTest() -{ -} - -void VadTest::SetUp() { -} - -void VadTest::TearDown() { -} - -TEST_F(VadTest, ApiTest) { - VadInst *vad_inst; - int i, j, k; - short zeros[960]; - short speech[960]; - char version[32]; - - // Valid test cases - int fs[3] = {8000, 16000, 32000}; - int nMode[4] = {0, 1, 2, 3}; - int framelen[3][3] = {{80, 160, 240}, - {160, 320, 480}, {320, 640, 960}} ; - int vad_counter = 0; - - memset(zeros, 0, sizeof(short) * 960); - memset(speech, 1, sizeof(short) * 960); - speech[13] = 1374; - speech[73] = -3747; - - - - // WebRtcVad_get_version() - WebRtcVad_get_version(version); - //printf("API Test for %s\n", version); - - // Null instance tests - EXPECT_EQ(-1, WebRtcVad_Create(NULL)); - EXPECT_EQ(-1, WebRtcVad_Init(NULL)); - EXPECT_EQ(-1, WebRtcVad_Assign(NULL, NULL)); - EXPECT_EQ(-1, WebRtcVad_Free(NULL)); - EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, nMode[0])); - EXPECT_EQ(-1, WebRtcVad_Process(NULL, fs[0], speech, framelen[0][0])); - - - EXPECT_EQ(WebRtcVad_Create(&vad_inst), 0); - - // Not initialized tests - EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], speech, framelen[0][0])); - EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, nMode[0])); - - // WebRtcVad_Init() tests - EXPECT_EQ(WebRtcVad_Init(vad_inst), 0); - - // WebRtcVad_set_mode() tests - EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, -1)); - EXPECT_EQ(-1, WebRtcVad_set_mode(vad_inst, 4)); - - for (i = 0; i < sizeof(nMode)/sizeof(nMode[0]); i++) { - EXPECT_EQ(WebRtcVad_set_mode(vad_inst, nMode[i]), 0); - } - - // WebRtcVad_Process() tests - EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], NULL, framelen[0][0])); - EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, 12000, speech, framelen[0][0])); - EXPECT_EQ(-1, WebRtcVad_Process(vad_inst, fs[0], speech, framelen[1][1])); - EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[0], zeros, framelen[0][0]), 0); - for (i = 0; i < sizeof(fs)/sizeof(fs[0]); i++) { - for (j = 0; j < sizeof(framelen[0])/sizeof(framelen[0][0]); j++) { - for (k = 0; k < sizeof(nMode)/sizeof(nMode[0]); k++) { - EXPECT_EQ(WebRtcVad_set_mode(vad_inst, nMode[k]), 0); -// printf("%d\n", WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j])); - if (vad_counter < 9) - { - EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j]), 1); - } else - { - EXPECT_EQ(WebRtcVad_Process(vad_inst, fs[i], speech, framelen[i][j]), 0); - } - vad_counter++; - } - } - } - - EXPECT_EQ(0, WebRtcVad_Free(vad_inst)); - -} - -int main(int argc, char** argv) { - ::testing::InitGoogleTest(&argc, argv); - VadEnvironment* env = new VadEnvironment; - ::testing::AddGlobalTestEnvironment(env); - - return RUN_ALL_TESTS(); -} diff --git a/src/common_audio/vad/main/test/unit_test/unit_test.h b/src/common_audio/vad/main/test/unit_test/unit_test.h deleted file mode 100644 index 62dac11de4..0000000000 --- a/src/common_audio/vad/main/test/unit_test/unit_test.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - - -/* - * This header file includes the declaration of the VAD unit test. - */ - -#ifndef WEBRTC_VAD_UNIT_TEST_H_ -#define WEBRTC_VAD_UNIT_TEST_H_ - -#include - -class VadTest : public ::testing::Test { - protected: - VadTest(); - virtual void SetUp(); - virtual void TearDown(); -}; - -#endif // WEBRTC_VAD_UNIT_TEST_H_ diff --git a/src/common_audio/vad/test/vad_unittest.cc b/src/common_audio/vad/test/vad_unittest.cc new file mode 100644 index 0000000000..1fcb6111db --- /dev/null +++ b/src/common_audio/vad/test/vad_unittest.cc @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include // size_t +#include + +#include "gtest/gtest.h" +#include "typedefs.h" +#include "webrtc_vad.h" + +namespace webrtc { +namespace { +const int16_t kModes[] = { 0, 1, 2, 3 }; +const size_t kModesSize = sizeof(kModes) / sizeof(*kModes); + +// Rates we support. +const int16_t kRates[] = { 8000, 16000, 32000 }; +const size_t kRatesSize = sizeof(kRates) / sizeof(*kRates); +// Frame lengths we support. +const int16_t kMaxFrameLength = 960; +const int16_t kFrameLengths[] = { 80, 160, 240, 320, 480, 640, 960 }; +const size_t kFrameLengthsSize = sizeof(kFrameLengths) / sizeof(*kFrameLengths); + +// Returns true if the rate and frame length combination is valid. +bool ValidRatesAndFrameLengths(int16_t rate, int16_t frame_length) { + if (rate == 8000) { + if (frame_length == 80 || frame_length == 160 || frame_length == 240) { + return true; + } + return false; + } else if (rate == 16000) { + if (frame_length == 160 || frame_length == 320 || frame_length == 480) { + return true; + } + return false; + } + if (rate == 32000) { + if (frame_length == 320 || frame_length == 640 || frame_length == 960) { + return true; + } + return false; + } + + return false; +} + +class VadTest : public ::testing::Test { + protected: + VadTest(); + virtual void SetUp(); + virtual void TearDown(); +}; + +VadTest::VadTest() { +} + +void VadTest::SetUp() { +} + +void VadTest::TearDown() { +} + +TEST_F(VadTest, ApiTest) { + // This API test runs through the APIs for all possible valid and invalid + // combinations. + + VadInst* handle = NULL; + int16_t zeros[kMaxFrameLength] = { 0 }; + + // Construct a speech signal that will trigger the VAD in all modes. It is + // known that (i * i) will wrap around, but that doesn't matter in this case. + int16_t speech[kMaxFrameLength]; + for (int16_t i = 0; i < kMaxFrameLength; i++) { + speech[i] = (i * i); + } + + // WebRtcVad_get_version() tests + char version[32]; + EXPECT_EQ(-1, WebRtcVad_get_version(NULL, sizeof(version))); + EXPECT_EQ(-1, WebRtcVad_get_version(version, 1)); + EXPECT_EQ(0, WebRtcVad_get_version(version, sizeof(version))); + + // Null instance tests + EXPECT_EQ(-1, WebRtcVad_Create(NULL)); + EXPECT_EQ(-1, WebRtcVad_Init(NULL)); + EXPECT_EQ(-1, WebRtcVad_Assign(NULL, NULL)); + EXPECT_EQ(-1, WebRtcVad_Free(NULL)); + EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, kModes[0])); + EXPECT_EQ(-1, WebRtcVad_Process(NULL, kRates[0], speech, kFrameLengths[0])); + + // WebRtcVad_AssignSize tests + int handle_size_bytes = 0; + EXPECT_EQ(0, WebRtcVad_AssignSize(&handle_size_bytes)); + EXPECT_EQ(576, handle_size_bytes); + + // WebRtcVad_Assign tests + void* tmp_handle = malloc(handle_size_bytes); + EXPECT_EQ(-1, WebRtcVad_Assign(&handle, NULL)); + EXPECT_EQ(0, WebRtcVad_Assign(&handle, tmp_handle)); + EXPECT_EQ(handle, tmp_handle); + free(tmp_handle); + + // WebRtcVad_Create() + ASSERT_EQ(0, WebRtcVad_Create(&handle)); + + // Not initialized tests + EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], speech, kFrameLengths[0])); + EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[0])); + + // WebRtcVad_Init() test + ASSERT_EQ(0, WebRtcVad_Init(handle)); + + // WebRtcVad_set_mode() invalid modes tests + EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[0] - 1)); + EXPECT_EQ(-1, WebRtcVad_set_mode(handle, kModes[kModesSize - 1] + 1)); + + // WebRtcVad_Process() tests + // NULL speech pointer + EXPECT_EQ(-1, WebRtcVad_Process(handle, kRates[0], NULL, kFrameLengths[0])); + // Invalid sampling rate + EXPECT_EQ(-1, WebRtcVad_Process(handle, 9999, speech, kFrameLengths[0])); + // All zeros as input should work + EXPECT_EQ(0, WebRtcVad_Process(handle, kRates[0], zeros, kFrameLengths[0])); + for (size_t k = 0; k < kModesSize; k++) { + // Test valid modes + EXPECT_EQ(0, WebRtcVad_set_mode(handle, kModes[k])); + // Loop through sampling rate and frame length combinations + for (size_t i = 0; i < kRatesSize; i++) { + for (size_t j = 0; j < kFrameLengthsSize; j++) { + if (ValidRatesAndFrameLengths(kRates[i], kFrameLengths[j])) { + EXPECT_EQ(1, WebRtcVad_Process(handle, + kRates[i], + speech, + kFrameLengths[j])); + } else { + EXPECT_EQ(-1, WebRtcVad_Process(handle, + kRates[i], + speech, + kFrameLengths[j])); + } + } + } + } + + EXPECT_EQ(0, WebRtcVad_Free(handle)); +} + +// TODO(bjornv): Add a process test, run on file. + +} // namespace +} // namespace webrtc