spu: some misc refactoring/cleanup
authornotaz <notasas@gmail.com>
Fri, 29 Jul 2011 23:42:28 +0000 (02:42 +0300)
committernotaz <notasas@gmail.com>
Mon, 1 Aug 2011 20:07:01 +0000 (23:07 +0300)
frontend/menu.c
plugins/dfsound/externals.h
plugins/dfsound/freeze.c
plugins/dfsound/spu.c
plugins/dfsound/xa.c

index 2b9c155..1bfac1d 100644 (file)
@@ -1083,14 +1083,13 @@ static int menu_loop_plugin_gpu(int id, int keys)
        return 0;
 }
 
        return 0;
 }
 
-static const char *men_spu_reverb[] = { "Off", "Fake", "On", NULL };
 static const char *men_spu_interp[] = { "None", "Simple", "Gaussian", "Cubic", NULL };
 static const char h_spu_irq_wait[]  = "Wait for CPU (recommended set to ON)";
 static const char h_spu_thread[]    = "Run sound emulation in main thread (recommended)";
 
 static menu_entry e_menu_plugin_spu[] =
 {
 static const char *men_spu_interp[] = { "None", "Simple", "Gaussian", "Cubic", NULL };
 static const char h_spu_irq_wait[]  = "Wait for CPU (recommended set to ON)";
 static const char h_spu_thread[]    = "Run sound emulation in main thread (recommended)";
 
 static menu_entry e_menu_plugin_spu[] =
 {
-       mee_enum      ("Reverb",                    0, iUseReverb, men_spu_reverb),
+       mee_onoff     ("Reverb",                    0, iUseReverb, 2),
        mee_enum      ("Interpolation",             0, iUseInterpolation, men_spu_interp),
        mee_onoff     ("Adjust XA pitch",           0, iXAPitch, 1),
        mee_onoff_h   ("SPU IRQ Wait",              0, iSPUIRQWait, 1, h_spu_irq_wait),
        mee_enum      ("Interpolation",             0, iUseInterpolation, men_spu_interp),
        mee_onoff     ("Adjust XA pitch",           0, iXAPitch, 1),
        mee_onoff_h   ("SPU IRQ Wait",              0, iSPUIRQWait, 1, h_spu_irq_wait),
index 73134d0..c69af81 100644 (file)
@@ -46,7 +46,8 @@
 \r
 // ~ 1 ms of data\r
 // note: must be even due to the way reverb works now\r
 \r
 // ~ 1 ms of data\r
 // note: must be even due to the way reverb works now\r
-#define NSSIZE 46\r
+#define FRAG_MSECS 2\r
+#define NSSIZE ((44100 * FRAG_MSECS / 1000 + 1) & ~1)\r
 \r
 ///////////////////////////////////////////////////////////\r
 // struct defines\r
 \r
 ///////////////////////////////////////////////////////////\r
 // struct defines\r
@@ -121,17 +122,10 @@ typedef struct
  int               iUsedFreq;                          // current pc pitch\r
  int               iLeftVolume;                        // left volume\r
  int               iRightVolume;                       // right volume\r
  int               iUsedFreq;                          // current pc pitch\r
  int               iLeftVolume;                        // left volume\r
  int               iRightVolume;                       // right volume\r
- int               s_1;                                // last decoding infos\r
- int               s_2;\r
  ADSRInfoEx        ADSRX;\r
  int               iRawPitch;                          // raw pitch (0...3fff)\r
 \r
  ADSRInfoEx        ADSRX;\r
  int               iRawPitch;                          // raw pitch (0...3fff)\r
 \r
- int               iRVBOffset;                         // reverb offset\r
- int               iRVBRepeat;                         // reverb repeat\r
- int               iRVBNum;                            // another reverb helper\r
- int               iOldNoise;                          // old noise val for this channel   \r
-\r
- int               SB[32+32];\r
+ int               SB[32+4];\r
 } SPUCHAN;\r
 \r
 ///////////////////////////////////////////////////////////\r
 } SPUCHAN;\r
 \r
 ///////////////////////////////////////////////////////////\r
