spu: move more things to StartSound
[pcsx_rearmed.git] / plugins / dfsound / registers.c
index ac495d6..8e4956e 100644 (file)
 \r
 #include "externals.h"\r
 #include "registers.h"\r
-#include "regs.h"\r
-#include "reverb.h"\r
+#include "spu_config.h"\r
 \r
-/*\r
-// adsr time values (in ms) by James Higgs ... see the end of\r
-// the adsr.c source for details\r
-\r
-#define ATTACK_MS     514L\r
-#define DECAYHALF_MS  292L\r
-#define DECAY_MS      584L\r
-#define SUSTAIN_MS    450L\r
-#define RELEASE_MS    446L\r
-*/\r
-\r
-// we have a timebase of 1.020408f ms, not 1 ms... so adjust adsr defines\r
-#define ATTACK_MS      494L\r
-#define DECAYHALF_MS   286L\r
-#define DECAY_MS       572L\r
-#define SUSTAIN_MS     441L\r
-#define RELEASE_MS     437L\r
+static void SoundOn(int start,int end,unsigned short val);\r
+static void SoundOff(int start,int end,unsigned short val);\r
+static void FModOn(int start,int end,unsigned short val);\r
+static void NoiseOn(int start,int end,unsigned short val);\r
+static void SetVolumeL(unsigned char ch,short vol);\r
+static void SetVolumeR(unsigned char ch,short vol);\r
+static void SetPitch(int ch,unsigned short val);\r
+static void ReverbOn(int start,int end,unsigned short val);\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // WRITE REGISTERS: called by main emu\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)\r
+static const uint32_t ignore_dupe[8] = {\r
+ // ch 0-15  c40         c80         cc0\r
+ 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f,\r
+ // ch 16-24 d40         control     reverb\r
+ 0x7f7f7f7f, 0x7f7f7f7f, 0xff05ff0f, 0xffffffff\r
+};\r
+\r
+void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,\r
+ unsigned int cycles)\r
 {\r
- const unsigned long r=reg&0xfff;\r
- regArea[(r-0xc00)>>1] = val;\r
+ int r = reg & 0xfff;\r
+ int rofs = (r - 0xc00) >> 1;\r
+ int changed = spu.regArea[rofs] != val;\r
+ spu.regArea[rofs] = val;\r
+\r
+ if (!changed && (ignore_dupe[rofs >> 5] & (1 << (rofs & 0x1f))))\r
+  return;\r
+ // zero keyon/keyoff?\r
+ if (val == 0 && (r & 0xff8) == 0xd88)\r
+  return;\r
+\r
+ do_samples_if_needed(cycles);\r
 \r
  if(r>=0x0c00 && r<0x0d80)                             // some channel info?\r
   {\r
@@ -67,11 +75,10 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
      //------------------------------------------------// pitch\r
      case 4:                                           \r
        SetPitch(ch,val);\r
-       break;\r
+       goto upd_irq;\r
      //------------------------------------------------// start\r
      case 6:      \r
-       // Brain Dead 13 - align to 16 boundary\r
-       s_chan[ch].pStart= spuMemC+(unsigned long)((val<<3)&~0xf);\r
+       // taken from regArea later\r
        break;\r
      //------------------------------------------------// level with pre-calcs\r
      case 8:\r
@@ -83,35 +90,6 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
         s_chan[ch].ADSRX.DecayRate=(lval>>4) & 0x000f;\r
         s_chan[ch].ADSRX.SustainLevel=lval & 0x000f;\r
         //---------------------------------------------//\r
-#if 0\r
-        if(!iDebugMode) break;\r
-        //---------------------------------------------// stuff below is only for debug mode\r
-\r
-        s_chan[ch].ADSR.AttackModeExp=(lval&0x8000)?1:0;        //0x007f\r
-\r
-        lx=(((lval>>8) & 0x007f)>>2);                  // attack time to run from 0 to 100% volume\r
-        lx=min(31,lx);                                 // no overflow on shift!\r
-        if(lx) \r
-         { \r
-          lx = (1<<lx);\r
-          if(lx<2147483) lx=(lx*ATTACK_MS)/10000L;     // another overflow check\r
-          else           lx=(lx/10000L)*ATTACK_MS;\r
-          if(!lx) lx=1;\r
-         }\r
-        s_chan[ch].ADSR.AttackTime=lx;                \r
-\r
-        s_chan[ch].ADSR.SustainLevel=                 // our adsr vol runs from 0 to 1024, so scale the sustain level\r
-         (1024*((lval) & 0x000f))/15;\r
-\r
-        lx=(lval>>4) & 0x000f;                         // decay:\r
-        if(lx)                                         // our const decay value is time it takes from 100% to 0% of volume\r
-         {\r
-          lx = ((1<<(lx))*DECAY_MS)/10000L;\r
-          if(!lx) lx=1;\r
-         }\r
-        s_chan[ch].ADSR.DecayTime =                   // so calc how long does it take to run from 100% to the wanted sus level\r
-         (lx*(1024-s_chan[ch].ADSR.SustainLevel))/1024;\r
-#endif\r
        }\r
       break;\r
      //------------------------------------------------// adsr times with pre-calcs\r
@@ -126,39 +104,6 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
        s_chan[ch].ADSRX.ReleaseModeExp = (lval&0x0020)?1:0;\r
        s_chan[ch].ADSRX.ReleaseRate = lval & 0x001f;\r
        //----------------------------------------------//\r
-#if 0\r
-       if(!iDebugMode) break;\r
-       //----------------------------------------------// stuff below is only for debug mode\r
-\r
-       s_chan[ch].ADSR.SustainModeExp = (lval&0x8000)?1:0;\r
-       s_chan[ch].ADSR.ReleaseModeExp = (lval&0x0020)?1:0;\r
-                   \r
-       lx=((((lval>>6) & 0x007f)>>2));                 // sustain time... often very high\r
-       lx=min(31,lx);                                  // values are used to hold the volume\r
-       if(lx)                                          // until a sound stop occurs\r
-        {                                              // the highest value we reach (due to \r
-         lx = (1<<lx);                                 // overflow checking) is: \r
-         if(lx<2147483) lx=(lx*SUSTAIN_MS)/10000L;     // 94704 seconds = 1578 minutes = 26 hours... \r
-         else           lx=(lx/10000L)*SUSTAIN_MS;     // should be enuff... if the stop doesn't \r
-         if(!lx) lx=1;                                 // come in this time span, I don't care :)\r
-        }\r
-       s_chan[ch].ADSR.SustainTime = lx;\r
-\r
-       lx=(lval & 0x001f);\r
-       s_chan[ch].ADSR.ReleaseVal     =lx;\r
-       if(lx)                                          // release time from 100% to 0%\r
-        {                                              // note: the release time will be\r
-         lx = (1<<lx);                                 // adjusted when a stop is coming,\r
-         if(lx<2147483) lx=(lx*RELEASE_MS)/10000L;     // so at this time the adsr vol will \r
-         else           lx=(lx/10000L)*RELEASE_MS;     // run from (current volume) to 0%\r
-         if(!lx) lx=1;\r
-        }\r
-       s_chan[ch].ADSR.ReleaseTime=lx;\r
-\r
-       if(lval & 0x4000)                               // add/dec flag\r
-            s_chan[ch].ADSR.SustainModeDec=-1;\r
-       else s_chan[ch].ADSR.SustainModeDec=1;\r
-#endif\r
       }\r
      break;\r
      //------------------------------------------------// adsr volume... mmm have to investigate this\r
