1 /*
2  * Copyright 2015 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 <sys/types.h>
19 #include "AudioProcessorBase.h"
20 
21 using namespace flowgraph;
22 
23 /***************************************************************************/
pullData(int64_t framePosition,int32_t numFrames)24 int32_t AudioProcessorBase::pullData(int64_t framePosition, int32_t numFrames) {
25     if (framePosition > mLastFramePosition) {
26         mLastFramePosition = framePosition;
27         mFramesValid = onProcess(framePosition, numFrames);
28     }
29     return mFramesValid;
30 }
31 
32 /***************************************************************************/
AudioFloatBlockPort(AudioProcessorBase & parent,int32_t samplesPerFrame,int32_t framesPerBlock)33 AudioFloatBlockPort::AudioFloatBlockPort(AudioProcessorBase &parent,
34                                int32_t samplesPerFrame,
35                                int32_t framesPerBlock)
36         : AudioPort(parent, samplesPerFrame)
37         , mFramesPerBlock(framesPerBlock)
38         , mSampleBlock(NULL) {
39     int32_t numFloats = framesPerBlock * getSamplesPerFrame();
40     mSampleBlock = new float[numFloats]{0.0f};
41 }
42 
~AudioFloatBlockPort()43 AudioFloatBlockPort::~AudioFloatBlockPort() {
44     delete[] mSampleBlock;
45 }
46 
47 /***************************************************************************/
pullData(int64_t framePosition,int32_t numFrames)48 int32_t AudioFloatOutputPort::pullData(int64_t framePosition, int32_t numFrames) {
49     numFrames = std::min(getFramesPerBlock(), numFrames);
50     return mParent.pullData(framePosition, numFrames);
51 }
52 
53 // These need to be in the .cpp file because of forward cross references.
connect(AudioFloatInputPort * port)54 void AudioFloatOutputPort::connect(AudioFloatInputPort *port) {
55     port->connect(this);
56 }
57 
disconnect(AudioFloatInputPort * port)58 void AudioFloatOutputPort::disconnect(AudioFloatInputPort *port) {
59     port->disconnect(this);
60 }
61 
62 /***************************************************************************/
pullData(int64_t framePosition,int32_t numFrames)63 int32_t AudioFloatInputPort::pullData(int64_t framePosition, int32_t numFrames) {
64     return (mConnected == NULL)
65             ? std::min(getFramesPerBlock(), numFrames)
66             : mConnected->pullData(framePosition, numFrames);
67 }
68 
getBlock()69 float *AudioFloatInputPort::getBlock() {
70     if (mConnected == NULL) {
71         return AudioFloatBlockPort::getBlock(); // loaded using setValue()
72     } else {
73         return mConnected->getBlock();
74     }
75 }
76 
77 /***************************************************************************/
pull(int32_t numFrames)78 int32_t AudioSink::pull(int32_t numFrames) {
79     int32_t actualFrames = input.pullData(mFramePosition, numFrames);
80     mFramePosition += actualFrames;
81     return actualFrames;
82 }