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 ***************************************************************************/
23 // will be included from spu.c
26 ////////////////////////////////////////////////////////////////////////
28 ////////////////////////////////////////////////////////////////////////
30 static int gauss_ptr = 0;
31 static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
33 #define gvall0 gauss_window[gauss_ptr]
34 #define gvall(x) gauss_window[(gauss_ptr+x)&3]
35 #define gvalr0 gauss_window[4+gauss_ptr]
36 #define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
38 ////////////////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////////////////
42 INLINE void SkipCD(int ns_to, int decode_pos)
44 int cursor = decode_pos;
47 if(spu.XAPlay != spu.XAFeed)
49 for(ns = 0; ns < ns_to*2; ns += 2)
51 if(spu.XAPlay != spu.XAFeed) spu.XAPlay++;
52 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
54 spu.spuMem[cursor] = 0;
55 spu.spuMem[cursor + 0x400/2] = 0;
56 cursor = (cursor + 1) & 0x1ff;
59 else if(spu.CDDAPlay != spu.CDDAFeed)
61 for(ns = 0; ns < ns_to*2; ns += 2)
63 if(spu.CDDAPlay != spu.CDDAFeed) spu.CDDAPlay++;
64 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
66 spu.spuMem[cursor] = 0;
67 spu.spuMem[cursor + 0x400/2] = 0;
68 cursor = (cursor + 1) & 0x1ff;
74 INLINE void MixCD(int *SSumLR, int *RVB, int ns_to, int decode_pos)
76 int vll = spu.iLeftXAVol * spu.cdv.ll >> 7;
77 int vrl = spu.iLeftXAVol * spu.cdv.rl >> 7;
78 int vlr = spu.iRightXAVol * spu.cdv.lr >> 7;
79 int vrr = spu.iRightXAVol * spu.cdv.rr >> 7;
80 int cursor = decode_pos;
83 uint32_t v = spu.XALastVal;
85 // note: spu volume doesn't affect cd capture
86 if ((spu.cdv.ll | spu.cdv.lr | spu.cdv.rl | spu.cdv.rr) == 0)
88 SkipCD(ns_to, decode_pos);
92 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
94 if(spu.XAPlay == spu.XAFeed)
97 for(ns = 0; ns < ns_to*2; ns += 2)
99 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
100 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
102 l1 = (short)v, r1 = (short)(v >> 16);
103 l = (l1 * vll + r1 * vrl) >> 15;
104 r = (r1 * vrr + l1 * vlr) >> 15;
107 if (spu.spuCtrl & CTRL_CD)
112 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
118 spu.spuMem[cursor] = HTOLE16(v);
119 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
120 cursor = (cursor + 1) & 0x1ff;
124 // occasionally CDDAFeed underflows by a few samples due to poor timing,
125 // hence this 'ns_to < 8'
126 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
128 for(ns = 0; ns < ns_to*2; ns += 2)
130 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
131 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
133 l1 = (short)v, r1 = (short)(v >> 16);
134 l = (l1 * vll + r1 * vrl) >> 15;
135 r = (r1 * vrr + l1 * vlr) >> 15;
138 if (spu.spuCtrl & CTRL_CD)
143 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
149 spu.spuMem[cursor] = HTOLE16(v);
150 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
151 cursor = (cursor + 1) & 0x1ff;
159 ////////////////////////////////////////////////////////////////////////
160 // small linux time helper... only used for watchdog
161 ////////////////////////////////////////////////////////////////////////
164 static unsigned long timeGetTime_spu()
168 #elif defined(_WIN32)
169 return GetTickCount();
172 gettimeofday(&tv, 0); // well, maybe there are better ways
173 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
178 ////////////////////////////////////////////////////////////////////////
180 ////////////////////////////////////////////////////////////////////////
182 void FeedXA(const xa_decode_t *xap)
184 int sinc,spos,i,iSize,iPlace,vl,vr;
186 if(!spu.bSPUIsOpen) return;
188 spu.XARepeat = 3; // set up repeat
191 iSize=((45500*xap->nsamples)/xap->freq); // get size
193 iSize=((44100*xap->nsamples)/xap->freq); // get size
195 if(!iSize) return; // none? bye
197 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
198 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
200 if(iPlace==0) return; // no place at all
202 //----------------------------------------------------//
204 if(spu_config.iXAPitch) // pitch change option?
207 static DWORD dwFPS=0;
208 static int iFPSCnt=0;
209 static int iLastSize=0;
211 DWORD dw=timeGetTime_spu(),dw1,dw2;
215 dwFPS+=dw-dwLT;iFPSCnt++;
223 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
225 dw2=(xap->freq*100/xap->nsamples);
226 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
229 iLastSize=iSize*dw2/dw1;
230 if(iLastSize>iPlace) iLastSize=iPlace;
237 if(iLastSize) iSize=iLastSize;
241 //----------------------------------------------------//
244 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
248 uint32_t * pS=(uint32_t *)xap->pcm;
252 if(spu_config.iXAPitch)
254 int32_t l1,l2;short s;
257 if(spu_config.iUseInterpolation==2)
259 while(spos>=0x10000L)
262 gauss_window[gauss_ptr] = (short)LOWORD(l);
263 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
264 gauss_ptr = (gauss_ptr+1) & 3;
267 vl = (spos >> 6) & ~3;
268 vr=(gauss[vl]*gvall0) >> 15;
269 vr+=(gauss[vl+1]*gvall(1)) >> 15;
270 vr+=(gauss[vl+2]*gvall(2)) >> 15;
271 vr+=(gauss[vl+3]*gvall(3)) >> 15;
273 vr=(gauss[vl]*gvalr0) >> 15;
274 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
275 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
276 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
281 while(spos>=0x10000L)
290 l1=(l1*iPlace)/iSize;
294 l2=(l2*iPlace)/iSize;
296 l=(l1&0xffff)|(l2<<16);
300 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
301 if(spu.XAFeed==spu.XAPlay)
303 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
315 if(spu_config.iUseInterpolation==2)
317 while(spos>=0x10000L)
320 gauss_window[gauss_ptr] = (short)LOWORD(l);
321 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
322 gauss_ptr = (gauss_ptr+1) & 3;
325 vl = (spos >> 6) & ~3;
326 vr=(gauss[vl]*gvall0) >> 15;
327 vr+=(gauss[vl+1]*gvall(1)) >> 15;
328 vr+=(gauss[vl+2]*gvall(2)) >> 15;
329 vr+=(gauss[vl+3]*gvall(3)) >> 15;
331 vr=(gauss[vl]*gvalr0) >> 15;
332 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
333 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
334 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
339 while(spos>=0x10000L)
348 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
349 if(spu.XAFeed==spu.XAPlay)
351 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
361 unsigned short * pS=(unsigned short *)xap->pcm;
362 uint32_t l;short s=0;
365 if(spu_config.iXAPitch)
370 if(spu_config.iUseInterpolation==2)
372 while(spos>=0x10000L)
374 gauss_window[gauss_ptr] = (short)*pS++;
375 gauss_ptr = (gauss_ptr+1) & 3;
378 vl = (spos >> 6) & ~3;
379 vr=(gauss[vl]*gvall0) >> 15;
380 vr+=(gauss[vl+1]*gvall(1)) >> 15;
381 vr+=(gauss[vl+2]*gvall(2)) >> 15;
382 vr+=(gauss[vl+3]*gvall(3)) >> 15;
388 while(spos>=0x10000L)
396 l1=(l1*iPlace)/iSize;
398 l=(l1&0xffff)|(l1<<16);
401 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
402 if(spu.XAFeed==spu.XAPlay)
404 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
416 if(spu_config.iUseInterpolation==2)
418 while(spos>=0x10000L)
420 gauss_window[gauss_ptr] = (short)*pS++;
421 gauss_ptr = (gauss_ptr+1) & 3;
424 vl = (spos >> 6) & ~3;
425 vr=(gauss[vl]*gvall0) >> 15;
426 vr+=(gauss[vl+1]*gvall(1)) >> 15;
427 vr+=(gauss[vl+2]*gvall(2)) >> 15;
428 vr+=(gauss[vl+3]*gvall(3)) >> 15;
433 while(spos>=0x10000L)
442 *spu.XAFeed++=(l|(l<<16));
444 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
445 if(spu.XAFeed==spu.XAPlay)
447 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
457 ////////////////////////////////////////////////////////////////////////
459 ////////////////////////////////////////////////////////////////////////
461 void FeedCDDA(unsigned char *pcm, int nBytes)
464 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
465 if (space < nBytes) {
466 log_unhandled("FeedCDDA: %d/%d\n", nBytes, space);
472 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
473 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
474 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
475 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
479 memcpy(spu.CDDAFeed,pcm,space);
480 spu.CDDAFeed+=space/4;
487 // vim:shiftwidth=1:expandtab