• 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 
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 rampSize = 5;
84     constexpr int numOutput = 100;
85     constexpr float value = 1.0f;
86     constexpr float target = 100.0f;
87     float output[numOutput] = {};
88     RampLinear rampLinear{1};
89     SinkFloat sinkFloat{1};
90 
91     rampLinear.input.setValue(value);
92     rampLinear.setLengthInFrames(rampSize);
93     rampLinear.setTarget(target);
94     rampLinear.forceCurrent(0.0f);
95 
96     rampLinear.output.connect(&sinkFloat.input);
97 
98     int32_t numRead = sinkFloat.read(output, numOutput);
99     ASSERT_EQ(numOutput, numRead);
100     constexpr float tolerance = 0.0001f; // arbitrary
101     int i = 0;
102     for (; i < rampSize; i++) {
103         float expected = i * value * target / rampSize;
104         EXPECT_NEAR(expected, output[i], tolerance);
105     }
106     for (; i < numOutput; i++) {
107         float expected = value * target;
108         EXPECT_NEAR(expected, output[i], tolerance);
109     }
110 }
111 
112 // It is easiest to represent packed 24-bit data as a byte array.
113 // This test will read from input, convert to float, then write
114 // back to output as bytes.
TEST(test_flowgraph,module_packed_24)115 TEST(test_flowgraph, module_packed_24) {
116     static const uint8_t input[] = {0x01, 0x23, 0x45,
117                                     0x67, 0x89, 0xAB,
118                                     0xCD, 0xEF, 0x5A};
119     uint8_t output[99] = {};
120     SourceI24 sourceI24{1};
121     SinkI24 sinkI24{1};
122 
123     int numInputFrames = sizeof(input) / kBytesPerI24Packed;
124     sourceI24.setData(input, numInputFrames);
125     sourceI24.output.connect(&sinkI24.input);
126 
127     int32_t numRead = sinkI24.read(output, sizeof(output) / kBytesPerI24Packed);
128     ASSERT_EQ(numInputFrames, numRead);
129     for (size_t i = 0; i < sizeof(input); i++) {
130         EXPECT_EQ(input[i], output[i]);
131     }
132 }
133 
TEST(test_flowgraph,module_clip_to_range)134 TEST(test_flowgraph, module_clip_to_range) {
135     constexpr float myMin = -2.0f;
136     constexpr float myMax = 1.5f;
137 
138     static const float input[] = {-9.7, 0.5f, -0.25, 1.0f, 12.3};
139     static const float expected[] = {myMin, 0.5f, -0.25, 1.0f, myMax};
140     float output[100];
141     SourceFloat sourceFloat{1};
142     ClipToRange clipper{1};
143     SinkFloat sinkFloat{1};
144 
145     int numInputFrames = sizeof(input) / sizeof(input[0]);
146     sourceFloat.setData(input, numInputFrames);
147 
148     clipper.setMinimum(myMin);
149     clipper.setMaximum(myMax);
150 
151     sourceFloat.output.connect(&clipper.input);
152     clipper.output.connect(&sinkFloat.input);
153 
154     int numOutputFrames = sizeof(output) / sizeof(output[0]);
155     int32_t numRead = sinkFloat.read(output, numOutputFrames);
156     ASSERT_EQ(numInputFrames, numRead);
157     constexpr float tolerance = 0.000001f; // arbitrary
158     for (int i = 0; i < numRead; i++) {
159         EXPECT_NEAR(expected[i], output[i], tolerance);
160     }
161 }
162 
163