- uint32_t l;
-
- for(ns=0;ns<NSSIZE*2 && XAPlay!=XAFeed;)
- {
- XALastVal=*XAPlay++;
- if(XAPlay==XAEnd) XAPlay=XAStart;
-#ifdef XA_HACK
- SSumLR[ns++]+=(((short)(XALastVal&0xffff)) * iLeftXAVol)/32768;
- SSumLR[ns++]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32768;
-#else
- SSumLR[ns++]+=(((short)(XALastVal&0xffff)) * iLeftXAVol)/32767;
- SSumLR[ns++]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32767;
-#endif
- }
-
- if(XAPlay==XAFeed && XARepeat)
- {
- XARepeat--;
- for(;ns<NSSIZE*2;)
- {
-#ifdef XA_HACK
- SSumLR[ns++]+=(((short)(XALastVal&0xffff)) * iLeftXAVol)/32768;
- SSumLR[ns++]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32768;
-#else
- SSumLR[ns++]+=(((short)(XALastVal&0xffff)) * iLeftXAVol)/32767;
- SSumLR[ns++]+=(((short)((XALastVal>>16)&0xffff)) * iRightXAVol)/32767;
-#endif
- }
- }
-
- for(ns=0;ns<NSSIZE*2 && CDDAPlay!=CDDAFeed && (CDDAPlay!=CDDAEnd-1||CDDAFeed!=CDDAStart);)
- {
- l=*CDDAPlay++;
- if(CDDAPlay==CDDAEnd) CDDAPlay=CDDAStart;
- SSumLR[ns++]+=(((short)(l&0xffff)) * iLeftXAVol) >> 15;
- SSumLR[ns++]+=(((short)((l>>16)&0xffff)) * iRightXAVol) >> 15;
- }
+ short l, r;
+ uint32_t v = spu.XALastVal;
+
+ if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
+ {
+ if(spu.XAPlay == spu.XAFeed)
+ spu.XARepeat--;
+
+ for(ns = 0; ns < ns_to*2; )
+ {
+ if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
+ if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
+
+ l = ((int)(short)v * spu.iLeftXAVol) >> 15;
+ r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
+ SSumLR[ns++] += l;
+ SSumLR[ns++] += r;
+
+ 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; )
+ {
+ if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
+ if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
+
+ l = ((int)(short)v * spu.iLeftXAVol) >> 15;
+ r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
+ SSumLR[ns++] += l;
+ SSumLR[ns++] += r;
+
+ spu.spuMem[cursor] = HTOLE16(v);
+ spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
+ cursor = (cursor + 1) & 0x1ff;
+ }
+ spu.XALastVal = v;
+ }
+ else
+ spu.XALastVal = 0;