X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=pcsx_rearmed.git;a=blobdiff_plain;f=plugins%2Fdfsound%2Fadsr.c;h=9e328620a0ca52201c9ec1193ce43b6d5820becc;hp=1e397af7851efe4feb2e6ed11ef71b37e5a2090e;hb=c632283d5c48d7731ec5704c3d5eef54951fec20;hpb=6d866bb7894b252fc430a24a97769511d5ead1ca diff --git a/plugins/dfsound/adsr.c b/plugins/dfsound/adsr.c index 1e397af7..9e328620 100644 --- a/plugins/dfsound/adsr.c +++ b/plugins/dfsound/adsr.c @@ -26,174 +26,322 @@ // ADSR func //////////////////////////////////////////////////////////////////////// -unsigned long RateTable[160]; +static int RateTableAdd[128]; +static int RateTableSub[128]; void InitADSR(void) // INIT ADSR { - unsigned long r,rs,rd;int i; - - memset(RateTable,0,sizeof(unsigned long)*160); // build the rate table according to Neill's rules (see at bottom of file) - - r=3;rs=1;rd=0; - - for(i=32;i<160;i++) // we start at pos 32 with the real values... everything before is 0 - { - if(r<0x3FFFFFFF) - { - r+=rs; - rd++;if(rd==5) {rd=1;rs*=2;} - } - if(r>0x3FFFFFFF) r=0x3FFFFFFF; - - RateTable[i]=r; - } + int lcv, denom; + + // Optimize table - Dr. Hell ADSR math + for (lcv = 0; lcv < 48; lcv++) + { + RateTableAdd[lcv] = (7 - (lcv&3)) << (11 + 16 - (lcv >> 2)); + RateTableSub[lcv] = (-8 + (lcv&3)) << (11 + 16 - (lcv >> 2)); + } + + for (; lcv < 128; lcv++) + { + denom = 1 << ((lcv>>2) - 11); + + RateTableAdd[lcv] = ((7 - (lcv&3)) << 16) / denom; + RateTableSub[lcv] = ((-8 + (lcv&3)) << 16) / denom; + + // XXX: this is wrong, we need more bits.. + if (RateTableAdd[lcv] == 0) + RateTableAdd[lcv] = 1; + } } //////////////////////////////////////////////////////////////////////// INLINE void StartADSR(int ch) // MIX ADSR { - s_chan[ch].ADSRX.State=0; // and init some adsr vars - s_chan[ch].ADSRX.EnvelopeVol=0; + spu.s_chan[ch].ADSRX.State = ADSR_ATTACK; // and init some adsr vars + spu.s_chan[ch].ADSRX.EnvelopeVol = 0; } //////////////////////////////////////////////////////////////////////// -INLINE int MixADSR(int ch) // MIX ADSR -{ - if(s_chan[ch].bStop) // should be stopped: - { // do release - if(s_chan[ch].ADSRX.ReleaseModeExp) - { - switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7) - { - case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +0 + 32]; break; - case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +4 + 32]; break; - case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +6 + 32]; break; - case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +8 + 32]; break; - case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +9 + 32]; break; - case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +10+ 32]; break; - case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +11+ 32]; break; - case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 +12+ 32]; break; - } - } +static int MixADSR(ADSRInfoEx *adsr, int ns_to) +{ + int EnvelopeVol = adsr->EnvelopeVol; + int ns = 0, val, rto, level; + + if (adsr->State == ADSR_RELEASE) + { + val = RateTableSub[adsr->ReleaseRate * 4]; + + if (adsr->ReleaseModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol <= 0) + break; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + } else - { - s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x0C + 32]; - } + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if (EnvelopeVol <= 0) + break; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + } + + goto done; + } + + switch (adsr->State) + { + case ADSR_ATTACK: // -> attack + rto = 0; + if (adsr->AttackModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->AttackRate + rto]; + + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if (EnvelopeVol < 0) + break; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + + if (EnvelopeVol < 0) // overflow + { + EnvelopeVol = 0x7fffffff; + adsr->State = ADSR_DECAY; + ns++; // sample is good already + goto decay; + } + break; - if(s_chan[ch].ADSRX.EnvelopeVol<0) - { - s_chan[ch].ADSRX.EnvelopeVol=0; - // don't stop if this chan can still cause irqs - if(!(spuCtrl&0x40) || (s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)) - //s_chan[ch].bOn=0; - s_chan[ch].pCurr=(unsigned char *)-1; - //s_chan[ch].bReverb=0; - //s_chan[ch].bNoise=0; - } + //--------------------------------------------------// + decay: + case ADSR_DECAY: // -> decay + val = RateTableSub[adsr->DecayRate * 4]; + level = adsr->SustainLevel; + + for (; ns < ns_to; ) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + EnvelopeVol = 0; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + ns++; + + if (((EnvelopeVol >> 27) & 0xf) <= level) + { + adsr->State = ADSR_SUSTAIN; + goto sustain; + } + } + break; - return s_chan[ch].ADSRX.EnvelopeVol>>21; - } - else // not stopped yet? - { - if(s_chan[ch].ADSRX.State==0) // -> attack - { - if(s_chan[ch].ADSRX.AttackModeExp) - { - if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000) - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32]; - else - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x18 + 32]; - } + //--------------------------------------------------// + sustain: + case ADSR_SUSTAIN: // -> sustain + if (adsr->SustainIncrease) + { + if (EnvelopeVol >= 0x7fff0000) + { + ns = ns_to; + break; + } + + rto = 0; + if (adsr->SustainModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->SustainRate + rto]; + + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if ((unsigned int)EnvelopeVol >= 0x7fe00000) + { + EnvelopeVol = 0x7fffffff; + ns = ns_to; + break; + } + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + } else - { - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x10 + 32]; - } + { + val = RateTableSub[adsr->SustainRate]; + if (adsr->SustainModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + break; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + } + else + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if (EnvelopeVol < 0) + break; + + ChanBuf[ns] *= EnvelopeVol >> 21; + ChanBuf[ns] >>= 10; + } + } + } + break; + } + +done: + adsr->EnvelopeVol = EnvelopeVol; + return ns; +} - if(s_chan[ch].ADSRX.EnvelopeVol<0) - { - s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF; - s_chan[ch].ADSRX.State=1; - } +static int SkipADSR(ADSRInfoEx *adsr, int ns_to) +{ + int EnvelopeVol = adsr->EnvelopeVol; + int ns = 0, val, rto, level; + int64_t v64; + + if (adsr->State == ADSR_RELEASE) + { + val = RateTableSub[adsr->ReleaseRate * 4]; + if (adsr->ReleaseModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol <= 0) + break; + } + } + else + { + v64 = EnvelopeVol; + v64 += (int64_t)val * ns_to; + EnvelopeVol = (int)v64; + if (v64 > 0) + ns = ns_to; + } + goto done; + } + + switch (adsr->State) + { + case ADSR_ATTACK: // -> attack + rto = 0; + if (adsr->AttackModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->AttackRate + rto]; + + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if (EnvelopeVol < 0) + break; + } + if (EnvelopeVol < 0) // overflow + { + EnvelopeVol = 0x7fffffff; + adsr->State = ADSR_DECAY; + ns++; + goto decay; + } + break; - return s_chan[ch].ADSRX.EnvelopeVol>>21; - } //--------------------------------------------------// - if(s_chan[ch].ADSRX.State==1) // -> decay - { - switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7) - { - case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+0 + 32]; break; - case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+4 + 32]; break; - case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+6 + 32]; break; - case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+8 + 32]; break; - case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+9 + 32]; break; - case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+10+ 32]; break; - case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+11+ 32]; break; - case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+12+ 32]; break; - } - - if(s_chan[ch].ADSRX.EnvelopeVol<0) s_chan[ch].ADSRX.EnvelopeVol=0; - if(((s_chan[ch].ADSRX.EnvelopeVol>>27)&0xF) <= s_chan[ch].ADSRX.SustainLevel) - { - s_chan[ch].ADSRX.State=2; - } + decay: + case ADSR_DECAY: // -> decay + val = RateTableSub[adsr->DecayRate * 4]; + level = adsr->SustainLevel; + + for (; ns < ns_to; ) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + EnvelopeVol = 0; + + ns++; + + if (((EnvelopeVol >> 27) & 0xf) <= level) + { + adsr->State = ADSR_SUSTAIN; + goto sustain; + } + } + break; - return s_chan[ch].ADSRX.EnvelopeVol>>21; - } //--------------------------------------------------// - if(s_chan[ch].ADSRX.State==2) // -> sustain - { - if(s_chan[ch].ADSRX.SustainIncrease) - { - if(s_chan[ch].ADSRX.SustainModeExp) - { - if(s_chan[ch].ADSRX.EnvelopeVol<0x60000000) - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32]; - else - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x18 + 32]; - } - else - { - s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x10 + 32]; - } - - if(s_chan[ch].ADSRX.EnvelopeVol<0) - { - s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF; - } - } + sustain: + case ADSR_SUSTAIN: // -> sustain + if (adsr->SustainIncrease) + { + ns = ns_to; + + if (EnvelopeVol >= 0x7fff0000) + break; + + rto = 0; + if (adsr->SustainModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->SustainRate + rto]; + + v64 = EnvelopeVol; + v64 += (int64_t)val * (ns_to - ns); + EnvelopeVol = (int)v64; + if (v64 >= 0x7fe00000ll) + EnvelopeVol = 0x7fffffff; + } else - { - if(s_chan[ch].ADSRX.SustainModeExp) - { - switch((s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7) - { - case 0: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +0 + 32];break; - case 1: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +4 + 32];break; - case 2: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +6 + 32];break; - case 3: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +8 + 32];break; - case 4: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +9 + 32];break; - case 5: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +10+ 32];break; - case 6: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +11+ 32];break; - case 7: s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B +12+ 32];break; - } - } + { + val = RateTableSub[adsr->SustainRate]; + if (adsr->SustainModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + break; + } + } else - { - s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x0F + 32]; - } - - if(s_chan[ch].ADSRX.EnvelopeVol<0) - { - s_chan[ch].ADSRX.EnvelopeVol=0; - } - } - return s_chan[ch].ADSRX.EnvelopeVol>>21; - } - } - return 0; + { + v64 = EnvelopeVol; + v64 += (int64_t)val * (ns_to - ns); + EnvelopeVol = (int)v64; + if (v64 > 0) + { + ns = ns_to; + break; + } + } + } + break; + } + +done: + adsr->EnvelopeVol = EnvelopeVol; + return ns; } #endif @@ -637,3 +785,4 @@ Logarithmic release mode: ----------------------------------------------------------------------------- */ +// vim:shiftwidth=1:expandtab