1 /***************************************************************************
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
16 ***************************************************************************/
22 #include "psemu_plugin_defs.h"
24 // will be included from spu.c
27 ////////////////////////////////////////////////////////////////////////
29 ////////////////////////////////////////////////////////////////////////
31 static int gauss_ptr = 0;
32 static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
34 #define gvall0 gauss_window[gauss_ptr]
35 #define gvall(x) gauss_window[(gauss_ptr+x)&3]
36 #define gvalr0 gauss_window[4+gauss_ptr]
37 #define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
39 ////////////////////////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////////////////
43 INLINE void SkipCD(int ns_to, int decode_pos)
45 int cursor = decode_pos;
48 if(spu.XAPlay != spu.XAFeed)
50 for(ns = 0; ns < ns_to*2; ns += 2)
52 if(spu.XAPlay != spu.XAFeed) spu.XAPlay++;
53 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
55 spu.spuMem[cursor] = 0;
56 spu.spuMem[cursor + 0x400/2] = 0;
57 cursor = (cursor + 1) & 0x1ff;
60 else if(spu.CDDAPlay != spu.CDDAFeed)
62 for(ns = 0; ns < ns_to*2; ns += 2)
64 if(spu.CDDAPlay != spu.CDDAFeed) spu.CDDAPlay++;
65 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
67 spu.spuMem[cursor] = 0;
68 spu.spuMem[cursor + 0x400/2] = 0;
69 cursor = (cursor + 1) & 0x1ff;
75 INLINE void MixCD(int *SSumLR, int *RVB, int ns_to, int decode_pos)
77 int vll = spu.iLeftXAVol * spu.cdv.ll >> 7;
78 int vrl = spu.iLeftXAVol * spu.cdv.rl >> 7;
79 int vlr = spu.iRightXAVol * spu.cdv.lr >> 7;
80 int vrr = spu.iRightXAVol * spu.cdv.rr >> 7;
81 int cursor = decode_pos;
84 uint32_t v = spu.XALastVal;
86 // note: spu volume doesn't affect cd capture
87 if ((spu.cdv.ll | spu.cdv.lr | spu.cdv.rl | spu.cdv.rr) == 0)
89 SkipCD(ns_to, decode_pos);
93 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
95 if(spu.XAPlay == spu.XAFeed)
98 for(ns = 0; ns < ns_to*2; ns += 2)
100 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
101 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
103 l1 = (short)v, r1 = (short)(v >> 16);
104 l = (l1 * vll + r1 * vrl) >> 15;
105 r = (r1 * vrr + l1 * vlr) >> 15;
108 if (spu.spuCtrl & CTRL_CD)
113 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
119 spu.spuMem[cursor] = HTOLE16(v);
120 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
121 cursor = (cursor + 1) & 0x1ff;
125 // occasionally CDDAFeed underflows by a few samples due to poor timing,
126 // hence this 'ns_to < 8'
127 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
129 for(ns = 0; ns < ns_to*2; ns += 2)
131 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
132 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
134 l1 = (short)v, r1 = (short)(v >> 16);
135 l = (l1 * vll + r1 * vrl) >> 15;
136 r = (r1 * vrr + l1 * vlr) >> 15;
139 if (spu.spuCtrl & CTRL_CD)
144 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
150 spu.spuMem[cursor] = HTOLE16(v);
151 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
152 cursor = (cursor + 1) & 0x1ff;
156 else if (spu.cdClearSamples > 0)
158 for(ns = 0; ns < ns_to; ns++)
160 spu.spuMem[cursor] = spu.spuMem[cursor + 0x400/2] = 0;
161 cursor = (cursor + 1) & 0x1ff;
163 spu.cdClearSamples -= ns_to;
168 ////////////////////////////////////////////////////////////////////////
169 // small linux time helper... only used for watchdog
170 ////////////////////////////////////////////////////////////////////////
173 static unsigned long timeGetTime_spu()
177 #elif defined(_WIN32)
178 return GetTickCount();
181 gettimeofday(&tv, 0); // well, maybe there are better ways
182 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
187 ////////////////////////////////////////////////////////////////////////
189 ////////////////////////////////////////////////////////////////////////
191 void FeedXA(const xa_decode_t *xap)
193 int sinc,spos,i,iSize,iPlace,vl,vr;
195 if(!spu.bSPUIsOpen) return;
197 spu.XARepeat = 3; // set up repeat
200 iSize=((45500*xap->nsamples)/xap->freq); // get size
202 iSize=((44100*xap->nsamples)/xap->freq); // get size
204 if(!iSize) return; // none? bye
206 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
207 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
209 if(iPlace==0) return; // no place at all
211 //----------------------------------------------------//
213 if(spu_config.iXAPitch) // pitch change option?
216 static DWORD dwFPS=0;
217 static int iFPSCnt=0;
218 static int iLastSize=0;
220 DWORD dw=timeGetTime_spu(),dw1,dw2;
224 dwFPS+=dw-dwLT;iFPSCnt++;
232 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
234 dw2=(xap->freq*100/xap->nsamples);
235 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
238 iLastSize=iSize*dw2/dw1;
239 if(iLastSize>iPlace) iLastSize=iPlace;
246 if(iLastSize) iSize=iLastSize;
250 //----------------------------------------------------//
253 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
257 uint32_t * pS=(uint32_t *)xap->pcm;
261 if(spu_config.iXAPitch)
263 int32_t l1,l2;short s;
266 if(spu_config.iUseInterpolation==2)
268 while(spos>=0x10000L)
271 gauss_window[gauss_ptr] = (short)LOWORD(l);
272 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
273 gauss_ptr = (gauss_ptr+1) & 3;
276 vl = (spos >> 6) & ~3;
277 vr=(gauss[vl]*gvall0) >> 15;
278 vr+=(gauss[vl+1]*gvall(1)) >> 15;
279 vr+=(gauss[vl+2]*gvall(2)) >> 15;
280 vr+=(gauss[vl+3]*gvall(3)) >> 15;
282 vr=(gauss[vl]*gvalr0) >> 15;
283 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
284 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
285 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
290 while(spos>=0x10000L)
299 l1=(l1*iPlace)/iSize;
303 l2=(l2*iPlace)/iSize;
305 l=(l1&0xffff)|(l2<<16);
309 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
310 if(spu.XAFeed==spu.XAPlay)
312 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
324 if(spu_config.iUseInterpolation==2)
326 while(spos>=0x10000L)
329 gauss_window[gauss_ptr] = (short)LOWORD(l);
330 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
331 gauss_ptr = (gauss_ptr+1) & 3;
334 vl = (spos >> 6) & ~3;
335 vr=(gauss[vl]*gvall0) >> 15;
336 vr+=(gauss[vl+1]*gvall(1)) >> 15;
337 vr+=(gauss[vl+2]*gvall(2)) >> 15;
338 vr+=(gauss[vl+3]*gvall(3)) >> 15;
340 vr=(gauss[vl]*gvalr0) >> 15;
341 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
342 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
343 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
348 while(spos>=0x10000L)
357 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
358 if(spu.XAFeed==spu.XAPlay)
360 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
370 unsigned short * pS=(unsigned short *)xap->pcm;
371 uint32_t l;short s=0;
374 if(spu_config.iXAPitch)
379 if(spu_config.iUseInterpolation==2)
381 while(spos>=0x10000L)
383 gauss_window[gauss_ptr] = (short)*pS++;
384 gauss_ptr = (gauss_ptr+1) & 3;
387 vl = (spos >> 6) & ~3;
388 vr=(gauss[vl]*gvall0) >> 15;
389 vr+=(gauss[vl+1]*gvall(1)) >> 15;
390 vr+=(gauss[vl+2]*gvall(2)) >> 15;
391 vr+=(gauss[vl+3]*gvall(3)) >> 15;
397 while(spos>=0x10000L)
405 l1=(l1*iPlace)/iSize;
407 l=(l1&0xffff)|(l1<<16);
410 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
411 if(spu.XAFeed==spu.XAPlay)
413 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
425 if(spu_config.iUseInterpolation==2)
427 while(spos>=0x10000L)
429 gauss_window[gauss_ptr] = (short)*pS++;
430 gauss_ptr = (gauss_ptr+1) & 3;
433 vl = (spos >> 6) & ~3;
434 vr=(gauss[vl]*gvall0) >> 15;
435 vr+=(gauss[vl+1]*gvall(1)) >> 15;
436 vr+=(gauss[vl+2]*gvall(2)) >> 15;
437 vr+=(gauss[vl+3]*gvall(3)) >> 15;
442 while(spos>=0x10000L)
451 *spu.XAFeed++=(l|(l<<16));
453 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
454 if(spu.XAFeed==spu.XAPlay)
456 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
466 ////////////////////////////////////////////////////////////////////////
468 ////////////////////////////////////////////////////////////////////////
470 void FeedCDDA(unsigned char *pcm, int nBytes)
473 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
474 if (space < nBytes) {
475 log_unhandled("FeedCDDA: %d/%d\n", nBytes, space);
481 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
482 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
483 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
484 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
488 memcpy(spu.CDDAFeed,pcm,space);
489 spu.CDDAFeed+=space/4;
496 // vim:shiftwidth=1:expandtab