spu: try to eliminate some cdda clicks/pops
[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#define _IN_XA
20#include <stdint.h>
21
22// will be included from spu.c
23#ifdef _IN_SPU
24
25////////////////////////////////////////////////////////////////////////
26// XA GLOBALS
27////////////////////////////////////////////////////////////////////////
28
29static int gauss_ptr = 0;
30static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0};
31
32#define gvall0 gauss_window[gauss_ptr]
33#define gvall(x) gauss_window[(gauss_ptr+x)&3]
34#define gvalr0 gauss_window[4+gauss_ptr]
35#define gvalr(x) gauss_window[4+((gauss_ptr+x)&3)]
36
37////////////////////////////////////////////////////////////////////////
38// MIX XA & CDDA
39////////////////////////////////////////////////////////////////////////
40
41INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos)
42{
43 int cursor = decode_pos;
44 int ns;
45 short l, r;
46 uint32_t v = spu.XALastVal;
47
48 if(spu.XAPlay != spu.XAFeed || spu.XARepeat > 0)
49 {
50 if(spu.XAPlay == spu.XAFeed)
51 spu.XARepeat--;
52
53 for(ns = 0; ns < ns_to*2; )
54 {
55 if(spu.XAPlay != spu.XAFeed) v=*spu.XAPlay++;
56 if(spu.XAPlay == spu.XAEnd) spu.XAPlay=spu.XAStart;
57
58 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
59 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
60 SSumLR[ns++] += l;
61 SSumLR[ns++] += r;
62
63 spu.spuMem[cursor] = v;
64 spu.spuMem[cursor + 0x400/2] = v >> 16;
65 cursor = (cursor + 1) & 0x1ff;
66 }
67 spu.XALastVal = v;
68 }
69 // occasionally CDDAFeed underflows by a few samples due to poor timing,
70 // hence this 'ns_to < 8'
71 else if(spu.CDDAPlay != spu.CDDAFeed || ns_to < 8)
72 {
73 for(ns = 0; ns < ns_to*2; )
74 {
75 if(spu.CDDAPlay != spu.CDDAFeed) v=*spu.CDDAPlay++;
76 if(spu.CDDAPlay == spu.CDDAEnd) spu.CDDAPlay=spu.CDDAStart;
77
78 l = ((int)(short)v * spu.iLeftXAVol) >> 15;
79 r = ((int)(short)(v >> 16) * spu.iLeftXAVol) >> 15;
80 SSumLR[ns++] += l;
81 SSumLR[ns++] += r;
82
83 spu.spuMem[cursor] = v;
84 spu.spuMem[cursor + 0x400/2] = v >> 16;
85 cursor = (cursor + 1) & 0x1ff;
86 }
87 spu.XALastVal = v;
88 }
89 else
90 spu.XALastVal = 0;
91}
92
93////////////////////////////////////////////////////////////////////////
94// small linux time helper... only used for watchdog
95////////////////////////////////////////////////////////////////////////
96
97static unsigned long timeGetTime_spu()
98{
99#if defined(NO_OS)
100 return 0;
101#elif defined(_WIN32)
102 return GetTickCount();
103#else
104 struct timeval tv;
105 gettimeofday(&tv, 0); // well, maybe there are better ways
106 return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works
107#endif
108}
109
110////////////////////////////////////////////////////////////////////////
111// FEED XA
112////////////////////////////////////////////////////////////////////////
113
114INLINE void FeedXA(xa_decode_t *xap)
115{
116 int sinc,spos,i,iSize,iPlace,vl,vr;
117
118 if(!spu.bSPUIsOpen) return;
119
120 spu.xapGlobal = xap; // store info for save states
121 spu.XARepeat = 100; // set up repeat
122
123#if 0//def XA_HACK
124 iSize=((45500*xap->nsamples)/xap->freq); // get size
125#else
126 iSize=((44100*xap->nsamples)/xap->freq); // get size
127#endif
128 if(!iSize) return; // none? bye
129
130 if(spu.XAFeed<spu.XAPlay) iPlace=spu.XAPlay-spu.XAFeed; // how much space in my buf?
131 else iPlace=(spu.XAEnd-spu.XAFeed) + (spu.XAPlay-spu.XAStart);
132
133 if(iPlace==0) return; // no place at all
134
135 //----------------------------------------------------//
136 if(spu_config.iXAPitch) // pitch change option?
137 {
138 static DWORD dwLT=0;
139 static DWORD dwFPS=0;
140 static int iFPSCnt=0;
141 static int iLastSize=0;
142 static DWORD dwL1=0;
143 DWORD dw=timeGetTime_spu(),dw1,dw2;
144
145 iPlace=iSize;
146
147 dwFPS+=dw-dwLT;iFPSCnt++;
148
149 dwLT=dw;
150
151 if(iFPSCnt>=10)
152 {
153 if(!dwFPS) dwFPS=1;
154 dw1=1000000/dwFPS;
155 if(dw1>=(dwL1-100) && dw1<=(dwL1+100)) dw1=dwL1;
156 else dwL1=dw1;
157 dw2=(xap->freq*100/xap->nsamples);
158 if((!dw1)||((dw2+100)>=dw1)) iLastSize=0;
159 else
160 {
161 iLastSize=iSize*dw2/dw1;
162 if(iLastSize>iPlace) iLastSize=iPlace;
163 iSize=iLastSize;
164 }
165 iFPSCnt=0;dwFPS=0;
166 }
167 else
168 {
169 if(iLastSize) iSize=iLastSize;
170 }
171 }
172 //----------------------------------------------------//
173
174 spos=0x10000L;
175 sinc = (xap->nsamples << 16) / iSize; // calc freq by num / size
176
177 if(xap->stereo)
178{
179 uint32_t * pS=(uint32_t *)xap->pcm;
180 uint32_t l=0;
181
182 if(spu_config.iXAPitch)
183 {
184 int32_t l1,l2;short s;
185 for(i=0;i<iSize;i++)
186 {
187 if(spu_config.iUseInterpolation==2)
188 {
189 while(spos>=0x10000L)
190 {
191 l = *pS++;
192 gauss_window[gauss_ptr] = (short)LOWORD(l);
193 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
194 gauss_ptr = (gauss_ptr+1) & 3;
195 spos -= 0x10000L;
196 }
197 vl = (spos >> 6) & ~3;
198 vr=(gauss[vl]*gvall0)&~2047;
199 vr+=(gauss[vl+1]*gvall(1))&~2047;
200 vr+=(gauss[vl+2]*gvall(2))&~2047;
201 vr+=(gauss[vl+3]*gvall(3))&~2047;
202 l= (vr >> 11) & 0xffff;
203 vr=(gauss[vl]*gvalr0)&~2047;
204 vr+=(gauss[vl+1]*gvalr(1))&~2047;
205 vr+=(gauss[vl+2]*gvalr(2))&~2047;
206 vr+=(gauss[vl+3]*gvalr(3))&~2047;
207 l |= vr << 5;
208 }
209 else
210 {
211 while(spos>=0x10000L)
212 {
213 l = *pS++;
214 spos -= 0x10000L;
215 }
216 }
217
218 s=(short)LOWORD(l);
219 l1=s;
220 l1=(l1*iPlace)/iSize;
221 ssat32_to_16(l1);
222 s=(short)HIWORD(l);
223 l2=s;
224 l2=(l2*iPlace)/iSize;
225 ssat32_to_16(l2);
226 l=(l1&0xffff)|(l2<<16);
227
228 *spu.XAFeed++=l;
229
230 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
231 if(spu.XAFeed==spu.XAPlay)
232 {
233 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
234 break;
235 }
236
237 spos += sinc;
238 }
239 }
240 else
241 {
242 for(i=0;i<iSize;i++)
243 {
244 if(spu_config.iUseInterpolation==2)
245 {
246 while(spos>=0x10000L)
247 {
248 l = *pS++;
249 gauss_window[gauss_ptr] = (short)LOWORD(l);
250 gauss_window[4+gauss_ptr] = (short)HIWORD(l);
251 gauss_ptr = (gauss_ptr+1) & 3;
252 spos -= 0x10000L;
253 }
254 vl = (spos >> 6) & ~3;
255 vr=(gauss[vl]*gvall0)&~2047;
256 vr+=(gauss[vl+1]*gvall(1))&~2047;
257 vr+=(gauss[vl+2]*gvall(2))&~2047;
258 vr+=(gauss[vl+3]*gvall(3))&~2047;
259 l= (vr >> 11) & 0xffff;
260 vr=(gauss[vl]*gvalr0)&~2047;
261 vr+=(gauss[vl+1]*gvalr(1))&~2047;
262 vr+=(gauss[vl+2]*gvalr(2))&~2047;
263 vr+=(gauss[vl+3]*gvalr(3))&~2047;
264 l |= vr << 5;
265 }
266 else
267 {
268 while(spos>=0x10000L)
269 {
270 l = *pS++;
271 spos -= 0x10000L;
272 }
273 }
274
275 *spu.XAFeed++=l;
276
277 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
278 if(spu.XAFeed==spu.XAPlay)
279 {
280 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
281 break;
282 }
283
284 spos += sinc;
285 }
286 }
287 }
288 else
289 {
290 unsigned short * pS=(unsigned short *)xap->pcm;
291 uint32_t l;short s=0;
292
293 if(spu_config.iXAPitch)
294 {
295 int32_t l1;
296 for(i=0;i<iSize;i++)
297 {
298 if(spu_config.iUseInterpolation==2)
299 {
300 while(spos>=0x10000L)
301 {
302 gauss_window[gauss_ptr] = (short)*pS++;
303 gauss_ptr = (gauss_ptr+1) & 3;
304 spos -= 0x10000L;
305 }
306 vl = (spos >> 6) & ~3;
307 vr=(gauss[vl]*gvall0)&~2047;
308 vr+=(gauss[vl+1]*gvall(1))&~2047;
309 vr+=(gauss[vl+2]*gvall(2))&~2047;
310 vr+=(gauss[vl+3]*gvall(3))&~2047;
311 l1=s= vr >> 11;
312 l1 &= 0xffff;
313 }
314 else
315 {
316 while(spos>=0x10000L)
317 {
318 s = *pS++;
319 spos -= 0x10000L;
320 }
321 l1=s;
322 }
323
324 l1=(l1*iPlace)/iSize;
325 ssat32_to_16(l1);
326 l=(l1&0xffff)|(l1<<16);
327 *spu.XAFeed++=l;
328
329 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
330 if(spu.XAFeed==spu.XAPlay)
331 {
332 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
333 break;
334 }
335
336 spos += sinc;
337 }
338 }
339 else
340 {
341 for(i=0;i<iSize;i++)
342 {
343 if(spu_config.iUseInterpolation==2)
344 {
345 while(spos>=0x10000L)
346 {
347 gauss_window[gauss_ptr] = (short)*pS++;
348 gauss_ptr = (gauss_ptr+1) & 3;
349 spos -= 0x10000L;
350 }
351 vl = (spos >> 6) & ~3;
352 vr=(gauss[vl]*gvall0)&~2047;
353 vr+=(gauss[vl+1]*gvall(1))&~2047;
354 vr+=(gauss[vl+2]*gvall(2))&~2047;
355 vr+=(gauss[vl+3]*gvall(3))&~2047;
356 l=s= vr >> 11;
357 }
358 else
359 {
360 while(spos>=0x10000L)
361 {
362 s = *pS++;
363 spos -= 0x10000L;
364 }
365 l=s;
366 }
367
368 l &= 0xffff;
369 *spu.XAFeed++=(l|(l<<16));
370
371 if(spu.XAFeed==spu.XAEnd) spu.XAFeed=spu.XAStart;
372 if(spu.XAFeed==spu.XAPlay)
373 {
374 if(spu.XAPlay!=spu.XAStart) spu.XAFeed=spu.XAPlay-1;
375 break;
376 }
377
378 spos += sinc;
379 }
380 }
381 }
382}
383
384////////////////////////////////////////////////////////////////////////
385// FEED CDDA
386////////////////////////////////////////////////////////////////////////
387
388INLINE int FeedCDDA(unsigned char *pcm, int nBytes)
389{
390 int space;
391 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
392 if(space<nBytes)
393 return 0x7761; // rearmed_wait
394
395 while(nBytes>0)
396 {
397 if(spu.CDDAFeed==spu.CDDAEnd) spu.CDDAFeed=spu.CDDAStart;
398 space=(spu.CDDAPlay-spu.CDDAFeed-1)*4 & (CDDA_BUFFER_SIZE - 1);
399 if(spu.CDDAFeed+space/4>spu.CDDAEnd)
400 space=(spu.CDDAEnd-spu.CDDAFeed)*4;
401 if(space>nBytes)
402 space=nBytes;
403
404 memcpy(spu.CDDAFeed,pcm,space);
405 spu.CDDAFeed+=space/4;
406 nBytes-=space;
407 pcm+=space;
408 }
409
410 return 0x676f; // rearmed_go
411}
412
413#endif