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