Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 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 <unistd.h> |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 19 | #include "FlowGraphNode.h" |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 20 | #include "RampLinear.h" |
| 21 | |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 22 | using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 23 | |
| 24 | RampLinear::RampLinear(int32_t channelCount) |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 25 | : FlowGraphFilter(channelCount) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 26 | mTarget.store(1.0f); |
| 27 | } |
| 28 | |
| 29 | void RampLinear::setLengthInFrames(int32_t frames) { |
| 30 | mLengthInFrames = frames; |
| 31 | } |
| 32 | |
| 33 | void RampLinear::setTarget(float target) { |
| 34 | mTarget.store(target); |
| 35 | } |
| 36 | |
| 37 | float RampLinear::interpolateCurrent() { |
| 38 | return mLevelTo - (mRemaining * mScaler); |
| 39 | } |
| 40 | |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 41 | int32_t RampLinear::onProcess(int32_t numFrames) { |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 42 | const float *inputBuffer = input.getBuffer(); |
| 43 | float *outputBuffer = output.getBuffer(); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 44 | int32_t channelCount = output.getSamplesPerFrame(); |
| 45 | |
| 46 | float target = getTarget(); |
| 47 | if (target != mLevelTo) { |
| 48 | // Start new ramp. Continue from previous level. |
| 49 | mLevelFrom = interpolateCurrent(); |
| 50 | mLevelTo = target; |
| 51 | mRemaining = mLengthInFrames; |
| 52 | mScaler = (mLevelTo - mLevelFrom) / mLengthInFrames; // for interpolation |
| 53 | } |
| 54 | |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 55 | int32_t framesLeft = numFrames; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 56 | |
| 57 | if (mRemaining > 0) { // Ramping? This doesn't happen very often. |
| 58 | int32_t framesToRamp = std::min(framesLeft, mRemaining); |
| 59 | framesLeft -= framesToRamp; |
| 60 | while (framesToRamp > 0) { |
| 61 | float currentLevel = interpolateCurrent(); |
| 62 | for (int ch = 0; ch < channelCount; ch++) { |
| 63 | *outputBuffer++ = *inputBuffer++ * currentLevel; |
| 64 | } |
| 65 | mRemaining--; |
| 66 | framesToRamp--; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Process any frames after the ramp. |
| 71 | int32_t samplesLeft = framesLeft * channelCount; |
| 72 | for (int i = 0; i < samplesLeft; i++) { |
| 73 | *outputBuffer++ = *inputBuffer++ * mLevelTo; |
| 74 | } |
| 75 | |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 76 | return numFrames; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 77 | } |