1 /* 2 * Copyright 2019 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 "SampleRateConverter.h" 18 19 using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; 20 using namespace resampler; 21 SampleRateConverter(int32_t channelCount,MultiChannelResampler & resampler)22SampleRateConverter::SampleRateConverter(int32_t channelCount, MultiChannelResampler &resampler) 23 : FlowGraphFilter(channelCount) 24 , mResampler(resampler) { 25 setDataPulledAutomatically(false); 26 } 27 reset()28void SampleRateConverter::reset() { 29 FlowGraphNode::reset(); 30 mInputCursor = kInitialCallCount; 31 } 32 33 // Return true if there is a sample available. isInputAvailable()34bool SampleRateConverter::isInputAvailable() { 35 // If we have consumed all of the input data then go out and get some more. 36 if (mInputCursor >= mNumValidInputFrames) { 37 mInputCallCount++; 38 mNumValidInputFrames = input.pullData(mInputCallCount, input.getFramesPerBuffer()); 39 mInputCursor = 0; 40 } 41 return (mInputCursor < mNumValidInputFrames); 42 } 43 getNextInputFrame()44const float *SampleRateConverter::getNextInputFrame() { 45 const float *inputBuffer = input.getBuffer(); 46 return &inputBuffer[mInputCursor++ * input.getSamplesPerFrame()]; 47 } 48 onProcess(int32_t numFrames)49int32_t SampleRateConverter::onProcess(int32_t numFrames) { 50 float *outputBuffer = output.getBuffer(); 51 int32_t channelCount = output.getSamplesPerFrame(); 52 int framesLeft = numFrames; 53 while (framesLeft > 0) { 54 // Gather input samples as needed. 55 if(mResampler.isWriteNeeded()) { 56 if (isInputAvailable()) { 57 const float *frame = getNextInputFrame(); 58 mResampler.writeNextFrame(frame); 59 } else { 60 break; 61 } 62 } else { 63 // Output frame is interpolated from input samples. 64 mResampler.readNextFrame(outputBuffer); 65 outputBuffer += channelCount; 66 framesLeft--; 67 } 68 } 69 return numFrames - framesLeft; 70 } 71