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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 285 - (hide annotations) (download)
Thu Oct 14 21:31:26 2004 UTC (19 years, 6 months ago) by schoenebeck
File size: 14655 byte(s)
* bunch of bugfixes (e.g. segfault on voice stealing)

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 53 * *
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 schoenebeck 252 const float EGADSR::FadeOutCoeff(CalculateFadeOutCoeff());
28 schoenebeck 239
29 schoenebeck 252 float EGADSR::CalculateFadeOutCoeff() {
30 schoenebeck 251 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 schoenebeck 285 return -1.0f / killSteps;
33 schoenebeck 239 }
34    
35 schoenebeck 53 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 schoenebeck 239 * @param TotalSamples - total number of sample points to be rendered in this
47 schoenebeck 53 * audio fragment cycle by the audio engine
48     * @param pEvents - event list with "release" and "cancel release" events
49 schoenebeck 271 * @param itTriggerEvent - event that caused triggering of the voice (only if
50 schoenebeck 239 * the voice was triggered in the current audio
51 schoenebeck 53 * fragment, NULL otherwise)
52     * @param SamplePos - current playback position
53     * @param CurrentPitch - current pitch value for playback
54 schoenebeck 271 * @param itKillEvent - (optional) event which caused this voice to be killed
55 schoenebeck 53 */
56 schoenebeck 271 void EGADSR::Process(uint TotalSamples, RTList<Event>* pEvents, RTList<Event>::Iterator itTriggerEvent, double SamplePos, double CurrentPitch, RTList<Event>::Iterator itKillEvent) {
57     // skip all events which occured before this voice was triggered
58     RTList<Event>::Iterator itTransitionEvent = (itTriggerEvent) ? ++itTriggerEvent : pEvents->first();
59 schoenebeck 53
60 schoenebeck 252 // if the voice was killed in this fragment we only process the time before this kill event, then switch to 'stage_fadeout'
61 schoenebeck 285 int Samples = (itKillEvent) ? RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos) : (int) TotalSamples;
62 schoenebeck 239
63 schoenebeck 53 int iSample = TriggerDelay;
64 schoenebeck 239 while (iSample < TotalSamples) {
65    
66     // if the voice was killed in this fragment and we already processed the time before this kill event
67 schoenebeck 271 if (itKillEvent && iSample >= Samples) Stage = stage_fadeout;
68 schoenebeck 239
69 schoenebeck 53 switch (Stage) {
70     case stage_attack: {
71     TriggerDelay = 0;
72     int to_process = RTMath::Min(AttackStepsLeft, Samples - iSample);
73     int process_end = iSample + to_process;
74     AttackStepsLeft -= to_process;
75     while (iSample < process_end) {
76     Level += AttackCoeff;
77     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
78     }
79 schoenebeck 239 if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment
80 schoenebeck 271 RTList<Event>::Iterator itLastEvent = pEvents->last();
81     if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release);
82 schoenebeck 53 }
83     if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;
84     break;
85     }
86     case stage_attack_hold: {
87     if (SamplePos >= LoopStart) {
88     Stage = stage_decay1;
89     break;
90     }
91     int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though
92     int to_process = RTMath::Min(holdstepsleft, Samples - iSample);
93     int process_end = iSample + to_process;
94 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
95     process_end = itTransitionEvent->FragmentPos();
96     Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
97     ++itTransitionEvent;
98 schoenebeck 53 }
99     else if (to_process == holdstepsleft) Stage = stage_decay1;
100     while (iSample < process_end) {
101     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
102     }
103     break;
104     }
105     case stage_decay1: {
106     int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft);
107     int process_end = iSample + to_process;
108 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
109     process_end = itTransitionEvent->FragmentPos();
110     Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
111     ++itTransitionEvent;
112 schoenebeck 53 }
113     else {
114     Decay1StepsLeft -= to_process;
115     if (!Decay1StepsLeft) Stage = (InfiniteSustain) ? stage_sustain : stage_decay2;
116     }
117     while (iSample < process_end) {
118     Level += Level * Decay1Coeff;
119     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
120     }
121     break;
122     }
123     case stage_decay2: {
124     int process_end;
125 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
126     process_end = itTransitionEvent->FragmentPos();
127     ++itTransitionEvent;
128     Stage = stage_release; // switch to release stage soon
129 schoenebeck 53 }
130     else process_end = Samples;
131     while (iSample < process_end) {
132     Level += Level * Decay2Coeff;
133     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
134     }
135 schoenebeck 252 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
136 schoenebeck 53 break;
137     }
138     case stage_sustain: {
139     int process_end;
140 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
141     process_end = itTransitionEvent->FragmentPos();
142     ++itTransitionEvent;
143     Stage = stage_release; // switch to release stage soon
144 schoenebeck 53 }
145     else process_end = Samples;
146     while (iSample < process_end) {
147     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
148     }
149     break;
150     }
151     case stage_release: {
152     int process_end;
153 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) {
154     process_end = itTransitionEvent->FragmentPos();
155     ++itTransitionEvent;
156     Stage = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon
157 schoenebeck 53 }
158     else process_end = Samples;
159     while (iSample < process_end) {
160     Level += Level * ReleaseCoeff;
161     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
162     }
163 schoenebeck 252 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
164 schoenebeck 53 break;
165     }
166 schoenebeck 252 case stage_fadeout: {
167     int to_process = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample);
168 schoenebeck 251 int process_end = iSample + to_process;
169     while (iSample < process_end) {
170 schoenebeck 252 Level += FadeOutCoeff;
171 schoenebeck 53 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
172     }
173 schoenebeck 285 Stage = stage_end;
174     if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n"));
175 senkov 259 } //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever!
176 schoenebeck 252 case stage_end: {
177 schoenebeck 251 while (iSample < TotalSamples) {
178     pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
179     }
180 schoenebeck 53 break;
181     }
182     }
183     }
184 schoenebeck 285
185     if (itKillEvent && Stage != stage_end) {
186     dmsg(1,("EGADSR: VOICE KILLING NOT COMPLETED !!!\n"));
187     dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos));
188     }
189 schoenebeck 53 }
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 schoenebeck 239 : 0.0;
236 schoenebeck 53 }
237    
238 schoenebeck 239 // calculate release stage parameters (exp. curve)
239 schoenebeck 53 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