blob: 55d8310e720e2a80758f0ce52688f4002563d3e0 [file] [log] [blame]
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SampleRateConverter.h"
using namespace flowgraph;
SampleRateConverter::SampleRateConverter(int32_t channelCount, MultiChannelResampler &resampler)
: AudioFilter(channelCount)
, mResampler(resampler) {
setDataPulledAutomatically(false);
}
// Return true if there is a sample available.
bool SampleRateConverter::isInputAvailable() {
if (mInputCursor >= mInputValid) {
mInputValid = input.pullData(mInputFramePosition, input.getFramesPerBuffer());
mInputFramePosition += mInputValid;
mInputCursor = 0;
}
return (mInputCursor < mInputValid);
}
const float *SampleRateConverter::getNextInputFrame() {
const float *inputBuffer = input.getBuffer();
return &inputBuffer[mInputCursor++ * input.getSamplesPerFrame()];
}
int32_t SampleRateConverter::onProcess(int32_t numFrames) {
float *outputBuffer = output.getBuffer();
int32_t channelCount = output.getSamplesPerFrame();
int framesLeft = numFrames;
while (framesLeft > 0) {
// Gather input samples as needed.
if(mResampler.isWriteReady()) {
if (isInputAvailable()) {
const float *frame = getNextInputFrame();
mResampler.writeFrame(frame);
mResampler.advanceWrite();
} else {
break;
}
} else {
// Output frame is interpolated from input samples based on phase.
mResampler.readFrame(outputBuffer);
mResampler.advanceRead();
outputBuffer += channelCount;
framesLeft--;
}
}
return numFrames - framesLeft;
}