NAP
rampedvalue.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 // Std includes
8 #include <atomic>
9 #include <iostream>
10 #include <assert.h>
11 
12 // Nap includes
13 #include <nap/signalslot.h>
14 #include <nap/logger.h>
15 
16 // Audo includes
17 #include <audio/utility/audiotypes.h>
18 #include <audio/utility/dirtyflag.h>
19 #include <cmath>
20 
21 namespace nap
22 {
23  namespace audio
24  {
25 
30  template<typename T>
32  {
33  public:
34  inline static constexpr T smallestFactor = 0.0001f;
35 
36  public:
37  RampedValue(const T& initValue) : mValue(initValue) { }
38 
39 
43  void setValue(const T& value)
44  {
45  stop();
46  mValue = value;
47  }
48 
55  void ramp(const T& destination, int stepCount, RampMode mode = RampMode::Linear)
56  {
57  assert(stepCount >= 0);
58 
59  mDestination = destination;
60  mStepCount = stepCount;
61  mRampMode = mode;
62 
63  updateRamp();
64  }
65 
69  void stop() { mStepCounter = 0; }
70 
76  {
77  if (mStepCounter > 0) {
78  switch (mRampMode) {
79  case RampMode::Linear:
80  mValue = mValue + mIncrement;
81  break;
82  case RampMode::Exponential:
83  mValue = mValue * mFactor;
84  break;
85  }
86  mStepCounter--;
87  if (mStepCounter == 0) {
88  if (mRampMode == RampMode::Exponential && mDestinationZero)
89  mValue = 0;
90  else
91  mValue = mDestination;
93  }
94  }
95 
96  return mValue;
97  }
98 
103  T getValue() const { return mValue; }
104 
109  bool isRamping() const { return mStepCounter > 0; }
110 
115 
116  private:
117  void updateRamp()
118  {
119  // if there are zero steps we reach the destination of the ramp immediately
120  if (mStepCount <= 0)
121  {
122  mStepCounter = 0;
123  mValue = mDestination;
124  destinationReachedSignal(mValue);
125  return;
126  }
127 
128  mStepCounter = mStepCount;
129 
130  switch (mRampMode)
131  {
132  case RampMode::Linear:
133  mIncrement = (mDestination - mValue) / T(mStepCount);
134  break;
135 
136  case RampMode::Exponential:
137  // avoid divisions by zero by avoiding mValue = 0
138  if (mValue == 0)
139  mValue = mDestination *
140  smallestFactor; // this is a 140dB ramp up from mValue to mDestination
141 
142  // avoid divisions by zero by avoiding mDestination = 0
143  if (mDestination == 0)
144  {
145  mDestination =
146  mValue * smallestFactor; // this is a 140 dB ramp down from mValue to mDestination
147  mDestinationZero = true;
148  }
149  else
150  mDestinationZero = false;
151 
152  // calculate the increment factor
153  mFactor = pow(double(mDestination / mValue), double(1.0 / mStepCount));
154  break;
155  }
156  }
157 
158  private:
159  T mValue; // Value that is being controlled by this object.
160 
161  union
162  {
163  T mIncrement; // Increment value per step of the current ramp when mode is linear.
164  T mFactor; // Factor value per step of the current ramp when mode is exponential.
165  };
166  T mDestination = 0; // Destination value of the current ramp.
167  int mStepCount = 0; // Number of steps in the ramp.
168  int mStepCounter = {0}; // Current step index, 0 means at destination
169  RampMode mRampMode = {RampMode::Linear}; // The mode of the current ramp
170  bool mDestinationZero = false; // In case of a linear ramp this indicates wether the destination value needs to be rounded to zero.
171  };
172 
173 
174  }
175 }
nap::audio::RampMode
RampMode
Definition: audiotypes.h:131
nap::audio::RampedValue::mIncrement
T mIncrement
Definition: rampedvalue.h:163
nap::audio::RampedValue::getValue
T getValue() const
Definition: rampedvalue.h:103
nap::audio::RampedValue::stop
void stop()
Definition: rampedvalue.h:69
nap::audio::RampedValue::destinationReachedSignal
nap::Signal< T > destinationReachedSignal
Definition: rampedvalue.h:114
nap::Signal< T >
nap::audio::RampedValue::smallestFactor
static constexpr T smallestFactor
Definition: rampedvalue.h:34
nap::audio::RampedValue::isRamping
bool isRamping() const
Definition: rampedvalue.h:109
nap::audio::RampedValue::RampedValue
RampedValue(const T &initValue)
Definition: rampedvalue.h:37
nap
Definition: templateapp.h:17
nap::audio::RampedValue::ramp
void ramp(const T &destination, int stepCount, RampMode mode=RampMode::Linear)
Definition: rampedvalue.h:55
nap::audio::RampedValue::mFactor
T mFactor
Definition: rampedvalue.h:164
nap::audio::RampedValue::getNextValue
T getNextValue()
Definition: rampedvalue.h:75
nap::audio::RampedValue
Definition: rampedvalue.h:31
nap::audio::RampedValue::setValue
void setValue(const T &value)
Definition: rampedvalue.h:43