spu: more status bits
[pcsx_rearmed.git] / plugins / dfsound / xa.c
... / ...
CommitLineData
1/***************************************************************************
2 xa.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8/***************************************************************************
9 * *
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. *
15 * *
16 ***************************************************************************/
17
18#include "stdafx.h"
19#include "spu.h"
20#define _IN_XA
21#include <stdint.h>
22
23// will be included from spu.c
24#ifdef _IN_SPU
25
26////////////////////////////////////////////////////////////////////////
27// XA GLOBALS
28////////////////////////////////////////////////////////////////////////
29
30static int gauss_ptr = 0;
31static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
32
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)]
37
38////////////////////////////////////////////////////////////////////////
39// MIX XA & CDDA
40////////////////////////////////////////////////////////////////////////
41
42INLINE void SkipCD(int ns_to, int decode_pos)
43{
44 int cursor = decode_pos;
45 int ns;
46
47 if(spu.XAPlay != spu.XAFeed)
48 {
49 for(ns = 0; ns < ns_to*2; ns += 2)
50 {
51 if(spu.XAPlay != spu.XAFeed) spu.XAPlay++;
52 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
53
54 spu.spuMem[cursor] = 0;
55 spu.spuMem[cursor + 0x400/2] = 0;
56 cursor = (cursor + 1) & 0x1ff;
57 }
58 }
59 else if(spu.CDDAPlay != spu.CDDAFeed)
60 {
61 for(ns = 0; ns < ns_to*2; ns += 2)
62 {
63 if(spu.CDDAPlay != spu.CDDAFeed) spu.CDDAPlay++;
64 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
65
66 spu.spuMem[cursor] = 0;
67 spu.spuMem[cursor + 0x400/2] = 0;
68 cursor = (cursor + 1) & 0x1ff;
69 }
70 }
71 spu.XALastVal = 0;
72}
73
74INLINE void MixCD(int *SSumLR, int *RVB, int ns_to, int decode_pos)
75{
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;
81 int l1, r1, l, r;
82 int ns;
83 uint32_t v = spu.XALastVal;
84
85 // note: spu volume doesn't affect cd capture
86 if ((spu.cdv.ll | spu.cdv.lr | spu.cdv.rl | spu.cdv.rr) == 0)
87 {
88 SkipCD(ns_to, decode_pos);
89 return;
90 }
91
92 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
93 {
94 if(spu.XAPlay == spu.XAFeed)
95 spu.XARepeat--;
96
97 for(ns = 0; ns < ns_to*2; ns += 2)
98 {
99 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
100 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
101
102 l1 = (short)v, r1 = (short)(v >> 16);
103 l = (l1 * vll + r1 * vrl) >> 15;
104 r = (r1 * vrr + l1 * vlr) >> 15;
105 ssat32_to_16(l);
106 ssat32_to_16(r);
107 if (spu.spuCtrl & CTRL_CD)
108 {
109 SSumLR[ns+0] += l;
110 SSumLR[ns+1] += r;
111 }
112 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
113 {
114 RVB[ns+0] += l;
115 RVB[ns+1] += r;
116 }
117
118 spu.spuMem[cursor] = HTOLE16(v);
119 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
120 cursor = (cursor + 1) & 0x1ff;
121 }
122 spu.XALastVal = v;
123 }
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)
127 {
128 for(ns = 0; ns < ns_to*2; ns += 2)
129 {
130 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
131 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
132
133 l1 = (short)v, r1 = (short)(v >> 16);
134 l = (l1 * vll + r1 * vrl) >> 15;
135 r = (r1 * vrr + l1 * vlr) >> 15;
136 ssat32_to_16(l);
137 ssat32_to_16(r);
138 if (spu.spuCtrl & CTRL_CD)
139 {
140 SSumLR[ns+0] += l;
141 SSumLR[ns+1] += r;
142 }
143 if (unlikely(spu.spuCtrl & CTRL_CDREVERB))
144 {
145 RVB[ns+0] += l;
146 RVB[ns+1] += r;
147 }
148
149 spu.spuMem[cursor] = HTOLE16(v);
150 spu.spuMem[cursor + 0x400/2] = HTOLE16(v >> 16);
151 cursor = (cursor + 1) & 0x1ff;
152 }
153 spu.XALastVal = v;
154 }
155 else
156 spu.XALastVal = 0;
157}
158
159////////////////////////////////////////////////////////////////////////
160// small linux time helper... only used for watchdog
161////////////////////////////////////////////////////////////////////////
162
163#if 0
164static unsigned long timeGetTime_spu()
165{
166#if defined(NO_OS)
167 return 0;
168#elif defined(_WIN32)
169 return GetTickCount();
170#else
171 struct timeval tv;
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
174#endif
175}
176#endif
177
178////////////////////////////////////////////////////////////////////////
179// FEED XA
180////////////////////////////////////////////////////////////////////////
181
182void FeedXA(const xa_decode_t *xap)
183{
184 int sinc,spos,i,iSize,iPlace,vl,vr;
185
186 if(!spu.bSPUIsOpen) return;
187
188 spu.XARepeat = 3; // set up repeat
189
190#if 0//def XA_HACK
191 iSize=((45500*xap->nsamples)/xap->freq); // get size
192#else
193 iSize=((44100*xap->nsamples)/xap->freq); // get size
194#endif
195 if(!iSize) return; // none? bye
196
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);
199
200 if(iPlace==0) return; // no place at all
201
202 //----------------------------------------------------//
203#if 0
204 if(spu_config.iXAPitch) // pitch change option?
205 {
206 static DWORD dwLT=0;
207 static DWORD dwFPS=0;
208 static int iFPSCnt=0;
209 static int iLastSize=0;
210 static DWORD dwL1=0;
211 DWORD dw=timeGetTime_spu(),dw1,dw2;
212
213 iPlace=iSize;
214
215 dwFPS+=dw-dwLT;iFPSCnt++;
216
217 dwLT=dw;
218
219 if(iFPSCnt>=10)
220 {
221 if(!dwFPS) dwFPS=1;
222 dw1=1000000/dwFPS;
223 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
224 else dwL1=dw1;
225 dw2=(xap->freq*100/xap->nsamples);
226 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
227 else
228 {
229 iLastSize=iSize*dw2/dw1;
230 if(iLastSize>iPlace) iLastSize=iPlace;
231 iSize=iLastSize;
232 }
233 iFPSCnt=0;dwFPS=0;
234 }
235 else
236 {
237 if(iLastSize) iSize=iLastSize;
238 }
239 }
240#endif
241 //----------------------------------------------------//
242
243 spos=0x10000L;
244 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
245
246 if(xap->stereo)
247{
248 uint32_t * pS=(uint32_t *)xap->pcm;
249 uint32_t l=0;
250
251#if 0
252 if(spu_config.iXAPitch)
253 {
254 int32_t l1,l2;short s;
255 for(i=0;i<iSize;i++)
256 {
257 if(spu_config.iUseInterpolation==2)
258 {
259 while(spos>=0x10000L)
260 {
261 l = *pS++;
262 gauss_window[gauss_ptr] = (short)LOWORD(l);
263 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
264 gauss_ptr = (gauss_ptr+1) & 3;
265 spos -= 0x10000L;
266 }
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;
272 l= vr & 0xffff;
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;
277 l |= vr << 16;
278 }
279 else
280 {
281 while(spos>=0x10000L)
282 {
283 l = *pS++;
284 spos -= 0x10000L;
285 }
286 }
287
288 s=(short)LOWORD(l);
289 l1=s;
290 l1=(l1*iPlace)/iSize;
291 ssat32_to_16(l1);
292 s=(short)HIWORD(l);
293 l2=s;
294 l2=(l2*iPlace)/iSize;
295 ssat32_to_16(l2);
296 l=(l1&0xffff)|(l2<<16);
297
298 *spu.XAFeed++=l;
299
300 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
301 if(spu.XAFeed==spu.XAPlay)
302 {
303 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
304 break;
305 }
306
307 spos += sinc;
308 }
309 }
310 else
311#endif
312 {
313 for(i=0;i<iSize;i++)
314 {
315 if(spu_config.iUseInterpolation==2)
316 {
317 while(spos>=0x10000L)
318 {
319 l = *pS++;
320 gauss_window[gauss_ptr] = (short)LOWORD(l);
321 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
322 gauss_ptr = (gauss_ptr+1) & 3;
323 spos -= 0x10000L;
324 }
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;
330 l= vr & 0xffff;
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;
335 l |= vr << 16;
336 }
337 else
338 {
339 while(spos>=0x10000L)
340 {
341 l = *pS++;
342 spos -= 0x10000L;
343 }
344 }
345
346 *spu.XAFeed++=l;
347
348 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
349 if(spu.XAFeed==spu.XAPlay)
350 {
351 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
352 break;
353 }
354
355 spos += sinc;
356 }
357 }
358 }
359 else
360 {
361 unsigned short * pS=(unsigned short *)xap->pcm;
362 uint32_t l;short s=0;
363
364#if 0
365 if(spu_config.iXAPitch)
366 {
367 int32_t l1;
368 for(i=0;i<iSize;i++)
369 {
370 if(spu_config.iUseInterpolation==2)
371 {
372 while(spos>=0x10000L)
373 {
374 gauss_window[gauss_ptr] = (short)*pS++;
375 gauss_ptr = (gauss_ptr+1) & 3;
376 spos -= 0x10000L;
377 }
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;
383 l1=s= vr;
384 l1 &= 0xffff;
385 }
386 else
387 {
388 while(spos>=0x10000L)
389 {
390 s = *pS++;
391 spos -= 0x10000L;
392 }
393 l1=s;
394 }
395
396 l1=(l1*iPlace)/iSize;
397 ssat32_to_16(l1);
398 l=(l1&0xffff)|(l1<<16);
399 *spu.XAFeed++=l;
400
401 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
402 if(spu.XAFeed==spu.XAPlay)
403 {
404 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
405 break;
406 }
407
408 spos += sinc;
409 }
410 }
411 else
412#endif
413 {
414 for(i=0;i<iSize;i++)
415 {
416 if(spu_config.iUseInterpolation==2)
417 {
418 while(spos>=0x10000L)
419 {
420 gauss_window[gauss_ptr] = (short)*pS++;
421 gauss_ptr = (gauss_ptr+1) & 3;
422 spos -= 0x10000L;
423 }
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;
429 l=s= vr;
430 }
431 else
432 {
433 while(spos>=0x10000L)
434 {
435 s = *pS++;
436 spos -= 0x10000L;
437 }
438 l=s;
439 }
440
441 l &= 0xffff;
442 *spu.XAFeed++=(l|(l<<16));
443
444 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
445 if(spu.XAFeed==spu.XAPlay)
446 {
447 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
448 break;
449 }
450
451 spos += sinc;
452 }
453 }
454 }
455}
456
457////////////////////////////////////////////////////////////////////////
458// FEED CDDA
459////////////////////////////////////////////////////////////////////////
460
461void FeedCDDA(unsigned char *pcm, int nBytes)
462{
463 int space;
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);
467 return;
468 }
469
470 while(nBytes>0)
471 {
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;
476 if(space>nBytes)
477 space=nBytes;
478
479 memcpy(spu.CDDAFeed,pcm,space);
480 spu.CDDAFeed+=space/4;
481 nBytes-=space;
482 pcm+=space;
483 }
484}
485
486#endif
487// vim:shiftwidth=1:expandtab