@@ -166,14 +111,10 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
        break;\r
      //------------------------------------------------//\r
      case 14:                                          // loop?\r
-       //WaitForSingleObject(s_chan[ch].hMutex,2000);        // -> no multithread fuckups\r
-       s_chan[ch].pLoop=spuMemC+((unsigned long)((val<<3)&~0xf));\r
-       //s_chan[ch].bIgnoreLoop=1;\r
-       //ReleaseMutex(s_chan[ch].hMutex);                    // -> oki, on with the thread\r
-       break;\r
+       s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3);\r
+       goto upd_irq;\r
      //------------------------------------------------//\r
     }\r
-   iSpuAsyncWait=0;\r
    return;\r
   }\r
 \r
@@ -181,23 +122,26 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
    {\r
     //-------------------------------------------------//\r
     case H_SPUaddr:\r
-      spuAddr = (unsigned long) val<<3;\r
+      spu.spuAddr = (unsigned long) val<<3;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUdata:\r
-      spuMem[spuAddr>>1] = val;\r
-      spuAddr+=2;\r
-      if(spuAddr>0x7ffff) spuAddr=0;\r
+      spu.spuMem[spu.spuAddr>>1] = val;\r
+      spu.spuAddr+=2;\r
+      if(spu.spuAddr>0x7ffff) spu.spuAddr=0;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUctrl:\r
-      if(!(spuCtrl & CTRL_IRQ))\r
-        spuStat&=~STAT_IRQ;\r
-      spuCtrl=val;\r
+      if (!(spu.spuCtrl & CTRL_IRQ)) {\r
+        spu.spuStat&=~STAT_IRQ;\r
+        if (val & CTRL_IRQ)\r
+         schedule_next_irq();\r
+      }\r
+      spu.spuCtrl=val;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUstat:\r
-      spuStat=val & 0xf800;\r
+      spu.spuStat=val&0xf800;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUReverbAddr:\r
@@ -212,12 +156,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
           rvb.CurrAddr=rvb.StartAddr;\r
          }\r
        }\r
