1 #ifndef STK_ASYMP_H
2 #define STK_ASYMP_H
3
4 #include "Generator.h"
5
6 namespace stk {
7
8 /***************************************************/
9 /*! \class Asymp
10 \brief STK asymptotic curve envelope class
11
12 This class implements a simple envelope generator
13 which asymptotically approaches a target value.
14 The algorithm used is of the form:
15
16 y[n] = a y[n-1] + (1-a) target,
17
18 where a = exp(-T/tau), T is the sample period, and
19 tau is a time constant. The user can set the time
20 constant (default value = 0.3) and target value.
21 Theoretically, this recursion never reaches its
22 target, though the calculations in this class are
23 stopped when the current value gets within a small
24 threshold value of the target (at which time the
25 current value is set to the target). It responds
26 to \e keyOn and \e keyOff messages by ramping to
27 1.0 on keyOn and to 0.0 on keyOff.
28
29 by Perry R. Cook and Gary P. Scavone, 1995--2021.
30 */
31 /***************************************************/
32
33 const StkFloat TARGET_THRESHOLD = 0.000001;
34
35 class Asymp : public Generator
36 {
37 public:
38
39 //! Default constructor.
40 Asymp( void );
41
42 //! Class destructor.
43 ~Asymp( void );
44
45 //! Set target = 1.
46 void keyOn( void );
47
48 //! Set target = 0.
49 void keyOff( void );
50
51 //! Set the asymptotic rate via the time factor \e tau (must be > 0).
52 /*!
53 The rate is computed as described above. The value of \e tau
54 must be greater than zero. Values of \e tau close to zero produce
55 fast approach rates, while values greater than 1.0 produce rather
56 slow rates.
57 */
58 void setTau( StkFloat tau );
59
60 //! Set the asymptotic rate based on a time duration (must be > 0).
61 void setTime( StkFloat time );
62
63 //! Set the asymptotic rate such that the target value is perceptually reached (to within -60dB of the target) in \e t60 seconds.
64 void setT60( StkFloat t60 );
65
66 //! Set the target value.
67 void setTarget( StkFloat target );
68
69 //! Set current and target values to \e value.
70 void setValue( StkFloat value );
71
72 //! Return the current envelope \e state (0 = at target, 1 otherwise).
getState(void)73 int getState( void ) const { return state_; };
74
75 //! Return the last computed output value.
lastOut(void)76 StkFloat lastOut( void ) const { return lastFrame_[0]; };
77
78 //! Compute and return one output sample.
79 StkFloat tick( void );
80
81 //! Fill a channel of the StkFrames object with computed outputs.
82 /*!
83 The \c channel argument must be less than the number of
84 channels in the StkFrames argument (the first channel is specified
85 by 0). However, range checking is only performed if _STK_DEBUG_
86 is defined during compilation, in which case an out-of-range value
87 will trigger an StkError exception.
88 */
89 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
90
91 protected:
92
93 void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
94
95 StkFloat value_;
96 StkFloat target_;
97 StkFloat factor_;
98 StkFloat constant_;
99 int state_;
100 };
101
tick(void)102 inline StkFloat Asymp :: tick( void )
103 {
104 if ( state_ ) {
105
106 value_ = factor_ * value_ + constant_;
107
108 // Check threshold.
109 if ( target_ > value_ ) {
110 if ( target_ - value_ <= TARGET_THRESHOLD ) {
111 value_ = target_;
112 state_ = 0;
113 }
114 }
115 else {
116 if ( value_ - target_ <= TARGET_THRESHOLD ) {
117 value_ = target_;
118 state_ = 0;
119 }
120 }
121 lastFrame_[0] = value_;
122 }
123
124 return value_;
125 }
126
tick(StkFrames & frames,unsigned int channel)127 inline StkFrames& Asymp :: tick( StkFrames& frames, unsigned int channel )
128 {
129 #if defined(_STK_DEBUG_)
130 if ( channel >= frames.channels() ) {
131 oStream_ << "Asymp::tick(): channel and StkFrames arguments are incompatible!";
132 handleError( StkError::FUNCTION_ARGUMENT );
133 }
134 #endif
135
136 StkFloat *samples = &frames[channel];
137 unsigned int hop = frames.channels();
138 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
139 *samples = Asymp::tick();
140
141 return frames;
142 }
143
144 } // stk namespace
145
146 #endif
147