3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
25 |
|
|
26 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
27 |
|
|
28 |
|
const float EGADSR::FadeOutCoeff(CalculateFadeOutCoeff()); |
29 |
|
|
30 |
|
float EGADSR::CalculateFadeOutCoeff() { |
31 |
|
const float sampleRate = 44100.0; // even if the sample rate will be 192kHz it won't hurt at all |
32 |
|
const float killSteps = CONFIG_EG_MIN_RELEASE_TIME * sampleRate; |
33 |
|
return -1.0f / killSteps; |
34 |
|
} |
35 |
|
|
36 |
EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) { |
EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) { |
37 |
this->pEngine = pEngine; |
this->pEngine = pEngine; |
38 |
this->ModulationDestination = ModulationDestination; |
this->ModulationDestination = ModulationDestination; |
44 |
* Will be called by the voice for every audio fragment to let the EG |
* Will be called by the voice for every audio fragment to let the EG |
45 |
* queue it's modulation changes for the current audio fragment. |
* queue it's modulation changes for the current audio fragment. |
46 |
* |
* |
47 |
* @param Samples - total number of sample points to be rendered in this |
* @param TotalSamples - total number of sample points to be rendered in this |
48 |
* audio fragment cycle by the audio engine |
* audio fragment cycle by the audio engine |
49 |
* @param pEvents - event list with "release" and "cancel release" events |
* @param pEvents - event list with "release" and "cancel release" events |
50 |
* @param pTriggerEvent - event that caused triggering of the voice (only if |
* @param itTriggerEvent - event that caused triggering of the voice (only if |
51 |
* the voices was triggered in the current audio |
* the voice was triggered in the current audio |
52 |
* fragment, NULL otherwise) |
* fragment, NULL otherwise) |
53 |
* @param SamplePos - current playback position |
* @param SamplePos - current playback position |
54 |
* @param CurrentPitch - current pitch value for playback |
* @param CurrentPitch - current pitch value for playback |
55 |
|
* @param itKillEvent - (optional) event which caused this voice to be killed |
56 |
*/ |
*/ |
57 |
void EGADSR::Process(uint Samples, RTEList<Event>* pEvents, Event* pTriggerEvent, double SamplePos, double CurrentPitch) { |
void EGADSR::Process(uint TotalSamples, RTList<Event>* pEvents, RTList<Event>::Iterator itTriggerEvent, double SamplePos, double CurrentPitch, RTList<Event>::Iterator itKillEvent) { |
58 |
Event* pTransitionEvent; |
// skip all events which occured before this voice was triggered |
59 |
if (pTriggerEvent) { |
RTList<Event>::Iterator itTransitionEvent = (itTriggerEvent) ? ++itTriggerEvent : pEvents->first(); |
60 |
pEvents->set_current(pTriggerEvent); |
|
61 |
pTransitionEvent = pEvents->next(); |
// if the voice was killed in this fragment we only process the time before this kill event, then switch to 'stage_fadeout' |
62 |
} |
int Samples = (itKillEvent) ? RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos) : (int) TotalSamples; |
|
else { |
|
|
pTransitionEvent = pEvents->first(); |
|
|
} |
|
63 |
|
|
64 |
int iSample = TriggerDelay; |
int iSample = TriggerDelay; |
65 |
while (iSample < Samples) { |
|
66 |
|
#if CONFIG_DEVMODE |
67 |
|
if (TriggerDelay > TotalSamples) { // just a sanity check for debugging |
68 |
|
dmsg(1,("EGADSR: ERROR, TriggerDelay > Totalsamples\n")); |
69 |
|
int* i = NULL; |
70 |
|
(*i)++; // force a segfault |
71 |
|
} |
72 |
|
#endif // CONFIG_DEVMODE |
73 |
|
|
74 |
|
while (iSample < TotalSamples) { |
75 |
|
|
76 |
|
// if the voice was killed in this fragment and we already processed the time before this kill event |
77 |
|
if (itKillEvent && iSample >= Samples) Stage = stage_fadeout; |
78 |
|
|
79 |
switch (Stage) { |
switch (Stage) { |
80 |
case stage_attack: { |
case stage_attack: { |
81 |
TriggerDelay = 0; |
TriggerDelay = 0; |
86 |
Level += AttackCoeff; |
Level += AttackCoeff; |
87 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
88 |
} |
} |
89 |
if (iSample == Samples) { // postpone last transition event for the next audio fragment |
if (iSample == TotalSamples && itTransitionEvent) { // postpone last transition event for the next audio fragment |
90 |
Event* pLastEvent = pEvents->last(); |
RTList<Event>::Iterator itLastEvent = pEvents->last(); |
91 |
if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release); |
if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release); |
92 |
} |
} |
93 |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
94 |
break; |
break; |
101 |
int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though |
int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though |
102 |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
103 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
104 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
105 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
106 |
Stage = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2; |
Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2; |
107 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
108 |
} |
} |
109 |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
110 |
while (iSample < process_end) { |
while (iSample < process_end) { |
115 |
case stage_decay1: { |
case stage_decay1: { |
116 |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
117 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
118 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
119 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
120 |
Stage = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2; |
Stage = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2; |
121 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
122 |
} |
} |
123 |
else { |
else { |
124 |
Decay1StepsLeft -= to_process; |
Decay1StepsLeft -= to_process; |
132 |
} |
} |
133 |
case stage_decay2: { |
case stage_decay2: { |
134 |
int process_end; |
int process_end; |
135 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
136 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
137 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
138 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
139 |
} |
} |
140 |
else process_end = Samples; |
else process_end = Samples; |
141 |
while (iSample < process_end) { |
while (iSample < process_end) { |
142 |
Level += Level * Decay2Coeff; |
Level += Level * Decay2Coeff; |
143 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
144 |
} |
} |
145 |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end; |
if (Level <= CONFIG_EG_BOTTOM) Stage = stage_fadeout; |
146 |
break; |
break; |
147 |
} |
} |
148 |
case stage_sustain: { |
case stage_sustain: { |
149 |
int process_end; |
int process_end; |
150 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
151 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
152 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
153 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
154 |
} |
} |
155 |
else process_end = Samples; |
else process_end = Samples; |
156 |
while (iSample < process_end) { |
while (iSample < process_end) { |
160 |
} |
} |
161 |
case stage_release: { |
case stage_release: { |
162 |
int process_end; |
int process_end; |
163 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) { |
164 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
165 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
166 |
Stage = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon |
Stage = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon |
167 |
} |
} |
168 |
else process_end = Samples; |
else process_end = Samples; |
169 |
while (iSample < process_end) { |
while (iSample < process_end) { |
170 |
Level += Level * ReleaseCoeff; |
Level += Level * ReleaseCoeff; |
171 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
172 |
} |
} |
173 |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end; |
if (Level <= CONFIG_EG_BOTTOM) Stage = stage_fadeout; |
174 |
break; |
break; |
175 |
} |
} |
176 |
case stage_end: { |
case stage_fadeout: { |
177 |
while (iSample < Samples) { |
int to_process = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample); |
178 |
Level += Level * ReleaseCoeff; |
int process_end = iSample + to_process; |
179 |
|
while (iSample < process_end) { |
180 |
|
Level += FadeOutCoeff; |
181 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
182 |
} |
} |
183 |
|
Stage = stage_end; |
184 |
|
if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n")); |
185 |
|
} //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever! |
186 |
|
case stage_end: { |
187 |
|
while (iSample < TotalSamples) { |
188 |
|
pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f; |
189 |
|
} |
190 |
break; |
break; |
191 |
} |
} |
192 |
} |
} |
193 |
} |
} |
194 |
|
|
195 |
|
#if CONFIG_DEVMODE |
196 |
|
if (itKillEvent && Stage != stage_end) { // just a sanity check for debugging |
197 |
|
dmsg(1,("EGADSR: ERROR, voice killing not completed !!!\n")); |
198 |
|
dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos)); |
199 |
|
} |
200 |
|
#endif // CONFIG_DEVMODE |
201 |
} |
} |
202 |
|
|
203 |
/** |
/** |
217 |
void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) { |
void EGADSR::Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay) { |
218 |
this->TriggerDelay = Delay; |
this->TriggerDelay = Delay; |
219 |
this->Stage = stage_attack; |
this->Stage = stage_attack; |
220 |
this->SustainLevel = (SustainLevel) ? (SustainLevel > EG_ENVELOPE_LIMIT) ? (float) SustainLevel / 1000.0 : EG_ENVELOPE_LIMIT : 1.0; |
this->SustainLevel = (SustainLevel) ? (SustainLevel > CONFIG_EG_BOTTOM) ? (float) SustainLevel / 1000.0 : CONFIG_EG_BOTTOM : 1.0; |
221 |
this->InfiniteSustain = InfiniteSustain; |
this->InfiniteSustain = InfiniteSustain; |
222 |
this->HoldAttack = HoldAttack; |
this->HoldAttack = HoldAttack; |
223 |
this->LoopStart = LoopStart; |
this->LoopStart = LoopStart; |
241 |
|
|
242 |
// calculate decay2 stage parameters (exp. curve) |
// calculate decay2 stage parameters (exp. curve) |
243 |
if (!InfiniteSustain) { |
if (!InfiniteSustain) { |
244 |
if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME; |
if (Decay2Time < CONFIG_EG_MIN_RELEASE_TIME) Decay2Time = CONFIG_EG_MIN_RELEASE_TIME; |
245 |
long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate()); |
long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate()); |
246 |
Decay2Coeff = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel |
Decay2Coeff = (Decay2Steps) ? exp((log(CONFIG_EG_BOTTOM) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel |
247 |
: 0.0; |
: 0.0; |
248 |
} |
} |
249 |
|
|
250 |
// calcuate release stage parameters (exp. curve) |
// calculate release stage parameters (exp. curve) |
251 |
if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback |
if (ReleaseTime < CONFIG_EG_MIN_RELEASE_TIME) ReleaseTime = CONFIG_EG_MIN_RELEASE_TIME; // to avoid click sounds at the end of the sample playback |
252 |
ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate()); |
ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate()); |
253 |
ReleaseCoeff = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel; |
ReleaseCoeff = exp((log(CONFIG_EG_BOTTOM) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel; |
254 |
|
|
255 |
dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n", |
dmsg(4,("PreAttack=%d, AttackLength=%d, AttackCoeff=%f, Decay1Coeff=%f, Decay2Coeff=%f, ReleaseLength=%d, ReleaseCoeff=%f\n", |
256 |
PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff)); |
PreAttack, AttackStepsLeft, AttackCoeff, Decay1Coeff, Decay2Coeff, ReleaseStepsLeft, ReleaseCoeff)); |