-      break;\r
+      goto rvbd;\r
     //-------------------------------------------------//\r
     case H_SPUirqAddr:\r
-      spuIrq = val;\r
-      pSpuIrq=spuMemC+(((unsigned long) val<<3)&~0xf);\r
-      break;\r
+      spu.pSpuIrq=spu.spuMemC+(((unsigned long) val<<3)&~0xf);\r
+      goto upd_irq;\r
     //-------------------------------------------------//\r
     case H_SPUrvolL:\r
       rvb.VolLeft=val;\r
@@ -271,12 +214,12 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
       break;\r
     //-------------------------------------------------//\r
     case H_CDLeft:\r
-      iLeftXAVol=val  & 0x7fff;\r
-      if(cddavCallback) cddavCallback(0,val);\r
+      spu.iLeftXAVol=val  & 0x7fff;\r
+      if(spu.cddavCallback) spu.cddavCallback(0,val);\r
       break;\r
     case H_CDRight:\r
-      iRightXAVol=val & 0x7fff;\r
-      if(cddavCallback) cddavCallback(1,val);\r
+      spu.iRightXAVol=val & 0x7fff;\r
+      if(spu.cddavCallback) spu.cddavCallback(1,val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_FMod1:\r
@@ -303,52 +246,48 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
       ReverbOn(16,24,val);\r
       break;\r
     //-------------------------------------------------//\r
-    case H_Reverb+0:\r
-\r
-      rvb.FB_SRC_A=val;\r
-\r
-      // OK, here's the fake REVERB stuff...\r
-      // depending on effect we do more or less delay and repeats... bah\r
-      // still... better than nothing :)\r
-\r
-      SetREVERB(val);\r
-      break;\r
-\r
-\r
-    case H_Reverb+2   : rvb.FB_SRC_B=(short)val;       break;\r
-    case H_Reverb+4   : rvb.IIR_ALPHA=(short)val;      break;\r
-    case H_Reverb+6   : rvb.ACC_COEF_A=(short)val;     break;\r
-    case H_Reverb+8   : rvb.ACC_COEF_B=(short)val;     break;\r
-    case H_Reverb+10  : rvb.ACC_COEF_C=(short)val;     break;\r
-    case H_Reverb+12  : rvb.ACC_COEF_D=(short)val;     break;\r
-    case H_Reverb+14  : rvb.IIR_COEF=(short)val;       break;\r
-    case H_Reverb+16  : rvb.FB_ALPHA=(short)val;       break;\r
-    case H_Reverb+18  : rvb.FB_X=(short)val;           break;\r
-    case H_Reverb+20  : rvb.IIR_DEST_A0=(short)val;    break;\r
-    case H_Reverb+22  : rvb.IIR_DEST_A1=(short)val;    break;\r
-    case H_Reverb+24  : rvb.ACC_SRC_A0=(short)val;     break;\r
-    case H_Reverb+26  : rvb.ACC_SRC_A1=(short)val;     break;\r
-    case H_Reverb+28  : rvb.ACC_SRC_B0=(short)val;     break;\r
-    case H_Reverb+30  : rvb.ACC_SRC_B1=(short)val;     break;\r
-    case H_Reverb+32  : rvb.IIR_SRC_A0=(short)val;     break;\r
-    case H_Reverb+34  : rvb.IIR_SRC_A1=(short)val;     break;\r
-    case H_Reverb+36  : rvb.IIR_DEST_B0=(short)val;    break;\r
-    case H_Reverb+38  : rvb.IIR_DEST_B1=(short)val;    break;\r
-    case H_Reverb+40  : rvb.ACC_SRC_C0=(short)val;     break;\r
-    case H_Reverb+42  : rvb.ACC_SRC_C1=(short)val;     break;\r
-    case H_Reverb+44  : rvb.ACC_SRC_D0=(short)val;     break;\r
-    case H_Reverb+46  : rvb.ACC_SRC_D1=(short)val;     break;\r
-    case H_Reverb+48  : rvb.IIR_SRC_B1=(short)val;     break;\r
-    case H_Reverb+50  : rvb.IIR_SRC_B0=(short)val;     break;\r
-    case H_Reverb+52  : rvb.MIX_DEST_A0=(short)val;    break;\r
-    case H_Reverb+54  : rvb.MIX_DEST_A1=(short)val;    break;\r
-    case H_Reverb+56  : rvb.MIX_DEST_B0=(short)val;    break;\r
-    case H_Reverb+58  : rvb.MIX_DEST_B1=(short)val;    break;\r
-    case H_Reverb+60  : rvb.IN_COEF_L=(short)val;      break;\r
-    case H_Reverb+62  : rvb.IN_COEF_R=(short)val;      break;\r
+    case H_Reverb+0   : rvb.FB_SRC_A=val*4;         goto rvbd;\r
+    case H_Reverb+2   : rvb.FB_SRC_B=val*4;         goto rvbd;\r
+    case H_Reverb+4   : rvb.IIR_ALPHA=(short)val;   goto rvbd;\r
+    case H_Reverb+6   : rvb.ACC_COEF_A=(short)val;  goto rvbd;\r
+    case H_Reverb+8   : rvb.ACC_COEF_B=(short)val;  goto rvbd;\r
+    case H_Reverb+10  : rvb.ACC_COEF_C=(short)val;  goto rvbd;\r
+    case H_Reverb+12  : rvb.ACC_COEF_D=(short)val;  goto rvbd;\r
+    case H_Reverb+14  : rvb.IIR_COEF=(short)val;    goto rvbd;\r
+    case H_Reverb+16  : rvb.FB_ALPHA=(short)val;    goto rvbd;\r
+    case H_Reverb+18  : rvb.FB_X=(short)val;        goto rvbd;\r
+    case H_Reverb+20  : rvb.IIR_DEST_A0=val*4;      goto rvbd;\r
+    case H_Reverb+22  : rvb.IIR_DEST_A1=val*4;      goto rvbd;\r
+    case H_Reverb+24  : rvb.ACC_SRC_A0=val*4;       goto rvbd;\r
+    case H_Reverb+26  : rvb.ACC_SRC_A1=val*4;       goto rvbd;\r
+    case H_Reverb+28  : rvb.ACC_SRC_B0=val*4;       goto rvbd;\r
+    case H_Reverb+30  : rvb.ACC_SRC_B1=val*4;       goto rvbd;\r
+    case H_Reverb+32  : rvb.IIR_SRC_A0=val*4;       goto rvbd;\r
+    case H_Reverb+34  : rvb.IIR_SRC_A1=val*4;       goto rvbd;\r
+    case H_Reverb+36  : rvb.IIR_DEST_B0=val*4;      goto rvbd;\r
+    case H_Reverb+38  : rvb.IIR_DEST_B1=val*4;      goto rvbd;\r
+    case H_Reverb+40  : rvb.ACC_SRC_C0=val*4;       goto rvbd;\r
+    case H_Reverb+42  : rvb.ACC_SRC_C1=val*4;       goto rvbd;\r
+    case H_Reverb+44  : rvb.ACC_SRC_D0=val*4;       goto rvbd;\r
+    case H_Reverb+46  : rvb.ACC_SRC_D1=val*4;       goto rvbd;\r
+    case H_Reverb+48  : rvb.IIR_SRC_B1=val*4;       goto rvbd;\r
+    case H_Reverb+50  : rvb.IIR_SRC_B0=val*4;       goto rvbd;\r
+    case H_Reverb+52  : rvb.MIX_DEST_A0=val*4;      goto rvbd;\r
+    case H_Reverb+54  : rvb.MIX_DEST_A1=val*4;      goto rvbd;\r
+    case H_Reverb+56  : rvb.MIX_DEST_B0=val*4;      goto rvbd;\r
+    case H_Reverb+58  : rvb.MIX_DEST_B1=val*4;      goto rvbd;\r
+    case H_Reverb+60  : rvb.IN_COEF_L=(short)val;   goto rvbd;\r
+    case H_Reverb+62  : rvb.IN_COEF_R=(short)val;   goto rvbd;\r
    }\r
