cdrom: change pause timing again
[pcsx_rearmed.git] / plugins / dfsound / xa.c
index df60346..380d138 100644 (file)
@@ -16,6 +16,7 @@
  ***************************************************************************/
 
 #include "stdafx.h"
+#include "spu.h"
 #define _IN_XA
 #include <stdint.h>
 
 // XA GLOBALS
 ////////////////////////////////////////////////////////////////////////
 
-xa_decode_t   * xapGlobal=0;
-
-uint32_t * XAFeed  = NULL;
-uint32_t * XAPlay  = NULL;
-uint32_t * XAStart = NULL;
-uint32_t * XAEnd   = NULL;
-
-uint32_t   XARepeat  = 0;
-uint32_t   XALastVal = 0;
-
-uint32_t * CDDAFeed  = NULL;
-uint32_t * CDDAPlay  = NULL;
-uint32_t * CDDAStart = NULL;
-uint32_t * CDDAEnd   = NULL;
-
-int             iLeftXAVol  = 32767;
-int             iRightXAVol = 32767;
-
 static int gauss_ptr = 0;
 static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
 
@@ -56,86 +39,177 @@ static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
 // MIX XA & CDDA
 ////////////////////////////////////////////////////////////////////////
 
-INLINE void MixXA(void)
+INLINE void SkipCD(int ns_to, int decode_pos)
 {
+ int cursor = decode_pos;
  int ns;
- uint32_t l;
 
- for(ns=0;ns<NSSIZE && XAPlay!=XAFeed;ns++)
-  {
-   XALastVal=*XAPlay++;
-   if(XAPlay==XAEnd) XAPlay=XAStart;
-#ifdef XA_HACK
-   SSumL[ns]+=(((short)(XALastVal&0xffff))       * iLeftXAVol)/32768;
-   SSumR[ns]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32768;
-#else
-   SSumL[ns]+=(((short)(XALastVal&0xffff))       * iLeftXAVol)/32767;
-   SSumR[ns]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32767;
-#endif
-  }
+ if(spu.XAPlay != spu.XAFeed)
+ {
+  for(ns = 0; ns < ns_to*2; ns += 2)
+   {
+    if(spu.XAPlay != spu.XAFeed) spu.XAPlay++;
+    if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
 
- if(XAPlay==XAFeed && XARepeat)
-  {
-   XARepeat--;
-   for(;ns<NSSIZE;ns++)
+    spu.spuMem[cursor] = 0;
+    spu.spuMem[cursor + 0x400/2] = 0;
+    cursor = (cursor + 1) & 0x1ff;
+   }
+ }
+ else if(spu.CDDAPlay != spu.CDDAFeed)
+ {
+  for(ns = 0; ns < ns_to*2; ns += 2)
+   {
+    if(spu.CDDAPlay != spu.CDDAFeed) spu.CDDAPlay++;
+    if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
+
+    spu.spuMem[cursor] = 0;
+    spu.spuMem[cursor + 0x400/2] = 0;
+    cursor = (cursor + 1) & 0x1ff;
+   }
+ }
+ spu.XALastVal = 0;
+}
+
+INLINE void MixCD(int *SSumLR, int *RVB, int ns_to, int decode_pos)
+{
+ int vll = spu.iLeftXAVol * spu.cdv.ll >> 7;
+ int vrl = spu.iLeftXAVol * spu.cdv.rl >> 7;
+ int vlr = spu.iRightXAVol * spu.cdv.lr >> 7;
+ int vrr = spu.iRightXAVol * spu.cdv.rr >> 7;
+ int cursor = decode_pos;
+ int l1, r1, l, r;
+ int ns;
+ uint32_t v = spu.XALastVal;
+
+ // note: spu volume doesn't affect cd capture
+ if ((spu.cdv.ll | spu.cdv.lr | spu.cdv.rl | spu.cdv.rr) == 0)
+ {
+  SkipCD(ns_to, decode_pos);
+  return;
+ }
+
+ if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
+ {
+  if(spu.XAPlay == spu.XAFeed)
+   spu.XARepeat--;
+
+  for(ns = 0; ns < ns_to*2; ns += 2)
+   {
+    if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
+    if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
+
+    l1 = (short)v, r1 = (short)(v >> 16);
+    l = (l1 * vll + r1 * vrl) >> 15;
+    r = (r1 * vrr + l1 * vlr) >> 15;
+    ssat32_to_16(l);
+    ssat32_to_16(r);
+    if (spu.spuCtrl & CTRL_CD)
     {
-#ifdef XA_HACK
-     SSumL[ns]+=(((short)(XALastVal&0xffff))       * iLeftXAVol)/32768;
-     SSumR[ns]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32768;
-#else
-     SSumL[ns]+=(((short)(XALastVal&0xffff))       * iLeftXAVol)/32767;
-     SSumR[ns]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32767;
-#endif
+     SSumLR[ns+0] += l;
+     SSumLR[ns+1] += r;
+    }
+    if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
+    {
+     RVB[ns+0] += l;
+     RVB[ns+1] += r;
     }
-  }
 
- for(ns=0;ns<NSSIZE && CDDAPlay!=CDDAFeed && (CDDAPlay!=CDDAEnd-1||CDDAFeed!=CDDAStart);ns++)
-  {
-   l=*CDDAPlay++;
-   if(CDDAPlay==CDDAEnd) CDDAPlay=CDDAStart;
-   SSumL[ns]+=(((short)(l&0xffff))       * iLeftXAVol)/32767;
-   SSumR[ns]+=(((short)((l>>16)&0xffff)) * iRightXAVol)/32767;
-  }
+    spu.spuMem[cursor] = HTOLE16(v);
+    spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
+    cursor = (cursor + 1) & 0x1ff;
+   }
+  spu.XALastVal = v;
+ }
+ // occasionally CDDAFeed underflows by a few samples due to poor timing,
+ // hence this 'ns_to < 8'
+ else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
+ {
+  for(ns = 0; ns < ns_to*2; ns += 2)
+   {
+    if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
+    if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
+
+    l1 = (short)v, r1 = (short)(v >> 16);
+    l = (l1 * vll + r1 * vrl) >> 15;
+    r = (r1 * vrr + l1 * vlr) >> 15;
+    ssat32_to_16(l);
+    ssat32_to_16(r);
+    if (spu.spuCtrl & CTRL_CD)
+    {
+     SSumLR[ns+0] += l;
+     SSumLR[ns+1] += r;
+    }
+    if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
+    {
+     RVB[ns+0] += l;
+     RVB[ns+1] += r;
+    }
+
+    spu.spuMem[cursor] = HTOLE16(v);
+    spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
+    cursor = (cursor + 1) & 0x1ff;
+   }
+  spu.XALastVal = v;
+ }
+ else if (spu.cdClearSamples > 0)
+ {
+  for(ns = 0; ns < ns_to; ns++)
+   {
+    spu.spuMem[cursor] = spu.spuMem[cursor + 0x400/2] = 0;
+    cursor = (cursor + 1) & 0x1ff;
+   }
+  spu.cdClearSamples -= ns_to;
+  spu.XALastVal = 0;
+ }
 }
 
 ////////////////////////////////////////////////////////////////////////
 // small linux time helper... only used for watchdog
 ////////////////////////////////////////////////////////////////////////
 
