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 "AudioProcessorBase.h"
25 
26 namespace flowgraph {
27 
28 class RampLinear : public AudioProcessorBase {
29 public:
30     explicit RampLinear(int32_t channelCount);
31 
32     virtual ~RampLinear() = default;
33 
34     int32_t onProcess(int64_t framePosition, int32_t numFrames) override;
35 
36     /**
37      * This is used for the next ramp.
38      * Calling this does not affect a ramp that is in progress.
39      */
40     void setLengthInFrames(int32_t frames);
41 
getLengthInFrames()42     int32_t getLengthInFrames() const {
43         return mLengthInFrames;
44     }
45 
46     /**
47      * This may be safely called by another thread.
48      * @param target
49      */
50     void setTarget(float target);
51 
getTarget()52     float getTarget() const {
53         return mTarget.load();
54     }
55 
56     /**
57      * Force the nextSegment to start from this level.
58      *
59      * WARNING: this can cause a discontinuity if called while the ramp is being used.
60      * Only call this when setting the initial ramp.
61      *
62      * @param level
63      */
forceCurrent(float level)64     void forceCurrent(float level) {
65         mLevelFrom = level;
66         mLevelTo = level;
67     }
68 
69     AudioFloatInputPort input;
70     AudioFloatOutputPort output;
71 
72 private:
73 
74     float interpolateCurrent();
75 
76     std::atomic<float>  mTarget;
77 
78     int32_t             mLengthInFrames  = 48000.0f / 100.0f ; // 10 msec at 48000 Hz;
79     int32_t             mRemaining       = 0;
80     float               mScaler          = 0.0f;
81     float               mLevelFrom       = 0.0f;
82     float               mLevelTo         = 0.0f;
83 };
84 
85 } /* namespace flowgraph */
86 
87 #endif //FLOWGRAPH_RAMP_LINEAR_H
88