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) ? itKillEvent->FragmentPos() : (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_fadeout; |
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) { |
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) { |