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

Contents of /linuxsampler/trunk/src/engines/gig/EGADSR.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 259 - (show annotations) (download)
Wed Sep 29 03:16:01 2004 UTC (19 years, 6 months ago) by senkov
File size: 14335 byte(s)
* Fixed a hang in stage_fadeout

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
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., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "EGADSR.h"
24
25 namespace LinuxSampler { namespace gig {
26
27 const float EGADSR::FadeOutCoeff(CalculateFadeOutCoeff());
28
29 float EGADSR::CalculateFadeOutCoeff() {
30 const float sampleRate = 44100.0; // even if the sample rate will be 192kHz it won't hurt at all
31 const float killSteps = EG_MIN_RELEASE_TIME * sampleRate;
32 return 1.0f / killSteps;
33 }
34
35 EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) {
36 this->pEngine = pEngine;
37 this->ModulationDestination = ModulationDestination;
38 Stage = stage_end;
39 Level = 0.0;
40 }
41
42 /**
43 * Will be called by the voice for every audio fragment to let the EG
44 * queue it's modulation changes for the current audio fragment.
45 *
46 * @param TotalSamples - total number of sample points to be rendered in this
47 * audio fragment cycle by the audio engine
48 * @param pEvents - event list with "release" and "cancel release" events
49 * @param pTriggerEvent - event that caused triggering of the voice (only if
50 * the voice was triggered in the current audio
51 * fragment, NULL otherwise)
52 * @param SamplePos - current playback position
53 * @param CurrentPitch - current pitch value for playback
54 * @param pKillEvent - (optional) event which caused this voice to be killed
55 */
56 void EGADSR::Process(uint TotalSamples, RTEList<Event>* pEvents, Event* pTriggerEvent, double SamplePos, double CurrentPitch, Event* pKillEvent) {
57 Event* pTransitionEvent;
58 if (pTriggerEvent) { // skip all events which occured before this voice was triggered
59 pEvents->set_current(pTriggerEvent);
60 pTransitionEvent = pEvents->next();
61 }
62 else {
63 pTransitionEvent = pEvents->first();
64 }
65
66 // if the voice was killed in this fragment we only process the time before this kill event, then switch to 'stage_fadeout'
67 int Samples = (pKillEvent) ? pKillEvent->FragmentPos() : (int) TotalSamples;
68
69 int iSample = TriggerDelay;
70 while (iSample < TotalSamples) {
71
72 // if the voice was killed in this fragment and we already processed the time before this kill event
73 if (pKillEvent && iSample >= Samples) Stage = stage_fadeout;
74
75 switch (Stage) {
76 case stage_attack: {
77 TriggerDelay = 0;
78 int to_process = RTMath::Min(AttackStepsLeft, Samples - iSample);
79 int process_end = iSample + to_process;
80 AttackStepsLeft -= to_process;
81 while (iSample < process_end) {
82 Level += AttackCoeff;
83 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
84 }
85 if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment
86 Event* pLastEvent = pEvents->last();
87 if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release);
88 }
89 if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;
90 break;
91 }
92 case stage_attack_hold: {
93 if (SamplePos >= LoopStart) {
94 Stage = stage_decay1;
95 break;
96 }
97 int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though
98 int to_process = RTMath::Min(holdstepsleft, Samples - iSample);
99 int process_end = iSample + to_process;
100 if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {
101 process_end = pTransitionEvent->FragmentPos();
102 Stage = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
103 pTransitionEvent = pEvents->next();
104 }
105 else if (to_process == holdstepsleft) Stage = stage_decay1;
106 while (iSample < process_end) {
107 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
108 }
109 break;
110 }
111 case stage_decay1: {
112 int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft);
113 int process_end = iSample + to_process;
114 if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {
115 process_end = pTransitionEvent->FragmentPos();
116 Stage = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
117 pTransitionEvent = pEvents->next();
118 }
119 else {
120 Decay1StepsLeft -= to_process;
121 if (!Decay1StepsLeft) Stage = (InfiniteSustain) ? stage_sustain : stage_decay2;
122 }
123 while (iSample < process_end) {
124 Level += Level * Decay1Coeff;
125 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
126 }
127 break;
128 }
129 case stage_decay2: {
130 int process_end;
131 if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {
132 process_end = pTransitionEvent->FragmentPos();
133 pTransitionEvent = pEvents->next();
134 Stage = stage_release; // switch to release stage soon
135 }
136 else process_end = Samples;
137 while (iSample < process_end) {
138 Level += Level * Decay2Coeff;
139 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
140 }
141 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
142 break;
143 }
144 case stage_sustain: {
145 int process_end;
146 if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {
147 process_end = pTransitionEvent->FragmentPos();
148 pTransitionEvent = pEvents->next();
149 Stage = stage_release; // switch to release stage soon
150 }
151 else process_end = Samples;
152 while (iSample < process_end) {
153 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
154 }
155 break;
156 }
157 case stage_release: {
158 int process_end;
159 if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) {
160 process_end = pTransitionEvent->FragmentPos();
161 pTransitionEvent = pEvents->next();
162 Stage = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon
163 }
164 else process_end = Samples;
165 while (iSample < process_end) {
166 Level += Level * ReleaseCoeff;
167 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
168 }
169 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
170 break;
171 }
172 case stage_fadeout: {
173 int to_process = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample);
174 int process_end = iSample + to_process;
175 while (iSample < process_end) {
176 Level += FadeOutCoeff;
177 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
178 }
179 if (Level <= FadeOutCoeff) Stage = stage_end;
180 } //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever!
181 case stage_end: {
182 while (iSample < TotalSamples) {
183 pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
184 }
185 break;
186 }
187 }
188 }
189 }
190
191 /**
192 * Will be called by the voice when the key / voice was triggered.
193 *
194 * @param PreAttack - Preattack value for the envelope (0 - 1000 permille)
195 * @param AttackTime - Attack time for the envelope (0.000 - 60.000s)
196 * @param HoldAttack - If true, Decay1 will be postponed until the sample reached the sample loop start.
197 * @param LoopStart - Sample position where sample loop starts (if any)
198 * @param Decay1Time - Decay1 time of the sample amplitude EG (0.000 - 60.000s).
199 * @param Decay2Time - Only if !InfiniteSustain: 2nd decay stage time of the sample amplitude EG (0.000 - 60.000s).
200 * @param InfiniteSustain - If true, instead of going into Decay2 phase, Decay1 level will be hold until note will be released.
201 * @param SustainLevel - Sustain level of the sample amplitude EG (0 - 1000 permille).
202 * @param ReleaseTIme - Release time for the envelope (0.000 - 60.000s)
203 * @param Delay - Number of sample points triggering should be delayed.
204 */
205 void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) {
206 this->TriggerDelay = Delay;
207 this->Stage = stage_attack;
208 this->SustainLevel = (SustainLevel) ? (SustainLevel > EG_ENVELOPE_LIMIT) ? (float) SustainLevel / 1000.0 : EG_ENVELOPE_LIMIT : 1.0;
209 this->InfiniteSustain = InfiniteSustain;
210 this->HoldAttack = HoldAttack;
211 this->LoopStart = LoopStart;
212 this->ReleasePostponed = false;
213
214 // calculate attack stage parameters (lin. curve)
215 AttackStepsLeft = (long) (AttackTime * pEngine->pAudioOutputDevice->SampleRate());
216 if (AttackStepsLeft) {
217 Level = (float) PreAttack / 1000.0;
218 AttackCoeff = (1.0 - Level) / AttackStepsLeft;
219 }
220 else {
221 Level = 1.0;
222 AttackCoeff = 0.0;
223 }
224
225 // calculate decay1 stage parameters (exp. curve)
226 Decay1StepsLeft = (long) (Decay1Time * pEngine->pAudioOutputDevice->SampleRate());
227 Decay1Coeff = (Decay1StepsLeft) ? exp(log(this->SustainLevel) / (double) Decay1StepsLeft) - 1.0
228 : 0.0;
229
230 // calculate decay2 stage parameters (exp. curve)
231 if (!InfiniteSustain) {
232 if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
233 long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
234 Decay2Coeff = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel
235 : 0.0;
236 }
237
238 // calculate release stage parameters (exp. curve)
239 if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
240 ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
241 ReleaseCoeff = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;
242
243 dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n",
244 PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff));
245 }
246
247 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC