29 |
float EGADSR::CalculateFadeOutCoeff() { |
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, then switch to 'stage_fadeout' |
// 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 |
|
|
65 |
|
#if DEVMODE |
66 |
|
if (TriggerDelay > TotalSamples) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
67 |
|
dmsg(1,("EGADSR: ERROR, TriggerDelay > Totalsamples\n")); |
68 |
|
int* i = NULL; |
69 |
|
(*i)++; // force a segfault |
70 |
|
} |
71 |
|
#endif // DEVMODE |
72 |
|
|
73 |
while (iSample < TotalSamples) { |
while (iSample < TotalSamples) { |
74 |
|
|
75 |
// 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 |
76 |
if (pKillEvent && iSample >= Samples) Stage = stage_fadeout; |
if (itKillEvent && iSample >= Samples) Stage = stage_fadeout; |
77 |
|
|
78 |
switch (Stage) { |
switch (Stage) { |
79 |
case stage_attack: { |
case stage_attack: { |
86 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
87 |
} |
} |
88 |
if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment |
if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment |
89 |
Event* pLastEvent = pEvents->last(); |
RTList<Event>::Iterator itLastEvent = pEvents->last(); |
90 |
if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release); |
if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release); |
91 |
} |
} |
92 |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1; |
93 |
break; |
break; |
100 |
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 |
101 |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
int to_process = RTMath::Min(holdstepsleft, Samples - iSample); |
102 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
103 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
104 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
105 |
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; |
106 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
107 |
} |
} |
108 |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
else if (to_process == holdstepsleft) Stage = stage_decay1; |
109 |
while (iSample < process_end) { |
while (iSample < process_end) { |
114 |
case stage_decay1: { |
case stage_decay1: { |
115 |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
int to_process = RTMath::Min(Samples - iSample, Decay1StepsLeft); |
116 |
int process_end = iSample + to_process; |
int process_end = iSample + to_process; |
117 |
if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) { |
if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) { |
118 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
119 |
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; |
120 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
121 |
} |
} |
122 |
else { |
else { |
123 |
Decay1StepsLeft -= to_process; |
Decay1StepsLeft -= to_process; |
131 |
} |
} |
132 |
case stage_decay2: { |
case stage_decay2: { |
133 |
int process_end; |
int process_end; |
134 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
135 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
136 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
137 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
138 |
} |
} |
139 |
else process_end = Samples; |
else process_end = Samples; |
140 |
while (iSample < process_end) { |
while (iSample < process_end) { |
146 |
} |
} |
147 |
case stage_sustain: { |
case stage_sustain: { |
148 |
int process_end; |
int process_end; |
149 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) { |
150 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
151 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
152 |
Stage = stage_release; // switch to release stage soon |
Stage = stage_release; // switch to release stage soon |
153 |
} |
} |
154 |
else process_end = Samples; |
else process_end = Samples; |
155 |
while (iSample < process_end) { |
while (iSample < process_end) { |
159 |
} |
} |
160 |
case stage_release: { |
case stage_release: { |
161 |
int process_end; |
int process_end; |
162 |
if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) { |
if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) { |
163 |
process_end = pTransitionEvent->FragmentPos(); |
process_end = itTransitionEvent->FragmentPos(); |
164 |
pTransitionEvent = pEvents->next(); |
++itTransitionEvent; |
165 |
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 |
166 |
} |
} |
167 |
else process_end = Samples; |
else process_end = Samples; |
168 |
while (iSample < process_end) { |
while (iSample < process_end) { |
179 |
Level += FadeOutCoeff; |
Level += FadeOutCoeff; |
180 |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level; |
181 |
} |
} |
182 |
if (Level <= FadeOutCoeff) Stage = stage_end; |
Stage = stage_end; |
183 |
|
if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n")); |
184 |
} //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever! |
} //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever! |
185 |
case stage_end: { |
case stage_end: { |
186 |
while (iSample < TotalSamples) { |
while (iSample < TotalSamples) { |
190 |
} |
} |
191 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
|
#if DEVMODE |
195 |
|
if (itKillEvent && Stage != stage_end) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
196 |
|
dmsg(1,("EGADSR: ERROR, voice killing not completed !!!\n")); |
197 |
|
dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos)); |
198 |
|
} |
199 |
|
#endif // DEVMODE |
200 |
} |
} |
201 |
|
|
202 |
/** |
/** |