/[svn]/linuxsampler/trunk/src/engines/sfz/EGADSR.cpp
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/sfz/EGADSR.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3034 - (hide annotations) (download)
Mon Oct 31 00:05:00 2016 UTC (7 years, 5 months ago) by schoenebeck
File size: 7662 byte(s)
* Fixed a bunch of minor issues (mostly compiler warnings).
* Bumped version (2.0.0.svn31).

1 persson 2055 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 persson 2311 * Copyright (C) 2005 - 2012 Christian Schoenebeck *
7 persson 2055 * *
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     void EGADSR::update(event_t Event, uint SampleRate) {
33     if (atEnd(Event)) return;
34    
35     switch (Stage) {
36     case stage_attack:
37     switch (Event) {
38     case event_release:
39     enterReleaseStage();
40     break;
41     case event_stage_end:
42 persson 2203 if (HoldSteps)
43 persson 2055 enterAttackHoldStage();
44     else
45     enterDecayStage(SampleRate);
46     break;
47 schoenebeck 3034 default: ; // noop
48 persson 2055 }
49     break;
50     case stage_attack_hold:
51     switch (Event) {
52     case event_stage_end:
53     enterDecayStage(SampleRate);
54     break;
55     case event_release:
56     enterReleaseStage();
57     break;
58 schoenebeck 3034 default: ; // noop
59 persson 2055 }
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 schoenebeck 3034 default: ; // noop
73 persson 2055 }
74     break;
75     case stage_sustain:
76     switch (Event) {
77     case event_stage_end: { // just refresh time
78     const int intMax = (unsigned int) -1 >> 1;
79     StepsLeft = intMax; // we use the highest possible value
80     break;
81     }
82     case event_release:
83     enterReleaseStage();
84     break;
85 schoenebeck 3034 default: ; // noop
86 persson 2055 }
87     break;
88     case stage_release:
89     switch (Event) {
90     case event_stage_end:
91     enterFadeOutStage();
92     break;
93     case event_cancel_release:
94     enterSustainStage();
95     break;
96 schoenebeck 3034 default: ; // noop
97 persson 2055 }
98     break;
99 schoenebeck 3034
100     case stage_fadeout:
101     case stage_end:
102     ; // noop
103 persson 2055 }
104     }
105    
106 persson 2311 void EGADSR::trigger(uint PreAttack, float AttackTime, float HoldTime, float DecayTime, uint SustainLevel, float ReleaseTime, uint SampleRate, bool LinearRelease) {
107 persson 2055 this->SustainLevel = SustainLevel / 1000.0;
108 persson 2203 this->HoldSteps = int(HoldTime * SampleRate);
109 persson 2055 this->DecayTime = DecayTime;
110 persson 2311 this->LinearRelease = LinearRelease;
111 persson 2055
112     if (ReleaseTime < CONFIG_EG_MIN_RELEASE_TIME) ReleaseTime = CONFIG_EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
113 persson 2311 this->ReleaseTime = ReleaseTime * SampleRate;
114 persson 2055 Offset = 0;
115     enterFirstStage();
116     enterAttackStage(PreAttack, AttackTime, SampleRate);
117     }
118    
119     void EGADSR::enterAttackStage(const uint PreAttack, const float AttackTime, const uint SampleRate) {
120     Stage = stage_attack;
121     Segment = segment_lin;
122    
123     if (AttackTime >= 0.0005f) {
124     StepsLeft = int(AttackTime * SampleRate);
125     Level = (float) PreAttack / 1000.0;
126     Coeff = (1.0f - Level) / StepsLeft;
127     } else { // attack is zero - immediately jump to the next stage
128     Level = 1.0f;
129 persson 2203 if (HoldSteps) enterAttackHoldStage();
130     else enterDecayStage(SampleRate);
131 persson 2055 }
132     }
133    
134     void EGADSR::enterAttackHoldStage() {
135     Stage = stage_attack_hold;
136     Segment = segment_lin;
137     Coeff = 0.0f; // don't rise anymore
138 persson 2203 StepsLeft = HoldSteps;
139 persson 2055 }
140    
141     void EGADSR::enterDecayStage(const uint SampleRate) {
142 persson 2311 if (LinearRelease) {
143     StepsLeft = int(DecayTime * SampleRate * (Level - SustainLevel));
144     if (StepsLeft > 0) {
145     Stage = stage_decay;
146     Segment = segment_lin;
147     Coeff = -1 / (DecayTime * SampleRate);
148     return;
149     }
150     } else {
151     StepsLeft = int(DecayTime * SampleRate);
152     if (StepsLeft && Level > SustainLevel) {
153     Stage = stage_decay;
154     Segment = segment_exp;
155     const float slope = -9.226 / StepsLeft;
156     Coeff = exp(slope);
157     StepsLeft = int(log(std::max(SustainLevel, float(CONFIG_EG_BOTTOM)) / Level) / slope);
158     if (StepsLeft > 0) return;
159     }
160 persson 2055 }
161 persson 2203 Level = SustainLevel;
162     enterSustainStage();
163 persson 2055 }
164    
165     void EGADSR::enterSustainStage() {
166     Stage = stage_sustain;
167     Segment = segment_lin;
168     Coeff = 0.0f; // don't change the envelope level in this stage
169     const int intMax = (unsigned int) -1 >> 1;
170     StepsLeft = intMax; // we use the highest value possible (we refresh StepsLeft in update() in case)
171     }
172    
173     void EGADSR::enterReleaseStage() {
174     Stage = stage_release;
175 persson 2311 if (LinearRelease) {
176     Segment = segment_lin;
177     StepsLeft = int(Level * ReleaseTime);
178     Coeff = -1 / ReleaseTime;
179     } else {
180     Segment = segment_exp;
181     const float slope = -9.226 / ReleaseTime;
182     StepsLeft = int(log(CONFIG_EG_BOTTOM / Level) / slope);
183     Coeff = exp(slope);
184     }
185 persson 2055 if (StepsLeft <= 0) enterFadeOutStage();
186     }
187    
188     }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC