1 /*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_power_tests"
19
20 #include <cmath>
21 #include <math.h>
22
23 #include <audio_utils/power.h>
24 #include <gtest/gtest.h>
25 #include <log/log.h>
26
27 typedef struct { uint8_t c[3]; } __attribute__((__packed__)) uint8x3_t;
28
testFloatValue(float f_value,size_t length)29 void testFloatValue(float f_value, size_t length) {
30 const float power = audio_utils_power_from_amplitude(f_value);
31 float f_ary[length];
32 uint8_t u8_ary[length];
33 int16_t i16_ary[length];
34 int32_t i32_ary[length];
35 int32_t q8_23_ary[length];
36 uint8x3_t p24_ary[length];
37
38 // magic formulas to convert floating point to fixed point representations.
39 // we negate the floating point value to ensure full integer range for 1.f.
40 const uint8_t u8_value((1.f - f_value) * 128);
41 const int16_t i16_value(f_value * INT16_MIN);
42 const int32_t i32_value (f_value * INT32_MIN);
43 const int32_t q8_23_value(f_value * -(1 << 23));
44
45 // PCM_24_BIT_PACKED is native endian.
46 #if HAVE_BIG_ENDIAN
47 const uint8x3_t p24_value{{
48 uint8_t(q8_23_value >> 16),
49 uint8_t(q8_23_value >> 8),
50 uint8_t(q8_23_value),
51 }};
52 #else
53 const uint8x3_t p24_value{{
54 uint8_t(q8_23_value),
55 uint8_t(q8_23_value >> 8),
56 uint8_t(q8_23_value >> 16),
57 }};
58 #endif
59
60 for (size_t i = 0; i < length; ++i) {
61 f_ary[i] = f_value;
62 u8_ary[i] = u8_value;
63 i16_ary[i] = i16_value;
64 i32_ary[i] = i32_value;
65 q8_23_ary[i] = q8_23_value;
66 p24_ary[i] = p24_value;
67 }
68
69 EXPECT_EQ(power,
70 audio_utils_compute_power_mono(f_ary, AUDIO_FORMAT_PCM_FLOAT, length));
71 EXPECT_EQ(power,
72 audio_utils_compute_power_mono(u8_ary, AUDIO_FORMAT_PCM_8_BIT, length));
73 EXPECT_EQ(power,
74 audio_utils_compute_power_mono(i16_ary, AUDIO_FORMAT_PCM_16_BIT, length));
75 EXPECT_EQ(power,
76 audio_utils_compute_power_mono(i32_ary, AUDIO_FORMAT_PCM_32_BIT, length));
77 EXPECT_EQ(power,
78 audio_utils_compute_power_mono(q8_23_ary, AUDIO_FORMAT_PCM_8_24_BIT, length));
79 EXPECT_EQ(power,
80 audio_utils_compute_power_mono(p24_ary, AUDIO_FORMAT_PCM_24_BIT_PACKED, length));
81 }
82
testFloatRamp(size_t length)83 void testFloatRamp(size_t length) {
84 float f_ary[length];
85 uint8_t u8_ary[length];
86 int16_t i16_ary[length];
87 int32_t i32_ary[length];
88 int32_t q8_23_ary[length];
89 uint8x3_t p24_ary[length];
90
91 for (size_t i = 0; i < length; ++i) {
92 // must be expressed cleanly in uint8_t
93 const float f_value = (int(length & 0xff) - 128) / 128.f;
94
95 // magic formulas to convert floating point to fixed point representations.
96 // we negate the floating point value to ensure full integer range for 1.f.
97 const uint8_t u8_value((1.f - f_value) * 128);
98 const int16_t i16_value(f_value * INT16_MIN);
99 const int32_t i32_value (f_value * INT32_MIN);
100 const int32_t q8_23_value(f_value * -(1 << 23));
101
102 // PCM_24_BIT_PACKED is native endian.
103 #if HAVE_BIG_ENDIAN
104 const uint8x3_t p24_value{{
105 uint8_t(q8_23_value >> 16),
106 uint8_t(q8_23_value >> 8),
107 uint8_t(q8_23_value),
108 }};
109 #else
110 const uint8x3_t p24_value{{
111 uint8_t(q8_23_value),
112 uint8_t(q8_23_value >> 8),
113 uint8_t(q8_23_value >> 16),
114 }};
115 #endif
116
117 f_ary[i] = f_value;
118 u8_ary[i] = u8_value;
119 i16_ary[i] = i16_value;
120 i32_ary[i] = i32_value;
121 q8_23_ary[i] = q8_23_value;
122 p24_ary[i] = p24_value;
123 }
124
125 const float power8 = audio_utils_compute_power_mono(u8_ary, AUDIO_FORMAT_PCM_8_BIT, length);
126
127 EXPECT_EQ(power8,
128 audio_utils_compute_power_mono(f_ary, AUDIO_FORMAT_PCM_FLOAT, length));
129 EXPECT_EQ(power8,
130 audio_utils_compute_power_mono(i16_ary, AUDIO_FORMAT_PCM_16_BIT, length));
131 EXPECT_EQ(power8,
132 audio_utils_compute_power_mono(i32_ary, AUDIO_FORMAT_PCM_32_BIT, length));
133 EXPECT_EQ(power8,
134 audio_utils_compute_power_mono(q8_23_ary, AUDIO_FORMAT_PCM_8_24_BIT, length));
135 EXPECT_EQ(power8,
136 audio_utils_compute_power_mono(p24_ary, AUDIO_FORMAT_PCM_24_BIT_PACKED, length));
137 }
138
139 // power_mono implicitly tests energy_mono
TEST(audio_utils_power,power_mono)140 TEST(audio_utils_power, power_mono) {
141 // f_values should have limited mantissa
142 for (float f_value : { 0.f, 0.25f, 0.5f, 0.75f, 1.f }) {
143 const float power = audio_utils_power_from_amplitude(f_value);
144 printf("power_mono: amplitude: %f power: %f\n", f_value, power);
145
146 for (size_t length : { 1, 3, 5, 7, 16, 21, 32, 37 }) {
147 testFloatValue(f_value, length);
148 }
149 }
150 }
151
152 // power_mono implicitly tests energy_mono
TEST(audio_utils_power,power_mono_ramp)153 TEST(audio_utils_power, power_mono_ramp) {
154 for (size_t length : { 1, 3, 5, 7, 16, 21, 32, 37, 297 }) {
155 testFloatRamp(length);
156 }
157 }
158
TEST(audio_utils_power,power_from)159 TEST(audio_utils_power, power_from) {
160 EXPECT_EQ(0.f, audio_utils_power_from_amplitude(1.f));
161 EXPECT_EQ(-INFINITY, audio_utils_power_from_amplitude(0.f));
162 EXPECT_EQ(0.f, audio_utils_power_from_amplitude(-1.f));
163
164 EXPECT_EQ(0.f, audio_utils_power_from_energy(1.f));
165 EXPECT_EQ(-INFINITY, audio_utils_power_from_energy(0.f));
166 EXPECT_TRUE(std::isnan(audio_utils_power_from_energy(-1.f)));
167 }
168