blob: eab863d2e7a7d4a0e6bfc5a4d7971a2e7602893e [file] [log] [blame]
Phil Burk04e805b2018-03-27 09:13:53 -07001/*
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#ifdef __ANDROID__
18#include <audio_utils/primitives.h>
19#endif
20
21#include "AudioProcessorBase.h"
22#include "FlowgraphUtilities.h"
23#include "SinkI32.h"
24
25using namespace flowgraph;
26
27SinkI32::SinkI32(int32_t channelCount)
28 : AudioSink(channelCount) {}
29
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 = pull(framesLeft);
38 if (framesRead <= 0) {
39 break;
40 }
41 const float *signal = input.getBlock();
42 int32_t numSamples = framesRead * channelCount;
43#ifdef __ANDROID__
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}