/[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 251 - (hide annotations) (download)
Mon Sep 20 12:27:48 2004 UTC (19 years, 6 months ago) by schoenebeck
File size: 14074 byte(s)
* changed curve type from exponential to linear in stage 'end' of
  EGADSR (fixes click sounds at the end of voices)

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 239 const float EGADSR::EndCoeff(CalculateEndCoeff());
28    
29     float EGADSR::CalculateEndCoeff() {
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     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     * @param pTriggerEvent - 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 239 * @param pKillEvent - (optional) event which caused this voice to be killed
55 schoenebeck 53 */
56 schoenebeck 239 void EGADSR::Process(uint TotalSamples, RTEList<Event>* pEvents, Event* pTriggerEvent, double SamplePos, double CurrentPitch, Event* pKillEvent) {
57 schoenebeck 53 Event* pTransitionEvent;
58 schoenebeck 239 if (pTriggerEvent) { // skip all events which occured before this voice was triggered
59 schoenebeck 53 pEvents->set_current(pTriggerEvent);
60     pTransitionEvent = pEvents->next();
61     }
62     else {
63     pTransitionEvent = pEvents->first();
64     }
65    
66 schoenebeck 239 // if the voice was killed in this fragment we only process the time before this kill event and then switch to 'stage_end'
67     int Samples = (pKillEvent) ? pKillEvent->FragmentPos() : (int) TotalSamples;
68    
69 schoenebeck 53 int iSample = TriggerDelay;
70 schoenebeck 239 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_end;
74    
75 schoenebeck 53 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 schoenebeck 239 if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment
86 schoenebeck 53 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 senkov 237 if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {
132 schoenebeck 53 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_end;
142     break;
143     }
144     case stage_sustain: {
145     int process_end;
146 senkov 237 if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {
147 schoenebeck 53 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 senkov 237 if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) {
160 schoenebeck 53 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_end;
170     break;
171     }
172     case stage_end: {
173 schoenebeck 251 int to_process = RTMath::Min(int(Level / EndCoeff), TotalSamples - iSample);
174     int process_end = iSample + to_process;
175     while (iSample < process_end) {
176     Level += EndCoeff;
177 schoenebeck 53 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
178     }
179 schoenebeck 251 while (iSample < TotalSamples) {
180     pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
181     }
182 schoenebeck 53 break;
183     }
184     }
185     }
186     }
187    
188     /**
189     * Will be called by the voice when the key / voice was triggered.
190     *
191     * @param PreAttack - Preattack value for the envelope (0 - 1000 permille)
192     * @param AttackTime - Attack time for the envelope (0.000 - 60.000s)
193     * @param HoldAttack - If true, Decay1 will be postponed until the sample reached the sample loop start.
194     * @param LoopStart - Sample position where sample loop starts (if any)
195     * @param Decay1Time - Decay1 time of the sample amplitude EG (0.000 - 60.000s).
196     * @param Decay2Time - Only if !InfiniteSustain: 2nd decay stage time of the sample amplitude EG (0.000 - 60.000s).
197     * @param InfiniteSustain - If true, instead of going into Decay2 phase, Decay1 level will be hold until note will be released.
198     * @param SustainLevel - Sustain level of the sample amplitude EG (0 - 1000 permille).
199     * @param ReleaseTIme - Release time for the envelope (0.000 - 60.000s)
200     * @param Delay - Number of sample points triggering should be delayed.
201     */
202     void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) {
203     this->TriggerDelay = Delay;
204     this->Stage = stage_attack;
205     this->SustainLevel = (SustainLevel) ? (SustainLevel > EG_ENVELOPE_LIMIT) ? (float) SustainLevel / 1000.0 : EG_ENVELOPE_LIMIT : 1.0;
206     this->InfiniteSustain = InfiniteSustain;
207     this->HoldAttack = HoldAttack;
208     this->LoopStart = LoopStart;
209     this->ReleasePostponed = false;
210    
211     // calculate attack stage parameters (lin. curve)
212     AttackStepsLeft = (long) (AttackTime * pEngine->pAudioOutputDevice->SampleRate());
213     if (AttackStepsLeft) {
214     Level = (float) PreAttack / 1000.0;
215     AttackCoeff = (1.0 - Level) / AttackStepsLeft;
216     }
217     else {
218     Level = 1.0;
219     AttackCoeff = 0.0;
220     }
221    
222     // calculate decay1 stage parameters (exp. curve)
223     Decay1StepsLeft = (long) (Decay1Time * pEngine->pAudioOutputDevice->SampleRate());
224     Decay1Coeff = (Decay1StepsLeft) ? exp(log(this->SustainLevel) / (double) Decay1StepsLeft) - 1.0
225     : 0.0;
226    
227     // calculate decay2 stage parameters (exp. curve)
228     if (!InfiniteSustain) {
229     if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
230     long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
231     Decay2Coeff = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel
232 schoenebeck 239 : 0.0;
233 schoenebeck 53 }
234    
235 schoenebeck 239 // calculate release stage parameters (exp. curve)
236 schoenebeck 53 if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
237     ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
238     ReleaseCoeff = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;
239    
240     dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n",
241     PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff));
242     }
243    
244     }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC