24 |
|
|
25 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
26 |
|
|
27 |
const float EGADSR::EndCoeff(CalculateEndCoeff()); |
const float EGADSR::FadeOutCoeff(CalculateFadeOutCoeff()); |
28 |
|
|
29 |
float EGADSR::CalculateEndCoeff() { |
float EGADSR::CalculateFadeOutCoeff() { |
30 |
const float sampleRate = 44100.0; // even if the sample rate will be 192kHz it won't hurt at all |
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; |
const float killSteps = EG_MIN_RELEASE_TIME * sampleRate; |
32 |
return 1.0f / killSteps; |
return -1.0f / killSteps; |
33 |
} |
} |
34 |
|
|
35 |
EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) { |
EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) { |
46 |
* @param TotalSamples - total number of sample points to be rendered in this |
* @param TotalSamples - total number of sample points to be rendered in this |
47 |
* audio fragment cycle by the audio engine |
* audio fragment cycle by the audio engine |
48 |
* @param pEvents - event list with "release" and "cancel release" events |
* @param pEvents - event list with "release" and "cancel release" events |
49 |
* @param pTriggerEvent - event that caused triggering of the voice (only if |
* @param itTriggerEvent - event that caused triggering of the voice (only if |
50 |
* the voice was triggered in the current audio |
* the voice was triggered in the current audio |
51 |
* fragment, NULL otherwise) |
* fragment, NULL otherwise) |
52 |
* @param SamplePos - current playback position |
* @param SamplePos - current playback position |
53 |
* @param CurrentPitch - current pitch value for playback |
* @param CurrentPitch - current pitch value for playback |
54 |
* @param pKillEvent - (optional) event which caused this voice to be killed |
* @param itKillEvent - (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) { |
void EGADSR::Process(uint TotalSamples, RTList<Event>* pEvents, RTList<Event>::Iterator itTriggerEvent, double SamplePos, double CurrentPitch, RTList<Event>::Iterator itKillEvent) { |
57 |
Event* pTransitionEvent; |
// skip all events which occured before this voice was triggered |
58 |
if (pTriggerEvent) { // skip all events which occured before this voice was triggered |
RTList<Event>::Iterator itTransitionEvent = (itTriggerEvent) ? ++itTriggerEvent : pEvents->first(); |
|
pEvents->set_current(pTriggerEvent); |
|
|
pTransitionEvent = pEvents->next(); |
|
|
} |
|
|
else { |
|
|
pTransitionEvent = pEvents->first(); |
|
|
} |
|
59 |
|
|
60 |
// if the voice was killed in this fragment we only process the time before this kill event and then switch to 'stage_end' |
// if the voice was killed in this fragment we only process the time before this kill event, then switch to 'stage_fadeout' |
61 |
int Samples = (pKillEvent) ? pKillEvent->FragmentPos() : (int) TotalSamples; |
int Samples = (itKillEvent) ? RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos) : (int) TotalSamples; |
62 |
|
|
63 |
int iSample = TriggerDelay; |
int iSample = TriggerDelay; |
64 |
while (iSample < TotalSamples) { |
while (iSample < TotalSamples) { |
65 |
|
|
66 |
// if the voice was killed in this fragment and we already processed the time before this kill event |
// if the voice was killed in this fragment and we already processed the time before this kill event |
67 |
if (pKillEvent && iSample >= Samples) Stage = stage_end; |
if (itKillEvent && iSample >= Samples) Stage = stage_fadeout; |
68 |
|
|
69 |
switch (Stage) { |
switch (Stage) { |
70 |
case stage_attack: { |
case stage_attack: { |
77 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
78 |
} |
} |
79 |
if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment |
if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment |
80 |
Event* pLastEvent = pEvents->last(); |
RTList<Event>::Iterator itLastEvent = pEvents->last(); |
81 |
if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release); |
if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release); |
82 |
} |
} |
83 |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
84 |
break; |
break; |
91 |
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 |
92 |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
93 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
94 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
95 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
96 |
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; |
97 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
98 |
} |
} |
99 |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
100 |
while (iSample < process_end) { |
while (iSample < process_end) { |
105 |
case stage_decay1: { |
case stage_decay1: { |
106 |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
107 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
108 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
109 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
110 |
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; |
111 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
112 |
} |
} |
113 |
else { |
else { |
114 |
Decay1StepsLeft -= to_process; |
Decay1StepsLeft -= to_process; |
122 |
} |
} |
123 |
case stage_decay2: { |
case stage_decay2: { |
124 |
int process_end; |
int process_end; |
125 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
126 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
127 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
128 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
129 |
} |
} |
130 |
else process_end = Samples; |
else process_end = Samples; |
131 |
while (iSample < process_end) { |
while (iSample < process_end) { |
132 |
Level += Level * Decay2Coeff; |
Level += Level * Decay2Coeff; |
133 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
134 |
} |
} |
135 |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end; |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout; |
136 |
break; |
break; |
137 |
} |
} |
138 |
case stage_sustain: { |
case stage_sustain: { |
139 |
int process_end; |
int process_end; |
140 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
141 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
142 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
143 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
144 |
} |
} |
145 |
else process_end = Samples; |
else process_end = Samples; |
146 |
while (iSample < process_end) { |
while (iSample < process_end) { |
150 |
} |
} |
151 |
case stage_release: { |
case stage_release: { |
152 |
int process_end; |
int process_end; |
153 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) { |
154 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
155 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
156 |
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 |
157 |
} |
} |
158 |
else process_end = Samples; |
else process_end = Samples; |
159 |
while (iSample < process_end) { |
while (iSample < process_end) { |
160 |
Level += Level * ReleaseCoeff; |
Level += Level * ReleaseCoeff; |
161 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
162 |
} |
} |
163 |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end; |
if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout; |
164 |
break; |
break; |
165 |
} |
} |
166 |
case stage_end: { |
case stage_fadeout: { |
167 |
int to_process = RTMath::Min(int(Level / EndCoeff), TotalSamples - iSample); |
int to_process = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample); |
168 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
169 |
while (iSample < process_end) { |
while (iSample < process_end) { |
170 |
Level += EndCoeff; |
Level += FadeOutCoeff; |
171 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
172 |
} |
} |
173 |
|
Stage = stage_end; |
174 |
|
if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n")); |
175 |
|
} //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever! |
176 |
|
case stage_end: { |
177 |
while (iSample < TotalSamples) { |
while (iSample < TotalSamples) { |
178 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f; |
179 |
} |
} |
181 |
} |
} |
182 |
} |
} |
183 |
} |
} |
184 |
|
|
185 |
|
if (itKillEvent && Stage != stage_end) { |
186 |
|
dmsg(1,("EGADSR: VOICE KILLING NOT COMPLETED !!!\n")); |
187 |
|
dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos)); |
188 |
|
} |
189 |
} |
} |
190 |
|
|
191 |
/** |
/** |