index 3969469..41c6f16 100644 (file)
@@ -121,7 +121,7 @@ extern int lastch;
 \r
 // we want to retain compatibility between versions,\r
 // so use original channel struct\r
 \r
 // we want to retain compatibility between versions,\r
 // so use original channel struct\r
-static void save_channel(SPUCHAN_orig *d, SPUCHAN *s, int ch)\r
+static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch)\r
 {\r
  memset(d, 0, sizeof(*d));\r
  d->bNew = !!(dwNewChannel & (1<<ch));\r
 {\r
  memset(d, 0, sizeof(*d));\r
  d->bNew = !!(dwNewChannel & (1<<ch));\r
@@ -141,15 +141,11 @@ static void save_channel(SPUCHAN_orig *d, SPUCHAN *s, int ch)
  d->bIgnoreLoop = s->bIgnoreLoop;\r
  d->iRightVolume = s->iRightVolume;\r
  d->iRawPitch = s->iRawPitch;\r
  d->bIgnoreLoop = s->bIgnoreLoop;\r
  d->iRightVolume = s->iRightVolume;\r
  d->iRawPitch = s->iRawPitch;\r
- d->s_1 = s->s_1;\r
- d->s_2 = s->s_2;\r
+ d->s_1 = s->SB[27]; // yes it's reversed\r
+ d->s_2 = s->SB[26];\r
  d->bRVBActive = s->bRVBActive;\r
  d->bRVBActive = s->bRVBActive;\r
- d->iRVBOffset = s->iRVBOffset;\r
- d->iRVBRepeat = s->iRVBRepeat;\r
  d->bNoise = s->bNoise;\r
  d->bFMod = s->bFMod;\r
  d->bNoise = s->bNoise;\r
  d->bFMod = s->bFMod;\r
- d->iRVBNum = s->iRVBNum;\r
- d->iOldNoise = s->iOldNoise;\r
  d->ADSRX.State = s->ADSRX.State;\r
  d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r
  d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r
  d->ADSRX.State = s->ADSRX.State;\r
  d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r
  d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r
@@ -164,7 +160,7 @@ static void save_channel(SPUCHAN_orig *d, SPUCHAN *s, int ch)
  d->ADSRX.lVolume = d->bOn; // hmh\r
 }\r
 \r
  d->ADSRX.lVolume = d->bOn; // hmh\r
 }\r
 \r
