-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSFZEG.cpp
More file actions
executable file
·215 lines (193 loc) · 5.12 KB
/
SFZEG.cpp
File metadata and controls
executable file
·215 lines (193 loc) · 5.12 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*************************************************************************************
* Original code copyright (C) 2012 Steve Folta
* Converted to Juce module (C) 2016 Leo Olivers
* Forked from https://github.com/stevefolta/SFZero
* For license info please see the LICENSE file distributed with this source code
*************************************************************************************/
#include "SFZEG.h"
static const float fastReleaseTime = 0.01f;
sfzero::EG::EG()
: segment_(), sampleRate_(0), exponentialDecay_(false), level_(0), slope_(0), samplesUntilNextSegment_(0), segmentIsExponential_(false)
{
}
void sfzero::EG::setExponentialDecay(bool newExponentialDecay) { exponentialDecay_ = newExponentialDecay; }
void sfzero::EG::startNote(const EGParameters *newParameters, float floatVelocity, double newSampleRate,
const EGParameters *velMod)
{
parameters_ = *newParameters;
if (velMod)
{
parameters_.delay += floatVelocity * velMod->delay;
parameters_.attack += floatVelocity * velMod->attack;
parameters_.hold += floatVelocity * velMod->hold;
parameters_.decay += floatVelocity * velMod->decay;
parameters_.sustain += floatVelocity * velMod->sustain;
if (parameters_.sustain < 0.0)
{
parameters_.sustain = 0.0;
}
else if (parameters_.sustain > 100.0)
{
parameters_.sustain = 100.0;
}
parameters_.release += floatVelocity * velMod->release;
}
sampleRate_ = newSampleRate;
startDelay();
}
void sfzero::EG::nextSegment()
{
switch (segment_)
{
case Delay:
startAttack();
break;
case Attack:
startHold();
break;
case Hold:
startDecay();
break;
case Decay:
startSustain();
break;
case Sustain:
// Shouldn't be called.
break;
case Release:
default:
segment_ = Done;
break;
}
}
void sfzero::EG::noteOff() { startRelease(); }
void sfzero::EG::fastRelease()
{
segment_ = Release;
samplesUntilNextSegment_ = static_cast<int>(fastReleaseTime * sampleRate_);
slope_ = -level_ / samplesUntilNextSegment_;
segmentIsExponential_ = false;
}
void sfzero::EG::startDelay()
{
if (parameters_.delay <= 0)
{
startAttack();
}
else
{
segment_ = Delay;
level_ = 0.0;
slope_ = 0.0;
samplesUntilNextSegment_ = static_cast<int>(parameters_.delay * sampleRate_);
segmentIsExponential_ = false;
}
}
void sfzero::EG::startAttack()
{
if (parameters_.attack <= 0)
{
startHold();
}
else
{
segment_ = Attack;
level_ = parameters_.start / 100.0f;
samplesUntilNextSegment_ = static_cast<int>(parameters_.attack * sampleRate_);
slope_ = 1.0f / samplesUntilNextSegment_;
segmentIsExponential_ = false;
}
}
void sfzero::EG::startHold()
{
if (parameters_.hold <= 0)
{
level_ = 1.0;
startDecay();
}
else
{
segment_ = Hold;
samplesUntilNextSegment_ = static_cast<int>(parameters_.hold * sampleRate_);
level_ = 1.0;
slope_ = 0.0;
segmentIsExponential_ = false;
}
}
void sfzero::EG::startDecay()
{
if (parameters_.decay <= 0)
{
startSustain();
}
else
{
segment_ = Decay;
samplesUntilNextSegment_ = static_cast<int>(parameters_.decay * sampleRate_);
level_ = 1.0;
if (exponentialDecay_)
{
// I don't truly understand this; just following what LinuxSampler does.
float mysterySlope = -9.226f / samplesUntilNextSegment_;
slope_ = exp(mysterySlope);
segmentIsExponential_ = true;
if (parameters_.sustain > 0.0)
{
// Again, this is following LinuxSampler's example, which is similar to
// SF2-style decay, where "decay" specifies the time it would take to
// get to zero, not to the sustain level. The SFZ spec is not that
// specific about what "decay" means, so perhaps it's really supposed
// to specify the time to reach the sustain level.
samplesUntilNextSegment_ = static_cast<int>(log((parameters_.sustain / 100.0) / level_) / mysterySlope);
if (samplesUntilNextSegment_ <= 0)
{
startSustain();
}
}
}
else
{
slope_ = (parameters_.sustain / 100.0f - 1.0f) / samplesUntilNextSegment_;
segmentIsExponential_ = false;
}
}
}
void sfzero::EG::startSustain()
{
if (parameters_.sustain <= 0)
{
startRelease();
}
else
{
segment_ = Sustain;
level_ = parameters_.sustain / 100.0f;
slope_ = 0.0;
samplesUntilNextSegment_ = 0x7FFFFFFF;
segmentIsExponential_ = false;
}
}
void sfzero::EG::startRelease()
{
float release = parameters_.release;
if (release <= 0)
{
// Enforce a short release, to prevent clicks.
release = fastReleaseTime;
}
segment_ = Release;
samplesUntilNextSegment_ = static_cast<int>(release * sampleRate_);
if (exponentialDecay_)
{
// I don't truly understand this; just following what LinuxSampler does.
float mysterySlope = -9.226f / samplesUntilNextSegment_;
slope_ = exp(mysterySlope);
segmentIsExponential_ = true;
}
else
{
slope_ = -level_ / samplesUntilNextSegment_;
segmentIsExponential_ = false;
}
}
const float sfzero::EG::BottomLevel = 0.001f;