| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | /* |
| 18 | * Test FlowGraph |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 19 | * |
| 20 | * This file also tests a few different conversion techniques because |
| 21 | * sometimes that have caused compiler bugs. |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <iostream> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "flowgraph/ClipToRange.h" |
| Robert Wu | d740083 | 2021-12-04 01:11:19 +0000 | [diff] [blame] | 29 | #include "flowgraph/MonoBlend.h" |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 30 | #include "flowgraph/MonoToMultiConverter.h" |
| 31 | #include "flowgraph/SourceFloat.h" |
| 32 | #include "flowgraph/RampLinear.h" |
| 33 | #include "flowgraph/SinkFloat.h" |
| 34 | #include "flowgraph/SinkI16.h" |
| 35 | #include "flowgraph/SinkI24.h" |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 36 | #include "flowgraph/SinkI32.h" |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 37 | #include "flowgraph/SourceI16.h" |
| 38 | #include "flowgraph/SourceI24.h" |
| 39 | |
| Robert Wu | edc850a | 2022-04-05 16:47:03 +0000 | [diff] [blame] | 40 | using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 41 | |
| 42 | constexpr int kBytesPerI24Packed = 3; |
| 43 | |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 44 | constexpr int kNumSamples = 8; |
| 45 | constexpr std::array<float, kNumSamples> kInputFloat = { |
| 46 | 1.0f, 0.5f, -0.25f, -1.0f, |
| 47 | 0.0f, 53.9f, -87.2f, -1.02f}; |
| 48 | |
| 49 | // Corresponding PCM values as integers. |
| 50 | constexpr std::array<int16_t, kNumSamples> kExpectedI16 = { |
| 51 | INT16_MAX, 1 << 14, INT16_MIN / 4, INT16_MIN, |
| 52 | 0, INT16_MAX, INT16_MIN, INT16_MIN}; |
| 53 | |
| 54 | constexpr std::array<int32_t, kNumSamples> kExpectedI32 = { |
| 55 | INT32_MAX, 1 << 30, INT32_MIN / 4, INT32_MIN, |
| 56 | 0, INT32_MAX, INT32_MIN, INT32_MIN}; |
| 57 | |
| 58 | // =================================== FLOAT to I16 ============== |
| 59 | |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 60 | // Simple test that tries to reproduce a Clang compiler bug. |
| 61 | __attribute__((noinline)) |
| 62 | void local_convert_float_to_int16(const float *input, |
| 63 | int16_t *output, |
| 64 | int count) { |
| 65 | for (int i = 0; i < count; i++) { |
| 66 | int32_t n = (int32_t) (*input++ * 32768.0f); |
| 67 | *output++ = std::min(INT16_MAX, std::max(INT16_MIN, n)); // clip |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | TEST(test_flowgraph, local_convert_float_int16) { |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 72 | std::array<int16_t, kNumSamples> output; |
| 73 | |
| 74 | // Do it inline, which will probably work even with the buggy compiler. |
| 75 | // This validates the expected data. |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 76 | const float *in = kInputFloat.data(); |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 77 | int16_t *out = output.data(); |
| 78 | output.fill(777); |
| 79 | for (int i = 0; i < kNumSamples; i++) { |
| 80 | int32_t n = (int32_t) (*in++ * 32768.0f); |
| 81 | *out++ = std::min(INT16_MAX, std::max(INT16_MIN, n)); // clip |
| 82 | } |
| 83 | for (int i = 0; i < kNumSamples; i++) { |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 84 | EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i; |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Convert audio signal using the function. |
| 88 | output.fill(777); |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 89 | local_convert_float_to_int16(kInputFloat.data(), output.data(), kNumSamples); |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 90 | for (int i = 0; i < kNumSamples; i++) { |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 91 | EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i; |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 95 | TEST(test_flowgraph, module_sinki16) { |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 96 | static constexpr int kNumSamples = 8; |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 97 | std::array<int16_t, kNumSamples + 10> output; // larger than input |
| 98 | |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 99 | SourceFloat sourceFloat{1}; |
| 100 | SinkI16 sinkI16{1}; |
| 101 | |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 102 | sourceFloat.setData(kInputFloat.data(), kNumSamples); |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 103 | sourceFloat.output.connect(&sinkI16.input); |
| 104 | |
| Phil Burk | 30e5893 | 2022-03-29 00:12:44 +0000 | [diff] [blame] | 105 | output.fill(777); |
| 106 | int32_t numRead = sinkI16.read(output.data(), output.size()); |
| 107 | ASSERT_EQ(kNumSamples, numRead); |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 108 | for (int i = 0; i < numRead; i++) { |
| Phil Burk | e2ac8fa | 2022-05-19 22:27:23 +0000 | [diff] [blame] | 109 | EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // =================================== FLOAT to I32 ============== |
| 114 | // Simple test that tries to reproduce a Clang compiler bug. |
| 115 | __attribute__((noinline)) |
| 116 | static int32_t clamp32FromFloat(float f) |
| 117 | { |
| 118 | static const float scale = (float)(1UL << 31); |
| 119 | static const float limpos = 1.; |
| 120 | static const float limneg = -1.; |
| 121 | |
| 122 | if (f <= limneg) { |
| 123 | return INT32_MIN; |
| 124 | } else if (f >= limpos) { |
| 125 | return INT32_MAX; |
| 126 | } |
| 127 | f *= scale; |
| 128 | /* integer conversion is through truncation (though int to float is not). |
| 129 | * ensure that we round to nearest, ties away from 0. |
| 130 | */ |
| 131 | return f > 0 ? f + 0.5 : f - 0.5; |
| 132 | } |
| 133 | |
| 134 | void local_convert_float_to_int32(const float *input, |
| 135 | int32_t *output, |
| 136 | int count) { |
| 137 | for (int i = 0; i < count; i++) { |
| 138 | *output++ = clamp32FromFloat(*input++); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | TEST(test_flowgraph, simple_convert_float_int32) { |
| 143 | std::array<int32_t, kNumSamples> output; |
| 144 | |
| 145 | // Do it inline, which will probably work even with a buggy compiler. |
| 146 | // This validates the expected data. |
| 147 | const float *in = kInputFloat.data(); |
| 148 | output.fill(777); |
| 149 | int32_t *out = output.data(); |
| 150 | for (int i = 0; i < kNumSamples; i++) { |
| 151 | int64_t n = (int64_t) (*in++ * 2147483648.0f); |
| 152 | *out++ = (int32_t)std::min((int64_t)INT32_MAX, |
| 153 | std::max((int64_t)INT32_MIN, n)); // clip |
| 154 | } |
| 155 | for (int i = 0; i < kNumSamples; i++) { |
| 156 | EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | TEST(test_flowgraph, local_convert_float_int32) { |
| 161 | std::array<int32_t, kNumSamples> output; |
| 162 | // Convert audio signal using the function. |
| 163 | output.fill(777); |
| 164 | local_convert_float_to_int32(kInputFloat.data(), output.data(), kNumSamples); |
| 165 | for (int i = 0; i < kNumSamples; i++) { |
| 166 | EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST(test_flowgraph, module_sinki32) { |
| 171 | std::array<int32_t, kNumSamples + 10> output; // larger than input |
| 172 | |
| 173 | SourceFloat sourceFloat{1}; |
| 174 | SinkI32 sinkI32{1}; |
| 175 | |
| 176 | sourceFloat.setData(kInputFloat.data(), kNumSamples); |
| 177 | sourceFloat.output.connect(&sinkI32.input); |
| 178 | |
| 179 | output.fill(777); |
| 180 | int32_t numRead = sinkI32.read(output.data(), output.size()); |
| 181 | ASSERT_EQ(kNumSamples, numRead); |
| 182 | for (int i = 0; i < numRead; i++) { |
| 183 | EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i; |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | TEST(test_flowgraph, module_mono_to_stereo) { |
| 188 | static const float input[] = {1.0f, 2.0f, 3.0f}; |
| 189 | float output[100] = {}; |
| 190 | SourceFloat sourceFloat{1}; |
| 191 | MonoToMultiConverter monoToStereo{2}; |
| 192 | SinkFloat sinkFloat{2}; |
| 193 | |
| 194 | sourceFloat.setData(input, 3); |
| 195 | |
| 196 | sourceFloat.output.connect(&monoToStereo.input); |
| 197 | monoToStereo.output.connect(&sinkFloat.input); |
| 198 | |
| 199 | int32_t numRead = sinkFloat.read(output, 8); |
| 200 | ASSERT_EQ(3, numRead); |
| 201 | EXPECT_EQ(input[0], output[0]); |
| 202 | EXPECT_EQ(input[0], output[1]); |
| 203 | EXPECT_EQ(input[1], output[2]); |
| 204 | EXPECT_EQ(input[1], output[3]); |
| 205 | } |
| 206 | |
| 207 | TEST(test_flowgraph, module_ramp_linear) { |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 208 | constexpr int singleNumOutput = 1; |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 209 | constexpr int rampSize = 5; |
| 210 | constexpr int numOutput = 100; |
| 211 | constexpr float value = 1.0f; |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 212 | constexpr float initialTarget = 10.0f; |
| 213 | constexpr float finalTarget = 100.0f; |
| 214 | constexpr float tolerance = 0.0001f; // arbitrary |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 215 | float output[numOutput] = {}; |
| 216 | RampLinear rampLinear{1}; |
| 217 | SinkFloat sinkFloat{1}; |
| 218 | |
| 219 | rampLinear.input.setValue(value); |
| 220 | rampLinear.setLengthInFrames(rampSize); |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 221 | rampLinear.output.connect(&sinkFloat.input); |
| 222 | |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 223 | // Check that the values go to the initial target instantly. |
| 224 | rampLinear.setTarget(initialTarget); |
| 225 | int32_t singleNumRead = sinkFloat.read(output, singleNumOutput); |
| 226 | ASSERT_EQ(singleNumRead, singleNumOutput); |
| 227 | EXPECT_NEAR(value * initialTarget, output[0], tolerance); |
| 228 | |
| 229 | // Now set target and check that the linear ramp works as expected. |
| 230 | rampLinear.setTarget(finalTarget); |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 231 | int32_t numRead = sinkFloat.read(output, numOutput); |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 232 | const float incrementSize = (finalTarget - initialTarget) / rampSize; |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 233 | ASSERT_EQ(numOutput, numRead); |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 234 | |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 235 | int i = 0; |
| 236 | for (; i < rampSize; i++) { |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 237 | float expected = value * (initialTarget + i * incrementSize); |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 238 | EXPECT_NEAR(expected, output[i], tolerance); |
| 239 | } |
| 240 | for (; i < numOutput; i++) { |
| Robert Wu | c3278fe | 2021-06-23 16:51:40 +0000 | [diff] [blame] | 241 | float expected = value * finalTarget; |
| Phil Burk | bb78a73 | 2018-03-28 15:37:19 -0700 | [diff] [blame] | 242 | EXPECT_NEAR(expected, output[i], tolerance); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // It is easiest to represent packed 24-bit data as a byte array. |
| 247 | // This test will read from input, convert to float, then write |
| 248 | // back to output as bytes. |
| 249 | TEST(test_flowgraph, module_packed_24) { |
| 250 | static const uint8_t input[] = {0x01, 0x23, 0x45, |
| 251 | 0x67, 0x89, 0xAB, |
| 252 | 0xCD, 0xEF, 0x5A}; |
| 253 | uint8_t output[99] = {}; |
| 254 | SourceI24 sourceI24{1}; |
| 255 | SinkI24 sinkI24{1}; |
| 256 | |
| 257 | int numInputFrames = sizeof(input) / kBytesPerI24Packed; |
| 258 | sourceI24.setData(input, numInputFrames); |
| 259 | sourceI24.output.connect(&sinkI24.input); |
| 260 | |
| 261 | int32_t numRead = sinkI24.read(output, sizeof(output) / kBytesPerI24Packed); |
| 262 | ASSERT_EQ(numInputFrames, numRead); |
| 263 | for (size_t i = 0; i < sizeof(input); i++) { |
| 264 | EXPECT_EQ(input[i], output[i]); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | TEST(test_flowgraph, module_clip_to_range) { |
| 269 | constexpr float myMin = -2.0f; |
| 270 | constexpr float myMax = 1.5f; |
| 271 | |
| 272 | static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3}; |
| 273 | static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax}; |
| 274 | float output[100]; |
| 275 | SourceFloat sourceFloat{1}; |
| 276 | ClipToRange clipper{1}; |
| 277 | SinkFloat sinkFloat{1}; |
| 278 | |
| 279 | int numInputFrames = sizeof(input) / sizeof(input[0]); |
| 280 | sourceFloat.setData(input, numInputFrames); |
| 281 | |
| 282 | clipper.setMinimum(myMin); |
| 283 | clipper.setMaximum(myMax); |
| 284 | |
| 285 | sourceFloat.output.connect(&clipper.input); |
| 286 | clipper.output.connect(&sinkFloat.input); |
| 287 | |
| 288 | int numOutputFrames = sizeof(output) / sizeof(output[0]); |
| 289 | int32_t numRead = sinkFloat.read(output, numOutputFrames); |
| 290 | ASSERT_EQ(numInputFrames, numRead); |
| 291 | constexpr float tolerance = 0.000001f; // arbitrary |
| 292 | for (int i = 0; i < numRead; i++) { |
| 293 | EXPECT_NEAR(expected[i], output[i], tolerance); |
| 294 | } |
| 295 | } |
| Robert Wu | d740083 | 2021-12-04 01:11:19 +0000 | [diff] [blame] | 296 | |
| 297 | TEST(test_flowgraph, module_mono_blend) { |
| 298 | // Two channel to two channel with 3 inputs and outputs. |
| 299 | constexpr int numChannels = 2; |
| 300 | constexpr int numFrames = 3; |
| 301 | |
| 302 | static const float input[] = {-0.7, 0.5, -0.25, 1.25, 1000, 2000}; |
| 303 | static const float expected[] = {-0.1, -0.1, 0.5, 0.5, 1500, 1500}; |
| 304 | float output[100]; |
| 305 | SourceFloat sourceFloat{numChannels}; |
| 306 | MonoBlend monoBlend{numChannels}; |
| 307 | SinkFloat sinkFloat{numChannels}; |
| 308 | |
| 309 | sourceFloat.setData(input, numFrames); |
| 310 | |
| 311 | sourceFloat.output.connect(&monoBlend.input); |
| 312 | monoBlend.output.connect(&sinkFloat.input); |
| 313 | |
| 314 | int32_t numRead = sinkFloat.read(output, numFrames); |
| 315 | ASSERT_EQ(numRead, numFrames); |
| 316 | constexpr float tolerance = 0.000001f; // arbitrary |
| 317 | for (int i = 0; i < numRead; i++) { |
| 318 | EXPECT_NEAR(expected[i], output[i], tolerance); |
| 319 | } |
| 320 | } |
| 321 | |