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 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; )
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;
64 spu.spuMem[cursor] = HTOLE16(v);
65 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
66 cursor = (cursor + 1) & 0x1ff;
70 // occasionally CDDAFeed underflows by a few samples due to poor timing,
71 // hence this 'ns_to < 8'
72 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
74 for(ns = 0; ns < ns_to*2; )
76 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
77 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
79 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
80 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
84 spu.spuMem[cursor] = HTOLE16(v);
85 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
86 cursor = (cursor + 1) & 0x1ff;
94 ////////////////////////////////////////////////////////////////////////
95 // small linux time helper... only used for watchdog
96 ////////////////////////////////////////////////////////////////////////
99 static unsigned long timeGetTime_spu()
103 #elif defined(_WIN32)
104 return GetTickCount();
107 gettimeofday(&tv, 0); // well, maybe there are better ways
108 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
113 ////////////////////////////////////////////////////////////////////////
115 ////////////////////////////////////////////////////////////////////////
117 INLINE void FeedXA(xa_decode_t *xap)
119 int sinc,spos,i,iSize,iPlace,vl,vr;
121 if(!spu.bSPUIsOpen) return;
123 spu.xapGlobal = xap; // store info for save states
124 spu.XARepeat = 3; // set up repeat
127 iSize=((45500*xap->nsamples)/xap->freq); // get size
129 iSize=((44100*xap->nsamples)/xap->freq); // get size
131 if(!iSize) return; // none? bye
133 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
134 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
136 if(iPlace==0) return; // no place at all
138 //----------------------------------------------------//
140 if(spu_config.iXAPitch) // pitch change option?
143 static DWORD dwFPS=0;
144 static int iFPSCnt=0;
145 static int iLastSize=0;
147 DWORD dw=timeGetTime_spu(),dw1,dw2;
151 dwFPS+=dw-dwLT;iFPSCnt++;
159 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
161 dw2=(xap->freq*100/xap->nsamples);
162 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
165 iLastSize=iSize*dw2/dw1;
166 if(iLastSize>iPlace) iLastSize=iPlace;
173 if(iLastSize) iSize=iLastSize;
177 //----------------------------------------------------//
180 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
184 uint32_t * pS=(uint32_t *)xap->pcm;
188 if(spu_config.iXAPitch)
190 int32_t l1,l2;short s;
193 if(spu_config.iUseInterpolation==2)
195 while(spos>=0x10000L)
198 gauss_window[gauss_ptr] = (short)LOWORD(l);
199 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
200 gauss_ptr = (gauss_ptr+1) & 3;
203 vl = (spos >> 6) & ~3;
204 vr=(gauss[vl]*gvall0) >> 15;
205 vr+=(gauss[vl+1]*gvall(1)) >> 15;
206 vr+=(gauss[vl+2]*gvall(2)) >> 15;
207 vr+=(gauss[vl+3]*gvall(3)) >> 15;
209 vr=(gauss[vl]*gvalr0) >> 15;
210 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
211 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
212 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
217 while(spos>=0x10000L)
226 l1=(l1*iPlace)/iSize;
230 l2=(l2*iPlace)/iSize;
232 l=(l1&0xffff)|(l2<<16);
236 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
237 if(spu.XAFeed==spu.XAPlay)
239 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
251 if(spu_config.iUseInterpolation==2)
253 while(spos>=0x10000L)
256 gauss_window[gauss_ptr] = (short)LOWORD(l);
257 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
258 gauss_ptr = (gauss_ptr+1) & 3;
261 vl = (spos >> 6) & ~3;
262 vr=(gauss[vl]*gvall0) >> 15;
263 vr+=(gauss[vl+1]*gvall(1)) >> 15;
264 vr+=(gauss[vl+2]*gvall(2)) >> 15;
265 vr+=(gauss[vl+3]*gvall(3)) >> 15;
267 vr=(gauss[vl]*gvalr0) >> 15;
268 vr+=(gauss[vl+1]*gvalr(1)) >> 15;
269 vr+=(gauss[vl+2]*gvalr(2)) >> 15;
270 vr+=(gauss[vl+3]*gvalr(3)) >> 15;
275 while(spos>=0x10000L)
284 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
285 if(spu.XAFeed==spu.XAPlay)
287 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
297 unsigned short * pS=(unsigned short *)xap->pcm;
298 uint32_t l;short s=0;
301 if(spu_config.iXAPitch)
306 if(spu_config.iUseInterpolation==2)
308 while(spos>=0x10000L)
310 gauss_window[gauss_ptr] = (short)*pS++;
311 gauss_ptr = (gauss_ptr+1) & 3;
314 vl = (spos >> 6) & ~3;
315 vr=(gauss[vl]*gvall0) >> 15;
316 vr+=(gauss[vl+1]*gvall(1)) >> 15;
317 vr+=(gauss[vl+2]*gvall(2)) >> 15;
318 vr+=(gauss[vl+3]*gvall(3)) >> 15;
324 while(spos>=0x10000L)
332 l1=(l1*iPlace)/iSize;
334 l=(l1&0xffff)|(l1<<16);
337 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
338 if(spu.XAFeed==spu.XAPlay)
340 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
352 if(spu_config.iUseInterpolation==2)
354 while(spos>=0x10000L)
356 gauss_window[gauss_ptr] = (short)*pS++;
357 gauss_ptr = (gauss_ptr+1) & 3;
360 vl = (spos >> 6) & ~3;
361 vr=(gauss[vl]*gvall0) >> 15;
362 vr+=(gauss[vl+1]*gvall(1)) >> 15;
363 vr+=(gauss[vl+2]*gvall(2)) >> 15;
364 vr+=(gauss[vl+3]*gvall(3)) >> 15;
369 while(spos>=0x10000L)
378 *spu.XAFeed++=(l|(l<<16));
380 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
381 if(spu.XAFeed==spu.XAPlay)
383 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
393 ////////////////////////////////////////////////////////////////////////
395 ////////////////////////////////////////////////////////////////////////
397 INLINE int FeedCDDA(unsigned char *pcm, int nBytes)
400 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
402 return 0x7761; // rearmed_wait
406 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
407 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
408 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
409 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
413 memcpy(spu.CDDAFeed,pcm,space);
414 spu.CDDAFeed+=space/4;
419 return 0x676f; // rearmed_go