• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <gtest/gtest.h>
6 
7 extern "C" {
8 #include "ewma_power.h"
9 }
10 
11 namespace {
12 
TEST(EWMAPower,RelativePowerValue)13 TEST(EWMAPower, RelativePowerValue) {
14   struct ewma_power ewma;
15   int16_t buf[480];
16   float f;
17   int i;
18 
19   for (i = 0; i < 480; i++)
20     buf[i] = 0x00fe;
21 
22   ewma_power_init(&ewma, 48000);
23   EXPECT_EQ(48, ewma.step_fr);
24 
25   ewma_power_calculate(&ewma, buf, 1, 480);
26   EXPECT_LT(0.0f, ewma.power);
27 
28   // After 10ms of silence the power value decreases.
29   f = ewma.power;
30   for (i = 0; i < 480; i++)
31     buf[i] = 0x00;
32   ewma_power_calculate(&ewma, buf, 1, 480);
33   EXPECT_LT(ewma.power, f);
34 
35   // After 300ms of silence the power value decreases to insignificant low.
36   for (i = 0; i < 30; i++)
37     ewma_power_calculate(&ewma, buf, 1, 480);
38   EXPECT_LT(ewma.power, 1.0e-10);
39 }
40 
TEST(EWMAPower,PowerInStereoData)41 TEST(EWMAPower, PowerInStereoData) {
42   struct ewma_power ewma;
43   int16_t buf[960];
44   int i;
45   float f;
46 
47   ewma_power_init(&ewma, 48000);
48 
49   for (i = 0; i < 960; i += 2) {
50     buf[i] = 0x0;
51     buf[i + 1] = 0x00fe;
52   }
53   ewma_power_calculate(&ewma, buf, 2, 480);
54   EXPECT_LT(0.0f, ewma.power);
55 
56   // After 10ms of silence the power value decreases.
57   f = ewma.power;
58   for (i = 0; i < 960; i++)
59     buf[i] = 0x0;
60   ewma_power_calculate(&ewma, buf, 2, 480);
61   EXPECT_LT(ewma.power, f);
62 
63   // After 300ms of silence the power value decreases to insignificant low.
64   for (i = 0; i < 30; i++)
65     ewma_power_calculate(&ewma, buf, 2, 480);
66   EXPECT_LT(ewma.power, 1.0e-10);
67 
68   // Assume the data is silent in the other channel.
69   ewma_power_init(&ewma, 48000);
70 
71   for (i = 0; i < 960; i += 2) {
72     buf[i] = 0x0ffe;
73     buf[i + 1] = 0x0;
74   }
75   ewma_power_calculate(&ewma, buf, 2, 480);
76   EXPECT_LT(0.0f, ewma.power);
77 }
78 
TEST(EWMAPower,PowerInAudioArea)79 TEST(EWMAPower, PowerInAudioArea) {
80   struct ewma_power ewma;
81   struct cras_audio_area* area = cras_audio_area_create(4);
82   struct cras_audio_format* fmt =
83       cras_audio_format_create(SND_PCM_FORMAT_S16_LE, 48000, 4);
84   int8_t layout[CRAS_CH_MAX] = {0, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1};
85   int16_t buf[1920];
86   int i;
87   float f;
88 
89   cras_audio_format_set_channel_layout(fmt, layout);
90   cras_audio_area_config_channels(area, fmt);
91 
92   for (i = 0; i < 1920; i += 4) {
93     buf[i] = 0x0ffe;
94     buf[i + 1] = 0x0;
95     buf[i + 2] = 0x0;
96     buf[i + 3] = 0x0ffe;
97   }
98   ewma_power_init(&ewma, 48000);
99   ewma_power_calculate_area(&ewma, buf, area, 480);
100   f = ewma.power;
101   EXPECT_LT(0.0f, f);
102 
103   /* Change the layout in the same audio area. Expect the power be lower because
104    * one of the channel is now silent. */
105   layout[CRAS_CH_FR] = 2;
106   cras_audio_format_set_channel_layout(fmt, layout);
107   cras_audio_area_config_channels(area, fmt);
108   ewma_power_init(&ewma, 48000);
109   ewma_power_calculate_area(&ewma, buf, area, 480);
110   EXPECT_GT(f, ewma.power);
111 
112   /* Change layout to the two silent channels. Expect power is 0.0f. */
113   layout[CRAS_CH_FL] = 1;
114   cras_audio_format_set_channel_layout(fmt, layout);
115   cras_audio_area_config_channels(area, fmt);
116   ewma_power_init(&ewma, 48000);
117   ewma_power_calculate_area(&ewma, buf, area, 480);
118   EXPECT_EQ(0.0f, ewma.power);
119 
120   cras_audio_format_destroy(fmt);
121   cras_audio_area_destroy(area);
122 }
123 
124 }  // namespace
125 
main(int argc,char ** argv)126 int main(int argc, char** argv) {
127   ::testing::InitGoogleTest(&argc, argv);
128   return RUN_ALL_TESTS();
129 }
130