+ return;\r
 \r
- iSpuAsyncWait=0;\r
+upd_irq:\r
+ if (spu.spuCtrl & CTRL_IRQ)\r
+  schedule_next_irq();\r
+ return;\r
+\r
+rvbd:\r
+ rvb.dirty = 1; // recalculate on next update\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
@@ -359,8 +298,6 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
 {\r
  const unsigned long r=reg&0xfff;\r
         \r
- iSpuAsyncWait=0;\r
-\r
  if(r>=0x0c00 && r<0x0d80)\r
   {\r
    switch(r&0x0f)\r
@@ -368,8 +305,8 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
      case 12:                                          // get adsr vol\r
       {\r
        const int ch=(r>>4)-0xc0;\r
-       if(dwNewChannel&(1<<ch)) return 1;              // we are started, but not processed? return 1\r
-       if((dwChannelOn&(1<<ch)) &&                     // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
+       if(spu.dwNewChannel&(1<<ch)) return 1;          // we are started, but not processed? return 1\r
+       if((spu.dwChannelOn&(1<<ch)) &&                 // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
           !s_chan[ch].ADSRX.EnvelopeVol)\r
         return 1;\r
        return (unsigned short)(s_chan[ch].ADSRX.EnvelopeVol>>16);\r
@@ -378,7 +315,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
      case 14:                                          // get loop address\r
       {\r
        const int ch=(r>>4)-0xc0;\r
-       return (unsigned short)((s_chan[ch].pLoop-spuMemC)>>3);\r
+       return (unsigned short)((s_chan[ch].pLoop-spu.spuMemC)>>3);\r
       }\r
     }\r
   }\r
@@ -386,25 +323,22 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
  switch(r)\r
   {\r
     case H_SPUctrl:\r
-     return spuCtrl;\r
+     return spu.spuCtrl;\r
 \r
     case H_SPUstat:\r
-     return spuStat;\r
+     return spu.spuStat;\r
         \r
     case H_SPUaddr:\r
-     return (unsigned short)(spuAddr>>3);\r
+     return (unsigned short)(spu.spuAddr>>3);\r
 \r
     case H_SPUdata:\r
      {\r
-      unsigned short s=spuMem[spuAddr>>1];\r
-      spuAddr+=2;\r
-      if(spuAddr>0x7ffff) spuAddr=0;\r
+      unsigned short s=spu.spuMem[spu.spuAddr>>1];\r
+      spu.spuAddr+=2;\r
+      if(spu.spuAddr>0x7ffff) spu.spuAddr=0;\r
       return s;\r
      }\r
 \r
-    case H_SPUirqAddr:\r
-     return spuIrq;\r
-\r
     //case H_SPUIsOn1:\r
     // return IsSoundOn(0,16);\r
 \r
@@ -413,30 +347,24 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
  \r
   }\r
 \r
- return regArea[(r-0xc00)>>1];\r
+ return spu.regArea[(r-0xc00)>>1];\r
 }\r
  \r
 ////////////////////////////////////////////////////////////////////////\r
 // SOUND ON register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SoundOn(int start,int end,unsigned short val)     // SOUND ON PSX COMAND\r
+static void SoundOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
  for(ch=start;ch<end;ch++,val>>=1)                     // loop channels\r
   {\r
-   if((val&1) && s_chan[ch].pStart)                    // mmm... start has to be set before key on !?!\r
+   if((val&1) && regAreaGet(ch,6))                     // mmm... start has to be set before key on !?!\r
     {\r
-     s_chan[ch].bIgnoreLoop=0;\r
-\r
-     // do this here, not in StartSound\r
-     // - fixes fussy timing issues\r
-     s_chan[ch].bStop=0;\r
-     s_chan[ch].pCurr=s_chan[ch].pStart;\r
-\r
-     dwNewChannel|=(1<<ch);                            // bitfield for faster testing\r
-     dwChannelOn|=1<<ch;\r
+     s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned\r
+     s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3);\r
+     spu.dwNewChannel|=(1<<ch);\r
     }\r
   }\r
 }\r
