standalone_vad_unittest.cc (3534B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 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 "modules/audio_processing/vad/standalone_vad.h" 12 13 #include <cstdint> 14 #include <cstdio> 15 #include <cstring> 16 #include <memory> 17 18 #include "modules/audio_processing/vad/common.h" 19 #include "test/gtest.h" 20 #include "test/testsupport/file_utils.h" 21 22 namespace webrtc { 23 24 TEST(StandaloneVadTest, Api) { 25 std::unique_ptr<StandaloneVad> vad(StandaloneVad::Create()); 26 int16_t data[kLength10Ms] = {0}; 27 28 // Valid frame length (for 32 kHz rate), but not what the VAD is expecting. 29 EXPECT_EQ(-1, vad->AddAudio(data, 320)); 30 31 const size_t kMaxNumFrames = 3; 32 double p[kMaxNumFrames]; 33 for (size_t n = 0; n < kMaxNumFrames; n++) 34 EXPECT_EQ(0, vad->AddAudio(data, kLength10Ms)); 35 36 // Pretend `p` is shorter that it should be. 37 EXPECT_EQ(-1, vad->GetActivity(p, kMaxNumFrames - 1)); 38 39 EXPECT_EQ(0, vad->GetActivity(p, kMaxNumFrames)); 40 41 // Ask for activity when buffer is empty. 42 EXPECT_EQ(-1, vad->GetActivity(p, kMaxNumFrames)); 43 44 // Should reset and result in one buffer. 45 for (size_t n = 0; n < kMaxNumFrames + 1; n++) 46 EXPECT_EQ(0, vad->AddAudio(data, kLength10Ms)); 47 EXPECT_EQ(0, vad->GetActivity(p, 1)); 48 49 // Wrong modes 50 EXPECT_EQ(-1, vad->set_mode(-1)); 51 EXPECT_EQ(-1, vad->set_mode(4)); 52 53 // Valid mode. 54 const int kMode = 2; 55 EXPECT_EQ(0, vad->set_mode(kMode)); 56 EXPECT_EQ(kMode, vad->mode()); 57 } 58 59 #if defined(WEBRTC_IOS) 60 TEST(StandaloneVadTest, DISABLED_ActivityDetection) { 61 #else 62 TEST(StandaloneVadTest, ActivityDetection) { 63 #endif 64 std::unique_ptr<StandaloneVad> vad(StandaloneVad::Create()); 65 const size_t kDataLength = kLength10Ms; 66 int16_t data[kDataLength] = {0}; 67 68 FILE* pcm_file = 69 fopen(test::ResourcePath("audio_processing/agc/agc_audio", "pcm").c_str(), 70 "rb"); 71 ASSERT_TRUE(pcm_file != nullptr); 72 73 FILE* reference_file = fopen( 74 test::ResourcePath("audio_processing/agc/agc_vad", "dat").c_str(), "rb"); 75 ASSERT_TRUE(reference_file != nullptr); 76 77 // Reference activities are prepared with 0 aggressiveness. 78 ASSERT_EQ(0, vad->set_mode(0)); 79 80 // Stand-alone VAD can operate on 1, 2 or 3 frames of length 10 ms. The 81 // reference file is created for 30 ms frame. 82 const int kNumVadFramesToProcess = 3; 83 int num_frames = 0; 84 while (fread(data, sizeof(int16_t), kDataLength, pcm_file) == kDataLength) { 85 vad->AddAudio(data, kDataLength); 86 num_frames++; 87 if (num_frames == kNumVadFramesToProcess) { 88 num_frames = 0; 89 int referece_activity; 90 double p[kNumVadFramesToProcess]; 91 EXPECT_EQ(1u, fread(&referece_activity, sizeof(referece_activity), 1, 92 reference_file)); 93 int activity = vad->GetActivity(p, kNumVadFramesToProcess); 94 EXPECT_EQ(referece_activity, activity); 95 if (activity != 0) { 96 // When active, probabilities are set to 0.5. 97 for (int n = 0; n < kNumVadFramesToProcess; n++) 98 EXPECT_EQ(0.5, p[n]); 99 } else { 100 // When inactive, probabilities are set to 0.01. 101 for (int n = 0; n < kNumVadFramesToProcess; n++) 102 EXPECT_EQ(0.01, p[n]); 103 } 104 } 105 } 106 fclose(reference_file); 107 fclose(pcm_file); 108 } 109 } // namespace webrtc