2015-10-10 02:16:12 +00:00
|
|
|
auto DSP::envelopeRun(Voice& v) -> void {
|
|
|
|
signed envelope = v.envelope;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2015-10-10 02:16:12 +00:00
|
|
|
if(v.envelopeMode == EnvelopeRelease) { //60%
|
|
|
|
envelope -= 0x8;
|
|
|
|
if(envelope < 0) envelope = 0;
|
|
|
|
v.envelope = envelope;
|
2010-08-09 13:28:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-10 02:16:12 +00:00
|
|
|
signed rate;
|
|
|
|
signed envelopeData = VREG(ADSR1);
|
|
|
|
if(state._adsr0 & 0x80) { //99% ADSR
|
|
|
|
if(v.envelopeMode >= EnvelopeDecay) { //99%
|
|
|
|
envelope--;
|
|
|
|
envelope -= envelope >> 8;
|
|
|
|
rate = envelopeData & 0x1f;
|
|
|
|
if(v.envelopeMode == EnvelopeDecay) { //1%
|
|
|
|
rate = ((state._adsr0 >> 3) & 0x0e) + 0x10;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
2013-05-05 09:21:30 +00:00
|
|
|
} else { //env_attack
|
2015-10-10 02:16:12 +00:00
|
|
|
rate = ((state._adsr0 & 0x0f) << 1) + 1;
|
|
|
|
envelope += rate < 31 ? 0x20 : 0x400;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
2013-05-05 09:21:30 +00:00
|
|
|
} else { //GAIN
|
2015-10-10 02:16:12 +00:00
|
|
|
envelopeData = VREG(GAIN);
|
|
|
|
signed mode = envelopeData >> 5;
|
2013-05-05 09:21:30 +00:00
|
|
|
if(mode < 4) { //direct
|
2015-10-10 02:16:12 +00:00
|
|
|
envelope = envelopeData << 4;
|
2010-08-09 13:28:56 +00:00
|
|
|
rate = 31;
|
|
|
|
} else {
|
2015-10-10 02:16:12 +00:00
|
|
|
rate = envelopeData & 0x1f;
|
2013-05-05 09:21:30 +00:00
|
|
|
if(mode == 4) { //4: linear decrease
|
2015-10-10 02:16:12 +00:00
|
|
|
envelope -= 0x20;
|
2013-05-05 09:21:30 +00:00
|
|
|
} else if(mode < 6) { //5: exponential decrease
|
2015-10-10 02:16:12 +00:00
|
|
|
envelope--;
|
|
|
|
envelope -= envelope >> 8;
|
2013-05-05 09:21:30 +00:00
|
|
|
} else { //6, 7: linear increase
|
2015-10-10 02:16:12 +00:00
|
|
|
envelope += 0x20;
|
|
|
|
if(mode > 6 && (unsigned)v.hiddenEnvelope >= 0x600) {
|
|
|
|
envelope += 0x8 - 0x20; //7: two-slope linear increase
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//sustain level
|
2015-10-10 02:16:12 +00:00
|
|
|
if((envelope >> 8) == (envelopeData >> 5) && v.envelopeMode == EnvelopeDecay) v.envelopeMode = EnvelopeSustain;
|
|
|
|
v.hiddenEnvelope = envelope;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
//unsigned cast because linear decrease underflowing also triggers this
|
2015-10-10 02:16:12 +00:00
|
|
|
if((unsigned)envelope > 0x7ff) {
|
|
|
|
envelope = (envelope < 0 ? 0 : 0x7ff);
|
|
|
|
if(v.envelopeMode == EnvelopeAttack) v.envelopeMode = EnvelopeDecay;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 02:16:12 +00:00
|
|
|
if(counterPoll(rate)) v.envelope = envelope;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|