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;
155 else if (spu.cdClearSamples > 0)
157 for(ns = 0; ns < ns_to; ns++)
159 spu.spuMem[cursor] = spu.spuMem[cursor + 0x400/2] = 0;
160 cursor = (cursor + 1) & 0x1ff;
162 spu.cdClearSamples -= ns_to;
167 ////////////////////////////////////////////////////////////////////////
168 // small linux time helper... only used for watchdog
169 ////////////////////////////////////////////////////////////////////////
172 static unsigned long timeGetTime_spu()
176 #elif defined(_WIN32)
177 return GetTickCount();
180 gettimeofday(&tv, 0); // well, maybe there are better ways
181 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
186 ////////////////////////////////////////////////////////////////////////
188 ////////////////////////////////////////////////////////////////////////
190 void FeedXA(const xa_decode_t *xap)
192 int sinc,spos,i,iSize,iPlace,vl,vr;
194 if(!spu.bSPUIsOpen) return;
196 spu.XARepeat = 3; // set up repeat
199 iSize=((45500*xap->nsamples)/xap->freq); // get size
201 iSize=((44100*xap->nsamples)/xap->freq); // get size
203 if(!iSize) return; // none? bye
205 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
206 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
208 if(iPlace==0) return; // no place at all
210 //----------------------------------------------------//
212 if(spu_config.iXAPitch) // pitch change option?
215 static DWORD dwFPS=0;
216 static int iFPSCnt=0;
217 static int iLastSize=0;
219 DWORD dw=timeGetTime_spu(),dw1,dw2;
223 dwFPS+=dw-dwLT;iFPSCnt++;
231 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
233 dw2=(xap->freq*100/xap->nsamples);
234 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
237 iLastSize=iSize*dw2/dw1;
238 if(iLastSize>iPlace) iLastSize=iPlace;
245 if(iLastSize) iSize=iLastSize;
249 //----------------------------------------------------//
252 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
256 uint32_t * pS=(uint32_t *)xap->pcm;
260 if(spu_config.iXAPitch)
262 int32_t l1,l2;short s;
265 if(spu_config.iUseInterpolation==2)
267 while(spos>=0x10000L)
270 gauss_window[gauss_ptr] = (short)LOWORD(l);
271 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
272 gauss_ptr = (gauss_ptr+1) & 3;
275 vl = (spos >> 6) & ~3;
276 vr=(gauss[vl]*gvall0) >> 15;
277 vr+=(gauss[vl+1]*gvall(1)) >> 15;
278 vr+=(gauss[vl+2]*gvall(2)) >> 15;
279 vr+=(gauss[vl+3]*gvall(3)) >> 15;
281 vr=(gauss[vl]*gvalr0) >> 15;
282 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
283 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
284 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
289 while(spos>=0x10000L)
298 l1=(l1*iPlace)/iSize;
302 l2=(l2*iPlace)/iSize;
304 l=(l1&0xffff)|(l2<<16);
308 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
309 if(spu.XAFeed==spu.XAPlay)
311 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
323 if(spu_config.iUseInterpolation==2)
325 while(spos>=0x10000L)
328 gauss_window[gauss_ptr] = (short)LOWORD(l);
329 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
330 gauss_ptr = (gauss_ptr+1) & 3;
333 vl = (spos >> 6) & ~3;
334 vr=(gauss[vl]*gvall0) >> 15;
335 vr+=(gauss[vl+1]*gvall(1)) >> 15;
336 vr+=(gauss[vl+2]*gvall(2)) >> 15;
337 vr+=(gauss[vl+3]*gvall(3)) >> 15;
339 vr=(gauss[vl]*gvalr0) >> 15;
340 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
341 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
342 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
347 while(spos>=0x10000L)
356 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
357 if(spu.XAFeed==spu.XAPlay)
359 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
369 unsigned short * pS=(unsigned short *)xap->pcm;
370 uint32_t l;short s=0;
373 if(spu_config.iXAPitch)
378 if(spu_config.iUseInterpolation==2)
380 while(spos>=0x10000L)
382 gauss_window[gauss_ptr] = (short)*pS++;
383 gauss_ptr = (gauss_ptr+1) & 3;
386 vl = (spos >> 6) & ~3;
387 vr=(gauss[vl]*gvall0) >> 15;
388 vr+=(gauss[vl+1]*gvall(1)) >> 15;
389 vr+=(gauss[vl+2]*gvall(2)) >> 15;
390 vr+=(gauss[vl+3]*gvall(3)) >> 15;
396 while(spos>=0x10000L)
404 l1=(l1*iPlace)/iSize;
406 l=(l1&0xffff)|(l1<<16);
409 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
410 if(spu.XAFeed==spu.XAPlay)
412 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
424 if(spu_config.iUseInterpolation==2)
426 while(spos>=0x10000L)
428 gauss_window[gauss_ptr] = (short)*pS++;
429 gauss_ptr = (gauss_ptr+1) & 3;
432 vl = (spos >> 6) & ~3;
433 vr=(gauss[vl]*gvall0) >> 15;
434 vr+=(gauss[vl+1]*gvall(1)) >> 15;
435 vr+=(gauss[vl+2]*gvall(2)) >> 15;
436 vr+=(gauss[vl+3]*gvall(3)) >> 15;
441 while(spos>=0x10000L)
450 *spu.XAFeed++=(l|(l<<16));
452 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
453 if(spu.XAFeed==spu.XAPlay)
455 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
465 ////////////////////////////////////////////////////////////////////////
467 ////////////////////////////////////////////////////////////////////////
469 void FeedCDDA(unsigned char *pcm, int nBytes)
472 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
473 if (space < nBytes) {
474 log_unhandled("FeedCDDA: %d/%d\n", nBytes, space);
480 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
481 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
482 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
483 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
487 memcpy(spu.CDDAFeed,pcm,space);
488 spu.CDDAFeed+=space/4;
495 // vim:shiftwidth=1:expandtab