-
Notifications
You must be signed in to change notification settings - Fork 572
/
testFlowgraph.cpp
269 lines (226 loc) · 8.94 KB
/
testFlowgraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Test FlowGraph
*/
#include "stdio.h"
#include <gtest/gtest.h>
#include <oboe/Oboe.h>
#include "flowgraph/ClipToRange.h"
#include "flowgraph/Limiter.h"
#include "flowgraph/MonoToMultiConverter.h"
#include "flowgraph/SourceFloat.h"
#include "flowgraph/RampLinear.h"
#include "flowgraph/SinkFloat.h"
#include "flowgraph/SinkI16.h"
#include "flowgraph/SinkI24.h"
#include "flowgraph/SinkI32.h"
#include "flowgraph/SourceI16.h"
#include "flowgraph/SourceI24.h"
using namespace oboe::flowgraph;
constexpr int kBytesPerI24Packed = 3;
TEST(test_flowgraph, module_sinki16) {
static const float input[] = {1.0f, 0.5f, -0.25f, -1.0f, 0.0f, 53.9f, -87.2f};
static const int16_t expected[] = {32767, 16384, -8192, -32768, 0, 32767, -32768};
int16_t output[20];
SourceFloat sourceFloat{1};
SinkI16 sinkI16{1};
int numInputFrames = sizeof(input) / sizeof(input[0]);
sourceFloat.setData(input, numInputFrames);
sourceFloat.output.connect(&sinkI16.input);
int numOutputFrames = sizeof(output) / sizeof(int16_t);
int32_t numRead = sinkI16.read(output, numOutputFrames);
ASSERT_EQ(numInputFrames, numRead);
for (int i = 0; i < numRead; i++) {
EXPECT_EQ(expected[i], output[i]);
}
}
TEST(test_flowgraph, module_mono_to_stereo) {
static const float input[] = {1.0f, 2.0f, 3.0f};
float output[100] = {};
SourceFloat sourceFloat{1};
MonoToMultiConverter monoToStereo{2};
SinkFloat sinkFloat{2};
sourceFloat.setData(input, 3);
sourceFloat.output.connect(&monoToStereo.input);
monoToStereo.output.connect(&sinkFloat.input);
int32_t numRead = sinkFloat.read(output, 8);
ASSERT_EQ(3, numRead);
EXPECT_EQ(input[0], output[0]);
EXPECT_EQ(input[0], output[1]);
EXPECT_EQ(input[1], output[2]);
EXPECT_EQ(input[1], output[3]);
}
TEST(test_flowgraph, module_ramp_linear) {
constexpr int singleNumOutput = 1;
constexpr int rampSize = 5;
constexpr int numOutput = 100;
constexpr float value = 1.0f;
constexpr float initialTarget = 10.0f;
constexpr float finalTarget = 100.0f;
constexpr float tolerance = 0.0001f; // arbitrary
float output[numOutput] = {};
RampLinear rampLinear{1};
SinkFloat sinkFloat{1};
rampLinear.input.setValue(value);
rampLinear.setLengthInFrames(rampSize);
rampLinear.output.connect(&sinkFloat.input);
// Check that the values go to the initial target instantly.
rampLinear.setTarget(initialTarget);
int32_t singleNumRead = sinkFloat.read(output, singleNumOutput);
ASSERT_EQ(singleNumRead, singleNumOutput);
EXPECT_NEAR(value * initialTarget, output[0], tolerance);
// Now set target and check that the linear ramp works as expected.
rampLinear.setTarget(finalTarget);
int32_t numRead = sinkFloat.read(output, numOutput);
const float incrementSize = (finalTarget - initialTarget) / rampSize;
ASSERT_EQ(numOutput, numRead);
int i = 0;
for (; i < rampSize; i++) {
float expected = value * (initialTarget + i * incrementSize);
EXPECT_NEAR(expected, output[i], tolerance);
}
for (; i < numOutput; i++) {
float expected = value * finalTarget;
EXPECT_NEAR(expected, output[i], tolerance);
}
}
// It is easiest to represent packed 24-bit data as a byte array.
// This test will read from input, convert to float, then write
// back to output as bytes.
TEST(test_flowgraph, module_packed_24) {
static const uint8_t input[] = {0x01, 0x23, 0x45,
0x67, 0x89, 0xAB,
0xCD, 0xEF, 0x5A};
uint8_t output[99] = {};
SourceI24 sourceI24{1};
SinkI24 sinkI24{1};
int numInputFrames = sizeof(input) / kBytesPerI24Packed;
sourceI24.setData(input, numInputFrames);
sourceI24.output.connect(&sinkI24.input);
int32_t numRead = sinkI24.read(output, sizeof(output) / kBytesPerI24Packed);
ASSERT_EQ(numInputFrames, numRead);
for (size_t i = 0; i < sizeof(input); i++) {
EXPECT_EQ(input[i], output[i]);
}
}
TEST(test_flowgraph, module_clip_to_range) {
constexpr float myMin = -2.0f;
constexpr float myMax = 1.5f;
static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3};
static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax};
float output[100];
SourceFloat sourceFloat{1};
ClipToRange clipper{1};
SinkFloat sinkFloat{1};
int numInputFrames = sizeof(input) / sizeof(input[0]);
sourceFloat.setData(input, numInputFrames);
clipper.setMinimum(myMin);
clipper.setMaximum(myMax);
sourceFloat.output.connect(&clipper.input);
clipper.output.connect(&sinkFloat.input);
int numOutputFrames = sizeof(output) / sizeof(output[0]);
int32_t numRead = sinkFloat.read(output, numOutputFrames);
ASSERT_EQ(numInputFrames, numRead);
constexpr float tolerance = 0.000001f; // arbitrary
for (int i = 0; i < numRead; i++) {
EXPECT_NEAR(expected[i], output[i], tolerance);
}
}
TEST(test_flowgraph, module_sinki32) {
static constexpr int kNumSamples = 8;
static const float input[] = {
1.0f, 0.5f, -0.25f, -1.0f,
0.0f, 53.9f, -87.2f, -1.02f};
static const int32_t expected[] = {
INT32_MAX, 1 << 30, INT32_MIN / 4, INT32_MIN,
0, INT32_MAX, INT32_MIN, INT32_MIN};
int32_t output[kNumSamples + 10]; // larger than input
SourceFloat sourceFloat{1};
SinkI32 sinkI32{1};
sourceFloat.setData(input, kNumSamples);
sourceFloat.output.connect(&sinkI32.input);
int numOutputFrames = sizeof(output) / sizeof(int32_t);
int32_t numRead = sinkI32.read(output, numOutputFrames);
ASSERT_EQ(kNumSamples, numRead);
for (int i = 0; i < numRead; i++) {
EXPECT_EQ(expected[i], output[i]) << ", i = " << i;
}
}
TEST(test_flowgraph, module_limiter) {
constexpr int kNumSamples = 101;
constexpr float kLastSample = 3.0f;
constexpr float kFirstSample = -kLastSample;
constexpr float kDeltaBetweenSamples = (kLastSample - kFirstSample) / (kNumSamples - 1);
constexpr float kTolerance = 0.00001f;
float input[kNumSamples];
float output[kNumSamples];
SourceFloat sourceFloat{1};
Limiter limiter{1};
SinkFloat sinkFloat{1};
for (int i = 0; i < kNumSamples; i++) {
input[i] = kFirstSample + i * kDeltaBetweenSamples;
}
const int numInputFrames = std::size(input);
sourceFloat.setData(input, numInputFrames);
sourceFloat.output.connect(&limiter.input);
limiter.output.connect(&sinkFloat.input);
const int numOutputFrames = std::size(output);
int32_t numRead = sinkFloat.read(output, numOutputFrames);
ASSERT_EQ(numInputFrames, numRead);
for (int i = 0; i < numRead; i++) {
// limiter must be symmetric wrt 0.
EXPECT_NEAR(output[i], -output[kNumSamples - i - 1], kTolerance);
if (i > 0) {
EXPECT_GE(output[i], output[i - 1]); // limiter must be monotonic
}
if (input[i] == 0.f) {
EXPECT_EQ(0.f, output[i]);
} else if (input[i] > 0.0f) {
EXPECT_GE(output[i], 0.0f);
EXPECT_LE(output[i], M_SQRT2); // limiter actually limits
EXPECT_LE(output[i], input[i]); // a limiter, gain <= 1
} else {
EXPECT_LE(output[i], 0.0f);
EXPECT_GE(output[i], -M_SQRT2); // limiter actually limits
EXPECT_GE(output[i], input[i]); // a limiter, gain <= 1
}
if (-1.f <= input[i] && input[i] <= 1.f) {
EXPECT_EQ(input[i], output[i]);
}
}
}
TEST(test_flowgraph, module_limiter_nan) {
constexpr int kArbitraryOutputSize = 100;
constexpr float kFloatNan = NAN;
static const float input[] = {kFloatNan, 0.5f, kFloatNan, kFloatNan, -10.0f, kFloatNan};
static const float expected[] = {0.0f, 0.5f, 0.5f, 0.5f, -M_SQRT2, -M_SQRT2};
constexpr float tolerance = 0.00001f;
float output[kArbitraryOutputSize];
SourceFloat sourceFloat{1};
Limiter limiter{1};
SinkFloat sinkFloat{1};
const int numInputFrames = std::size(input);
sourceFloat.setData(input, numInputFrames);
sourceFloat.output.connect(&limiter.input);
limiter.output.connect(&sinkFloat.input);
const int numOutputFrames = std::size(output);
int32_t numRead = sinkFloat.read(output, numOutputFrames);
ASSERT_EQ(numInputFrames, numRead);
for (int i = 0; i < numRead; i++) {
EXPECT_NEAR(expected[i], output[i], tolerance);
}
}