-static void load_channel(SPUCHAN *d, SPUCHAN_orig *s, int ch)\r
+static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch)\r
 {\r
  memset(d, 0, sizeof(*d));\r
  if (s->bNew) dwNewChannel |= 1<<ch;\r
 {\r
  memset(d, 0, sizeof(*d));\r
  if (s->bNew) dwNewChannel |= 1<<ch;\r
@@ -184,15 +180,9 @@ static void load_channel(SPUCHAN *d, SPUCHAN_orig *s, int ch)
  d->bIgnoreLoop = s->bIgnoreLoop;\r
  d->iRightVolume = s->iRightVolume;\r
  d->iRawPitch = s->iRawPitch;\r
  d->bIgnoreLoop = s->bIgnoreLoop;\r
  d->iRightVolume = s->iRightVolume;\r
  d->iRawPitch = s->iRawPitch;\r
- d->s_1 = s->s_1;\r
- d->s_2 = s->s_2;\r
  d->bRVBActive = s->bRVBActive;\r
  d->bRVBActive = s->bRVBActive;\r
- d->iRVBOffset = s->iRVBOffset;\r
- d->iRVBRepeat = s->iRVBRepeat;\r
  d->bNoise = s->bNoise;\r
  d->bFMod = s->bFMod;\r
  d->bNoise = s->bNoise;\r
  d->bFMod = s->bFMod;\r
- d->iRVBNum = s->iRVBNum;\r
- d->iOldNoise = s->iOldNoise;\r
  d->ADSRX.State = s->ADSRX.State;\r
  d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r
  d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r
  d->ADSRX.State = s->ADSRX.State;\r
  d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp;\r
  d->ADSRX.AttackRate = s->ADSRX.AttackRate;\r
index d6cd952..7b435ac 100644 (file)
@@ -267,8 +267,8 @@ INLINE void StartSound(int ch)
  //s_chan[ch].bStop=0;
  //s_chan[ch].bOn=1;
 
  //s_chan[ch].bStop=0;
  //s_chan[ch].bOn=1;
 
- s_chan[ch].s_1=0;                                     // init mixing vars
- s_chan[ch].s_2=0;
+ s_chan[ch].SB[26]=0;                                  // init mixing vars
+ s_chan[ch].SB[27]=0;
  s_chan[ch].iSBPos=28;
 
  s_chan[ch].SB[29]=0;                                  // init our interpolation helpers
  s_chan[ch].iSBPos=28;
 
  s_chan[ch].SB[29]=0;                                  // init our interpolation helpers
@@ -355,12 +355,7 @@ INLINE void StoreInterpolationVal(int ch,int fa)
   s_chan[ch].SB[29]=fa;
  else
   {
   s_chan[ch].SB[29]=fa;
  else
   {
-   if((spuCtrl&0x4000)==0) fa=0;                       // muted?
-   else                                                // else adjust
-    {
-     if(fa>32767L)  fa=32767L;
-     if(fa<-32767L) fa=-32767L;              
-    }
+   ssat32_to_16(fa);
 
    if(iUseInterpolation>=2)                            // gauss/cubic interpolation
     {     
 
    if(iUseInterpolation>=2)                            // gauss/cubic interpolation
     {     
@@ -451,16 +446,40 @@ static void do_irq(void)
  }
 }
 
  }
 }
 
+static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
+{
+ int nSample;
+ int fa, s_1, s_2, d, s;
+
+ s_1 = dest[27];
+ s_2 = dest[26];
+
+ for (nSample = 0; nSample < 28; src++)
+ {
+  d = (int)*src;
+  s = (int)(signed short)((d & 0x0f) << 12);
+
+  fa = s >> shift_factor;
+  fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
+  s_2=s_1;s_1=fa;
+
+  dest[nSample++] = fa;
+
+  s = (int)(signed short)((d & 0xf0) << 8);
+  fa = s >> shift_factor;
+  fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
+  s_2=s_1;s_1=fa;
+
+  dest[nSample++] = fa;
+ }
+}
+
 static int decode_block(int ch)
 {
  unsigned char *start;
 static int decode_block(int ch)
 {
  unsigned char *start;
- unsigned int nSample;
- int predict_nr,shift_factor,flags,d,s;
- int fa,s_1,s_2;
+ int predict_nr,shift_factor,flags;
  int ret = 0;
 
  int ret = 0;
 
- s_chan[ch].iSBPos=0;
-
  start=s_chan[ch].pCurr;                   // set up the current pos
  if(start == (unsigned char*)-1 ||         // special "stop" sign
     (dwPendingChanOff&(1<<ch)))
  start=s_chan[ch].pCurr;                   // set up the current pos
  if(start == (unsigned char*)-1 ||         // special "stop" sign
     (dwPendingChanOff&(1<<ch)))
@@ -483,42 +502,19 @@ static int decode_block(int ch)
   }
  }
 
   }
  }
 
- s_1=s_chan[ch].s_1;
- s_2=s_chan[ch].s_2;
-
- predict_nr=(int)*start;start++;
+ predict_nr=(int)start[0];
  shift_factor=predict_nr&0xf;
  predict_nr >>= 4;
  shift_factor=predict_nr&0xf;
  predict_nr >>= 4;
