blob: 33b2712c806bb26f290b21c03026833318a9481c [file] [log] [blame]
Phil Burk0817a2b2019-01-03 14:50:18 -08001/*
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 Burk328860f2019-11-04 18:43:40 -080019#include "FlowGraphNode.h"
Phil Burk0817a2b2019-01-03 14:50:18 -080020#include "RampLinear.h"
21
Phil Burk9425ffd2020-07-09 13:21:09 -070022using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
Phil Burk0817a2b2019-01-03 14:50:18 -080023
24RampLinear::RampLinear(int32_t channelCount)
Phil Burk328860f2019-11-04 18:43:40 -080025 : FlowGraphFilter(channelCount) {
Phil Burk0817a2b2019-01-03 14:50:18 -080026 mTarget.store(1.0f);
27}
28
29void RampLinear::setLengthInFrames(int32_t frames) {
30 mLengthInFrames = frames;
31}
32
33void RampLinear::setTarget(float target) {
34 mTarget.store(target);
35}
36
37float RampLinear::interpolateCurrent() {
38 return mLevelTo - (mRemaining * mScaler);
39}
40
Phil Burk22f21132019-03-08 12:43:19 -080041int32_t RampLinear::onProcess(int32_t numFrames) {
Phil Burk91362012019-03-04 12:00:30 -080042 const float *inputBuffer = input.getBuffer();
43 float *outputBuffer = output.getBuffer();
Phil Burk0817a2b2019-01-03 14:50:18 -080044 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 Burk22f21132019-03-08 12:43:19 -080055 int32_t framesLeft = numFrames;
Phil Burk0817a2b2019-01-03 14:50:18 -080056
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 Burk22f21132019-03-08 12:43:19 -080076 return numFrames;
Phil Burk0817a2b2019-01-03 14:50:18 -080077}