// ADSR func\r
////////////////////////////////////////////////////////////////////////\r
\r
-unsigned int RateTable[160];\r
+static int RateTableAdd[128];\r
+static int RateTableSub[128];\r
\r
void InitADSR(void) // INIT ADSR\r
{\r
- unsigned int 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
}\r
\r
////////////////////////////////////////////////////////////////////////\r
\r
////////////////////////////////////////////////////////////////////////\r
\r
-INLINE int MixADSR(int ch) // MIX ADSR\r
-{ \r
- static const char ratetable_offset[8] = { 0, 4, 6, 8, 9, 10, 11, 12 };\r
- int rto;\r
+static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR\r
+{\r
+ int EnvelopeVol = s_chan[ch].ADSRX.EnvelopeVol;\r
+ int val, rto, level;\r
+\r
+ if (s_chan[ch].bStop) // should be stopped:\r
+ { // do release\r
+ val = RateTableSub[s_chan[ch].ADSRX.ReleaseRate * 4];\r
+ if (s_chan[ch].ADSRX.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
- if(s_chan[ch].bStop) // should be stopped:\r
- { // do release\r
- if(s_chan[ch].ADSRX.ReleaseModeExp)\r
- {\r
- rto=ratetable_offset[(s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7];\r
- }\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
+ }\r
+ }\r
else\r
- {\r
- rto=12;\r
- }\r
- s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.ReleaseRate^0x1F))-0x18 + rto + 32];\r
+ {\r
+ for (; ns < ns_to; ns++)\r
+ {\r
+ EnvelopeVol += val;\r
+ if (EnvelopeVol < 0)\r
+ break;\r
\r
- if(s_chan[ch].ADSRX.EnvelopeVol<0) \r
- {\r
- s_chan[ch].ADSRX.EnvelopeVol=0;\r
- // FIXME: 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
- dwChannelOn&=~(1<<ch);\r
- //s_chan[ch].bReverb=0;\r
- //s_chan[ch].bNoise=0;\r
- }\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
+ }\r
+ }\r
+\r
+ if (EnvelopeVol < 0)\r
+ goto stop;\r
\r
goto done;\r
- }\r
+ }\r
\r
- switch(s_chan[ch].ADSRX.State) // not stopped yet\r
- {\r
+ switch (s_chan[ch].ADSRX.State)\r
+ {\r
case 0: // -> attack\r
- rto=8;\r
- if(s_chan[ch].ADSRX.AttackModeExp&&s_chan[ch].ADSRX.EnvelopeVol>=0x60000000)\r
rto = 0;\r
- s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.AttackRate^0x7F)-0x18 + rto + 32];\r
+ if (s_chan[ch].ADSRX.AttackModeExp && EnvelopeVol >= 0x60000000)\r
+ rto = 8;\r
+ val = RateTableAdd[s_chan[ch].ADSRX.AttackRate + rto];\r
\r
- if(s_chan[ch].ADSRX.EnvelopeVol<0) \r
+ for (; ns < ns_to; ns++)\r
{\r
- s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;\r
- s_chan[ch].ADSRX.State=1;\r
+ EnvelopeVol += val;\r
+ if (EnvelopeVol < 0)\r
+ break;\r
+\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
}\r
- break;\r
+\r
+ if (EnvelopeVol < 0) // overflow\r
+ {\r
+ EnvelopeVol = 0x7fffffff;\r
+ s_chan[ch].ADSRX.State = 1;\r
+ ns++; // sample is good already\r
+ goto decay;\r
+ }\r
+ break;\r
\r
//--------------------------------------------------//\r
+ decay:\r
case 1: // -> decay\r
- rto=ratetable_offset[(s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7];\r
- s_chan[ch].ADSRX.EnvelopeVol-=RateTable[(4*(s_chan[ch].ADSRX.DecayRate^0x1F))-0x18+ rto + 32];\r
+ val = RateTableSub[s_chan[ch].ADSRX.DecayRate * 4];\r
+ level = s_chan[ch].ADSRX.SustainLevel;\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
+ for (; ns < ns_to; )\r
{\r
- s_chan[ch].ADSRX.State=2;\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
+ s_chan[ch].ADSRX.State = 2;\r
+ goto sustain;\r
+ }\r
}\r
- break;\r
+ break;\r
\r
//--------------------------------------------------//\r
+ sustain:\r
case 2: // -> sustain\r
- if(s_chan[ch].ADSRX.SustainIncrease)\r
+ if (s_chan[ch].ADSRX.SustainIncrease)\r
{\r
- rto=8;\r
- if(s_chan[ch].ADSRX.SustainModeExp&&s_chan[ch].ADSRX.EnvelopeVol>=0x60000000)\r
- rto=0;\r
- s_chan[ch].ADSRX.EnvelopeVol+=RateTable[(s_chan[ch].ADSRX.SustainRate^0x7F)-0x18 + rto + 32];\r
+ if (EnvelopeVol >= 0x7fff0000)\r
+ break;\r
+\r
+ rto = 0;\r
+ if (s_chan[ch].ADSRX.SustainModeExp && EnvelopeVol >= 0x60000000)\r
+ rto = 8;\r
+ val = RateTableAdd[s_chan[ch].ADSRX.SustainRate + rto];\r
\r
- if(s_chan[ch].ADSRX.EnvelopeVol<0) \r
+ for (; ns < ns_to; ns++)\r
{\r
- s_chan[ch].ADSRX.EnvelopeVol=0x7FFFFFFF;\r
+ EnvelopeVol += val;\r
+ if ((unsigned int)EnvelopeVol >= 0x7fe00000)\r
+ {\r
+ EnvelopeVol = 0x7fffffff;\r
+ break;\r
+ }\r
+\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
}\r
}\r
- else\r
+ else\r
{\r
- if(s_chan[ch].ADSRX.SustainModeExp)\r
- {\r
- rto=ratetable_offset[(s_chan[ch].ADSRX.EnvelopeVol>>28)&0x7];\r
- }\r
- else\r
+ val = RateTableSub[s_chan[ch].ADSRX.SustainRate];\r
+ if (s_chan[ch].ADSRX.SustainModeExp)\r
{\r
- rto=12;\r
+ for (; ns < ns_to; ns++)\r
+ {\r
+ EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16);\r
+ if (EnvelopeVol < 0) \r
+ goto stop;\r
+\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
+ }\r
}\r
- s_chan[ch].ADSRX.EnvelopeVol-=RateTable[((s_chan[ch].ADSRX.SustainRate^0x7F))-0x1B + rto + 32];\r
-\r
- if(s_chan[ch].ADSRX.EnvelopeVol<0) \r
+ else\r
{\r
- s_chan[ch].ADSRX.EnvelopeVol=0;\r
+ for (; ns < ns_to; ns++)\r
+ {\r
+ EnvelopeVol += val;\r
+ if (EnvelopeVol < 0) \r
+ goto stop;\r
+\r
+ ChanBuf[ns] *= EnvelopeVol >> 21;\r
+ ChanBuf[ns] >>= 10;\r
+ }\r
}\r
}\r
- break;\r
- }\r
+ break;\r
+ }\r
\r
done:\r
- return s_chan[ch].ADSRX.EnvelopeVol>>21;\r
+ s_chan[ch].ADSRX.EnvelopeVol = EnvelopeVol;\r
+ return;\r
+\r
+stop:\r
+ memset(&ChanBuf[ns], 0, (ns_to - ns) * sizeof(ChanBuf[0]));\r
+ s_chan[ch].ADSRX.EnvelopeVol = 0;\r
+ dwChannelOn &= ~(1<<ch);\r
}\r
\r
#endif\r
{ 115, -52 },
{ 98, -55 },
{ 122, -60 } };
+int ChanBuf[NSSIZE];
int SSumLR[NSSIZE*2];
int iFMod[NSSIZE];
int iCycle = 0;
for(ns=ns_from;ns<ns_to;ns++) // loop until 1 ms of data is reached
{
- int sval;
-
if(!(dwChannelOn&(1<<ch))) break; // something turned ch off (adsr or flags)
if(s_chan[ch].bFMod==1 && iFMod[ns]) // fmod freq channel
if(s_chan[ch].bNoise)
fa=iGetNoiseVal(ch); // get noise val
else fa=iGetInterpolationVal(ch); // get sample val
+ ChanBuf[ns]=fa;
+
+ ////////////////////////////////////////////////
+ // ok, go on until 1 ms data of this channel is collected
+
+ s_chan[ch].spos += s_chan[ch].sinc;
+ENDX: ;
+ }
- sval = (MixADSR(ch) * fa) / 1024; // mix adsr
+ MixADSR(ch, ns_from, ns_to);
+
+ if(s_chan[ch].bFMod==2) // fmod freq channel
+ memcpy(iFMod, ChanBuf, sizeof(iFMod));
+ else for(ns=ns_from;ns<ns_to;ns++)
+ {
+ int sval = ChanBuf[ns];
- if(s_chan[ch].bFMod==2) // fmod freq channel
- iFMod[ns]=sval; // -> store 1T sample data, use that to do fmod on next channel
- else // no fmod freq channel
{
//////////////////////////////////////////////
// ok, left/right sound volume (psx volume goes from 0 ... 0x3fff)
if(s_chan[ch].bRVBActive) StoreREVERB(ch,ns,sval);
}
-
- ////////////////////////////////////////////////
- // ok, go on until 1 ms data of this channel is collected
-
- s_chan[ch].spos += s_chan[ch].sinc;
}
-ENDX: ;
}
}