@@ -445,7 +373,7 @@ void SoundOn(int start,int end,unsigned short val)     // SOUND ON PSX COMAND
 // SOUND OFF register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SoundOff(int start,int end,unsigned short val)    // SOUND OFF PSX COMMAND\r
+static void SoundOff(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
  for(ch=start;ch<end;ch++,val>>=1)                     // loop channels\r
@@ -456,7 +384,7 @@ void SoundOff(int start,int end,unsigned short val)    // SOUND OFF PSX COMMAND
 \r
      // Jungle Book - Rhythm 'n Groove\r
      // - turns off buzzing sound (loop hangs)\r
-     dwNewChannel &= ~(1<<ch);\r
+     spu.dwNewChannel &= ~(1<<ch);\r
     }                                                  \r
   }\r
 }\r
@@ -465,7 +393,7 @@ void SoundOff(int start,int end,unsigned short val)    // SOUND OFF PSX COMMAND
 // FMOD register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void FModOn(int start,int end,unsigned short val)      // FMOD ON PSX COMMAND\r
+static void FModOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
@@ -482,6 +410,8 @@ void FModOn(int start,int end,unsigned short val)      // FMOD ON PSX COMMAND
    else\r
     {\r
      s_chan[ch].bFMod=0;                               // --> turn off fmod\r
+     if(ch>0&&s_chan[ch-1].bFMod==2)\r
+      s_chan[ch-1].bFMod=0;\r
     }\r
   }\r
 }\r
