blob: b0a26ff5a3bedd0031ac5c692c9bac0098bd654d [file] [log] [blame]
Phil Burk83ceba12019-05-29 09:49:38 -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
Phil Burkf67a97f2019-05-30 12:48:17 -070021#include "stdio.h"
22
Phil Burk83ceba12019-05-29 09:49:38 -070023#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"
Phil Burk68b6ffa2019-06-04 14:58:41 -070032#include "flowgraph/LinearResampler.h"
Phil Burk83ceba12019-05-29 09:49:38 -070033#include "flowgraph/SampleRateConverter.h"
34#include "flowgraph/SinkFloat.h"
35#include "flowgraph/SinkI16.h"
36#include "flowgraph/SinkI24.h"
37#include "flowgraph/SourceI16.h"
38#include "flowgraph/SourceI24.h"
39
40using namespace flowgraph;
41
42constexpr int kBytesPerI24Packed = 3;
43
44TEST(test_flowgraph, module_sinki16) {
45 static const float input[] = {1.0f, 0.5f, -0.25f, -1.0f, 0.0f, 53.9f, -87.2f};
46 static const int16_t expected[] = {32767, 16384, -8192, -32768, 0, 32767, -32768};
47 int16_t output[20];
48 SourceFloat sourceFloat{1};
49 SinkI16 sinkI16{1};
50
51 int numInputFrames = sizeof(input) / sizeof(input[0]);
52 sourceFloat.setData(input, numInputFrames);
53 sourceFloat.output.connect(&sinkI16.input);
54
55 int numOutputFrames = sizeof(output) / sizeof(int16_t);
56 int32_t numRead = sinkI16.read(0 /* framePosition */, output, numOutputFrames);
57 ASSERT_EQ(numInputFrames, numRead);
58 for (int i = 0; i < numRead; i++) {
59 EXPECT_EQ(expected[i], output[i]);
60 }
61}
62
63TEST(test_flowgraph, module_mono_to_stereo) {
64 static const float input[] = {1.0f, 2.0f, 3.0f};
65 float output[100] = {};
66 SourceFloat sourceFloat{1};
67 MonoToMultiConverter monoToStereo{2};
68 SinkFloat sinkFloat{2};
69
70 sourceFloat.setData(input, 3);
71
72 sourceFloat.output.connect(&monoToStereo.input);
73 monoToStereo.output.connect(&sinkFloat.input);
74
75 int32_t numRead = sinkFloat.read(0 /* framePosition */, output, 8);
76 ASSERT_EQ(3, numRead);
77 EXPECT_EQ(input[0], output[0]);
78 EXPECT_EQ(input[0], output[1]);
79 EXPECT_EQ(input[1], output[2]);
80 EXPECT_EQ(input[1], output[3]);
81}
82
83TEST(test_flowgraph, module_ramp_linear) {
84 constexpr int rampSize = 5;
85 constexpr int numOutput = 100;
86 constexpr float value = 1.0f;
87 constexpr float target = 100.0f;
88 float output[numOutput] = {};
89 RampLinear rampLinear{1};
90 SinkFloat sinkFloat{1};
91
92 rampLinear.input.setValue(value);
93 rampLinear.setLengthInFrames(rampSize);
94 rampLinear.setTarget(target);
95 rampLinear.forceCurrent(0.0f);
96
97 rampLinear.output.connect(&sinkFloat.input);
98
99 int32_t numRead = sinkFloat.read(0 /* framePosition */, output, numOutput);
100 ASSERT_EQ(numOutput, numRead);
101 constexpr float tolerance = 0.0001f; // arbitrary
102 int i = 0;
103 for (; i < rampSize; i++) {
104 float expected = i * value * target / rampSize;
105 EXPECT_NEAR(expected, output[i], tolerance);
106 }
107 for (; i < numOutput; i++) {
108 float expected = value * target;
109 EXPECT_NEAR(expected, output[i], tolerance);
110 }
111}
112
113// It is easiest to represent packed 24-bit data as a byte array.
114// This test will read from input, convert to float, then write
115// back to output as bytes.
116TEST(test_flowgraph, module_packed_24) {
117 static const uint8_t input[] = {0x01, 0x23, 0x45,
118 0x67, 0x89, 0xAB,
119 0xCD, 0xEF, 0x5A};
120 uint8_t output[99] = {};
121 SourceI24 sourceI24{1};
122 SinkI24 sinkI24{1};
123
124 int numInputFrames = sizeof(input) / kBytesPerI24Packed;
125 sourceI24.setData(input, numInputFrames);
126 sourceI24.output.connect(&sinkI24.input);
127
128 int32_t numRead = sinkI24.read(0 /* framePosition */, output, sizeof(output) / kBytesPerI24Packed);
129 ASSERT_EQ(numInputFrames, numRead);
130 for (size_t i = 0; i < sizeof(input); i++) {
131 EXPECT_EQ(input[i], output[i]);
132 }
133}
134
135TEST(test_flowgraph, module_clip_to_range) {
136 constexpr float myMin = -2.0f;
137 constexpr float myMax = 1.5f;
138
139 static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3};
140 static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax};
141 float output[100];
142 SourceFloat sourceFloat{1};
143 ClipToRange clipper{1};
144 SinkFloat sinkFloat{1};
145
146 int numInputFrames = sizeof(input) / sizeof(input[0]);
147 sourceFloat.setData(input, numInputFrames);
148
149 clipper.setMinimum(myMin);
150 clipper.setMaximum(myMax);
151
152 sourceFloat.output.connect(&clipper.input);
153 clipper.output.connect(&sinkFloat.input);
154
155 int numOutputFrames = sizeof(output) / sizeof(output[0]);
156 int32_t numRead = sinkFloat.read(0 /* framePosition */, output, numOutputFrames);
157 ASSERT_EQ(numInputFrames, numRead);
158 constexpr float tolerance = 0.000001f; // arbitrary
159 for (int i = 0; i < numRead; i++) {
160 EXPECT_NEAR(expected[i], output[i], tolerance);
161 }
162}
163
164
165TEST(test_flowgraph, module_sample_rate_converter) {
166//void foo() {
167 static const double rateScaler = 0.5;
168 static const float input[] = {0.0, 1.0, 2.0};
Phil Burkf67a97f2019-05-30 12:48:17 -0700169 static const float expected[] = {0.0, 0.0, 0.0, 0.5, 1.0, 1.5};
Phil Burk83ceba12019-05-29 09:49:38 -0700170 float output[100];
171 SourceFloat sourceFloat{1};
Phil Burk68b6ffa2019-06-04 14:58:41 -0700172 LinearResampler linear{1};
173 SampleRateConverter rateConverter{1, linear};
Phil Burk83ceba12019-05-29 09:49:38 -0700174 SinkFloat sinkFloat{1};
175
Phil Burkf67a97f2019-05-30 12:48:17 -0700176 // printf("test_flowgraph::module_sample_rate_converter ===========================\n");
177
Phil Burk68b6ffa2019-06-04 14:58:41 -0700178 rateConverter.setPhaseIncrement(rateScaler);
Phil Burk83ceba12019-05-29 09:49:38 -0700179
180 int numInputFrames = sizeof(input) / sizeof(input[0]);
181 sourceFloat.setData(input, numInputFrames);
182
Phil Burk68b6ffa2019-06-04 14:58:41 -0700183 sourceFloat.output.connect(&rateConverter.input);
184 rateConverter.output.connect(&sinkFloat.input);
Phil Burk83ceba12019-05-29 09:49:38 -0700185
186 int numExpectedFrames = sizeof(expected) / sizeof(expected[0]);
187 int numOutputFrames = sizeof(output) / sizeof(output[0]);
Phil Burkf67a97f2019-05-30 12:48:17 -0700188 // printf("test_flowgraph::module_sample_rate_converter first read -------------\n");
Phil Burk83ceba12019-05-29 09:49:38 -0700189 int32_t numRead = sinkFloat.read(0 /* framePosition */, output, numOutputFrames);
190 EXPECT_EQ(numExpectedFrames, numRead);
191 constexpr float tolerance = 0.000001f; // arbitrary
192 for (int i = 0; i < numRead; i++) {
193 EXPECT_NEAR(expected[i], output[i], tolerance);
Phil Burkf67a97f2019-05-30 12:48:17 -0700194 // printf("test_flowgraph::module_sample_rate_converter output = %f\n", output[i]);
Phil Burk83ceba12019-05-29 09:49:38 -0700195 }
Phil Burkf67a97f2019-05-30 12:48:17 -0700196
197 // printf("test_flowgraph::module_sample_rate_converter second read -------------\n");
Phil Burk83ceba12019-05-29 09:49:38 -0700198 numRead = sinkFloat.read(numRead /* framePosition */, output, numOutputFrames);
199 EXPECT_EQ(0, numRead);
Phil Burk68b6ffa2019-06-04 14:58:41 -0700200}
201
202TEST(test_flowgraph, module_sinc_resampler) {
203//void foo() {
204 static const float zeroFrame[] = {0.0};
205 static const float oneFrame[] = {0.0};
206 float output = 0.0f;
207 SincResampler sincResampler{1};
208
209 printf("test_flowgraph::module_sinc_resampler ===========================\n");
210 fflush(stdout);
211
212 for (int i = 0; i <= (sincResampler.getSpread()*2*10); i++) {
213 float phase = i / 10.0;
214 float sinc = sincResampler.calculateWindowedSinc(phase);
215 printf("test_flowgraph::calculateWindowedSinc(%f) => %f\n", phase , sinc);
216 fflush(stdout);
217 }
218
219 for (int i = 0; i < 20; i++) {
220 printf("test_flowgraph: writeFrame %d\n", i);
221 fflush(stdout);
222 sincResampler.writeFrame(zeroFrame);
223 }
224 sincResampler.writeFrame(oneFrame); // write an impulse
225 for (int i = 0; i < 20; i++) {
226 sincResampler.writeFrame(zeroFrame);
227 printf("test_flowgraph: readFrame %d\n", i);
228 fflush(stdout);
229 sincResampler.readFrame(&output, 0.0);
230 printf("test_flowgraph::module_sinc_resampler output = %f\n", output);
231 }
232
233}