-unsigned long timeGetTime_spu()
+#if 0
+static unsigned long timeGetTime_spu()
 {
+#if defined(NO_OS)
+ return 0;
+#elif defined(_WIN32)
+ return GetTickCount();
+#else
  struct timeval tv;
  gettimeofday(&tv, 0);                                 // well, maybe there are better ways
  return tv.tv_sec * 1000 + tv.tv_usec/1000;            // to do that, but at least it works
+#endif
 }
+#endif
 
 ////////////////////////////////////////////////////////////////////////
 // FEED XA 
 ////////////////////////////////////////////////////////////////////////
 
-INLINE void FeedXA(xa_decode_t *xap)
+void FeedXA(const xa_decode_t *xap)
 {
  int sinc,spos,i,iSize,iPlace,vl,vr;
 
- if(!bSPUIsOpen) return;
+ if(!spu.bSPUIsOpen) return;
 
- xapGlobal = xap;                                      // store info for save states
- XARepeat  = 100;                                      // set up repeat
+ spu.XARepeat  = 3;                                    // set up repeat
 
-#ifdef XA_HACK
+#if 0//def XA_HACK
  iSize=((45500*xap->nsamples)/xap->freq);              // get size
 #else
  iSize=((44100*xap->nsamples)/xap->freq);              // get size
 #endif
  if(!iSize) return;                                    // none? bye
 
- if(XAFeed<XAPlay) iPlace=XAPlay-XAFeed;               // how much space in my buf?
- else              iPlace=(XAEnd-XAFeed) + (XAPlay-XAStart);
+ if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
+ else              iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
 
  if(iPlace==0) return;                                 // no place at all
 
  //----------------------------------------------------//
- if(iXAPitch)                                          // pitch change option?
+#if 0
+ if(spu_config.iXAPitch)                               // pitch change option?
   {
    static DWORD dwLT=0;
    static DWORD dwFPS=0;
@@ -171,6 +245,7 @@ INLINE void FeedXA(xa_decode_t *xap)
      if(iLastSize) iSize=iLastSize;
     }
   }
