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 #define LOG_TAG "RampLinear"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <algorithm>
22 #include <unistd.h>
23 #include "AudioProcessorBase.h"
24 #include "RampLinear.h"
25
26 using namespace flowgraph;
27
RampLinear(int32_t channelCount)28 RampLinear::RampLinear(int32_t channelCount)
29 : input(*this, channelCount)
30 , output(*this, channelCount) {
31 mTarget.store(1.0f);
32 }
33
setLengthInFrames(int32_t frames)34 void RampLinear::setLengthInFrames(int32_t frames) {
35 mLengthInFrames = frames;
36 }
37
setTarget(float target)38 void RampLinear::setTarget(float target) {
39 mTarget.store(target);
40 }
41
interpolateCurrent()42 float RampLinear::interpolateCurrent() {
43 return mLevelTo - (mRemaining * mScaler);
44 }
45
onProcess(int64_t framePosition,int32_t numFrames)46 int32_t RampLinear::onProcess(int64_t framePosition, int32_t numFrames) {
47 int32_t framesToProcess = input.pullData(framePosition, numFrames);
48 const float *inputBuffer = input.getBlock();
49 float *outputBuffer = output.getBlock();
50 int32_t channelCount = output.getSamplesPerFrame();
51
52 float target = getTarget();
53 if (target != mLevelTo) {
54 // Start new ramp. Continue from previous level.
55 mLevelFrom = interpolateCurrent();
56 mLevelTo = target;
57 mRemaining = mLengthInFrames;
58 ALOGV("%s() mLevelFrom = %f, mLevelTo = %f, mRemaining = %d, mScaler = %f",
59 __func__, mLevelFrom, mLevelTo, mRemaining, mScaler);
60 mScaler = (mLevelTo - mLevelFrom) / mLengthInFrames; // for interpolation
61 }
62
63 int32_t framesLeft = framesToProcess;
64
65 if (mRemaining > 0) { // Ramping? This doesn't happen very often.
66 int32_t framesToRamp = std::min(framesLeft, mRemaining);
67 framesLeft -= framesToRamp;
68 while (framesToRamp > 0) {
69 float currentLevel = interpolateCurrent();
70 for (int ch = 0; ch < channelCount; ch++) {
71 *outputBuffer++ = *inputBuffer++ * currentLevel;
72 }
73 mRemaining--;
74 framesToRamp--;
75 }
76 }
77
78 // Process any frames after the ramp.
79 int32_t samplesLeft = framesLeft * channelCount;
80 for (int i = 0; i < samplesLeft; i++) {
81 *outputBuffer++ = *inputBuffer++ * mLevelTo;
82 }
83
84 return framesToProcess;
85 }
86