74 |
break; |
break; |
75 |
case stage_decay1_part1: |
case stage_decay1_part1: |
76 |
switch (Event) { |
switch (Event) { |
77 |
case stage_end: |
case event_stage_end: |
78 |
enterDecay1Part2Stage(); |
enterDecay1Part2Stage(SampleRate); |
79 |
break; |
break; |
80 |
case event_release: |
case event_release: |
81 |
enterReleasePart1Stage(); |
enterReleasePart1Stage(); |
162 |
|
|
163 |
void EGADSR::trigger(uint PreAttack, float AttackTime, bool HoldAttack, long LoopStart, float Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, float ReleaseTime, float Volume, uint SampleRate) { |
void EGADSR::trigger(uint PreAttack, float AttackTime, bool HoldAttack, long LoopStart, float Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, float ReleaseTime, float Volume, uint SampleRate) { |
164 |
|
|
|
enterAttackStage(PreAttack, AttackTime, SampleRate, 0.0, 1.0f); |
|
|
|
|
165 |
if (SustainLevel) { |
if (SustainLevel) { |
166 |
this->SustainLevel = SustainLevel / 1000.0; |
this->SustainLevel = SustainLevel / 1000.0; |
167 |
} else { |
} else { |
189 |
ReleaseCoeff2 = exp(ReleaseSlope); |
ReleaseCoeff2 = exp(ReleaseSlope); |
190 |
ReleaseCoeff3 = ExpOffset * (1 - ReleaseCoeff2); |
ReleaseCoeff3 = ExpOffset * (1 - ReleaseCoeff2); |
191 |
ReleaseLevel2 = 0.25 * invVolume; |
ReleaseLevel2 = 0.25 * invVolume; |
192 |
|
|
193 |
|
enterAttackStage(PreAttack, AttackTime, SampleRate, 0.0, 1.0f); |
194 |
} |
} |
195 |
|
|
196 |
void EGADSR::enterAttackStage(const uint PreAttack, const float AttackTime, const uint SampleRate, const double SamplePos, const float CurrentPitch) { |
void EGADSR::enterAttackStage(const uint PreAttack, const float AttackTime, const uint SampleRate, const double SamplePos, const float CurrentPitch) { |
203 |
Level = (float) PreAttack / 1000.0; |
Level = (float) PreAttack / 1000.0; |
204 |
Coeff = 0.896f * (1.0f - Level) / StepsLeft; // max level is a bit lower if attack != 0 |
Coeff = 0.896f * (1.0f - Level) / StepsLeft; // max level is a bit lower if attack != 0 |
205 |
} else { // immediately jump to the next stage |
} else { // immediately jump to the next stage |
206 |
|
Level = 1.0; |
207 |
if (HoldAttack) enterAttackHoldStage(SamplePos, CurrentPitch); |
if (HoldAttack) enterAttackHoldStage(SamplePos, CurrentPitch); |
208 |
else enterDecay1Part1Stage(SampleRate); |
else enterDecay1Part1Stage(SampleRate); |
209 |
} |
} |
234 |
Decay1Slope = 1.365 * (SustainLevel - 1.0) / StepsLeft; |
Decay1Slope = 1.365 * (SustainLevel - 1.0) / StepsLeft; |
235 |
Coeff = Decay1Slope * invVolume; |
Coeff = Decay1Slope * invVolume; |
236 |
Decay1Level2 = 0.25 * invVolume; |
Decay1Level2 = 0.25 * invVolume; |
237 |
StepsLeft = int((RTMath::Max(Decay1Level2, SustainLevel) - Level) / Coeff); |
if (Level < Decay1Level2) enterDecay1Part2Stage(SampleRate); |
238 |
|
else StepsLeft = int((RTMath::Max(Decay1Level2, SustainLevel) - Level) / Coeff); |
239 |
} else { |
} else { |
|
Level = SustainLevel; |
|
240 |
if (InfiniteSustain) enterSustainStage(); |
if (InfiniteSustain) enterSustainStage(); |
241 |
else enterDecay2Stage(SampleRate); |
else enterDecay2Stage(SampleRate); |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
245 |
void EGADSR::enterDecay1Part2Stage() { |
void EGADSR::enterDecay1Part2Stage(const uint SampleRate) { |
246 |
Stage = stage_decay1_part2; |
if (SustainLevel < Decay1Level2) { |
247 |
Segment = segment_exp; |
Stage = stage_decay1_part2; |
248 |
Decay1Slope *= 3.55; |
Segment = segment_exp; |
249 |
Coeff = exp(Decay1Slope); |
Decay1Slope *= 3.55; |
250 |
Offset = ExpOffset * (1 - Coeff); |
Coeff = exp(Decay1Slope); |
251 |
Level = Decay1Level2; |
Offset = ExpOffset * (1 - Coeff); |
252 |
StepsLeft = int(log((SustainLevel - ExpOffset) / (Level - ExpOffset)) / Decay1Slope); |
StepsLeft = int(log((SustainLevel - ExpOffset) / (Level - ExpOffset)) / Decay1Slope); |
253 |
|
} else { |
254 |
|
if (InfiniteSustain) enterSustainStage(); |
255 |
|
else enterDecay2Stage(SampleRate); |
256 |
|
} |
257 |
} |
} |
258 |
|
|
259 |
void EGADSR::enterDecay2Stage(const uint SampleRate) { |
void EGADSR::enterDecay2Stage(const uint SampleRate) { |
264 |
Coeff = (-1.03 / StepsLeft) * invVolume; |
Coeff = (-1.03 / StepsLeft) * invVolume; |
265 |
//FIXME: do we really have to calculate 'StepsLeft' two times? |
//FIXME: do we really have to calculate 'StepsLeft' two times? |
266 |
StepsLeft = int((CONFIG_EG_BOTTOM - Level) / Coeff); |
StepsLeft = int((CONFIG_EG_BOTTOM - Level) / Coeff); |
267 |
|
if (StepsLeft == 0) enterEndStage(); |
268 |
} |
} |
269 |
|
|
270 |
void EGADSR::enterSustainStage() { |
void EGADSR::enterSustainStage() { |
295 |
Segment = segment_lin; |
Segment = segment_lin; |
296 |
StepsLeft = int(Level / (-FadeOutCoeff)); |
StepsLeft = int(Level / (-FadeOutCoeff)); |
297 |
Coeff = FadeOutCoeff; |
Coeff = FadeOutCoeff; |
298 |
|
if (StepsLeft == 0) enterEndStage(); |
299 |
} |
} |
300 |
|
|
301 |
void EGADSR::enterEndStage() { |
void EGADSR::enterEndStage() { |