blob: 6f55ae432c1357c0801e482122e39d034e6936f5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
Copyright (C) 2010-2012 Florian Jung
This file is part of flo's FM synth.
flo's FM synth is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
flo's FM synth is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with flo's FM synth. If not, see <http://www.gnu.org/licenses/>.
*/
#include "envelope.h"
Envelope::Envelope(env_settings_t s, int frames)
{
level=0;
t=0;
state=ATTACK;
max=ONE;
nth_frame=frames;
set_ratefactor(1.0);
has_release_phase=(s.release>=0);
if (!has_release_phase)
s.hold=false;
set_attack(s.attack);
set_decay(s.decay);
set_sustain(s.sustain);
set_release(s.release);
set_hold(s.hold);
}
void Envelope::set_ratefactor(double factor)
{
ratefactor=ONE*factor;
set_attack(attack_orig);
set_decay(decay_orig);
set_release(release_orig);
}
void Envelope::set_attack(jack_nframes_t a)
{
attack_orig=a;
attack=(a*ratefactor >>SCALE)/nth_frame;
if (state==ATTACK)
t=attack*level >>SCALE;
}
void Envelope::set_decay(jack_nframes_t d)
{
decay_orig=d;
decay=(d*ratefactor >>SCALE)/nth_frame;
if ((state==DECAY) && (sustain!=ONE))
if (sustain<ONE) //to avoid a div. by zero
t=decay*(ONE-level)/(ONE-sustain);
}
void Envelope::set_sustain(fixed_t s)
{
sustain=s;
sustain_orig=s;
}
void Envelope::set_release(jack_nframes_t r)
{
release_orig=r;
release=(r*ratefactor >>SCALE)/nth_frame;
if (state==RELEASE)
if (sustain>0) //to avoid a div. by zero
t=release*(sustain-level)/sustain;
}
void Envelope::set_hold(bool h)
{
hold=h;
if ((h==false) && (state==HOLD))
{
t=0;
state=RELEASE;
}
}
void Envelope::reattack()
{
state=ATTACK;
t=attack*level >>SCALE;
sustain=sustain_orig;
}
void Envelope::reset()
{
state=ATTACK;
sustain=sustain_orig;
level=0;
t=0;
}
void Envelope::release_key()
{
if (has_release_phase)
if ((state!=RELEASE) && (state!=DONE))
{
t=0;
state=RELEASE;
sustain=level;
}
//if (!has_release_phase) ignore();
}
bool Envelope::still_active()
{
return (state!=DONE);
}
fixed_t Envelope::get_level() //must be called each frame
{
switch (state)
{
case ATTACK:
if (t>=attack)
{
level=max;
state=DECAY;
t=0;
}
else //will only happen, if t < attack. so attack will
{ //always be greater than zero -> no div. by zero
level=max * t / attack ;
}
break;
case DECAY:
if (t>=decay)
{
level=max*sustain >>SCALE;
if (has_release_phase)
if (hold)
state=HOLD;
else
state=RELEASE;
else
state=DONE;
t=0;
}
else //will only happen, if t < decay. so decay will
{ //always be greater than zero -> no div. by zero
level=(ONE - (ONE-sustain)*t/decay)*max >>SCALE;
}
break;
case HOLD:
//does nothing. level must be set properly before entering HOLD state
break;
case RELEASE:
if (t>=release)
{
level=0;
state=DONE;
}
else //will only happen, if t < release. so release will
{ //always be greater than zero -> no div. by zero
level=(sustain - sustain * t/release)*max >>SCALE;
}
break;
case DONE:
//does nothing. level must be set properly before entering DONE state
break;
}
++t;
return level;
}
|