1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2010 Andreas Persson * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
20 |
* MA 02110-1301 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include "EG.h" |
24 |
|
25 |
#include "../../common/global_private.h" |
26 |
|
27 |
namespace LinuxSampler { namespace sfz { |
28 |
|
29 |
// Modeled after output from Rapture Demo 1.0, which means the |
30 |
// stages are using a pow function and the "curve" parameter of |
31 |
// the stage is ignored. The "shape" parameter decides the slope |
32 |
// of the stage, a value of 1.0 or 0.0 means linear shape. The |
33 |
// length of the stages are dependent on the velocity. |
34 |
|
35 |
// TODO: add support for loops |
36 |
// TODO: optimization: use segment_lin for linear stages |
37 |
|
38 |
void EG::update(event_t Event, uint SampleRate) { |
39 |
if (atEnd(Event)) return; |
40 |
|
41 |
if (Event == event_stage_end || Event == event_release) { |
42 |
if (Stage == eg->node.size() - 1) { |
43 |
enterFadeOutStage(); |
44 |
} else if (Stage == eg->sustain && Stage != 0 && |
45 |
Event != event_release) { |
46 |
enterSustainStage(); |
47 |
} else { |
48 |
if (Event == event_release) { |
49 |
Stage = eg->sustain; |
50 |
} else { |
51 |
Level = eg->node[Stage].level; |
52 |
} |
53 |
|
54 |
Stage++; |
55 |
|
56 |
float shape = eg->node[Stage].shape; |
57 |
if (shape < 0.000001) shape = 1; |
58 |
|
59 |
float xd = eg->node[Stage].time * SampleRate * TimeCoeff; |
60 |
float yd = eg->node[Stage].level - Level; |
61 |
|
62 |
if (eg->node[Stage - 1].shape > 0.999999 || |
63 |
eg->node[Stage - 1].shape < 0.000001) { |
64 |
Exp = 1 / shape; |
65 |
Offset = Level; |
66 |
X = 0; |
67 |
XDelta = 1 / xd; |
68 |
Coeff = yd; |
69 |
} else { |
70 |
Exp = shape; |
71 |
Offset = eg->node[Stage].level; |
72 |
X = 1; |
73 |
XDelta = -1 / xd; |
74 |
Coeff = -yd; |
75 |
} |
76 |
Segment = segment_pow; |
77 |
StepsLeft = int(xd); |
78 |
} |
79 |
} |
80 |
} |
81 |
|
82 |
void EG::trigger(::sfz::EG& eg, uint SampleRate, uint8_t Velocity) { |
83 |
Stage = 0; |
84 |
this->eg = ⪚ |
85 |
TimeCoeff = exp(0.0054578518 * Velocity); // pow(2, Velcocity / 127) |
86 |
|
87 |
enterFirstStage(); |
88 |
update(event_stage_end, SampleRate); |
89 |
} |
90 |
|
91 |
void EG::enterSustainStage() { |
92 |
Segment = segment_lin; |
93 |
Coeff = 0.0f; // don't change the envelope level in this stage |
94 |
const int intMax = (unsigned int) -1 >> 1; |
95 |
StepsLeft = intMax; // we use the highest value possible (we refresh StepsLeft in update() in case) |
96 |
Level = eg->node[Stage].level; |
97 |
} |
98 |
}} // namespace LinuxSampler::sfz |