1 |
persson |
2055 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
|
* Copyright (C) 2005 - 2010 Christian Schoenebeck * |
7 |
|
|
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
21 |
|
|
* MA 02110-1301 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "EGADSR.h" |
25 |
|
|
|
26 |
|
|
#include "../../common/global_private.h" |
27 |
|
|
|
28 |
|
|
namespace LinuxSampler { namespace sfz { |
29 |
|
|
|
30 |
|
|
// Modeled after output from SFZ Player 1.97 / Rapture Demo 1.0. |
31 |
|
|
|
32 |
|
|
// TODO: implement hold stage correctly |
33 |
|
|
|
34 |
|
|
void EGADSR::update(event_t Event, uint SampleRate) { |
35 |
|
|
if (atEnd(Event)) return; |
36 |
|
|
|
37 |
|
|
switch (Stage) { |
38 |
|
|
case stage_attack: |
39 |
|
|
switch (Event) { |
40 |
|
|
case event_release: |
41 |
|
|
enterReleaseStage(); |
42 |
|
|
break; |
43 |
|
|
case event_stage_end: |
44 |
|
|
if (HoldAttack) |
45 |
|
|
enterAttackHoldStage(); |
46 |
|
|
else |
47 |
|
|
enterDecayStage(SampleRate); |
48 |
|
|
break; |
49 |
|
|
} |
50 |
|
|
break; |
51 |
|
|
case stage_attack_hold: |
52 |
|
|
switch (Event) { |
53 |
|
|
case event_stage_end: |
54 |
|
|
enterDecayStage(SampleRate); |
55 |
|
|
break; |
56 |
|
|
case event_release: |
57 |
|
|
enterReleaseStage(); |
58 |
|
|
break; |
59 |
|
|
} |
60 |
|
|
break; |
61 |
|
|
case stage_decay: |
62 |
|
|
switch (Event) { |
63 |
|
|
case event_release: |
64 |
|
|
enterReleaseStage(); |
65 |
|
|
break; |
66 |
|
|
case event_stage_end: |
67 |
|
|
if (Level < CONFIG_EG_BOTTOM) |
68 |
|
|
enterEndStage(); |
69 |
|
|
else |
70 |
|
|
enterSustainStage(); |
71 |
|
|
break; |
72 |
|
|
} |
73 |
|
|
break; |
74 |
|
|
case stage_sustain: |
75 |
|
|
switch (Event) { |
76 |
|
|
case event_stage_end: { // just refresh time |
77 |
|
|
const int intMax = (unsigned int) -1 >> 1; |
78 |
|
|
StepsLeft = intMax; // we use the highest possible value |
79 |
|
|
break; |
80 |
|
|
} |
81 |
|
|
case event_release: |
82 |
|
|
enterReleaseStage(); |
83 |
|
|
break; |
84 |
|
|
} |
85 |
|
|
break; |
86 |
|
|
case stage_release: |
87 |
|
|
switch (Event) { |
88 |
|
|
case event_stage_end: |
89 |
|
|
enterFadeOutStage(); |
90 |
|
|
break; |
91 |
|
|
case event_cancel_release: |
92 |
|
|
enterSustainStage(); |
93 |
|
|
break; |
94 |
|
|
} |
95 |
|
|
break; |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
// TODO: HoldAttack should be HoldTime and float |
100 |
|
|
|
101 |
|
|
void EGADSR::trigger(uint PreAttack, float AttackTime, bool HoldAttack, float DecayTime, uint SustainLevel, float ReleaseTime, uint SampleRate) { |
102 |
|
|
this->SustainLevel = SustainLevel / 1000.0; |
103 |
|
|
this->HoldAttack = HoldAttack; |
104 |
|
|
this->DecayTime = DecayTime; |
105 |
|
|
|
106 |
|
|
if (ReleaseTime < CONFIG_EG_MIN_RELEASE_TIME) ReleaseTime = CONFIG_EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback |
107 |
|
|
ReleaseSlope = -9.226 / (ReleaseTime * SampleRate); |
108 |
|
|
Offset = 0; |
109 |
|
|
enterFirstStage(); |
110 |
|
|
enterAttackStage(PreAttack, AttackTime, SampleRate); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
void EGADSR::enterAttackStage(const uint PreAttack, const float AttackTime, const uint SampleRate) { |
114 |
|
|
Stage = stage_attack; |
115 |
|
|
Segment = segment_lin; |
116 |
|
|
|
117 |
|
|
if (AttackTime >= 0.0005f) { |
118 |
|
|
StepsLeft = int(AttackTime * SampleRate); |
119 |
|
|
Level = (float) PreAttack / 1000.0; |
120 |
|
|
Coeff = (1.0f - Level) / StepsLeft; |
121 |
|
|
} else { // attack is zero - immediately jump to the next stage |
122 |
|
|
Level = 1.0f; |
123 |
|
|
if (HoldAttack) enterAttackHoldStage(); |
124 |
|
|
else enterDecayStage(SampleRate); |
125 |
|
|
} |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
void EGADSR::enterAttackHoldStage() { |
129 |
|
|
Stage = stage_attack_hold; |
130 |
|
|
Segment = segment_lin; |
131 |
|
|
Coeff = 0.0f; // don't rise anymore |
132 |
|
|
const int intMax = (unsigned int) -1 >> 1; |
133 |
|
|
StepsLeft = intMax; // we use the highest value possible (we refresh StepsLeft in update() in case) |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
void EGADSR::enterDecayStage(const uint SampleRate) { |
137 |
|
|
StepsLeft = (int) (DecayTime * SampleRate); |
138 |
|
|
if (StepsLeft && Level > SustainLevel) { |
139 |
|
|
Stage = stage_decay; |
140 |
|
|
Segment = segment_exp; |
141 |
|
|
const float slope = -9.226 / StepsLeft; |
142 |
|
|
Coeff = exp(slope); |
143 |
|
|
StepsLeft = int(log(SustainLevel / Level) / slope); |
144 |
|
|
if (StepsLeft > 0) return; |
145 |
|
|
|
146 |
|
|
enterSustainStage(); |
147 |
|
|
} else { |
148 |
|
|
enterSustainStage(); |
149 |
|
|
} |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
void EGADSR::enterSustainStage() { |
153 |
|
|
Stage = stage_sustain; |
154 |
|
|
Segment = segment_lin; |
155 |
|
|
Coeff = 0.0f; // don't change the envelope level in this stage |
156 |
|
|
const int intMax = (unsigned int) -1 >> 1; |
157 |
|
|
StepsLeft = intMax; // we use the highest value possible (we refresh StepsLeft in update() in case) |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
void EGADSR::enterReleaseStage() { |
161 |
|
|
Stage = stage_release; |
162 |
|
|
Segment = segment_exp; |
163 |
|
|
StepsLeft = int(log(CONFIG_EG_BOTTOM / Level) / ReleaseSlope); |
164 |
|
|
Coeff = exp(ReleaseSlope); |
165 |
|
|
if (StepsLeft <= 0) enterFadeOutStage(); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
}} // namespace LinuxSampler::sfz |