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