blob: 611cbf725478c396880c47608aeda6a11f412446 [file] [log] [blame]
Phil Burkbb78a732018-03-28 15:37:19 -07001/*
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
21#include <iostream>
22
23#include <gtest/gtest.h>
24
25#include "flowgraph/ClipToRange.h"
26#include "flowgraph/MonoToMultiConverter.h"
27#include "flowgraph/SourceFloat.h"
28#include "flowgraph/RampLinear.h"
29#include "flowgraph/SinkFloat.h"
30#include "flowgraph/SinkI16.h"
31#include "flowgraph/SinkI24.h"
32#include "flowgraph/SourceI16.h"
33#include "flowgraph/SourceI24.h"
34
35using namespace flowgraph;
36
37constexpr int kBytesPerI24Packed = 3;
38
39TEST(test_flowgraph, module_sinki16) {
40 static const float input[] = {1.0f, 0.5f, -0.25f, -1.0f, 0.0f, 53.9f, -87.2f};
41 static const int16_t expected[] = {32767, 16384, -8192, -32768, 0, 32767, -32768};
42 int16_t output[20];
43 SourceFloat sourceFloat{1};
44 SinkI16 sinkI16{1};
45
46 int numInputFrames = sizeof(input) / sizeof(input[0]);
47 sourceFloat.setData(input, numInputFrames);
48 sourceFloat.output.connect(&sinkI16.input);
49
50 int numOutputFrames = sizeof(output) / sizeof(int16_t);
51 int32_t numRead = sinkI16.read(output, numOutputFrames);
52 ASSERT_EQ(numInputFrames, numRead);
53 for (int i = 0; i < numRead; i++) {
54 EXPECT_EQ(expected[i], output[i]);
55 }
56}
57
58TEST(test_flowgraph, module_mono_to_stereo) {
59 static const float input[] = {1.0f, 2.0f, 3.0f};
60 float output[100] = {};
61 SourceFloat sourceFloat{1};
62 MonoToMultiConverter monoToStereo{2};
63 SinkFloat sinkFloat{2};
64
65 sourceFloat.setData(input, 3);
66
67 sourceFloat.output.connect(&monoToStereo.input);
68 monoToStereo.output.connect(&sinkFloat.input);
69
70 int32_t numRead = sinkFloat.read(output, 8);
71 ASSERT_EQ(3, numRead);
72 EXPECT_EQ(input[0], output[0]);
73 EXPECT_EQ(input[0], output[1]);
74 EXPECT_EQ(input[1], output[2]);
75 EXPECT_EQ(input[1], output[3]);
76}
77
78TEST(test_flowgraph, module_ramp_linear) {
Robert Wuc3278fe2021-06-23 16:51:40 +000079 constexpr int singleNumOutput = 1;
Phil Burkbb78a732018-03-28 15:37:19 -070080 constexpr int rampSize = 5;
81 constexpr int numOutput = 100;
82 constexpr float value = 1.0f;
Robert Wuc3278fe2021-06-23 16:51:40 +000083 constexpr float initialTarget = 10.0f;
84 constexpr float finalTarget = 100.0f;
85 constexpr float tolerance = 0.0001f; // arbitrary
Phil Burkbb78a732018-03-28 15:37:19 -070086 float output[numOutput] = {};
87 RampLinear rampLinear{1};
88 SinkFloat sinkFloat{1};
89
90 rampLinear.input.setValue(value);
91 rampLinear.setLengthInFrames(rampSize);
Phil Burkbb78a732018-03-28 15:37:19 -070092 rampLinear.output.connect(&sinkFloat.input);
93
Robert Wuc3278fe2021-06-23 16:51:40 +000094 // Check that the values go to the initial target instantly.
95 rampLinear.setTarget(initialTarget);
96 int32_t singleNumRead = sinkFloat.read(output, singleNumOutput);
97 ASSERT_EQ(singleNumRead, singleNumOutput);
98 EXPECT_NEAR(value * initialTarget, output[0], tolerance);
99
100 // Now set target and check that the linear ramp works as expected.
101 rampLinear.setTarget(finalTarget);
Phil Burkbb78a732018-03-28 15:37:19 -0700102 int32_t numRead = sinkFloat.read(output, numOutput);
Robert Wuc3278fe2021-06-23 16:51:40 +0000103 const float incrementSize = (finalTarget - initialTarget) / rampSize;
Phil Burkbb78a732018-03-28 15:37:19 -0700104 ASSERT_EQ(numOutput, numRead);
Robert Wuc3278fe2021-06-23 16:51:40 +0000105
Phil Burkbb78a732018-03-28 15:37:19 -0700106 int i = 0;
107 for (; i < rampSize; i++) {
Robert Wuc3278fe2021-06-23 16:51:40 +0000108 float expected = value * (initialTarget + i * incrementSize);
Phil Burkbb78a732018-03-28 15:37:19 -0700109 EXPECT_NEAR(expected, output[i], tolerance);
110 }
111 for (; i < numOutput; i++) {
Robert Wuc3278fe2021-06-23 16:51:40 +0000112 float expected = value * finalTarget;
Phil Burkbb78a732018-03-28 15:37:19 -0700113 EXPECT_NEAR(expected, output[i], tolerance);
114 }
115}
116
117// It is easiest to represent packed 24-bit data as a byte array.
118// This test will read from input, convert to float, then write
119// back to output as bytes.
120TEST(test_flowgraph, module_packed_24) {
121 static const uint8_t input[] = {0x01, 0x23, 0x45,
122 0x67, 0x89, 0xAB,
123 0xCD, 0xEF, 0x5A};
124 uint8_t output[99] = {};
125 SourceI24 sourceI24{1};
126 SinkI24 sinkI24{1};
127
128 int numInputFrames = sizeof(input) / kBytesPerI24Packed;
129 sourceI24.setData(input, numInputFrames);
130 sourceI24.output.connect(&sinkI24.input);
131
132 int32_t numRead = sinkI24.read(output, sizeof(output) / kBytesPerI24Packed);
133 ASSERT_EQ(numInputFrames, numRead);
134 for (size_t i = 0; i < sizeof(input); i++) {
135 EXPECT_EQ(input[i], output[i]);
136 }
137}
138
139TEST(test_flowgraph, module_clip_to_range) {
140 constexpr float myMin = -2.0f;
141 constexpr float myMax = 1.5f;
142
143 static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3};
144 static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax};
145 float output[100];
146 SourceFloat sourceFloat{1};
147 ClipToRange clipper{1};
148 SinkFloat sinkFloat{1};
149
150 int numInputFrames = sizeof(input) / sizeof(input[0]);
151 sourceFloat.setData(input, numInputFrames);
152
153 clipper.setMinimum(myMin);
154 clipper.setMaximum(myMax);
155
156 sourceFloat.output.connect(&clipper.input);
157 clipper.output.connect(&sinkFloat.input);
158
159 int numOutputFrames = sizeof(output) / sizeof(output[0]);
160 int32_t numRead = sinkFloat.read(output, numOutputFrames);
161 ASSERT_EQ(numInputFrames, numRead);
162 constexpr float tolerance = 0.000001f; // arbitrary
163 for (int i = 0; i < numRead; i++) {
164 EXPECT_NEAR(expected[i], output[i], tolerance);
165 }
166}