/[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 396 - (hide annotations) (download)
Sun Feb 20 20:32:24 2005 UTC (19 years, 2 months ago) by persson
File size: 15163 byte(s)
* fixed a bug that made some release triggered samples too short
  (envelope went directly from attack to release)

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 286
65     #if DEVMODE
66     if (TriggerDelay > TotalSamples) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
67     dmsg(1,("EGADSR: ERROR, TriggerDelay > Totalsamples\n"));
68     int* i = NULL;
69     (*i)++; // force a segfault
70     }
71     #endif // DEVMODE
72    
73 schoenebeck 239 while (iSample < TotalSamples) {
74    
75     // if the voice was killed in this fragment and we already processed the time before this kill event
76 schoenebeck 271 if (itKillEvent && iSample >= Samples) Stage = stage_fadeout;
77 schoenebeck 239
78 schoenebeck 53 switch (Stage) {
79     case stage_attack: {
80     TriggerDelay = 0;
81     int to_process = RTMath::Min(AttackStepsLeft, Samples - iSample);
82     int process_end = iSample + to_process;
83     AttackStepsLeft -= to_process;
84     while (iSample < process_end) {
85     Level += AttackCoeff;
86     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
87     }
88 persson 396 if (iSample == TotalSamples && itTransitionEvent) { // postpone last transition event for the next audio fragment
89 schoenebeck 271 RTList<Event>::Iterator itLastEvent = pEvents->last();
90     if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release);
91 schoenebeck 53 }
92     if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;
93     break;
94     }
95     case stage_attack_hold: {
96     if (SamplePos >= LoopStart) {
97     Stage = stage_decay1;
98     break;
99     }
100     int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though
101     int to_process = RTMath::Min(holdstepsleft, Samples - iSample);
102     int process_end = iSample + to_process;
103 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
104     process_end = itTransitionEvent->FragmentPos();
105     Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
106     ++itTransitionEvent;
107 schoenebeck 53 }
108     else if (to_process == holdstepsleft) Stage = stage_decay1;
109     while (iSample < process_end) {
110     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
111     }
112     break;
113     }
114     case stage_decay1: {
115     int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft);
116     int process_end = iSample + to_process;
117 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
118     process_end = itTransitionEvent->FragmentPos();
119     Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
120     ++itTransitionEvent;
121 schoenebeck 53 }
122     else {
123     Decay1StepsLeft -= to_process;
124     if (!Decay1StepsLeft) Stage = (InfiniteSustain) ? stage_sustain : stage_decay2;
125     }
126     while (iSample < process_end) {
127     Level += Level * Decay1Coeff;
128     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
129     }
130     break;
131     }
132     case stage_decay2: {
133     int process_end;
134 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
135     process_end = itTransitionEvent->FragmentPos();
136     ++itTransitionEvent;
137     Stage = stage_release; // switch to release stage soon
138 schoenebeck 53 }
139     else process_end = Samples;
140     while (iSample < process_end) {
141     Level += Level * Decay2Coeff;
142     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
143     }
144 schoenebeck 252 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
145 schoenebeck 53 break;
146     }
147     case stage_sustain: {
148     int process_end;
149 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
150     process_end = itTransitionEvent->FragmentPos();
151     ++itTransitionEvent;
152     Stage = stage_release; // switch to release stage soon
153 schoenebeck 53 }
154     else process_end = Samples;
155     while (iSample < process_end) {
156     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
157     }
158     break;
159     }
160     case stage_release: {
161     int process_end;
162 schoenebeck 271 if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) {
163     process_end = itTransitionEvent->FragmentPos();
164     ++itTransitionEvent;
165     Stage = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon
166 schoenebeck 53 }
167     else process_end = Samples;
168     while (iSample < process_end) {
169     Level += Level * ReleaseCoeff;
170     pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
171     }
172 schoenebeck 252 if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
173 schoenebeck 53 break;
174     }
175 schoenebeck 252 case stage_fadeout: {
176     int to_process = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample);
177 schoenebeck 251 int process_end = iSample + to_process;
178     while (iSample < process_end) {
179 schoenebeck 252 Level += FadeOutCoeff;
180 schoenebeck 53 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
181     }
182 schoenebeck 285 Stage = stage_end;
183     if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n"));
184 senkov 259 } //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever!
185 schoenebeck 252 case stage_end: {
186 schoenebeck 251 while (iSample < TotalSamples) {
187     pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
188     }
189 schoenebeck 53 break;
190     }
191     }
192     }
193 schoenebeck 285
194 schoenebeck 286 #if DEVMODE
195     if (itKillEvent && Stage != stage_end) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
196     dmsg(1,("EGADSR: ERROR, voice killing not completed !!!\n"));
197 schoenebeck 285 dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos));
198     }
199 schoenebeck 286 #endif // DEVMODE
200 schoenebeck 53 }
201    
202     /**
203     * Will be called by the voice when the key / voice was triggered.
204     *
205     * @param PreAttack - Preattack value for the envelope (0 - 1000 permille)
206     * @param AttackTime - Attack time for the envelope (0.000 - 60.000s)
207     * @param HoldAttack - If true, Decay1 will be postponed until the sample reached the sample loop start.
208     * @param LoopStart - Sample position where sample loop starts (if any)
209     * @param Decay1Time - Decay1 time of the sample amplitude EG (0.000 - 60.000s).
210     * @param Decay2Time - Only if !InfiniteSustain: 2nd decay stage time of the sample amplitude EG (0.000 - 60.000s).
211     * @param InfiniteSustain - If true, instead of going into Decay2 phase, Decay1 level will be hold until note will be released.
212     * @param SustainLevel - Sustain level of the sample amplitude EG (0 - 1000 permille).
213     * @param ReleaseTIme - Release time for the envelope (0.000 - 60.000s)
214     * @param Delay - Number of sample points triggering should be delayed.
215     */
216     void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) {
217     this->TriggerDelay = Delay;
218     this->Stage = stage_attack;
219     this->SustainLevel = (SustainLevel) ? (SustainLevel > EG_ENVELOPE_LIMIT) ? (float) SustainLevel / 1000.0 : EG_ENVELOPE_LIMIT : 1.0;
220     this->InfiniteSustain = InfiniteSustain;
221     this->HoldAttack = HoldAttack;
222     this->LoopStart = LoopStart;
223     this->ReleasePostponed = false;
224    
225     // calculate attack stage parameters (lin. curve)
226     AttackStepsLeft = (long) (AttackTime * pEngine->pAudioOutputDevice->SampleRate());
227     if (AttackStepsLeft) {
228     Level = (float) PreAttack / 1000.0;
229     AttackCoeff = (1.0 - Level) / AttackStepsLeft;
230     }
231     else {
232     Level = 1.0;
233     AttackCoeff = 0.0;
234     }
235    
236     // calculate decay1 stage parameters (exp. curve)
237     Decay1StepsLeft = (long) (Decay1Time * pEngine->pAudioOutputDevice->SampleRate());
238     Decay1Coeff = (Decay1StepsLeft) ? exp(log(this->SustainLevel) / (double) Decay1StepsLeft) - 1.0
239     : 0.0;
240    
241     // calculate decay2 stage parameters (exp. curve)
242     if (!InfiniteSustain) {
243     if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
244     long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
245     Decay2Coeff = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel
246 schoenebeck 239 : 0.0;
247 schoenebeck 53 }
248    
249 schoenebeck 239 // calculate release stage parameters (exp. curve)
250 schoenebeck 53 if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
251     ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
252     ReleaseCoeff = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;
253    
254     dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n",
255     PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff));
256     }
257    
258     }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC