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), mStepCount(stepCount)
25  {
26  }
27 
33  void reset(const T& initValue)
34  {
35  mNewDestination.store(initValue);
36  mValue = initValue;
37  mDestination = initValue;
38  }
39 
43  void setStepCount(int stepCount) { mStepCount.store(stepCount); }
44 
49  void setValue(const T& destination) { mNewDestination.store(destination); }
50 
56  {
57  T newDestination = mNewDestination.load();
58  int stepCount = mStepCount.load();
59 
60  if (newDestination != mDestination)
61  {
62  mDestination = newDestination;
63  mStepCounter = stepCount;
64  if (mStepCounter == 0)
65  mValue = mDestination;
66  else
67  mIncrement = (mDestination - mValue) / T(stepCount);
68  }
69 
70  if (mStepCounter > 0)
71  {
72  mValue = mValue + mIncrement;
73  mStepCounter--;
74  if (mStepCounter == 0)
75  mValue = mDestination;
76  }
77 
78  return mValue;
79  }
80 
85  inline T getValue() const { return mValue; }
86 
87  inline T getDestination() const { return mNewDestination.load(); }
88 
93  inline bool isRamping() const { return mStepCounter > 0 || mDestination != mNewDestination.load(); }
94 
95  private:
96  std::atomic<T> mNewDestination = 0;
97 
98  T mValue; // Value that is being controlled by this object.
99  T mIncrement; // Increment value per step of the current ramp when mode is linear.
100  T mDestination = 0; // Destination value of the current ramp.
101  std::atomic<int> mStepCount = 0; // Number of steps in the ramp.
102  int mStepCounter = 0; // Current step index, 0 means at destination
103  };
104 
105 
106  }
107 }
108 
nap::audio::LinearSmoothedValue::setValue
void setValue(const T &destination)
Definition: linearsmoothedvalue.h:49
nap::audio::LinearSmoothedValue::isRamping
bool isRamping() const
Definition: linearsmoothedvalue.h:93
nap::audio::LinearSmoothedValue::reset
void reset(const T &initValue)
Definition: linearsmoothedvalue.h:33
nap::audio::LinearSmoothedValue::LinearSmoothedValue
LinearSmoothedValue(const T &initValue, int stepCount)
Definition: linearsmoothedvalue.h:24
nap::audio::LinearSmoothedValue::getValue
T getValue() const
Definition: linearsmoothedvalue.h:85
nap::audio::LinearSmoothedValue::getDestination
T getDestination() const
Definition: linearsmoothedvalue.h:87
nap::audio::LinearSmoothedValue::setStepCount
void setStepCount(int stepCount)
Definition: linearsmoothedvalue.h:43
nap
Definition: templateapp.h:17
nap::audio::LinearSmoothedValue
Definition: linearsmoothedvalue.h:19
nap::audio::LinearSmoothedValue::getNextValue
T getNextValue()
Definition: linearsmoothedvalue.h:55