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 #include <algorithm>
18 #include <unistd.h>
19 #include "FlowGraphNode.h"
20 #include "SinkFloat.h"
21 
22 using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
23 
SinkFloat(int32_t channelCount)24 SinkFloat::SinkFloat(int32_t channelCount)
25         : FlowGraphSink(channelCount) {
26 }
27 
read(void * data,int32_t numFrames)28 int32_t SinkFloat::read(void *data, int32_t numFrames) {
29     // printf("SinkFloat::read(,,%d)\n", numFrames);
30     float *floatData = (float *) data;
31     const int32_t channelCount = input.getSamplesPerFrame();
32 
33     int32_t framesLeft = numFrames;
34     while (framesLeft > 0) {
35         // Run the graph and pull data through the input port.
36         int32_t framesPulled = pullData(framesLeft);
37         // printf("SinkFloat::read: framesLeft = %d, framesPulled = %d\n", framesLeft, framesPulled);
38         if (framesPulled <= 0) {
39             break;
40         }
41         const float *signal = input.getBuffer();
42         int32_t numSamples = framesPulled * channelCount;
43         memcpy(floatData, signal, numSamples * sizeof(float));
44         floatData += numSamples;
45         framesLeft -= framesPulled;
46     }
47     // printf("SinkFloat returning %d\n", numFrames - framesLeft);
48     return numFrames - framesLeft;
49 }
50