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 #ifndef FLOWGRAPH_RAMP_LINEAR_H 18 #define FLOWGRAPH_RAMP_LINEAR_H 19 20 #include <atomic> 21 #include <unistd.h> 22 #include <sys/types.h> 23 24 #include "FlowGraphNode.h" 25 26 namespace FLOWGRAPH_OUTER_NAMESPACE { 27 namespace flowgraph { 28 29 /** 30 * When the target is modified then the output will ramp smoothly 31 * between the original and the new target value. 32 * This can be used to smooth out control values and reduce pops. 33 * 34 * The target may be updated while a ramp is in progress, which will trigger 35 * a new ramp from the current value. 36 */ 37 class RampLinear : public FlowGraphFilter { 38 public: 39 explicit RampLinear(int32_t channelCount); 40 41 virtual ~RampLinear() = default; 42 43 int32_t onProcess(int32_t numFrames) override; 44 45 /** 46 * This is used for the next ramp. 47 * Calling this does not affect a ramp that is in progress. 48 */ 49 void setLengthInFrames(int32_t frames); 50 getLengthInFrames()51 int32_t getLengthInFrames() const { 52 return mLengthInFrames; 53 } 54 55 /** 56 * This may be safely called by another thread. 57 * @param target 58 */ 59 void setTarget(float target); 60 getTarget()61 float getTarget() const { 62 return mTarget.load(); 63 } 64 65 /** 66 * Force the nextSegment to start from this level. 67 * 68 * WARNING: this can cause a discontinuity if called while the ramp is being used. 69 * Only call this when setting the initial ramp. 70 * 71 * @param level 72 */ forceCurrent(float level)73 void forceCurrent(float level) { 74 mLevelFrom = level; 75 mLevelTo = level; 76 } 77 getName()78 const char *getName() override { 79 return "RampLinear"; 80 } 81 82 private: 83 84 float interpolateCurrent(); 85 86 std::atomic<float> mTarget; 87 88 int32_t mLengthInFrames = 48000.0f / 100.0f ; // 10 msec at 48000 Hz; 89 int32_t mRemaining = 0; 90 float mScaler = 0.0f; 91 float mLevelFrom = 0.0f; 92 float mLevelTo = 0.0f; 93 }; 94 95 } /* namespace flowgraph */ 96 } /* namespace FLOWGRAPH_OUTER_NAMESPACE */ 97 98 #endif //FLOWGRAPH_RAMP_LINEAR_H 99