blob: b1ae4bd82bc68c466c2d416bf61347aaf5ccdd91 [file] [log] [blame]
Phil Burkdd3971d2019-05-17 18:42:58 -07001/*
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
Phil Burkdd3971d2019-05-17 18:42:58 -070017#include "SampleRateConverter.h"
18
Phil Burk9425ffd2020-07-09 13:21:09 -070019using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
Phil Burk0a784e62019-07-20 12:43:15 -070020using namespace resampler;
Phil Burkdd3971d2019-05-17 18:42:58 -070021
Phil Burk68b6ffa2019-06-04 14:58:41 -070022SampleRateConverter::SampleRateConverter(int32_t channelCount, MultiChannelResampler &resampler)
Phil Burk328860f2019-11-04 18:43:40 -080023 : FlowGraphFilter(channelCount)
Phil Burk68b6ffa2019-06-04 14:58:41 -070024 , mResampler(resampler) {
Phil Burkdd3971d2019-05-17 18:42:58 -070025 setDataPulledAutomatically(false);
26}
27
Phil Burk7c3bff22020-08-24 17:29:21 -070028void SampleRateConverter::reset() {
29 FlowGraphNode::reset();
30 mInputCursor = kInitialCallCount;
31}
32
Phil Burk68b6ffa2019-06-04 14:58:41 -070033// Return true if there is a sample available.
Phil Burkf67a97f2019-05-30 12:48:17 -070034bool SampleRateConverter::isInputAvailable() {
Phil Burk7c3bff22020-08-24 17:29:21 -070035 // If we have consumed all of the input data then go out and get some more.
Phil Burk2da4a842019-08-16 12:15:23 -070036 if (mInputCursor >= mNumValidInputFrames) {
Phil Burk7c3bff22020-08-24 17:29:21 -070037 mInputCallCount++;
38 mNumValidInputFrames = input.pullData(mInputCallCount, input.getFramesPerBuffer());
Phil Burkdd3971d2019-05-17 18:42:58 -070039 mInputCursor = 0;
40 }
Phil Burk2da4a842019-08-16 12:15:23 -070041 return (mInputCursor < mNumValidInputFrames);
Phil Burkdd3971d2019-05-17 18:42:58 -070042}
43
44const float *SampleRateConverter::getNextInputFrame() {
45 const float *inputBuffer = input.getBuffer();
46 return &inputBuffer[mInputCursor++ * input.getSamplesPerFrame()];
47}
48
49int32_t SampleRateConverter::onProcess(int32_t numFrames) {
50 float *outputBuffer = output.getBuffer();
51 int32_t channelCount = output.getSamplesPerFrame();
Phil Burkdd3971d2019-05-17 18:42:58 -070052 int framesLeft = numFrames;
53 while (framesLeft > 0) {
54 // Gather input samples as needed.
Phil Burk92527332019-07-10 11:49:39 -070055 if(mResampler.isWriteNeeded()) {
Phil Burk9b4e6312019-06-29 12:47:45 -070056 if (isInputAvailable()) {
57 const float *frame = getNextInputFrame();
Phil Burk941fd5e2019-07-16 14:57:41 -070058 mResampler.writeNextFrame(frame);
Phil Burk9b4e6312019-06-29 12:47:45 -070059 } else {
60 break;
61 }
62 } else {
Phil Burk941fd5e2019-07-16 14:57:41 -070063 // Output frame is interpolated from input samples.
64 mResampler.readNextFrame(outputBuffer);
Phil Burkdd3971d2019-05-17 18:42:58 -070065 outputBuffer += channelCount;
66 framesLeft--;
Phil Burkdd3971d2019-05-17 18:42:58 -070067 }
68 }
69 return numFrames - framesLeft;
70}