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