/[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 239 - (show annotations) (download)
Sun Sep 12 14:48:19 2004 UTC (16 years, 1 month ago) by schoenebeck
File size: 13771 byte(s)
* implemented key groups (a.k.a exclusive groups) which is important for
  drum patches and monphonic instruments
* src/engines/gig/Engine.cpp: bugfix in ProcessNoteOn() - did not always
  stick the note on event to the respective key's own event list although
  e.g. the EGADRS expects it to find there

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::EndCoeff(CalculateEndCoeff());
28
29 float EGADSR::CalculateEndCoeff() {
30 const double sampleRate = 44100.0; // even if the sample rate will be 192kHz it won't hurt at all
31 const double killSteps = EG_MIN_RELEASE_TIME * sampleRate;
32 return float(exp(1.0 / killSteps) - 1.0);
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 and then switch to 'stage_end'
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_end;
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_end;
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_end;
170 break;
171 }
172 case stage_end: {
173 while (iSample < TotalSamples) {
174 Level += Level * EndCoeff;
175 pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
176 }
177 break;
178 }
179 }
180 }
181 }
182
183 /**
184 * Will be called by the voice when the key / voice was triggered.
185 *
186 * @param PreAttack - Preattack value for the envelope (0 - 1000 permille)
187 * @param AttackTime - Attack time for the envelope (0.000 - 60.000s)
188 * @param HoldAttack - If true, Decay1 will be postponed until the sample reached the sample loop start.
189 * @param LoopStart - Sample position where sample loop starts (if any)
190 * @param Decay1Time - Decay1 time of the sample amplitude EG (0.000 - 60.000s).
191 * @param Decay2Time - Only if !InfiniteSustain: 2nd decay stage time of the sample amplitude EG (0.000 - 60.000s).
192 * @param InfiniteSustain - If true, instead of going into Decay2 phase, Decay1 level will be hold until note will be released.
193 * @param SustainLevel - Sustain level of the sample amplitude EG (0 - 1000 permille).
194 * @param ReleaseTIme - Release time for the envelope (0.000 - 60.000s)
195 * @param Delay - Number of sample points triggering should be delayed.
196 */
197 void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) {
198 this->TriggerDelay = Delay;
199 this->Stage = stage_attack;
200 this->SustainLevel = (SustainLevel) ? (SustainLevel > EG_ENVELOPE_LIMIT) ? (float) SustainLevel / 1000.0 : EG_ENVELOPE_LIMIT : 1.0;
201 this->InfiniteSustain = InfiniteSustain;
202 this->HoldAttack = HoldAttack;
203 this->LoopStart = LoopStart;
204 this->ReleasePostponed = false;
205
206 // calculate attack stage parameters (lin. curve)
207 AttackStepsLeft = (long) (AttackTime * pEngine->pAudioOutputDevice->SampleRate());
208 if (AttackStepsLeft) {
209 Level = (float) PreAttack / 1000.0;
210 AttackCoeff = (1.0 - Level) / AttackStepsLeft;
211 }
212 else {
213 Level = 1.0;
214 AttackCoeff = 0.0;
215 }
216
217 // calculate decay1 stage parameters (exp. curve)
218 Decay1StepsLeft = (long) (Decay1Time * pEngine->pAudioOutputDevice->SampleRate());
219 Decay1Coeff = (Decay1StepsLeft) ? exp(log(this->SustainLevel) / (double) Decay1StepsLeft) - 1.0
220 : 0.0;
221
222 // calculate decay2 stage parameters (exp. curve)
223 if (!InfiniteSustain) {
224 if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
225 long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
226 Decay2Coeff = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel
227 : 0.0;
228 }
229
230 // calculate release stage parameters (exp. curve)
231 if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback
232 ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
233 ReleaseCoeff = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;
234
235 dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n",
236 PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff));
237 }
238
239 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC