NAP
linearsmoothedvalue.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Nap includes
8 #include <nap/signalslot.h>
9 
10 namespace nap
11 {
12  namespace audio
13  {
14 
18  template<typename T>
20  {
21  public:
22 
23  public:
24  LinearSmoothedValue(const T& initValue, int stepCount) : mNewDestination(initValue), mValue(initValue), mDestination(initValue)
25  {
26  mStepCount = stepCount;
27  }
28 
34  void reset(const T& initValue)
35  {
36  mNewDestination = initValue;
37  mValue = initValue;
38  mDestination = initValue;
39  }
40 
44  void setStepCount(int stepCount) { mStepCount = stepCount; }
45 
50  void setValue(const T& destination) { mNewDestination = destination; }
51 
57  {
58  if (mNewDestination != mDestination)
59  {
60  mDestination = mNewDestination;
61  mStepCounter = mStepCount;
62  if (mStepCounter == 0)
63  mValue = mDestination;
64  else
65  mIncrement = (mDestination - mValue) / T(mStepCount);
66  }
67 
68  if (mStepCounter > 0)
69  {
70  mValue = mValue + mIncrement;
71  mStepCounter--;
72  if (mStepCounter == 0)
73  mValue = mDestination;
74  }
75 
76  return mValue;
77  }
78 
83  inline T getValue() const { return mValue; }
84 
85  inline T getDestination() const { return mNewDestination; }
86 
91  inline bool isRamping() const { return mStepCounter > 0 || mDestination != mNewDestination; }
92 
93  private:
94  T mNewDestination = 0;
95 
96  T mValue; // Value that is being controlled by this object.
97  T mIncrement; // Increment value per step of the current ramp when mode is linear.
98  T mDestination = 0; // Destination value of the current ramp.
99  int mStepCount = 0; // Number of steps in the ramp.
100  int mStepCounter = 0; // Current step index, 0 means at destination
101  };
102 
103 
104  }
105 }
106 
nap::audio::LinearSmoothedValue::setValue
void setValue(const T &destination)
Definition: linearsmoothedvalue.h:50
nap::audio::LinearSmoothedValue::isRamping
bool isRamping() const
Definition: linearsmoothedvalue.h:91
nap::audio::LinearSmoothedValue::reset
void reset(const T &initValue)
Definition: linearsmoothedvalue.h:34
nap::audio::LinearSmoothedValue::LinearSmoothedValue
LinearSmoothedValue(const T &initValue, int stepCount)
Definition: linearsmoothedvalue.h:24
nap::audio::LinearSmoothedValue::getValue
T getValue() const
Definition: linearsmoothedvalue.h:83
nap::audio::LinearSmoothedValue::getDestination
T getDestination() const
Definition: linearsmoothedvalue.h:85
nap::audio::LinearSmoothedValue::setStepCount
void setStepCount(int stepCount)
Definition: linearsmoothedvalue.h:44
nap
Definition: templateapp.h:17
nap::audio::LinearSmoothedValue
Definition: linearsmoothedvalue.h:19
nap::audio::LinearSmoothedValue::getNextValue
T getNextValue()
Definition: linearsmoothedvalue.h:56