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