• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19  *
20  * This file also tests a few different conversion techniques because
21  * sometimes that have caused compiler bugs.
22  */
23 
24 #include <iostream>
25 
26 #include <gtest/gtest.h>
27 
28 #include <aaudio/AAudio.h>
29 #include "client/AAudioFlowGraph.h"
30 #include "flowgraph/ClipToRange.h"
31 #include "flowgraph/Limiter.h"
32 #include "flowgraph/MonoBlend.h"
33 #include "flowgraph/MonoToMultiConverter.h"
34 #include "flowgraph/RampLinear.h"
35 #include "flowgraph/SinkFloat.h"
36 #include "flowgraph/SinkI16.h"
37 #include "flowgraph/SinkI24.h"
38 #include "flowgraph/SinkI32.h"
39 #include "flowgraph/SinkI8_24.h"
40 #include "flowgraph/SourceFloat.h"
41 #include "flowgraph/SourceI16.h"
42 #include "flowgraph/SourceI24.h"
43 #include "flowgraph/SourceI8_24.h"
44 #include "flowgraph/resampler/IntegerRatio.h"
45 
46 using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
47 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
48 
49 using TestFlowgraphResamplerParams = std::tuple<int32_t, int32_t, MultiChannelResampler::Quality>;
50 
51 enum {
52     PARAM_SOURCE_SAMPLE_RATE = 0,
53     PARAM_SINK_SAMPLE_RATE,
54     PARAM_RESAMPLER_QUALITY
55 };
56 
57 constexpr int kInt24Min = 0xff800000;
58 constexpr int kInt24Max = 0x007fffff;
59 
60 constexpr int kBytesPerI24Packed = 3;
61 
62 constexpr int kNumSamples = 8;
63 constexpr std::array<float, kNumSamples> kInputFloat = {
64     1.0f, 0.5f, -0.25f, -1.0f,
65     0.0f, 53.9f, -87.2f, -1.02f};
66 
67 // Corresponding PCM values as integers.
68 constexpr std::array<int16_t, kNumSamples> kExpectedI16 = {
69     INT16_MAX, 1 << 14, INT16_MIN / 4, INT16_MIN,
70     0, INT16_MAX, INT16_MIN, INT16_MIN};
71 
72 constexpr std::array<int32_t, kNumSamples> kExpectedI32 = {
73     INT32_MAX, 1 << 30, INT32_MIN / 4, INT32_MIN,
74     0, INT32_MAX, INT32_MIN, INT32_MIN};
75 
76 constexpr std::array<int32_t, kNumSamples> kExpectedI8_24 = {
77     kInt24Max, 1 << 22, kInt24Min / 4, kInt24Min,
78     0, kInt24Max, kInt24Min, kInt24Min};
79 
80 // =================================== FLOAT to I16 ==============
81 
82 // Simple test that tries to reproduce a Clang compiler bug.
83 __attribute__((noinline))
local_convert_float_to_int16(const float * input,int16_t * output,int count)84 void local_convert_float_to_int16(const float *input,
85                                   int16_t *output,
86                                   int count) {
87     for (int i = 0; i < count; i++) {
88         int32_t n = (int32_t) (*input++ * 32768.0f);
89         *output++ = std::min(INT16_MAX, std::max(INT16_MIN, n)); // clip
90     }
91 }
92 
TEST(test_flowgraph,local_convert_float_int16)93 TEST(test_flowgraph, local_convert_float_int16) {
94     std::array<int16_t, kNumSamples> output;
95 
96     // Do it inline, which will probably work even with the buggy compiler.
97     // This validates the expected data.
98     const float *in = kInputFloat.data();
99     int16_t *out = output.data();
100     output.fill(777);
101     for (int i = 0; i < kNumSamples; i++) {
102         int32_t n = (int32_t) (*in++ * 32768.0f);
103         *out++ = std::min(INT16_MAX, std::max(INT16_MIN, n)); // clip
104     }
105     for (int i = 0; i < kNumSamples; i++) {
106         EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i;
107     }
108 
109     // Convert audio signal using the function.
110     output.fill(777);
111     local_convert_float_to_int16(kInputFloat.data(), output.data(), kNumSamples);
112     for (int i = 0; i < kNumSamples; i++) {
113         EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i;
114     }
115 }
116 
TEST(test_flowgraph,module_sinki16)117 TEST(test_flowgraph, module_sinki16) {
118     static constexpr int kNumSamples = 8;
119     std::array<int16_t, kNumSamples + 10> output; // larger than input
120 
121     SourceFloat sourceFloat{1};
122     SinkI16 sinkI16{1};
123 
124     sourceFloat.setData(kInputFloat.data(), kNumSamples);
125     sourceFloat.output.connect(&sinkI16.input);
126 
127     output.fill(777);
128     int32_t numRead = sinkI16.read(output.data(), output.size());
129     ASSERT_EQ(kNumSamples, numRead);
130     for (int i = 0; i < numRead; i++) {
131         EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i;
132     }
133 }
134 
135 // =================================== FLOAT to I32 ==============
136 // Simple test that tries to reproduce a Clang compiler bug.
137 __attribute__((noinline))
clamp32FromFloat(float f)138 static int32_t clamp32FromFloat(float f)
139 {
140     static const float scale = (float)(1UL << 31);
141     static const float limpos = 1.;
142     static const float limneg = -1.;
143 
144     if (f <= limneg) {
145         return INT32_MIN;
146     } else if (f >= limpos) {
147         return INT32_MAX;
148     }
149     f *= scale;
150     /* integer conversion is through truncation (though int to float is not).
151      * ensure that we round to nearest, ties away from 0.
152      */
153     return f > 0 ? f + 0.5 : f - 0.5;
154 }
155 
local_convert_float_to_int32(const float * input,int32_t * output,int count)156 void local_convert_float_to_int32(const float *input,
157                                   int32_t *output,
158                                   int count) {
159     for (int i = 0; i < count; i++) {
160         *output++ = clamp32FromFloat(*input++);
161     }
162 }
163 
TEST(test_flowgraph,simple_convert_float_int32)164 TEST(test_flowgraph, simple_convert_float_int32) {
165     std::array<int32_t, kNumSamples> output;
166 
167     // Do it inline, which will probably work even with a buggy compiler.
168     // This validates the expected data.
169     const float *in = kInputFloat.data();
170     output.fill(777);
171     int32_t *out = output.data();
172     for (int i = 0; i < kNumSamples; i++) {
173         int64_t n = (int64_t) (*in++ * 2147483648.0f);
174         *out++ = (int32_t)std::min((int64_t)INT32_MAX,
175                                    std::max((int64_t)INT32_MIN, n)); // clip
176     }
177     for (int i = 0; i < kNumSamples; i++) {
178         EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i;
179     }
180 }
181 
TEST(test_flowgraph,local_convert_float_int32)182 TEST(test_flowgraph, local_convert_float_int32) {
183     std::array<int32_t, kNumSamples> output;
184     // Convert audio signal using the function.
185     output.fill(777);
186     local_convert_float_to_int32(kInputFloat.data(), output.data(), kNumSamples);
187     for (int i = 0; i < kNumSamples; i++) {
188         EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i;
189     }
190 }
191 
TEST(test_flowgraph,module_sinki32)192 TEST(test_flowgraph, module_sinki32) {
193     std::array<int32_t, kNumSamples + 10> output; // larger than input
194 
195     SourceFloat sourceFloat{1};
196     SinkI32 sinkI32{1};
197 
198     sourceFloat.setData(kInputFloat.data(), kNumSamples);
199     sourceFloat.output.connect(&sinkI32.input);
200 
201     output.fill(777);
202     int32_t numRead = sinkI32.read(output.data(), output.size());
203     ASSERT_EQ(kNumSamples, numRead);
204     for (int i = 0; i < numRead; i++) {
205         EXPECT_EQ(kExpectedI32.at(i), output.at(i)) << ", i = " << i;
206     }
207 }
208 
TEST(test_flowgraph,module_mono_to_stereo)209 TEST(test_flowgraph, module_mono_to_stereo) {
210     static const float input[] = {1.0f, 2.0f, 3.0f};
211     float output[100] = {};
212     SourceFloat sourceFloat{1};
213     MonoToMultiConverter monoToStereo{2};
214     SinkFloat sinkFloat{2};
215 
216     sourceFloat.setData(input, 3);
217 
218     sourceFloat.output.connect(&monoToStereo.input);
219     monoToStereo.output.connect(&sinkFloat.input);
220 
221     int32_t numRead = sinkFloat.read(output, 8);
222     ASSERT_EQ(3, numRead);
223     EXPECT_EQ(input[0], output[0]);
224     EXPECT_EQ(input[0], output[1]);
225     EXPECT_EQ(input[1], output[2]);
226     EXPECT_EQ(input[1], output[3]);
227     EXPECT_EQ(input[2], output[4]);
228     EXPECT_EQ(input[2], output[5]);
229 }
230 
TEST(test_flowgraph,module_ramp_linear)231 TEST(test_flowgraph, module_ramp_linear) {
232     constexpr int singleNumOutput = 1;
233     constexpr int rampSize = 5;
234     constexpr int numOutput = 100;
235     constexpr float value = 1.0f;
236     constexpr float initialTarget = 10.0f;
237     constexpr float finalTarget = 100.0f;
238     constexpr float tolerance = 0.0001f; // arbitrary
239     float output[numOutput] = {};
240     RampLinear rampLinear{1};
241     SinkFloat sinkFloat{1};
242 
243     rampLinear.input.setValue(value);
244     rampLinear.setLengthInFrames(rampSize);
245     rampLinear.output.connect(&sinkFloat.input);
246 
247     // Check that the values go to the initial target instantly.
248     rampLinear.setTarget(initialTarget);
249     int32_t singleNumRead = sinkFloat.read(output, singleNumOutput);
250     ASSERT_EQ(singleNumRead, singleNumOutput);
251     EXPECT_NEAR(value * initialTarget, output[0], tolerance);
252 
253     // Now set target and check that the linear ramp works as expected.
254     rampLinear.setTarget(finalTarget);
255     int32_t numRead = sinkFloat.read(output, numOutput);
256     const float incrementSize = (finalTarget - initialTarget) / rampSize;
257     ASSERT_EQ(numOutput, numRead);
258 
259     int i = 0;
260     for (; i < rampSize; i++) {
261         float expected = value * (initialTarget + i * incrementSize);
262         EXPECT_NEAR(expected, output[i], tolerance);
263     }
264     for (; i < numOutput; i++) {
265         float expected = value * finalTarget;
266         EXPECT_NEAR(expected, output[i], tolerance);
267     }
268 }
269 
270 // It is easiest to represent packed 24-bit data as a byte array.
271 // This test will read from input, convert to float, then write
272 // back to output as bytes.
TEST(test_flowgraph,module_packed_24)273 TEST(test_flowgraph, module_packed_24) {
274     static const uint8_t input[] = {0x01, 0x23, 0x45,
275                                     0x67, 0x89, 0xAB,
276                                     0xCD, 0xEF, 0x5A};
277     uint8_t output[99] = {};
278     SourceI24 sourceI24{1};
279     SinkI24 sinkI24{1};
280 
281     int numInputFrames = sizeof(input) / kBytesPerI24Packed;
282     sourceI24.setData(input, numInputFrames);
283     sourceI24.output.connect(&sinkI24.input);
284 
285     int32_t numRead = sinkI24.read(output, sizeof(output) / kBytesPerI24Packed);
286     ASSERT_EQ(numInputFrames, numRead);
287     for (size_t i = 0; i < sizeof(input); i++) {
288         EXPECT_EQ(input[i], output[i]);
289     }
290 }
291 
TEST(test_flowgraph,module_clip_to_range)292 TEST(test_flowgraph, module_clip_to_range) {
293     constexpr float myMin = -2.0f;
294     constexpr float myMax = 1.5f;
295 
296     static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3};
297     static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax};
298     float output[100];
299     SourceFloat sourceFloat{1};
300     ClipToRange clipper{1};
301     SinkFloat sinkFloat{1};
302 
303     int numInputFrames = sizeof(input) / sizeof(input[0]);
304     sourceFloat.setData(input, numInputFrames);
305 
306     clipper.setMinimum(myMin);
307     clipper.setMaximum(myMax);
308 
309     sourceFloat.output.connect(&clipper.input);
310     clipper.output.connect(&sinkFloat.input);
311 
312     int numOutputFrames = sizeof(output) / sizeof(output[0]);
313     int32_t numRead = sinkFloat.read(output, numOutputFrames);
314     ASSERT_EQ(numInputFrames, numRead);
315     constexpr float tolerance = 0.000001f; // arbitrary
316     for (int i = 0; i < numRead; i++) {
317         EXPECT_NEAR(expected[i], output[i], tolerance);
318     }
319 }
320 
TEST(test_flowgraph,module_mono_blend)321 TEST(test_flowgraph, module_mono_blend) {
322     // Two channel to two channel with 3 inputs and outputs.
323     constexpr int numChannels = 2;
324     constexpr int numFrames = 3;
325 
326     static const float input[] = {-0.7, 0.5, -0.25, 1.25, 1000, 2000};
327     static const float expected[] = {-0.1, -0.1, 0.5, 0.5, 1500, 1500};
328     float output[100];
329     SourceFloat sourceFloat{numChannels};
330     MonoBlend monoBlend{numChannels};
331     SinkFloat sinkFloat{numChannels};
332 
333     sourceFloat.setData(input, numFrames);
334 
335     sourceFloat.output.connect(&monoBlend.input);
336     monoBlend.output.connect(&sinkFloat.input);
337 
338     int32_t numRead = sinkFloat.read(output, numFrames);
339     ASSERT_EQ(numRead, numFrames);
340     constexpr float tolerance = 0.000001f; // arbitrary
341     for (int i = 0; i < numRead; i++) {
342         EXPECT_NEAR(expected[i], output[i], tolerance);
343     }
344 }
345 
TEST(test_flowgraph,module_limiter)346 TEST(test_flowgraph, module_limiter) {
347     constexpr int kNumSamples = 101;
348     constexpr float kLastSample = 3.0f;
349     constexpr float kFirstSample = -kLastSample;
350     constexpr float kDeltaBetweenSamples = (kLastSample - kFirstSample) / (kNumSamples - 1);
351     constexpr float kTolerance = 0.00001f;
352 
353     float input[kNumSamples];
354     float output[kNumSamples];
355     SourceFloat sourceFloat{1};
356     Limiter limiter{1};
357     SinkFloat sinkFloat{1};
358 
359     for (int i = 0; i < kNumSamples; i++) {
360         input[i] = kFirstSample + i * kDeltaBetweenSamples;
361     }
362 
363     const int numInputFrames = std::size(input);
364     sourceFloat.setData(input, numInputFrames);
365 
366     sourceFloat.output.connect(&limiter.input);
367     limiter.output.connect(&sinkFloat.input);
368 
369     const int numOutputFrames = std::size(output);
370     int32_t numRead = sinkFloat.read(output, numOutputFrames);
371     ASSERT_EQ(numInputFrames, numRead);
372 
373     for (int i = 0; i < numRead; i++) {
374         // limiter must be symmetric wrt 0.
375         EXPECT_NEAR(output[i], -output[kNumSamples - i - 1], kTolerance);
376         if (i > 0) {
377             EXPECT_GE(output[i], output[i - 1]); // limiter must be monotonic
378         }
379         if (input[i] == 0.f) {
380             EXPECT_EQ(0.f, output[i]);
381         } else if (input[i] > 0.0f) {
382             EXPECT_GE(output[i], 0.0f);
383             EXPECT_LE(output[i], M_SQRT2); // limiter actually limits
384             EXPECT_LE(output[i], input[i]); // a limiter, gain <= 1
385         } else {
386             EXPECT_LE(output[i], 0.0f);
387             EXPECT_GE(output[i], -M_SQRT2); // limiter actually limits
388             EXPECT_GE(output[i], input[i]); // a limiter, gain <= 1
389         }
390         if (-1.f <= input[i] && input[i] <= 1.f) {
391             EXPECT_EQ(input[i], output[i]);
392         }
393     }
394 }
395 
TEST(test_flowgraph,module_limiter_nan)396 TEST(test_flowgraph, module_limiter_nan) {
397     constexpr int kArbitraryOutputSize = 100;
398     static const float input[] = {NAN, 0.5f, NAN, NAN, -10.0f, NAN};
399     static const float expected[] = {0.0f, 0.5f, 0.5f, 0.5f, -M_SQRT2, -M_SQRT2};
400     constexpr float tolerance = 0.00001f;
401     float output[kArbitraryOutputSize];
402     SourceFloat sourceFloat{1};
403     Limiter limiter{1};
404     SinkFloat sinkFloat{1};
405 
406     const int numInputFrames = std::size(input);
407     sourceFloat.setData(input, numInputFrames);
408 
409     sourceFloat.output.connect(&limiter.input);
410     limiter.output.connect(&sinkFloat.input);
411 
412     const int numOutputFrames = std::size(output);
413     int32_t numRead = sinkFloat.read(output, numOutputFrames);
414     ASSERT_EQ(numInputFrames, numRead);
415 
416     for (int i = 0; i < numRead; i++) {
417         EXPECT_NEAR(expected[i], output[i], tolerance);
418     }
419 }
420 
TEST(test_flowgraph,module_sinki16_multiple_reads)421 TEST(test_flowgraph, module_sinki16_multiple_reads) {
422     static constexpr int kNumSamples = 8;
423     std::array<int16_t, kNumSamples + 10> output; // larger than input
424 
425     SourceFloat sourceFloat{1};
426     SinkI16 sinkI16{1};
427 
428     sourceFloat.setData(kInputFloat.data(), kNumSamples);
429     sourceFloat.output.connect(&sinkI16.input);
430 
431     output.fill(777);
432 
433     // Read the first half of the data
434     int32_t numRead = sinkI16.read(output.data(), kNumSamples / 2);
435     ASSERT_EQ(kNumSamples / 2, numRead);
436     for (int i = 0; i < numRead; i++) {
437         EXPECT_EQ(kExpectedI16.at(i), output.at(i)) << ", i = " << i;
438     }
439 
440     // Read the rest of the data
441     numRead = sinkI16.read(output.data(), output.size());
442     ASSERT_EQ(kNumSamples / 2, numRead);
443     for (int i = 0; i < numRead; i++) {
444         EXPECT_EQ(kExpectedI16.at(i + kNumSamples / 2), output.at(i)) << ", i = " << i;
445     }
446 }
447 
448 // =================================== FLOAT to Q8.23 ==============
449 __attribute__((noinline))
clamp24FromFloat(float f)450 static int32_t clamp24FromFloat(float f)
451 {
452     static const float scale = 1 << 23;
453     return (int32_t) lroundf(fmaxf(fminf(f * scale, scale - 1.f), -scale));
454 }
455 
local_convert_float_to_i8_24(const float * input,int32_t * output,int count)456 void local_convert_float_to_i8_24(const float *input,
457                                   int32_t *output,
458                                   int count) {
459     for (int i = 0; i < count; i++) {
460         *output++ = clamp24FromFloat(*input++);
461     }
462 }
463 
TEST(test_flowgraph,local_convert_float_to_i8_24)464 TEST(test_flowgraph, local_convert_float_to_i8_24) {
465     std::array<int32_t, kNumSamples> output;
466     // Convert audio signal using the function.
467     output.fill(777);
468     local_convert_float_to_i8_24(kInputFloat.data(), output.data(), kNumSamples);
469     for (int i = 0; i < kNumSamples; i++) {
470         EXPECT_EQ(kExpectedI8_24.at(i), output.at(i)) << ", i = " << i;
471     }
472 }
473 
TEST(test_flowgraph,module_sinkI8_24)474 TEST(test_flowgraph, module_sinkI8_24) {
475     std::array<int32_t, kNumSamples + 10> output; // larger than input
476 
477     SourceFloat sourceFloat{2};
478     SinkI8_24 sinkI8_24{2};
479 
480     sourceFloat.setData(kInputFloat.data(), kNumSamples);
481     sourceFloat.output.connect(&sinkI8_24.input);
482 
483     output.fill(777);
484     int32_t numRead = sinkI8_24.read(output.data(), output.size());
485     ASSERT_EQ(kNumSamples, numRead);
486     for (int i = 0; i < numRead; i++) {
487         EXPECT_EQ(kExpectedI8_24.at(i), output.at(i)) << ", i = " << i;
488     }
489 }
490 
TEST(test_flowgraph,module_sourceI8_24)491 TEST(test_flowgraph, module_sourceI8_24) {
492     static const int32_t input[] = {1 << 23, 1 << 22, -(1 << 21), -(1 << 23), 0, 1 << 25,
493             -(1 << 25)};
494     static const float expected[] = {1.0f, 0.5f, -0.25f, -1.0f, 0.0f, 4.0f, -4.0f};
495     float output[100];
496 
497     SourceI8_24 sourceI8_24{1};
498     SinkFloat sinkFloat{1};
499 
500     int numSamples = std::size(input);
501 
502     sourceI8_24.setData(input, numSamples);
503     sourceI8_24.output.connect(&sinkFloat.input);
504 
505     int32_t numRead = sinkFloat.read(output, numSamples);
506     ASSERT_EQ(numSamples, numRead);
507     for (int i = 0; i < numRead; i++) {
508         EXPECT_EQ(expected[i], output[i]) << ", i = " << i;
509     }
510 }
511 
checkSampleRateConversionVariedSizes(int32_t sourceSampleRate,int32_t sinkSampleRate,MultiChannelResampler::Quality resamplerQuality)512 void checkSampleRateConversionVariedSizes(int32_t sourceSampleRate,
513                     int32_t sinkSampleRate,
514                     MultiChannelResampler::Quality resamplerQuality) {
515     AAudioFlowGraph flowgraph;
516     aaudio_result_t result = flowgraph.configure(AUDIO_FORMAT_PCM_FLOAT /* sourceFormat */,
517             1 /* sourceChannelCount */,
518             sourceSampleRate,
519             AUDIO_FORMAT_PCM_FLOAT /* sinkFormat */,
520             1 /* sinkChannelCount */,
521             sinkSampleRate,
522             false /* useMonoBlend */,
523             false /* useVolumeRamps */,
524             0.0f /* audioBalance */,
525             resamplerQuality);
526 
527     IntegerRatio ratio(sourceSampleRate, sinkSampleRate);
528     ratio.reduce();
529 
530     ASSERT_EQ(AAUDIO_OK, result);
531 
532     const int inputSize = ratio.getNumerator();
533     const int outputSize = ratio.getDenominator();
534     float input[inputSize];
535     float output[outputSize];
536 
537     for (int i = 0; i < inputSize; i++) {
538         input[i] = i * 1.0f / inputSize;
539     }
540 
541     int inputUsed = 0;
542     int outputRead = 0;
543     int curInputSize = 1;
544 
545     // Process the data with larger and larger input buffer sizes.
546     while (inputUsed < inputSize) {
547         outputRead += flowgraph.process((void *) (input + inputUsed),
548                 curInputSize,
549                 (void *) (output + outputRead),
550                 outputSize - outputRead);
551         inputUsed += curInputSize;
552         curInputSize = std::min(curInputSize + 5, inputSize - inputUsed);
553     }
554 
555     ASSERT_EQ(outputSize, outputRead);
556 
557     for (int i = 1; i < outputSize; i++) {
558         // The first values of the flowgraph will be close to zero.
559         // Besides those, the values should be strictly increasing.
560         if (output[i - 1] > 0.01f) {
561             EXPECT_GT(output[i], output[i - 1]);
562         }
563     }
564 }
565 
TEST(test_flowgraph,flowgraph_varied_sizes_all)566 TEST(test_flowgraph, flowgraph_varied_sizes_all) {
567     const int rates[] = {8000, 11025, 22050, 32000, 44100, 48000, 64000, 88200, 96000};
568     const MultiChannelResampler::Quality qualities[] =
569     {
570         MultiChannelResampler::Quality::Fastest,
571         MultiChannelResampler::Quality::Low,
572         MultiChannelResampler::Quality::Medium,
573         MultiChannelResampler::Quality::High,
574         MultiChannelResampler::Quality::Best
575     };
576     for (int srcRate : rates) {
577         for (int destRate : rates) {
578             for (auto quality : qualities) {
579                 if (srcRate != destRate) {
580                     checkSampleRateConversionVariedSizes(srcRate, destRate, quality);
581                 }
582             }
583         }
584     }
585 }
586 
checkSampleRateConversionPullLater(int32_t sourceSampleRate,int32_t sinkSampleRate,MultiChannelResampler::Quality resamplerQuality)587 void checkSampleRateConversionPullLater(int32_t sourceSampleRate,
588                     int32_t sinkSampleRate,
589                     MultiChannelResampler::Quality resamplerQuality) {
590     AAudioFlowGraph flowgraph;
591     aaudio_result_t result = flowgraph.configure(AUDIO_FORMAT_PCM_FLOAT /* sourceFormat */,
592             1 /* sourceChannelCount */,
593             sourceSampleRate,
594             AUDIO_FORMAT_PCM_FLOAT /* sinkFormat */,
595             1 /* sinkChannelCount */,
596             sinkSampleRate,
597             false /* useMonoBlend */,
598             false /* useVolumeRamps */,
599             0.0f /* audioBalance */,
600             resamplerQuality);
601 
602     IntegerRatio ratio(sourceSampleRate, sinkSampleRate);
603     ratio.reduce();
604 
605     ASSERT_EQ(AAUDIO_OK, result);
606 
607     const int inputSize = ratio.getNumerator();
608     const int outputSize = ratio.getDenominator();
609     float input[inputSize];
610     float output[outputSize];
611 
612     for (int i = 0; i < inputSize; i++) {
613         input[i] = i * 1.0f / inputSize;
614     }
615 
616     // Read half the data with process.
617     int outputRead = flowgraph.process((void *) input,
618             inputSize,
619             (void *) output,
620             outputSize / 2);
621 
622     ASSERT_EQ(outputSize / 2, outputRead);
623 
624     // Now read the other half of the data with pull.
625     outputRead += flowgraph.pull(
626             (void *) (output + outputRead),
627             outputSize - outputRead);
628 
629     ASSERT_EQ(outputSize, outputRead);
630     for (int i = 1; i < outputSize; i++) {
631         // The first values of the flowgraph will be close to zero.
632         // Besides those, the values should be strictly increasing.
633         if (output[i - 1] > 0.01f) {
634             EXPECT_GT(output[i], output[i - 1]);
635         }
636     }
637 }
638 
639 // TODO: b/289508408 - Remove non-parameterized tests if they get noisy.
TEST(test_flowgraph,flowgraph_pull_later_all)640 TEST(test_flowgraph, flowgraph_pull_later_all) {
641     const int rates[] = {8000, 11025, 22050, 32000, 44100, 48000, 64000, 88200, 96000};
642     const MultiChannelResampler::Quality qualities[] =
643     {
644         MultiChannelResampler::Quality::Fastest,
645         MultiChannelResampler::Quality::Low,
646         MultiChannelResampler::Quality::Medium,
647         MultiChannelResampler::Quality::High,
648         MultiChannelResampler::Quality::Best
649     };
650     for (int srcRate : rates) {
651         for (int destRate : rates) {
652             for (auto quality : qualities) {
653                 if (srcRate != destRate) {
654                     checkSampleRateConversionPullLater(srcRate, destRate, quality);
655                 }
656             }
657         }
658     }
659 }
660 
661 class TestFlowgraphSampleRateConversion : public ::testing::Test,
662                         public ::testing::WithParamInterface<TestFlowgraphResamplerParams> {
663 };
664 
resamplerQualityToString(MultiChannelResampler::Quality quality)665 const char* resamplerQualityToString(MultiChannelResampler::Quality quality) {
666     switch (quality) {
667         case MultiChannelResampler::Quality::Fastest: return "FASTEST";
668         case MultiChannelResampler::Quality::Low: return "LOW";
669         case MultiChannelResampler::Quality::Medium: return "MEDIUM";
670         case MultiChannelResampler::Quality::High: return "HIGH";
671         case MultiChannelResampler::Quality::Best: return "BEST";
672     }
673     return "UNKNOWN";
674 }
675 
getTestName(const::testing::TestParamInfo<TestFlowgraphResamplerParams> & info)676 static std::string getTestName(
677         const ::testing::TestParamInfo<TestFlowgraphResamplerParams>& info) {
678     return std::string()
679             + std::to_string(std::get<PARAM_SOURCE_SAMPLE_RATE>(info.param))
680             + "__" + std::to_string(std::get<PARAM_SINK_SAMPLE_RATE>(info.param))
681             + "__" + resamplerQualityToString(std::get<PARAM_RESAMPLER_QUALITY>(info.param));
682 }
683 
TEST_P(TestFlowgraphSampleRateConversion,test_flowgraph_pull_later)684 TEST_P(TestFlowgraphSampleRateConversion, test_flowgraph_pull_later) {
685     checkSampleRateConversionPullLater(std::get<PARAM_SOURCE_SAMPLE_RATE>(GetParam()),
686             std::get<PARAM_SINK_SAMPLE_RATE>(GetParam()),
687             std::get<PARAM_RESAMPLER_QUALITY>(GetParam()));
688 }
689 
TEST_P(TestFlowgraphSampleRateConversion,test_flowgraph_varied_sizes)690 TEST_P(TestFlowgraphSampleRateConversion, test_flowgraph_varied_sizes) {
691     checkSampleRateConversionVariedSizes(std::get<PARAM_SOURCE_SAMPLE_RATE>(GetParam()),
692             std::get<PARAM_SINK_SAMPLE_RATE>(GetParam()),
693             std::get<PARAM_RESAMPLER_QUALITY>(GetParam()));
694 }
695 
696 INSTANTIATE_TEST_SUITE_P(
697         test_flowgraph,
698         TestFlowgraphSampleRateConversion,
699         ::testing::Values(
700                 TestFlowgraphResamplerParams({8000, 11025, MultiChannelResampler::Quality::Best}),
701                 TestFlowgraphResamplerParams({8000, 48000, MultiChannelResampler::Quality::Best}),
702                 TestFlowgraphResamplerParams({8000, 44100, MultiChannelResampler::Quality::Best}),
703                 TestFlowgraphResamplerParams({11025, 24000, MultiChannelResampler::Quality::Best}),
704                 TestFlowgraphResamplerParams({11025, 48000,
705                         MultiChannelResampler::Quality::Fastest}),
706                 TestFlowgraphResamplerParams({11025, 48000, MultiChannelResampler::Quality::Low}),
707                 TestFlowgraphResamplerParams({11025, 48000,
708                         MultiChannelResampler::Quality::Medium}),
709                 TestFlowgraphResamplerParams({11025, 48000, MultiChannelResampler::Quality::High}),
710                 TestFlowgraphResamplerParams({11025, 48000, MultiChannelResampler::Quality::Best}),
711                 TestFlowgraphResamplerParams({11025, 44100, MultiChannelResampler::Quality::Best}),
712                 TestFlowgraphResamplerParams({11025, 88200, MultiChannelResampler::Quality::Best}),
713                 TestFlowgraphResamplerParams({16000, 48000, MultiChannelResampler::Quality::Best}),
714                 TestFlowgraphResamplerParams({44100, 48000, MultiChannelResampler::Quality::Low}),
715                 TestFlowgraphResamplerParams({44100, 48000, MultiChannelResampler::Quality::Best}),
716                 TestFlowgraphResamplerParams({48000, 11025, MultiChannelResampler::Quality::Best}),
717                 TestFlowgraphResamplerParams({48000, 44100, MultiChannelResampler::Quality::Best}),
718                 TestFlowgraphResamplerParams({44100, 11025, MultiChannelResampler::Quality::Best})),
719         &getTestName
720 );
721