f@0
|
1 #ifndef STK_ENVELOPE_H
|
f@0
|
2 #define STK_ENVELOPE_H
|
f@0
|
3
|
f@0
|
4 #include "Generator.h"
|
f@0
|
5
|
f@0
|
6 namespace stk {
|
f@0
|
7
|
f@0
|
8 /***************************************************/
|
f@0
|
9 /*! \class Envelope
|
f@0
|
10 \brief STK linear line envelope class.
|
f@0
|
11
|
f@0
|
12 This class implements a simple linear line envelope generator
|
f@0
|
13 which is capable of ramping to an arbitrary target value by a
|
f@0
|
14 specified \e rate. It also responds to simple \e keyOn and \e
|
f@0
|
15 keyOff messages, ramping to 1.0 on keyOn and to 0.0 on keyOff.
|
f@0
|
16
|
f@0
|
17 by Perry R. Cook and Gary P. Scavone, 1995--2014.
|
f@0
|
18 */
|
f@0
|
19 /***************************************************/
|
f@0
|
20
|
f@0
|
21 class Envelope : public Generator
|
f@0
|
22 {
|
f@0
|
23 public:
|
f@0
|
24
|
f@0
|
25 //! Default constructor.
|
f@0
|
26 Envelope( void );
|
f@0
|
27
|
f@0
|
28 //! Class destructor.
|
f@0
|
29 ~Envelope( void );
|
f@0
|
30
|
f@0
|
31 //! Assignment operator.
|
f@0
|
32 Envelope& operator= ( const Envelope& e );
|
f@0
|
33
|
f@0
|
34 //! Set target = 1.
|
f@0
|
35 void keyOn( void ) { this->setTarget( 1.0 ); };
|
f@0
|
36
|
f@0
|
37 //! Set target = 0.
|
f@0
|
38 void keyOff( void ) { this->setTarget( 0.0 ); };
|
f@0
|
39
|
f@0
|
40 //! Set the \e rate.
|
f@0
|
41 /*!
|
f@0
|
42 The \e rate must be positive (though a value of 0.0 is allowed).
|
f@0
|
43 */
|
f@0
|
44 void setRate( StkFloat rate );
|
f@0
|
45
|
f@0
|
46 //! Set the \e rate based on a positive time duration (seconds).
|
f@0
|
47 /*!
|
f@0
|
48 The \e rate is calculated such that the envelope will ramp from
|
f@0
|
49 a value of 0.0 to 1.0 in the specified time duration.
|
f@0
|
50 */
|
f@0
|
51 void setTime( StkFloat time );
|
f@0
|
52
|
f@0
|
53 //! Set the target value.
|
f@0
|
54 void setTarget( StkFloat target );
|
f@0
|
55
|
f@0
|
56 //! Set current and target values to \e value.
|
f@0
|
57 void setValue( StkFloat value );
|
f@0
|
58
|
f@0
|
59 //! Return the current envelope \e state (0 = at target, 1 otherwise).
|
f@0
|
60 int getState( void ) const { return state_; };
|
f@0
|
61
|
f@0
|
62 //! Return the last computed output value.
|
f@0
|
63 StkFloat lastOut( void ) const { return lastFrame_[0]; };
|
f@0
|
64
|
f@0
|
65 //! Compute and return one output sample.
|
f@0
|
66 StkFloat tick( void );
|
f@0
|
67
|
f@0
|
68 //! Fill a channel of the StkFrames object with computed outputs.
|
f@0
|
69 /*!
|
f@0
|
70 The \c channel argument must be less than the number of
|
f@0
|
71 channels in the StkFrames argument (the first channel is specified
|
f@0
|
72 by 0). However, range checking is only performed if _STK_DEBUG_
|
f@0
|
73 is defined during compilation, in which case an out-of-range value
|
f@0
|
74 will trigger an StkError exception.
|
f@0
|
75 */
|
f@0
|
76 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
|
f@0
|
77
|
f@0
|
78 protected:
|
f@0
|
79
|
f@0
|
80 void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
|
f@0
|
81
|
f@0
|
82 StkFloat value_;
|
f@0
|
83 StkFloat target_;
|
f@0
|
84 StkFloat rate_;
|
f@0
|
85 int state_;
|
f@0
|
86 };
|
f@0
|
87
|
f@0
|
88 inline StkFloat Envelope :: tick( void )
|
f@0
|
89 {
|
f@0
|
90 if ( state_ ) {
|
f@0
|
91 if ( target_ > value_ ) {
|
f@0
|
92 value_ += rate_;
|
f@0
|
93 if ( value_ >= target_ ) {
|
f@0
|
94 value_ = target_;
|
f@0
|
95 state_ = 0;
|
f@0
|
96 }
|
f@0
|
97 }
|
f@0
|
98 else {
|
f@0
|
99 value_ -= rate_;
|
f@0
|
100 if ( value_ <= target_ ) {
|
f@0
|
101 value_ = target_;
|
f@0
|
102 state_ = 0;
|
f@0
|
103 }
|
f@0
|
104 }
|
f@0
|
105 lastFrame_[0] = value_;
|
f@0
|
106 }
|
f@0
|
107
|
f@0
|
108 return value_;
|
f@0
|
109 }
|
f@0
|
110
|
f@0
|
111 inline StkFrames& Envelope :: tick( StkFrames& frames, unsigned int channel )
|
f@0
|
112 {
|
f@0
|
113 #if defined(_STK_DEBUG_)
|
f@0
|
114 if ( channel >= frames.channels() ) {
|
f@0
|
115 oStream_ << "Envelope::tick(): channel and StkFrames arguments are incompatible!";
|
f@0
|
116 handleError( StkError::FUNCTION_ARGUMENT );
|
f@0
|
117 }
|
f@0
|
118 #endif
|
f@0
|
119
|
f@0
|
120 StkFloat *samples = &frames[channel];
|
f@0
|
121 unsigned int hop = frames.channels();
|
f@0
|
122 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
|
f@0
|
123 *samples = tick();
|
f@0
|
124
|
f@0
|
125 return frames;
|
f@0
|
126 }
|
f@0
|
127
|
f@0
|
128 } // stk namespace
|
f@0
|
129
|
f@0
|
130 #endif
|