xref: /aosp_15_r20/external/oboe/src/flowgraph/SinkI32.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1*05767d91SRobert Wu /*
2*05767d91SRobert Wu  * Copyright 2020 The Android Open Source Project
3*05767d91SRobert Wu  *
4*05767d91SRobert Wu  * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu  * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu  * You may obtain a copy of the License at
7*05767d91SRobert Wu  *
8*05767d91SRobert Wu  *      http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu  *
10*05767d91SRobert Wu  * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu  * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu  * See the License for the specific language governing permissions and
14*05767d91SRobert Wu  * limitations under the License.
15*05767d91SRobert Wu  */
16*05767d91SRobert Wu 
17*05767d91SRobert Wu #include "FlowGraphNode.h"
18*05767d91SRobert Wu #include "FlowgraphUtilities.h"
19*05767d91SRobert Wu #include "SinkI32.h"
20*05767d91SRobert Wu 
21*05767d91SRobert Wu #if FLOWGRAPH_ANDROID_INTERNAL
22*05767d91SRobert Wu #include <audio_utils/primitives.h>
23*05767d91SRobert Wu #endif
24*05767d91SRobert Wu 
25*05767d91SRobert Wu using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
26*05767d91SRobert Wu 
SinkI32(int32_t channelCount)27*05767d91SRobert Wu SinkI32::SinkI32(int32_t channelCount)
28*05767d91SRobert Wu         : FlowGraphSink(channelCount) {}
29*05767d91SRobert Wu 
read(void * data,int32_t numFrames)30*05767d91SRobert Wu int32_t SinkI32::read(void *data, int32_t numFrames) {
31*05767d91SRobert Wu     int32_t *intData = (int32_t *) data;
32*05767d91SRobert Wu     const int32_t channelCount = input.getSamplesPerFrame();
33*05767d91SRobert Wu 
34*05767d91SRobert Wu     int32_t framesLeft = numFrames;
35*05767d91SRobert Wu     while (framesLeft > 0) {
36*05767d91SRobert Wu         // Run the graph and pull data through the input port.
37*05767d91SRobert Wu         int32_t framesRead = pullData(framesLeft);
38*05767d91SRobert Wu         if (framesRead <= 0) {
39*05767d91SRobert Wu             break;
40*05767d91SRobert Wu         }
41*05767d91SRobert Wu         const float *signal = input.getBuffer();
42*05767d91SRobert Wu         int32_t numSamples = framesRead * channelCount;
43*05767d91SRobert Wu #if FLOWGRAPH_ANDROID_INTERNAL
44*05767d91SRobert Wu         memcpy_to_i32_from_float(intData, signal, numSamples);
45*05767d91SRobert Wu         intData += numSamples;
46*05767d91SRobert Wu         signal += numSamples;
47*05767d91SRobert Wu #else
48*05767d91SRobert Wu         for (int i = 0; i < numSamples; i++) {
49*05767d91SRobert Wu             *intData++ = FlowgraphUtilities::clamp32FromFloat(*signal++);
50*05767d91SRobert Wu         }
51*05767d91SRobert Wu #endif
52*05767d91SRobert Wu         framesLeft -= framesRead;
53*05767d91SRobert Wu     }
54*05767d91SRobert Wu     return numFrames - framesLeft;
55*05767d91SRobert Wu }
56