1 /*
2 * Copyright (c) 2018 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/agc2/rnn_vad/rnn.h"
12
13 #include "api/array_view.h"
14 #include "modules/audio_processing/agc2/cpu_features.h"
15 #include "modules/audio_processing/agc2/rnn_vad/common.h"
16 #include "test/gtest.h"
17
18 namespace webrtc {
19 namespace rnn_vad {
20 namespace {
21
22 constexpr std::array<float, kFeatureVectorSize> kFeatures = {
23 -1.00131f, -0.627069f, -7.81097f, 7.86285f, -2.87145f, 3.32365f,
24 -0.653161f, 0.529839f, -0.425307f, 0.25583f, 0.235094f, 0.230527f,
25 -0.144687f, 0.182785f, 0.57102f, 0.125039f, 0.479482f, -0.0255439f,
26 -0.0073141f, -0.147346f, -0.217106f, -0.0846906f, -8.34943f, 3.09065f,
27 1.42628f, -0.85235f, -0.220207f, -0.811163f, 2.09032f, -2.01425f,
28 -0.690268f, -0.925327f, -0.541354f, 0.58455f, -0.606726f, -0.0372358f,
29 0.565991f, 0.435854f, 0.420812f, 0.162198f, -2.13f, 10.0089f};
30
WarmUpRnnVad(RnnVad & rnn_vad)31 void WarmUpRnnVad(RnnVad& rnn_vad) {
32 for (int i = 0; i < 10; ++i) {
33 rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/false);
34 }
35 }
36
37 // Checks that the speech probability is zero with silence.
TEST(RnnVadTest,CheckZeroProbabilityWithSilence)38 TEST(RnnVadTest, CheckZeroProbabilityWithSilence) {
39 RnnVad rnn_vad(GetAvailableCpuFeatures());
40 WarmUpRnnVad(rnn_vad);
41 EXPECT_EQ(rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/true), 0.f);
42 }
43
44 // Checks that the same output is produced after reset given the same input
45 // sequence.
TEST(RnnVadTest,CheckRnnVadReset)46 TEST(RnnVadTest, CheckRnnVadReset) {
47 RnnVad rnn_vad(GetAvailableCpuFeatures());
48 WarmUpRnnVad(rnn_vad);
49 float pre = rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/false);
50 rnn_vad.Reset();
51 WarmUpRnnVad(rnn_vad);
52 float post = rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/false);
53 EXPECT_EQ(pre, post);
54 }
55
56 // Checks that the same output is produced after silence is observed given the
57 // same input sequence.
TEST(RnnVadTest,CheckRnnVadSilence)58 TEST(RnnVadTest, CheckRnnVadSilence) {
59 RnnVad rnn_vad(GetAvailableCpuFeatures());
60 WarmUpRnnVad(rnn_vad);
61 float pre = rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/false);
62 rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/true);
63 WarmUpRnnVad(rnn_vad);
64 float post = rnn_vad.ComputeVadProbability(kFeatures, /*is_silence=*/false);
65 EXPECT_EQ(pre, post);
66 }
67
68 } // namespace
69 } // namespace rnn_vad
70 } // namespace webrtc
71