- flags=(int)*start;start++;
-
- // -------------------------------------- // 
-
- for (nSample=0;nSample<28;start++)      
- {
-  d=(int)*start;
-  s=((d&0xf)<<12);
-  if(s&0x8000) s|=0xffff0000;
 
 
-  fa=(s >> shift_factor);
-  fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
-  s_2=s_1;s_1=fa;
-  s=((d & 0xf0) << 8);
-
-  s_chan[ch].SB[nSample++]=fa;
-
-  if(s&0x8000) s|=0xffff0000;
-  fa=(s>>shift_factor);
-  fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
-  s_2=s_1;s_1=fa;
-
-  s_chan[ch].SB[nSample++]=fa;
- }
+ decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
 
  //////////////////////////////////////////// flag handler
 
 
  //////////////////////////////////////////// flag handler
 
- if((flags&4) && (!s_chan[ch].bIgnoreLoop))
-  s_chan[ch].pLoop=start-16;               // loop adress
+ flags=(int)start[1];
+ if(flags&4)
+  s_chan[ch].pLoop=start;                  // loop adress
 
 
+ start+=16;
  if(flags&1)                               // 1: stop/loop
  {
   if(!(flags&2))
  if(flags&1)                               // 1: stop/loop
  {
   if(!(flags&2))
@@ -530,9 +526,7 @@ static int decode_block(int ch)
  if (start - spuMemC >= 0x80000)
   start = (unsigned char*)-1;
 
  if (start - spuMemC >= 0x80000)
   start = (unsigned char*)-1;
 
- s_chan[ch].pCurr=start;                   // store values for next cycle
- s_chan[ch].s_1=s_1;
- s_chan[ch].s_2=s_2;
+ s_chan[ch].pCurr = start;                 // store values for next cycle
 
  return ret;
 }
 
  return ret;
 }
@@ -553,7 +547,7 @@ static int skip_block(int ch)
   ret = 1;
  }
 
   ret = 1;
  }
 
- if((flags & 4) && !s_chan[ch].bIgnoreLoop)
+ if(flags & 4)
   s_chan[ch].pLoop=start;
 
  s_chan[ch].pCurr += 16;
   s_chan[ch].pLoop=start;
 
  s_chan[ch].pCurr += 16;