+#endif
  //----------------------------------------------------//
 
  spos=0x10000L;
@@ -181,12 +256,13 @@ INLINE void FeedXA(xa_decode_t *xap)
    uint32_t * pS=(uint32_t *)xap->pcm;
    uint32_t l=0;
 
-   if(iXAPitch)
+#if 0
+   if(spu_config.iXAPitch)
     {
      int32_t l1,l2;short s;
      for(i=0;i<iSize;i++)
       {
-       if(iUseInterpolation==2) 
+       if(spu_config.iUseInterpolation==2)
         {
          while(spos>=0x10000L)
           {
@@ -197,16 +273,16 @@ INLINE void FeedXA(xa_decode_t *xap)
            spos -= 0x10000L;
           }
          vl = (spos >> 6) & ~3;
-         vr=(gauss[vl]*gvall0)&~2047;
-         vr+=(gauss[vl+1]*gvall(1))&~2047;
-         vr+=(gauss[vl+2]*gvall(2))&~2047;
-         vr+=(gauss[vl+3]*gvall(3))&~2047;
-         l= (vr >> 11) & 0xffff;
-         vr=(gauss[vl]*gvalr0)&~2047;
-         vr+=(gauss[vl+1]*gvalr(1))&~2047;
-         vr+=(gauss[vl+2]*gvalr(2))&~2047;
-         vr+=(gauss[vl+3]*gvalr(3))&~2047;
-         l |= vr << 5;
+         vr=(gauss[vl]*gvall0) >> 15;
+         vr+=(gauss[vl+1]*gvall(1)) >> 15;
+         vr+=(gauss[vl+2]*gvall(2)) >> 15;
+         vr+=(gauss[vl+3]*gvall(3)) >> 15;
+         l= vr & 0xffff;
+         vr=(gauss[vl]*gvalr0) >> 15;
+         vr+=(gauss[vl+1]*gvalr(1)) >> 15;
+         vr+=(gauss[vl+2]*gvalr(2)) >> 15;
+         vr+=(gauss[vl+3]*gvalr(3)) >> 15;
+         l |= vr << 16;
         }
        else
         {
@@ -220,21 +296,19 @@ INLINE void FeedXA(xa_decode_t *xap)
        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;
-       if(l2<-32767) l2=-32767;
-       if(l2> 32767) l2=32767;
+       ssat32_to_16(l2);
        l=(l1&0xffff)|(l2<<16);
 
-       *XAFeed++=l;
+       *spu.XAFeed++=l;
 
-       if(XAFeed==XAEnd) XAFeed=XAStart;
-       if(XAFeed==XAPlay) 
+       if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
+       if(spu.XAFeed==spu.XAPlay)
         {
-         if(XAPlay!=XAStart) XAFeed=XAPlay-1;
+         if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
          break;
         }
 
@@ -242,10 +316,11 @@ INLINE void FeedXA(xa_decode_t *xap)
       }
     }
    else
+#endif
     {
      for(i=0;i<iSize;i++)
       {
-       if(iUseInterpolation==2) 
+       if(spu_config.iUseInterpolation==2)
         {
          while(spos>=0x10000L)
           {
@@ -256,16 +331,16 @@ INLINE void FeedXA(xa_decode_t *xap)
            spos -= 0x10000L;
           }
          vl = (spos >> 6) & ~3;
-         vr=(gauss[vl]*gvall0)&~2047;
-         vr+=(gauss[vl+1]*gvall(1))&~2047;
-         vr+=(gauss[vl+2]*gvall(2))&~2047;
-         vr+=(gauss[vl+3]*gvall(3))&~2047;
-         l= (vr >> 11) & 0xffff;
-         vr=(gauss[vl]*gvalr0)&~2047;
-         vr+=(gauss[vl+1]*gvalr(1))&~2047;
-         vr+=(gauss[vl+2]*gvalr(2))&~2047;
-         vr+=(gauss[vl+3]*gvalr(3))&~2047;
-         l |= vr << 5;
+         vr=(gauss[vl]*gvall0) >> 15;
+         vr+=(gauss[vl+1]*gvall(1)) >> 15;
+         vr+=(gauss[vl+2]*gvall(2)) >> 15;
+         vr+=(gauss[vl+3]*gvall(3)) >> 15;
+         l= vr & 0xffff;
+         vr=(gauss[vl]*gvalr0) >> 15;
+         vr+=(gauss[vl+1]*gvalr(1)) >> 15;
+         vr+=(gauss[vl+2]*gvalr(2)) >> 15;
+         vr+=(gauss[vl+3]*gvalr(3)) >> 15;
+         l |= vr << 16;
         }
        else
         {
@@ -276,12 +351,12 @@ INLINE void FeedXA(xa_decode_t *xap)
           }
         }
 
-       *XAFeed++=l;
+       *spu.XAFeed++=l;
 
-       if(XAFeed==XAEnd) XAFeed=XAStart;
-       if(XAFeed==XAPlay) 
+       if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
+       if(spu.XAFeed==spu.XAPlay)
         {
-         if(XAPlay!=XAStart) XAFeed=XAPlay-1;
+         if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
          break;
         }
 
@@ -294,12 +369,13 @@ INLINE void FeedXA(xa_decode_t *xap)
    unsigned short * pS=(unsigned short *)xap->pcm;
    uint32_t l;short s=0;
 
-   if(iXAPitch)
+#if 0
+   if(spu_config.iXAPitch)
     {
      int32_t l1;
      for(i=0;i<iSize;i++)
       {
-       if(iUseInterpolation==2) 
+       if(spu_config.iUseInterpolation==2)
         {
          while(spos>=0x10000L)
           {
@@ -308,11 +384,11 @@ INLINE void FeedXA(xa_decode_t *xap)
            spos -= 0x10000L;
           }
          vl = (spos >> 6) & ~3;
-         vr=(gauss[vl]*gvall0)&~2047;
-         vr+=(gauss[vl+1]*gvall(1))&~2047;
-         vr+=(gauss[vl+2]*gvall(2))&~2047;
-         vr+=(gauss[vl+3]*gvall(3))&~2047;
-         l1=s= vr >> 11;
+         vr=(gauss[vl]*gvall0) >> 15;
+         vr+=(gauss[vl+1]*gvall(1)) >> 15;
+         vr+=(gauss[vl+2]*gvall(2)) >> 15;
+         vr+=(gauss[vl+3]*gvall(3)) >> 15;
+         l1=s= vr;
          l1 &= 0xffff;
         }
        else
@@ -326,15 +402,14 @@ INLINE void FeedXA(xa_decode_t *xap)
         }
 
        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;
+       *spu.XAFeed++=l;
 
-       if(XAFeed==XAEnd) XAFeed=XAStart;
-       if(XAFeed==XAPlay) 
+       if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
+       if(spu.XAFeed==spu.XAPlay)
         {
-         if(XAPlay!=XAStart) XAFeed=XAPlay-1;
+         if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
          break;
         }
 
@@ -342,10 +417,11 @@ INLINE void FeedXA(xa_decode_t *xap)
       }
     }
    else
+#endif
     {
      for(i=0;i<iSize;i++)
       {
-       if(iUseInterpolation==2) 
+       if(spu_config.iUseInterpolation==2)
         {
          while(spos>=0x10000L)
           {
@@ -354,11 +430,11 @@ INLINE void FeedXA(xa_decode_t *xap)
            spos -= 0x10000L;
           }
          vl = (spos >> 6) & ~3;
-         vr=(gauss[vl]*gvall0)&~2047;
-         vr+=(gauss[vl+1]*gvall(1))&~2047;
-         vr+=(gauss[vl+2]*gvall(2))&~2047;
-         vr+=(gauss[vl+3]*gvall(3))&~2047;
-         l=s= vr >> 11;
+         vr=(gauss[vl]*gvall0) >> 15;
+         vr+=(gauss[vl+1]*gvall(1)) >> 15;
+         vr+=(gauss[vl+2]*gvall(2)) >> 15;
+         vr+=(gauss[vl+3]*gvall(3)) >> 15;
+         l=s= vr;
         }
        else
         {
@@ -371,12 +447,12 @@ INLINE void FeedXA(xa_decode_t *xap)
         }
 
        l &= 0xffff;
-       *XAFeed++=(l|(l<<16));
+       *spu.XAFeed++=(l|(l<<16));
 
-       if(XAFeed==XAEnd) XAFeed=XAStart;
-       if(XAFeed==XAPlay) 
+       if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
+       if(spu.XAFeed==spu.XAPlay)
         {
-         if(XAPlay!=XAStart) XAFeed=XAPlay-1;
+         if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
          break;
         }
 
@@ -390,21 +466,30 @@ INLINE void FeedXA(xa_decode_t *xap)
 // FEED CDDA
 ////////////////////////////////////////////////////////////////////////
 
-INLINE void FeedCDDA(unsigned char *pcm, int nBytes)
+void FeedCDDA(unsigned char *pcm, int nBytes)
 {
+ int space;
+ space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
+ if (space < nBytes) {
+  log_unhandled("FeedCDDA: %d/%d\n", nBytes, space);
+  return;
+ }
+
  while(nBytes>0)
   {
-   if(CDDAFeed==CDDAEnd) CDDAFeed=CDDAStart;
-   while(CDDAFeed==CDDAPlay-1||
-         (CDDAFeed==CDDAEnd-1&&CDDAPlay==CDDAStart))
-   {
-    if (!iUseTimer) usleep(1000);
-    else return;
-   }
-   *CDDAFeed++=(*pcm | (*(pcm+1)<<8) | (*(pcm+2)<<16) | (*(pcm+3)<<24));
-   nBytes-=4;
-   pcm+=4;
+   if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
+   space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
+   if(spu.CDDAFeed+space/4>spu.CDDAEnd)
+    space=(spu.CDDAEnd-spu.CDDAFeed)*4;
+   if(space>nBytes)
+    space=nBytes;
+
+   memcpy(spu.CDDAFeed,pcm,space);
+   spu.CDDAFeed+=space/4;
+   nBytes-=space;
+   pcm+=space;
   }
 }
 
 #endif
+// vim:shiftwidth=1:expandtab