power_tests.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "audio_utils_power_tests"
  18. #include <cmath>
  19. #include <math.h>
  20. #include <audio_utils/power.h>
  21. #include <gtest/gtest.h>
  22. #include <log/log.h>
  23. typedef struct { uint8_t c[3]; } __attribute__((__packed__)) uint8x3_t;
  24. void testFloatValue(float f_value, size_t length) {
  25. const float power = audio_utils_power_from_amplitude(f_value);
  26. float f_ary[length];
  27. uint8_t u8_ary[length];
  28. int16_t i16_ary[length];
  29. int32_t i32_ary[length];
  30. int32_t q8_23_ary[length];
  31. uint8x3_t p24_ary[length];
  32. // magic formulas to convert floating point to fixed point representations.
  33. // we negate the floating point value to ensure full integer range for 1.f.
  34. const uint8_t u8_value((1.f - f_value) * 128);
  35. const int16_t i16_value(f_value * INT16_MIN);
  36. const int32_t i32_value (f_value * INT32_MIN);
  37. const int32_t q8_23_value(f_value * -(1 << 23));
  38. // PCM_24_BIT_PACKED is native endian.
  39. #if HAVE_BIG_ENDIAN
  40. const uint8x3_t p24_value{{
  41. uint8_t(q8_23_value >> 16),
  42. uint8_t(q8_23_value >> 8),
  43. uint8_t(q8_23_value),
  44. }};
  45. #else
  46. const uint8x3_t p24_value{{
  47. uint8_t(q8_23_value),
  48. uint8_t(q8_23_value >> 8),
  49. uint8_t(q8_23_value >> 16),
  50. }};
  51. #endif
  52. for (size_t i = 0; i < length; ++i) {
  53. f_ary[i] = f_value;
  54. u8_ary[i] = u8_value;
  55. i16_ary[i] = i16_value;
  56. i32_ary[i] = i32_value;
  57. q8_23_ary[i] = q8_23_value;
  58. p24_ary[i] = p24_value;
  59. }
  60. // check offset by 1, 2, 3 elements for unaligned NEON vector handling.
  61. for (size_t i = 0; i < 3; ++i) {
  62. if (i >= length) break;
  63. EXPECT_EQ(power,
  64. audio_utils_compute_power_mono(f_ary + i, AUDIO_FORMAT_PCM_FLOAT, length - i));
  65. EXPECT_EQ(power,
  66. audio_utils_compute_power_mono(u8_ary + i, AUDIO_FORMAT_PCM_8_BIT, length - i));
  67. EXPECT_EQ(power,
  68. audio_utils_compute_power_mono(i16_ary + i, AUDIO_FORMAT_PCM_16_BIT, length - i));
  69. EXPECT_EQ(power,
  70. audio_utils_compute_power_mono(i32_ary + i, AUDIO_FORMAT_PCM_32_BIT, length - i));
  71. EXPECT_EQ(power,
  72. audio_utils_compute_power_mono(
  73. q8_23_ary + i, AUDIO_FORMAT_PCM_8_24_BIT, length - i));
  74. EXPECT_EQ(power,
  75. audio_utils_compute_power_mono(
  76. p24_ary + i, AUDIO_FORMAT_PCM_24_BIT_PACKED, length - i));
  77. }
  78. }
  79. void testFloatRamp(size_t length) {
  80. float f_ary[length];
  81. uint8_t u8_ary[length];
  82. int16_t i16_ary[length];
  83. int32_t i32_ary[length];
  84. int32_t q8_23_ary[length];
  85. uint8x3_t p24_ary[length];
  86. for (size_t i = 0; i < length; ++i) {
  87. // must be expressed cleanly in uint8_t
  88. const float f_value = (int(length & 0xff) - 128) / 128.f;
  89. // magic formulas to convert floating point to fixed point representations.
  90. // we negate the floating point value to ensure full integer range for 1.f.
  91. const uint8_t u8_value((1.f - f_value) * 128);
  92. const int16_t i16_value(f_value * INT16_MIN);
  93. const int32_t i32_value (f_value * INT32_MIN);
  94. const int32_t q8_23_value(f_value * -(1 << 23));
  95. // PCM_24_BIT_PACKED is native endian.
  96. #if HAVE_BIG_ENDIAN
  97. const uint8x3_t p24_value{{
  98. uint8_t(q8_23_value >> 16),
  99. uint8_t(q8_23_value >> 8),
  100. uint8_t(q8_23_value),
  101. }};
  102. #else
  103. const uint8x3_t p24_value{{
  104. uint8_t(q8_23_value),
  105. uint8_t(q8_23_value >> 8),
  106. uint8_t(q8_23_value >> 16),
  107. }};
  108. #endif
  109. f_ary[i] = f_value;
  110. u8_ary[i] = u8_value;
  111. i16_ary[i] = i16_value;
  112. i32_ary[i] = i32_value;
  113. q8_23_ary[i] = q8_23_value;
  114. p24_ary[i] = p24_value;
  115. }
  116. const float power8 = audio_utils_compute_power_mono(u8_ary, AUDIO_FORMAT_PCM_8_BIT, length);
  117. EXPECT_EQ(power8,
  118. audio_utils_compute_power_mono(f_ary, AUDIO_FORMAT_PCM_FLOAT, length));
  119. EXPECT_EQ(power8,
  120. audio_utils_compute_power_mono(i16_ary, AUDIO_FORMAT_PCM_16_BIT, length));
  121. EXPECT_EQ(power8,
  122. audio_utils_compute_power_mono(i32_ary, AUDIO_FORMAT_PCM_32_BIT, length));
  123. EXPECT_EQ(power8,
  124. audio_utils_compute_power_mono(q8_23_ary, AUDIO_FORMAT_PCM_8_24_BIT, length));
  125. EXPECT_EQ(power8,
  126. audio_utils_compute_power_mono(p24_ary, AUDIO_FORMAT_PCM_24_BIT_PACKED, length));
  127. }
  128. // power_mono implicitly tests energy_mono
  129. TEST(audio_utils_power, power_mono) {
  130. // f_values should have limited mantissa
  131. for (float f_value : { 0.f, 0.25f, 0.5f, 0.75f, 1.f }) {
  132. const float power = audio_utils_power_from_amplitude(f_value);
  133. printf("power_mono: amplitude: %f power: %f\n", f_value, power);
  134. for (size_t length : { 1, 3, 5, 7, 16, 21, 32, 37 }) {
  135. testFloatValue(f_value, length);
  136. }
  137. }
  138. }
  139. // power_mono implicitly tests energy_mono
  140. TEST(audio_utils_power, power_mono_ramp) {
  141. for (size_t length : { 1, 3, 5, 7, 16, 21, 32, 37, 297 }) {
  142. testFloatRamp(length);
  143. }
  144. }
  145. TEST(audio_utils_power, power_from) {
  146. EXPECT_EQ(0.f, audio_utils_power_from_amplitude(1.f));
  147. EXPECT_EQ(-INFINITY, audio_utils_power_from_amplitude(0.f));
  148. EXPECT_EQ(0.f, audio_utils_power_from_amplitude(-1.f));
  149. EXPECT_EQ(0.f, audio_utils_power_from_energy(1.f));
  150. EXPECT_EQ(-INFINITY, audio_utils_power_from_energy(0.f));
  151. EXPECT_TRUE(std::isnan(audio_utils_power_from_energy(-1.f)));
  152. }