/[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 53 - (show annotations) (download)
Mon Apr 26 17:15:51 2004 UTC (19 years, 11 months ago) by schoenebeck
File size: 12720 byte(s)
* completely restructured source tree
* implemented multi channel support
* implemented instrument manager, which controls sharing of instruments
  between multiple sampler engines / sampler channels
* created abstract classes 'AudioOutputDevice' and 'MidiInputDevice' for
  convenient implementation of further audio output driver and MIDI input
  driver for LinuxSampler
* implemented following LSCP commands: 'SET CHANNEL MIDI INPUT TYPE',
  'LOAD ENGINE', 'GET CHANNELS', 'ADD CHANNEL', 'REMOVE CHANNEL',
  'SET CHANNEL AUDIO OUTPUT TYPE'
* temporarily removed all command line options
* LSCP server is now launched by default

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

  ViewVC Help
Powered by ViewVC