Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 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 | |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 17 | #include "SampleRateConverter.h" |
| 18 | |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 19 | using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 20 | using namespace resampler; |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 21 | |
Phil Burk | 68b6ffa | 2019-06-04 14:58:41 -0700 | [diff] [blame] | 22 | SampleRateConverter::SampleRateConverter(int32_t channelCount, MultiChannelResampler &resampler) |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 23 | : FlowGraphFilter(channelCount) |
Phil Burk | 68b6ffa | 2019-06-04 14:58:41 -0700 | [diff] [blame] | 24 | , mResampler(resampler) { |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 25 | setDataPulledAutomatically(false); |
| 26 | } |
| 27 | |
Phil Burk | 7c3bff2 | 2020-08-24 17:29:21 -0700 | [diff] [blame] | 28 | void SampleRateConverter::reset() { |
| 29 | FlowGraphNode::reset(); |
| 30 | mInputCursor = kInitialCallCount; |
| 31 | } |
| 32 | |
Phil Burk | 68b6ffa | 2019-06-04 14:58:41 -0700 | [diff] [blame] | 33 | // Return true if there is a sample available. |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 34 | bool SampleRateConverter::isInputAvailable() { |
Phil Burk | 7c3bff2 | 2020-08-24 17:29:21 -0700 | [diff] [blame] | 35 | // If we have consumed all of the input data then go out and get some more. |
Phil Burk | 2da4a84 | 2019-08-16 12:15:23 -0700 | [diff] [blame] | 36 | if (mInputCursor >= mNumValidInputFrames) { |
Phil Burk | 7c3bff2 | 2020-08-24 17:29:21 -0700 | [diff] [blame] | 37 | mInputCallCount++; |
| 38 | mNumValidInputFrames = input.pullData(mInputCallCount, input.getFramesPerBuffer()); |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 39 | mInputCursor = 0; |
| 40 | } |
Phil Burk | 2da4a84 | 2019-08-16 12:15:23 -0700 | [diff] [blame] | 41 | return (mInputCursor < mNumValidInputFrames); |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const float *SampleRateConverter::getNextInputFrame() { |
| 45 | const float *inputBuffer = input.getBuffer(); |
| 46 | return &inputBuffer[mInputCursor++ * input.getSamplesPerFrame()]; |
| 47 | } |
| 48 | |
| 49 | int32_t SampleRateConverter::onProcess(int32_t numFrames) { |
| 50 | float *outputBuffer = output.getBuffer(); |
| 51 | int32_t channelCount = output.getSamplesPerFrame(); |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 52 | int framesLeft = numFrames; |
| 53 | while (framesLeft > 0) { |
| 54 | // Gather input samples as needed. |
Phil Burk | 9252733 | 2019-07-10 11:49:39 -0700 | [diff] [blame] | 55 | if(mResampler.isWriteNeeded()) { |
Phil Burk | 9b4e631 | 2019-06-29 12:47:45 -0700 | [diff] [blame] | 56 | if (isInputAvailable()) { |
| 57 | const float *frame = getNextInputFrame(); |
Phil Burk | 941fd5e | 2019-07-16 14:57:41 -0700 | [diff] [blame] | 58 | mResampler.writeNextFrame(frame); |
Phil Burk | 9b4e631 | 2019-06-29 12:47:45 -0700 | [diff] [blame] | 59 | } else { |
| 60 | break; |
| 61 | } |
| 62 | } else { |
Phil Burk | 941fd5e | 2019-07-16 14:57:41 -0700 | [diff] [blame] | 63 | // Output frame is interpolated from input samples. |
| 64 | mResampler.readNextFrame(outputBuffer); |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 65 | outputBuffer += channelCount; |
| 66 | framesLeft--; |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | return numFrames - framesLeft; |
| 70 | } |