1 /* 2 * Copyright 2020 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 "FlowGraphNode.h" 18 #include "FlowgraphUtilities.h" 19 #include "SinkI32.h" 20 21 #if FLOWGRAPH_ANDROID_INTERNAL 22 #include <audio_utils/primitives.h> 23 #endif 24 25 using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; 26 SinkI32(int32_t channelCount)27SinkI32::SinkI32(int32_t channelCount) 28 : FlowGraphSink(channelCount) {} 29 read(void * data,int32_t numFrames)30int32_t SinkI32::read(void *data, int32_t numFrames) { 31 int32_t *intData = (int32_t *) data; 32 const int32_t channelCount = input.getSamplesPerFrame(); 33 34 int32_t framesLeft = numFrames; 35 while (framesLeft > 0) { 36 // Run the graph and pull data through the input port. 37 int32_t framesRead = pullData(framesLeft); 38 if (framesRead <= 0) { 39 break; 40 } 41 const float *signal = input.getBuffer(); 42 int32_t numSamples = framesRead * channelCount; 43 #if FLOWGRAPH_ANDROID_INTERNAL 44 memcpy_to_i32_from_float(intData, signal, numSamples); 45 intData += numSamples; 46 signal += numSamples; 47 #else 48 for (int i = 0; i < numSamples; i++) { 49 *intData++ = FlowgraphUtilities::clamp32FromFloat(*signal++); 50 } 51 #endif 52 framesLeft -= framesRead; 53 } 54 return numFrames - framesLeft; 55 } 56