Debian Codegraph @

lib-src/libnyquist/nyquist/nyqstk/src/Envelope.cpp

raw
1 /***************************************************/
2 /*! \class Envelope
3 \brief STK envelope base class.
4
5 This class implements a simple envelope
6 generator which is capable of ramping to
7 a target value by a specified \e rate.
8 It also responds to simple \e keyOn and
9 \e keyOff messages, ramping to 1.0 on
10 keyOn and to 0.0 on keyOff.
11
12 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
13 */
14 /***************************************************/
15
16 #include "Envelope.h"
17
18 using namespace Nyq;
19
20 Envelope :: Envelope(void) : Generator()
21 {
22 target_ = 0.0;
23 value_ = 0.0;
24 rate_ = 0.001;
25 state_ = 0;
26 }
27
28 Envelope :: Envelope ( const Envelope& e )
29 {
30 target_ = 0.0;
31 value_ = 0.0;
32 rate_ = 0.001;
33 state_ = 0;
34 }
35
36 Envelope :: ~Envelope(void)
37 {
38 }
39
40 Envelope& Envelope :: operator= ( const Envelope& e )
41 {
42 if ( this != &e ) {
43 target_ = e.target_;
44 value_ = e.value_;
45 rate_ = e.rate_;
46 state_ = e.state_;
47 }
48
49 return *this;
50 }
51
52 void Envelope :: keyOn(void)
53 {
54 target_ = 1.0;
55 if (value_ != target_) state_ = 1;
56 }
57
58 void Envelope :: keyOff(void)
59 {
60 target_ = 0.0;
61 if (value_ != target_) state_ = 1;
62 }
63
64 void Envelope :: setRate(StkFloat rate)
65 {
66 if (rate < 0.0) {
67 errorString_ << "Envelope::setRate: negative rates not allowed ... correcting!";
68 handleError( StkError::WARNING );
69 rate_ = -rate;
70 }
71 else
72 rate_ = rate;
73 }
74
75 void Envelope :: setTime(StkFloat time)
76 {
77 if (time < 0.0) {
78 errorString_ << "Envelope::setTime: negative times not allowed ... correcting!";
79 handleError( StkError::WARNING );
80 rate_ = 1.0 / (-time * Stk::sampleRate());
81 }
82 else
83 rate_ = 1.0 / (time * Stk::sampleRate());
84 }
85
86 void Envelope :: setTarget(StkFloat target)
87 {
88 target_ = target;
89 if (value_ != target_) state_ = 1;
90 }
91
92 void Envelope :: setValue(StkFloat value)
93 {
94 state_ = 0;
95 target_ = value;
96 value_ = value;
97 }
98
99 int Envelope :: getState(void) const
100 {
101 return state_;
102 }
103
104 StkFloat Envelope :: computeSample(void )
105 {
106 if (state_) {
107 if (target_ > value_) {
108 value_ += rate_;
109 if (value_ >= target_) {
110 value_ = target_;
111 state_ = 0;
112 }
113 }
114 else {
115 value_ -= rate_;
116 if (value_ <= target_) {
117 value_ = target_;
118 state_ = 0;
119 }
120 }
121 }
122
123 lastOutput_ = value_;
124 return value_;
125 }