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 MixXA(int *SSumLR, int *RVB, int ns_to, int decode_pos)
44 int cursor = decode_pos;
47 uint32_t v = spu.XALastVal;
49 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
51 if(spu.XAPlay == spu.XAFeed)
54 for(ns = 0; ns < ns_to*2; ns += 2)
56 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
57 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
59 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
60 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
61 if (spu.spuCtrl & CTRL_CD)
66 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
72 spu.spuMem[cursor] = HTOLE16(v);
73 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
74 cursor = (cursor + 1) & 0x1ff;
78 // occasionally CDDAFeed underflows by a few samples due to poor timing,
79 // hence this 'ns_to < 8'
80 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
82 for(ns = 0; ns < ns_to*2; ns += 2)
84 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
85 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
87 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
88 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
89 if (spu.spuCtrl & CTRL_CD)
94 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
100 spu.spuMem[cursor] = HTOLE16(v);
101 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
102 cursor = (cursor + 1) & 0x1ff;
110 ////////////////////////////////////////////////////////////////////////
111 // small linux time helper... only used for watchdog
112 ////////////////////////////////////////////////////////////////////////
115 static unsigned long timeGetTime_spu()
119 #elif defined(_WIN32)
120 return GetTickCount();
123 gettimeofday(&tv, 0); // well, maybe there are better ways
124 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
129 ////////////////////////////////////////////////////////////////////////
131 ////////////////////////////////////////////////////////////////////////
133 INLINE void FeedXA(xa_decode_t *xap)
135 int sinc,spos,i,iSize,iPlace,vl,vr;
137 if(!spu.bSPUIsOpen) return;
139 spu.xapGlobal = xap; // store info for save states
140 spu.XARepeat = 3; // set up repeat
143 iSize=((45500*xap->nsamples)/xap->freq); // get size
145 iSize=((44100*xap->nsamples)/xap->freq); // get size
147 if(!iSize) return; // none? bye
149 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
150 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
152 if(iPlace==0) return; // no place at all
154 //----------------------------------------------------//
156 if(spu_config.iXAPitch) // pitch change option?
159 static DWORD dwFPS=0;
160 static int iFPSCnt=0;
161 static int iLastSize=0;
163 DWORD dw=timeGetTime_spu(),dw1,dw2;
167 dwFPS+=dw-dwLT;iFPSCnt++;
175 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
177 dw2=(xap->freq*100/xap->nsamples);
178 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
181 iLastSize=iSize*dw2/dw1;
182 if(iLastSize>iPlace) iLastSize=iPlace;
189 if(iLastSize) iSize=iLastSize;
193 //----------------------------------------------------//
196 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
200 uint32_t * pS=(uint32_t *)xap->pcm;
204 if(spu_config.iXAPitch)
206 int32_t l1,l2;short s;
209 if(spu_config.iUseInterpolation==2)
211 while(spos>=0x10000L)
214 gauss_window[gauss_ptr] = (short)LOWORD(l);
215 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
216 gauss_ptr = (gauss_ptr+1) & 3;
219 vl = (spos >> 6) & ~3;
220 vr=(gauss[vl]*gvall0) >> 15;
221 vr+=(gauss[vl+1]*gvall(1)) >> 15;
222 vr+=(gauss[vl+2]*gvall(2)) >> 15;
223 vr+=(gauss[vl+3]*gvall(3)) >> 15;
225 vr=(gauss[vl]*gvalr0) >> 15;
226 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
227 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
228 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
233 while(spos>=0x10000L)
242 l1=(l1*iPlace)/iSize;
246 l2=(l2*iPlace)/iSize;
248 l=(l1&0xffff)|(l2<<16);
252 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
253 if(spu.XAFeed==spu.XAPlay)
255 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
267 if(spu_config.iUseInterpolation==2)
269 while(spos>=0x10000L)
272 gauss_window[gauss_ptr] = (short)LOWORD(l);
273 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
274 gauss_ptr = (gauss_ptr+1) & 3;
277 vl = (spos >> 6) & ~3;
278 vr=(gauss[vl]*gvall0) >> 15;
279 vr+=(gauss[vl+1]*gvall(1)) >> 15;
280 vr+=(gauss[vl+2]*gvall(2)) >> 15;
281 vr+=(gauss[vl+3]*gvall(3)) >> 15;
283 vr=(gauss[vl]*gvalr0) >> 15;
284 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
285 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
286 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
291 while(spos>=0x10000L)
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;
313 unsigned short * pS=(unsigned short *)xap->pcm;
314 uint32_t l;short s=0;
317 if(spu_config.iXAPitch)
322 if(spu_config.iUseInterpolation==2)
324 while(spos>=0x10000L)
326 gauss_window[gauss_ptr] = (short)*pS++;
327 gauss_ptr = (gauss_ptr+1) & 3;
330 vl = (spos >> 6) & ~3;
331 vr=(gauss[vl]*gvall0) >> 15;
332 vr+=(gauss[vl+1]*gvall(1)) >> 15;
333 vr+=(gauss[vl+2]*gvall(2)) >> 15;
334 vr+=(gauss[vl+3]*gvall(3)) >> 15;
340 while(spos>=0x10000L)
348 l1=(l1*iPlace)/iSize;
350 l=(l1&0xffff)|(l1<<16);
353 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
354 if(spu.XAFeed==spu.XAPlay)
356 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
368 if(spu_config.iUseInterpolation==2)
370 while(spos>=0x10000L)
372 gauss_window[gauss_ptr] = (short)*pS++;
373 gauss_ptr = (gauss_ptr+1) & 3;
376 vl = (spos >> 6) & ~3;
377 vr=(gauss[vl]*gvall0) >> 15;
378 vr+=(gauss[vl+1]*gvall(1)) >> 15;
379 vr+=(gauss[vl+2]*gvall(2)) >> 15;
380 vr+=(gauss[vl+3]*gvall(3)) >> 15;
385 while(spos>=0x10000L)
394 *spu.XAFeed++=(l|(l<<16));
396 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
397 if(spu.XAFeed==spu.XAPlay)
399 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
409 ////////////////////////////////////////////////////////////////////////
411 ////////////////////////////////////////////////////////////////////////
413 INLINE int FeedCDDA(unsigned char *pcm, int nBytes)
416 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
418 return 0x7761; // rearmed_wait
422 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
423 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
424 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
425 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
429 memcpy(spu.CDDAFeed,pcm,space);
430 spu.CDDAFeed+=space/4;
435 return 0x676f; // rearmed_go
439 // vim:shiftwidth=1:expandtab