@@ -490,7 +420,7 @@ void FModOn(int start,int end,unsigned short val)      // FMOD ON PSX COMMAND
 // NOISE register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void NoiseOn(int start,int end,unsigned short val)     // NOISE ON PSX COMMAND\r
+static void NoiseOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
@@ -507,7 +437,7 @@ void NoiseOn(int start,int end,unsigned short val)     // NOISE ON PSX COMMAND
 // please note: sweep and phase invert are wrong... but I've never seen\r
 // them used\r
 \r
-void SetVolumeL(unsigned char ch,short vol)            // LEFT VOLUME\r
+static void SetVolumeL(unsigned char ch,short vol)     // LEFT VOLUME\r
 {\r
  if(vol&0x8000)                                        // sweep?\r
   {\r
@@ -533,7 +463,7 @@ void SetVolumeL(unsigned char ch,short vol)            // LEFT VOLUME
 // RIGHT VOLUME register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SetVolumeR(unsigned char ch,short vol)            // RIGHT VOLUME\r
+static void SetVolumeR(unsigned char ch,short vol)     // RIGHT VOLUME\r
 {\r
  if(vol&0x8000)                                        // comments... see above :)\r
   {\r
@@ -559,24 +489,23 @@ void SetVolumeR(unsigned char ch,short vol)            // RIGHT VOLUME
 // PITCH register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SetPitch(int ch,unsigned short val)               // SET PITCH\r
+static void SetPitch(int ch,unsigned short val)               // SET PITCH\r
 {\r
  int NP;\r
  if(val>0x3fff) NP=0x3fff;                             // get pitch val\r
  else           NP=val;\r
 \r
  s_chan[ch].iRawPitch=NP;\r
-\r
- NP=(44100L*NP)/4096L;                                 // calc frequency\r
- if(NP<1) NP=1;                                        // some security\r
- s_chan[ch].iActFreq=NP;                               // store frequency\r
+ s_chan[ch].sinc=(NP<<4)|8;\r
+ s_chan[ch].sinc_inv=0;\r
+ if(spu_config.iUseInterpolation==1) s_chan[ch].SB[32]=1; // -> freq change in simple interpolation mode: set flag\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // REVERB register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void ReverbOn(int start,int end,unsigned short val)    // REVERB ON PSX COMMAND\r
+static void ReverbOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r