1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2018 The Android Open Source Project
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5cb93a386Sopenharmony_ci * you may not use this file except in compliance with the License.
6cb93a386Sopenharmony_ci * You may obtain a copy of the License at
7cb93a386Sopenharmony_ci *
8cb93a386Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
9cb93a386Sopenharmony_ci *
10cb93a386Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11cb93a386Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12cb93a386Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cb93a386Sopenharmony_ci * See the License for the specific language governing permissions and
14cb93a386Sopenharmony_ci * limitations under the License.
15cb93a386Sopenharmony_ci */
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#include <algorithm>
18cb93a386Sopenharmony_ci#include <unistd.h>
19cb93a386Sopenharmony_ci#include "FlowGraphNode.h"
20cb93a386Sopenharmony_ci#include "SinkFloat.h"
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ciusing namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ciSinkFloat::SinkFloat(int32_t channelCount)
25cb93a386Sopenharmony_ci        : FlowGraphSink(channelCount) {
26cb93a386Sopenharmony_ci}
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ciint32_t SinkFloat::read(void *data, int32_t numFrames) {
29cb93a386Sopenharmony_ci    // printf("SinkFloat::read(,,%d)\n", numFrames);
30cb93a386Sopenharmony_ci    float *floatData = (float *) data;
31cb93a386Sopenharmony_ci    int32_t channelCount = input.getSamplesPerFrame();
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    int32_t framesLeft = numFrames;
34cb93a386Sopenharmony_ci    while (framesLeft > 0) {
35cb93a386Sopenharmony_ci        // Run the graph and pull data through the input port.
36cb93a386Sopenharmony_ci        int32_t framesPulled = pullData(framesLeft);
37cb93a386Sopenharmony_ci        // printf("SinkFloat::read: framesLeft = %d, framesPulled = %d\n", framesLeft, framesPulled);
38cb93a386Sopenharmony_ci        if (framesPulled <= 0) {
39cb93a386Sopenharmony_ci            break;
40cb93a386Sopenharmony_ci        }
41cb93a386Sopenharmony_ci        const float *signal = input.getBuffer();
42cb93a386Sopenharmony_ci        int32_t numSamples = framesPulled * channelCount;
43cb93a386Sopenharmony_ci        memcpy(floatData, signal, numSamples * sizeof(float));
44cb93a386Sopenharmony_ci        floatData += numSamples;
45cb93a386Sopenharmony_ci        framesLeft -= framesPulled;
46cb93a386Sopenharmony_ci    }
47cb93a386Sopenharmony_ci    // printf("SinkFloat returning %d\n", numFrames - framesLeft);
48cb93a386Sopenharmony_ci    return numFrames - framesLeft;
49cb93a386Sopenharmony_ci}
50