@@ -569,6 +563,8 @@ static int do_samples_##name(int ch, int ns, int ns_to) \
 {                                            \
  int sinc = s_chan[ch].sinc;                 \
  int spos = s_chan[ch].spos;                 \
 {                                            \
  int sinc = s_chan[ch].sinc;                 \
  int spos = s_chan[ch].spos;                 \
+ int sbpos = s_chan[ch].iSBPos;              \
+ int *SB = s_chan[ch].SB;                    \
  int ret = -1;                               \
  int d, fa;                                  \
  interp_start;                               \
  int ret = -1;                               \
  int d, fa;                                  \
  interp_start;                               \
@@ -579,8 +575,9 @@ static int do_samples_##name(int ch, int ns, int ns_to) \
                                              \
   while (spos >= 0x10000)                    \
   {                                          \
                                              \
   while (spos >= 0x10000)                    \
   {                                          \
-   if(s_chan[ch].iSBPos == 28)               \
+   if(sbpos == 28)                           \
    {                                         \
    {                                         \
+    sbpos = 0;                               \
     d = decode_block(ch);                    \
     if(d && iSPUIRQWait)                     \
     {                                        \
     d = decode_block(ch);                    \
     if(d && iSPUIRQWait)                     \
     {                                        \
@@ -589,7 +586,7 @@ static int do_samples_##name(int ch, int ns, int ns_to) \
     }                                        \
    }                                         \
                                              \
     }                                        \
    }                                         \
                                              \
-   fa = s_chan[ch].SB[s_chan[ch].iSBPos++];  \
+   fa = SB[sbpos++];                         \
    interp1_code;                             \
    spos -= 0x10000;                          \
   }                                          \
    interp1_code;                             \
    spos -= 0x10000;                          \
   }                                          \
@@ -601,6 +598,7 @@ static int do_samples_##name(int ch, int ns, int ns_to) \
 out:                                         \
  s_chan[ch].sinc = sinc;                     \
  s_chan[ch].spos = spos;                     \
 out:                                         \
  s_chan[ch].sinc = sinc;                     \
  s_chan[ch].spos = spos;                     \
+ s_chan[ch].iSBPos = sbpos;                  \
  interp_end;                                 \
                                              \
  return ret;                                 \
  interp_end;                                 \
                                              \
  return ret;                                 \
@@ -888,7 +886,7 @@ static void *MAINThread(void *arg)
 
   // feed the sound
   // wanna have around 1/60 sec (16.666 ms) updates
 
   // feed the sound
   // wanna have around 1/60 sec (16.666 ms) updates
-  if (iCycle++ > 16)
+  if (iCycle++ > 16/FRAG_MSECS)
    {
     SoundFeedStreamData((unsigned char *)pSpuBuffer,
                         ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
    {
     SoundFeedStreamData((unsigned char *)pSpuBuffer,
                         ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
@@ -912,7 +910,7 @@ void CALLBACK SPUasync(unsigned long cycle)
  if(iSpuAsyncWait)
   {
    iSpuAsyncWait++;
  if(iSpuAsyncWait)
   {
    iSpuAsyncWait++;
-   if(iSpuAsyncWait<=16) return;
+   if(iSpuAsyncWait<=16/FRAG_MSECS) return;
    iSpuAsyncWait=0;
   }
 
    iSpuAsyncWait=0;
   }
 
index bdea89a..b45aef2 100644 (file)
@@ -95,8 +95,8 @@ INLINE void MixXA(void)
   {
    l=*CDDAPlay++;
    if(CDDAPlay==CDDAEnd) CDDAPlay=CDDAStart;
   {
    l=*CDDAPlay++;
    if(CDDAPlay==CDDAEnd) CDDAPlay=CDDAStart;
-   SSumLR[ns++]+=(((short)(l&0xffff))       * iLeftXAVol)/32767;
-   SSumLR[ns++]+=(((short)((l>>16)&0xffff)) * iRightXAVol)/32767;
+   SSumLR[ns++]+=(((short)(l&0xffff))       * iLeftXAVol) >> 15;
+   SSumLR[ns++]+=(((short)((l>>16)&0xffff)) * iRightXAVol) >> 15;
   }
 }
 
   }
 }
 
@@ -222,13 +222,11 @@ INLINE void FeedXA(xa_decode_t *xap)
        s=(short)LOWORD(l);
        l1=s;
        l1=(l1*iPlace)/iSize;
        s=(short)LOWORD(l);
        l1=s;
        l1=(l1*iPlace)/iSize;
-       if(l1<-32767) l1=-32767;
-       if(l1> 32767) l1=32767;
+       ssat32_to_16(l1);
        s=(short)HIWORD(l);
        l2=s;
        l2=(l2*iPlace)/iSize;
        s=(short)HIWORD(l);
        l2=s;
        l2=(l2*iPlace)/iSize;
-       if(l2<-32767) l2=-32767;
-       if(l2> 32767) l2=32767;
+       ssat32_to_16(l2);
        l=(l1&0xffff)|(l2<<16);
 
        *XAFeed++=l;
        l=(l1&0xffff)|(l2<<16);
 
        *XAFeed++=l;
@@ -328,8 +326,7 @@ INLINE void FeedXA(xa_decode_t *xap)
         }
 
        l1=(l1*iPlace)/iSize;
         }
 
        l1=(l1*iPlace)/iSize;
-       if(l1<-32767) l1=-32767;
-       if(l1> 32767) l1=32767;
+       ssat32_to_16(l1);
        l=(l1&0xffff)|(l1<<16);
        *XAFeed++=l;
 
        l=(l1&0xffff)|(l1<<16);
        *XAFeed++=l;