spu: fix first sample, don't play ahead
[pcsx_rearmed.git] / plugins / dfsound / spu.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 spu.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
07a6dd2c 7
bdd050c3 8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014
07a6dd2c 9
ef79bbde
P
10 ***************************************************************************/
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. See also the license.txt file for *
17 * additional informations. *
18 * *
19 ***************************************************************************/
20
21#include "stdafx.h"
22
23#define _IN_SPU
24
25#include "externals.h"
3fc2a4c2 26#include "registers.h"
07c13dfd 27#include "out.h"
665f33e1 28#include "arm_features.h"
3154bfab 29#include "spu_config.h"
ef79bbde
P
30
31#ifdef ENABLE_NLS
32#include <libintl.h>
33#include <locale.h>
34#define _(x) gettext(x)
35#define N_(x) (x)
36#else
37#define _(x) (x)
38#define N_(x) (x)
39#endif
40
a80ae4a0 41#ifdef __ARM_ARCH_7A__
1775933a 42 #define ssat32_to_16(v) \
43 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
44#else
45 #define ssat32_to_16(v) do { \
46 if (v < -32768) v = -32768; \
47 else if (v > 32767) v = 32767; \
48 } while (0)
49#endif
50
fb552464 51#define PSXCLK 33868800 /* 33.8688 MHz */
52
650adfd2 53// intended to be ~1 frame
54#define IRQ_NEAR_BLOCKS 32
55
6d866bb7 56/*
ef79bbde
P
57#if defined (USEMACOSX)
58static char * libraryName = N_("Mac OS X Sound");
59#elif defined (USEALSA)
60static char * libraryName = N_("ALSA Sound");
61#elif defined (USEOSS)
62static char * libraryName = N_("OSS Sound");
63#elif defined (USESDL)
64static char * libraryName = N_("SDL Sound");
65#elif defined (USEPULSEAUDIO)
66static char * libraryName = N_("PulseAudio Sound");
67#else
68static char * libraryName = N_("NULL Sound");
69#endif
70
71static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
6d866bb7 72*/
ef79bbde
P
73
74// globals
75
3154bfab 76SPUInfo spu;
77SPUConfig spu_config;
ef79bbde
P
78
79// MAIN infos struct for each channel
80
81SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
82REVERBInfo rvb;
83
ef79bbde
P
84// certain globals (were local before, but with the new timeproc I need em global)
85
6d866bb7 86static const int f[8][2] = { { 0, 0 },
ef79bbde
P
87 { 60, 0 },
88 { 115, -52 },
89 { 98, -55 },
90 { 122, -60 } };
3154bfab 91int ChanBuf[NSSIZE];
92int SSumLR[NSSIZE*2];
ef79bbde 93int iFMod[NSSIZE];
ef79bbde 94
983a7cfd 95#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
96
ef79bbde
P
97////////////////////////////////////////////////////////////////////////
98// CODE AREA
99////////////////////////////////////////////////////////////////////////
100
101// dirty inline func includes
102
103#include "reverb.c"
104#include "adsr.c"
105
106////////////////////////////////////////////////////////////////////////
107// helpers for simple interpolation
108
109//
110// easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
111//
112// instead of having n equal sample values in a row like:
113// ____
114// |____
115//
116// we compare the current delta change with the next delta change.
117//
118// if curr_delta is positive,
119//
120// - and next delta is smaller (or changing direction):
121// \.
122// -__
123//
124// - and next delta significant (at least twice) bigger:
125// --_
126// \.
127//
128// - and next delta is nearly same:
129// \.
130// \.
131//
132//
133// if curr_delta is negative,
134//
135// - and next delta is smaller (or changing direction):
136// _--
137// /
138//
139// - and next delta significant (at least twice) bigger:
140// /
141// __-
142//
143// - and next delta is nearly same:
144// /
145// /
146//
147
148
149INLINE void InterpolateUp(int ch)
150{
151 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
152 {
153 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
154 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
155
156 s_chan[ch].SB[32]=0;
157
158 if(id1>0) // curr delta positive
159 {
160 if(id2<id1)
161 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
162 else
163 if(id2<(id1<<1))
164 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
165 else
166 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
167 }
168 else // curr delta negative
169 {
170 if(id2>id1)
171 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
172 else
173 if(id2>(id1<<1))
174 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
175 else
176 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
177 }
178 }
179 else
180 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
181 {
182 s_chan[ch].SB[32]=0;
183
184 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
76d1d09c 185 //if(s_chan[ch].sinc<=0x8000)
186 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
187 //else
188 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
ef79bbde
P
189 }
190 else // no flags? add bigger val (if possible), calc smaller step, set flag1
191 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
192}
193
194//
195// even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
196//
197
198INLINE void InterpolateDown(int ch)
199{
200 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
201 {
202 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
203 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
204 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
205 }
206}
207
208////////////////////////////////////////////////////////////////////////
209// helpers for gauss interpolation
210
3154bfab 211#define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos&3])
f05d6ca2 212#define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
ef79bbde
P
213
214#include "gauss_i.h"
215
216////////////////////////////////////////////////////////////////////////
217
218#include "xa.c"
219
e4f075af 220static void do_irq(void)
221{
3154bfab 222 //if(!(spu.spuStat & STAT_IRQ))
e4f075af 223 {
3154bfab 224 spu.spuStat |= STAT_IRQ; // asserted status?
225 if(spu.irqCallback) spu.irqCallback();
e4f075af 226 }
227}
228
229static int check_irq(int ch, unsigned char *pos)
230{
3154bfab 231 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
e4f075af 232 {
3154bfab 233 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
e4f075af 234 do_irq();
235 return 1;
236 }
237 return 0;
238}
239
ef79bbde
P
240////////////////////////////////////////////////////////////////////////
241// START SOUND... called by main thread to setup a new sound on a channel
242////////////////////////////////////////////////////////////////////////
243
244INLINE void StartSound(int ch)
245{
246 StartADSR(ch);
247 StartREVERB(ch);
248
b00afb77 249 // fussy timing issues - do in VoiceOn
250 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
251 //s_chan[ch].bStop=0;
252 //s_chan[ch].bOn=1;
ef79bbde 253
381ea103 254 s_chan[ch].SB[26]=0; // init mixing vars
255 s_chan[ch].SB[27]=0;
bdd050c3 256 s_chan[ch].iSBPos=27;
ef79bbde 257
bdd050c3 258 s_chan[ch].SB[28]=0;
ef79bbde
P
259 s_chan[ch].SB[29]=0; // init our interpolation helpers
260 s_chan[ch].SB[30]=0;
bdd050c3 261 s_chan[ch].SB[31]=0;
262 s_chan[ch].spos=0;
ef79bbde 263
3154bfab 264 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
ef79bbde
P
265}
266
267////////////////////////////////////////////////////////////////////////
268// ALL KIND OF HELPERS
269////////////////////////////////////////////////////////////////////////
270
07a6dd2c 271INLINE int FModChangeFrequency(int ch,int ns)
ef79bbde 272{
7e44d49d 273 unsigned int NP=s_chan[ch].iRawPitch;
07a6dd2c 274 int sinc;
ef79bbde
P
275
276 NP=((32768L+iFMod[ns])*NP)/32768L;
277
278 if(NP>0x3fff) NP=0x3fff;
279 if(NP<0x1) NP=0x1;
280
7e44d49d 281 sinc=NP<<4; // calc frequency
3154bfab 282 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
07a6dd2c 283 s_chan[ch].SB[32]=1;
ef79bbde 284 iFMod[ns]=0;
07a6dd2c 285
286 return sinc;
ef79bbde
P
287}
288
289////////////////////////////////////////////////////////////////////////
290
ef79bbde
P
291INLINE void StoreInterpolationVal(int ch,int fa)
292{
293 if(s_chan[ch].bFMod==2) // fmod freq channel
294 s_chan[ch].SB[29]=fa;
295 else
296 {
381ea103 297 ssat32_to_16(fa);
ef79bbde 298
3154bfab 299 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
ef79bbde
P
300 {
301 int gpos = s_chan[ch].SB[28];
302 gval0 = fa;
303 gpos = (gpos+1) & 3;
304 s_chan[ch].SB[28] = gpos;
305 }
306 else
3154bfab 307 if(spu_config.iUseInterpolation==1) // simple interpolation
ef79bbde
P
308 {
309 s_chan[ch].SB[28] = 0;
310 s_chan[ch].SB[29] = s_chan[ch].SB[30]; // -> helpers for simple linear interpolation: delay real val for two slots, and calc the two deltas, for a 'look at the future behaviour'
311 s_chan[ch].SB[30] = s_chan[ch].SB[31];
312 s_chan[ch].SB[31] = fa;
313 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
314 }
315 else s_chan[ch].SB[29]=fa; // no interpolation
316 }
317}
318
319////////////////////////////////////////////////////////////////////////
320
8cd23d6d 321INLINE int iGetInterpolationVal(int ch, int spos)
ef79bbde
P
322{
323 int fa;
324
325 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
326
3154bfab 327 switch(spu_config.iUseInterpolation)
ef79bbde
P
328 {
329 //--------------------------------------------------//
330 case 3: // cubic interpolation
331 {
332 long xd;int gpos;
8cd23d6d 333 xd = (spos >> 1)+1;
ef79bbde
P
334 gpos = s_chan[ch].SB[28];
335
336 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
337 fa *= (xd - (2<<15)) / 6;
338 fa >>= 15;
339 fa += gval(2) - gval(1) - gval(1) + gval0;
340 fa *= (xd - (1<<15)) >> 1;
341 fa >>= 15;
342 fa += gval(1) - gval0;
343 fa *= xd;
344 fa >>= 15;
345 fa = fa + gval0;
346
347 } break;
348 //--------------------------------------------------//
349 case 2: // gauss interpolation
350 {
351 int vl, vr;int gpos;
8cd23d6d 352 vl = (spos >> 6) & ~3;
ef79bbde 353 gpos = s_chan[ch].SB[28];
f05d6ca2 354 vr=(gauss[vl]*(int)gval0)&~2047;
ef79bbde
P
355 vr+=(gauss[vl+1]*gval(1))&~2047;
356 vr+=(gauss[vl+2]*gval(2))&~2047;
357 vr+=(gauss[vl+3]*gval(3))&~2047;
358 fa = vr>>11;
359 } break;
360 //--------------------------------------------------//
361 case 1: // simple interpolation
362 {
363 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
364 InterpolateUp(ch); // --> interpolate up
365 else InterpolateDown(ch); // --> else down
366 fa=s_chan[ch].SB[29];
367 } break;
368 //--------------------------------------------------//
369 default: // no interpolation
370 {
371 fa=s_chan[ch].SB[29];
372 } break;
373 //--------------------------------------------------//
374 }
375
376 return fa;
377}
378
381ea103 379static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
380{
381 int nSample;
382 int fa, s_1, s_2, d, s;
383
384 s_1 = dest[27];
385 s_2 = dest[26];
386
387 for (nSample = 0; nSample < 28; src++)
388 {
389 d = (int)*src;
390 s = (int)(signed short)((d & 0x0f) << 12);
391
392 fa = s >> shift_factor;
393 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
394 s_2=s_1;s_1=fa;
395
396 dest[nSample++] = fa;
397
398 s = (int)(signed short)((d & 0xf0) << 8);
399 fa = s >> shift_factor;
400 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
401 s_2=s_1;s_1=fa;
402
403 dest[nSample++] = fa;
404 }
405}
406
e11ae5c5 407static int decode_block(int ch)
408{
409 unsigned char *start;
381ea103 410 int predict_nr,shift_factor,flags;
f9fe4923 411 int stop = 0;
e11ae5c5 412 int ret = 0;
413
f9fe4923 414 start = s_chan[ch].pCurr; // set up the current pos
3154bfab 415 if(start == spu.spuMemC) // ?
f9fe4923 416 stop = 1;
e11ae5c5 417
e4f075af 418 if(s_chan[ch].prevflags&1) // 1: stop/loop
3fc2a4c2 419 {
e4f075af 420 if(!(s_chan[ch].prevflags&2))
f9fe4923 421 stop = 1;
e4f075af 422
423 start = s_chan[ch].pLoop;
3fc2a4c2 424 }
89cb2058 425 else
426 ret = check_irq(ch, start); // hack, see check_irq below..
e11ae5c5 427
f9fe4923 428 if(stop)
429 {
3154bfab 430 spu.dwChannelOn &= ~(1<<ch); // -> turn everything off
f9fe4923 431 s_chan[ch].bStop = 1;
432 s_chan[ch].ADSRX.EnvelopeVol = 0;
433 }
434
381ea103 435 predict_nr=(int)start[0];
e11ae5c5 436 shift_factor=predict_nr&0xf;
437 predict_nr >>= 4;
e11ae5c5 438
381ea103 439 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
e11ae5c5 440
381ea103 441 flags=(int)start[1];
442 if(flags&4)
443 s_chan[ch].pLoop=start; // loop adress
e11ae5c5 444
381ea103 445 start+=16;
3fc2a4c2 446
89cb2058 447 if(flags&1) { // 1: stop/loop
3fc2a4c2 448 start = s_chan[ch].pLoop;
89cb2058 449 ret |= check_irq(ch, start); // hack.. :(
450 }
e11ae5c5 451
3154bfab 452 if (start - spu.spuMemC >= 0x80000)
453 start = spu.spuMemC;
e4f075af 454
381ea103 455 s_chan[ch].pCurr = start; // store values for next cycle
e4f075af 456 s_chan[ch].prevflags = flags;
e11ae5c5 457
458 return ret;
459}
460
07a6dd2c 461// do block, but ignore sample data
462static int skip_block(int ch)
463{
464 unsigned char *start = s_chan[ch].pCurr;
465 int flags = start[1];
89cb2058 466 int ret = check_irq(ch, start);
07a6dd2c 467
e4f075af 468 if(s_chan[ch].prevflags & 1)
469 start = s_chan[ch].pLoop;
07a6dd2c 470
381ea103 471 if(flags & 4)
be1cb678 472 s_chan[ch].pLoop = start;
07a6dd2c 473
e4f075af 474 start += 16;
07a6dd2c 475
476 if(flags & 1)
e4f075af 477 start = s_chan[ch].pLoop;
07a6dd2c 478
e4f075af 479 s_chan[ch].pCurr = start;
480 s_chan[ch].prevflags = flags;
481
89cb2058 482 return ret;
07a6dd2c 483}
484
650adfd2 485// if irq is going to trigger sooner than in upd_samples, set upd_samples
486static void scan_for_irq(int ch, unsigned int *upd_samples)
487{
488 int pos, sinc, sinc_inv, end;
489 unsigned char *block;
490 int flags;
491
492 block = s_chan[ch].pCurr;
493 pos = s_chan[ch].spos;
494 sinc = s_chan[ch].sinc;
495 end = pos + *upd_samples * sinc;
496
497 pos += (28 - s_chan[ch].iSBPos) << 16;
498 while (pos < end)
499 {
3154bfab 500 if (block == spu.pSpuIrq)
650adfd2 501 break;
502 flags = block[1];
503 block += 16;
504 if (flags & 1) { // 1: stop/loop
505 block = s_chan[ch].pLoop;
3154bfab 506 if (block == spu.pSpuIrq) // hack.. (see decode_block)
650adfd2 507 break;
508 }
509 pos += 28 << 16;
510 }
511
512 if (pos < end)
513 {
514 sinc_inv = s_chan[ch].sinc_inv;
515 if (sinc_inv == 0)
516 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
517
518 pos -= s_chan[ch].spos;
519 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
520 //xprintf("ch%02d: irq sched: %3d %03d\n",
521 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
522 }
523}
524
07a6dd2c 525#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
650adfd2 526static noinline int do_samples_##name(int ch, int ns, int ns_to) \
07a6dd2c 527{ \
528 int sinc = s_chan[ch].sinc; \
529 int spos = s_chan[ch].spos; \
381ea103 530 int sbpos = s_chan[ch].iSBPos; \
531 int *SB = s_chan[ch].SB; \
07a6dd2c 532 int ret = -1; \
533 int d, fa; \
534 interp_start; \
535 \
536 for (; ns < ns_to; ns++) \
537 { \
538 fmod_code; \
539 \
650adfd2 540 spos += sinc; \
07a6dd2c 541 while (spos >= 0x10000) \
542 { \
650adfd2 543 fa = SB[sbpos++]; \
544 if(sbpos >= 28) \
07a6dd2c 545 { \
381ea103 546 sbpos = 0; \
07a6dd2c 547 d = decode_block(ch); \
f2aec10a 548 if(d) \
650adfd2 549 ret = /*ns_to =*/ ns + 1; \
07a6dd2c 550 } \
551 \
07a6dd2c 552 interp1_code; \
553 spos -= 0x10000; \
554 } \
555 \
556 interp2_code; \
07a6dd2c 557 } \
558 \
07a6dd2c 559 s_chan[ch].sinc = sinc; \
560 s_chan[ch].spos = spos; \
381ea103 561 s_chan[ch].iSBPos = sbpos; \
07a6dd2c 562 interp_end; \
563 \
564 return ret; \
565}
566
567#define fmod_recv_check \
568 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
569 sinc = FModChangeFrequency(ch,ns)
570
571make_do_samples(default, fmod_recv_check, ,
572 StoreInterpolationVal(ch, fa),
8cd23d6d 573 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
07a6dd2c 574make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
575
576#define simple_interp_store \
577 s_chan[ch].SB[28] = 0; \
578 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
579 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
580 s_chan[ch].SB[31] = fa; \
581 s_chan[ch].SB[32] = 1
582
583#define simple_interp_get \
584 if(sinc<0x10000) /* -> upsampling? */ \
585 InterpolateUp(ch); /* --> interpolate up */ \
586 else InterpolateDown(ch); /* --> else down */ \
587 ChanBuf[ns] = s_chan[ch].SB[29]
588
589make_do_samples(simple, , ,
590 simple_interp_store, simple_interp_get, )
591
650adfd2 592static noinline int do_samples_noise(int ch, int ns, int ns_to)
07a6dd2c 593{
b1094d0e 594 int level, shift, bit;
89cb2058 595 int ret = -1, d;
b1094d0e 596
07a6dd2c 597 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
598 while (s_chan[ch].spos >= 28*0x10000)
599 {
89cb2058 600 d = skip_block(ch);
601 if (d)
602 ret = ns_to;
07a6dd2c 603 s_chan[ch].spos -= 28*0x10000;
604 }
605
b1094d0e 606 // modified from DrHell/shalma, no fraction
3154bfab 607 level = (spu.spuCtrl >> 10) & 0x0f;
b1094d0e 608 level = 0x8000 >> level;
609
07a6dd2c 610 for (; ns < ns_to; ns++)
b1094d0e 611 {
3154bfab 612 spu.dwNoiseCount += 2;
613 if (spu.dwNoiseCount >= level)
b1094d0e 614 {
3154bfab 615 spu.dwNoiseCount -= level;
616 shift = (spu.dwNoiseVal >> 10) & 0x1f;
b1094d0e 617 bit = (0x69696969 >> shift) & 1;
3154bfab 618 if (spu.dwNoiseVal & 0x8000)
b1094d0e 619 bit ^= 1;
3154bfab 620 spu.dwNoiseVal = (spu.dwNoiseVal << 1) | bit;
b1094d0e 621 }
622
3154bfab 623 ChanBuf[ns] = (signed short)spu.dwNoiseVal;
b1094d0e 624 }
07a6dd2c 625
89cb2058 626 return ret;
07a6dd2c 627}
628
665f33e1 629#ifdef HAVE_ARMV5
3a721c1f 630// asm code; lv and rv must be 0-3fff
b17618c0 631extern void mix_chan(int start, int count, int lv, int rv);
3154bfab 632extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
b17618c0 633#else
634static void mix_chan(int start, int count, int lv, int rv)
635{
636 int *dst = SSumLR + start * 2;
637 const int *src = ChanBuf + start;
638 int l, r;
639
640 while (count--)
641 {
642 int sval = *src++;
643
644 l = (sval * lv) >> 14;
645 r = (sval * rv) >> 14;
646 *dst++ += l;
647 *dst++ += r;
648 }
649}
650
3154bfab 651static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
b17618c0 652{
653 int *dst = SSumLR + start * 2;
3154bfab 654 int *drvb = rvb + start * 2;
b17618c0 655 const int *src = ChanBuf + start;
656 int l, r;
657
658 while (count--)
659 {
660 int sval = *src++;
661
662 l = (sval * lv) >> 14;
663 r = (sval * rv) >> 14;
664 *dst++ += l;
665 *dst++ += r;
666 *drvb++ += l;
667 *drvb++ += r;
668 }
669}
670#endif
671
b72f17a1 672// 0x0800-0x0bff Voice 1
673// 0x0c00-0x0fff Voice 3
650adfd2 674static noinline void do_decode_bufs(int which, int start, int count)
b72f17a1 675{
676 const int *src = ChanBuf + start;
3154bfab 677 unsigned short *dst = &spu.spuMem[0x800/2 + which*0x400/2];
678 int cursor = spu.decode_pos + start;
b72f17a1 679
680 while (count-- > 0)
681 {
ee9afdbd 682 cursor &= 0x1ff;
b72f17a1 683 dst[cursor] = *src++;
ee9afdbd 684 cursor++;
b72f17a1 685 }
686
687 // decode_pos is updated and irqs are checked later, after voice loop
688}
689
ef79bbde
P
690////////////////////////////////////////////////////////////////////////
691// MAIN SPU FUNCTION
6d75977b 692// here is the main job handler...
ef79bbde
P
693// basically the whole sound processing is done in this fat func!
694////////////////////////////////////////////////////////////////////////
695
650adfd2 696void do_samples(unsigned int cycles_to)
ef79bbde 697{
650adfd2 698 const int ns_from = 0;
699 int ns,ns_to,ns_len;
3154bfab 700 int volmult = spu_config.iVolume;
174c454a 701 int ch,d,silentch;
650adfd2 702 int cycle_diff;
ef79bbde 703
3154bfab 704 cycle_diff = cycles_to - spu.cycles_played;
650adfd2 705 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
706 {
707 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
3154bfab 708 spu.cycles_played = cycles_to;
650adfd2 709 return;
710 }
e34a4bd3 711
650adfd2 712 if (cycle_diff < 2 * 768)
713 return;
e34a4bd3 714
650adfd2 715 ns_to = (cycle_diff / 768 + 1) & ~1;
716 if (ns_to > NSSIZE) {
717 // should never happen
718 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
719 ns_to = NSSIZE;
720 }
721
722 //////////////////////////////////////////////////////
723 // special irq handling in the decode buffers (0x0000-0x1000)
724 // we know:
725 // the decode buffers are located in spu memory in the following way:
726 // 0x0000-0x03ff CD audio left
727 // 0x0400-0x07ff CD audio right
728 // 0x0800-0x0bff Voice 1
729 // 0x0c00-0x0fff Voice 3
730 // and decoded data is 16 bit for one sample
731 // we assume:
732 // even if voices 1/3 are off or no cd audio is playing, the internal
733 // play positions will move on and wrap after 0x400 bytes.
734 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
735 // increase this pointer on each sample by 2 bytes. If this pointer
736 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
737 // an IRQ.
738
3154bfab 739 if (unlikely((spu.spuCtrl & CTRL_IRQ)
c4c66b22 740 && spu.pSpuIrq < spu.spuMemC+0x1000))
650adfd2 741 {
3154bfab 742 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
743 int left = (irq_pos - spu.decode_pos) & 0x1ff;
650adfd2 744 if (0 < left && left <= ns_to)
745 {
3154bfab 746 //xprintf("decoder irq %x\n", spu.decode_pos);
650adfd2 747 do_irq();
650adfd2 748 }
749 }
750
751 InitREVERB(ns_to);
ef79bbde 752
650adfd2 753 {
3154bfab 754 silentch=~(spu.dwChannelOn|spu.dwNewChannel);
174c454a 755
ef79bbde
P
756 //--------------------------------------------------//
757 //- main channel loop -//
758 //--------------------------------------------------//
759 {
650adfd2 760 for(ch=0;ch<MAXCHAN;ch++) // loop em all...
ef79bbde 761 {
3154bfab 762 if(spu.dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
763 if(!(spu.dwChannelOn&(1<<ch))) continue; // channel not playing? next
ef79bbde 764
07a6dd2c 765 if(s_chan[ch].bNoise)
650adfd2 766 do_samples_noise(ch, ns_from, ns_to);
3154bfab 767 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && spu_config.iUseInterpolation==0))
650adfd2 768 do_samples_noint(ch, ns_from, ns_to);
3154bfab 769 else if(s_chan[ch].bFMod==0 && spu_config.iUseInterpolation==1)
650adfd2 770 do_samples_simple(ch, ns_from, ns_to);
07a6dd2c 771 else
650adfd2 772 do_samples_default(ch, ns_from, ns_to);
773
e391d21c 774 ns_len = ns_to - ns_from;
ef79bbde 775
1ab7621a 776 MixADSR(ch, ns_from, ns_to);
777
b72f17a1 778 if(ch==1 || ch==3)
779 {
e391d21c 780 do_decode_bufs(ch/2, ns_from, ns_len);
3154bfab 781 spu.decode_dirty_ch |= 1<<ch;
b72f17a1 782 }
783
1ab7621a 784 if(s_chan[ch].bFMod==2) // fmod freq channel
e391d21c 785 memcpy(&iFMod[ns_from], &ChanBuf[ns_from], ns_len * sizeof(iFMod[0]));
786 if(s_chan[ch].bRVBActive)
3154bfab 787 mix_chan_rvb(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
1775933a 788 else
e391d21c 789 mix_chan(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
ef79bbde
P
790 }
791 }
792
78c60846 793 // advance "stopped" channels that can cause irqs
794 // (all chans are always playing on the real thing..)
3154bfab 795 if(spu.spuCtrl&CTRL_IRQ)
78c60846 796 for(ch=0;ch<MAXCHAN;ch++)
797 {
174c454a 798 if(!(silentch&(1<<ch))) continue; // already handled
3154bfab 799 if(spu.dwChannelDead&(1<<ch)) continue;
800 if(s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
78c60846 801 continue;
802
f2aec10a 803 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
78c60846 804 while(s_chan[ch].spos >= 28 * 0x10000)
805 {
7e44d49d 806 unsigned char *start = s_chan[ch].pCurr;
78c60846 807
fb552464 808 skip_block(ch);
3154bfab 809 if(start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
78c60846 810 {
f9fe4923 811 // looping on self or stopped(?)
3154bfab 812 spu.dwChannelDead |= 1<<ch;
7e44d49d 813 s_chan[ch].spos = 0;
78c60846 814 break;
815 }
816
78c60846 817 s_chan[ch].spos -= 28 * 0x10000;
818 }
819 }
820
3154bfab 821 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
b72f17a1 822 {
3154bfab 823 memset(&spu.spuMem[0x800/2], 0, 0x400);
824 spu.decode_dirty_ch &= ~(1<<1);
b72f17a1 825 }
3154bfab 826 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
b72f17a1 827 {
3154bfab 828 memset(&spu.spuMem[0xc00/2], 0, 0x400);
829 spu.decode_dirty_ch &= ~(1<<3);
b72f17a1 830 }
8680e822 831
ef79bbde
P
832 //---------------------------------------------------//
833 // mix XA infos (if any)
834
650adfd2 835 MixXA(ns_to);
ef79bbde
P
836
837 ///////////////////////////////////////////////////////
838 // mix all channels (including reverb) into one buffer
839
3154bfab 840 if(spu_config.iUseReverb)
650adfd2 841 REVERBDo(ns_to);
1775933a 842
3154bfab 843 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
1775933a 844 {
3154bfab 845 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
846 spu.pS += ns_to * 2;
1775933a 847 }
848 else
650adfd2 849 for (ns = 0; ns < ns_to * 2; )
ef79bbde 850 {
9e7a7352 851 d = SSumLR[ns]; SSumLR[ns] = 0;
852 d = d * volmult >> 10;
1775933a 853 ssat32_to_16(d);
3154bfab 854 *spu.pS++ = d;
97ea4077 855 ns++;
ef79bbde 856
9e7a7352 857 d = SSumLR[ns]; SSumLR[ns] = 0;
858 d = d * volmult >> 10;
1775933a 859 ssat32_to_16(d);
3154bfab 860 *spu.pS++ = d;
97ea4077 861 ns++;
ef79bbde
P
862 }
863
3154bfab 864 spu.cycles_played += ns_to * 768;
16187bfb 865
3154bfab 866 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
650adfd2 867 }
868}
ef79bbde 869
650adfd2 870void schedule_next_irq(void)
871{
872 unsigned int upd_samples;
873 int ch;
ef79bbde 874
3154bfab 875 if (spu.scheduleCallback == NULL)
650adfd2 876 return;
e34a4bd3 877
650adfd2 878 upd_samples = 44100 / 50;
e34a4bd3 879
650adfd2 880 for (ch = 0; ch < MAXCHAN; ch++)
881 {
3154bfab 882 if (spu.dwChannelDead & (1 << ch))
650adfd2 883 continue;
3154bfab 884 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
885 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
650adfd2 886 continue;
16187bfb 887
650adfd2 888 scan_for_irq(ch, &upd_samples);
ef79bbde
P
889 }
890
3154bfab 891 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
650adfd2 892 {
3154bfab 893 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
894 int left = (irq_pos - spu.decode_pos) & 0x1ff;
650adfd2 895 if (0 < left && left < upd_samples) {
3154bfab 896 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
650adfd2 897 upd_samples = left;
898 }
899 }
16187bfb 900
650adfd2 901 if (upd_samples < 44100 / 50)
3154bfab 902 spu.scheduleCallback(upd_samples * 768);
ef79bbde
P
903}
904
905// SPU ASYNC... even newer epsxe func
906// 1 time every 'cycle' cycles... harhar
907
650adfd2 908// rearmed: called dynamically now
554a2220 909
650adfd2 910void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
ef79bbde 911{
650adfd2 912 do_samples(cycle);
fb552464 913
3154bfab 914 if (spu.spuCtrl & CTRL_IRQ)
650adfd2 915 schedule_next_irq();
f8edb5bc 916
650adfd2 917 if (flags & 1) {
3154bfab 918 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
919 spu.pS = (short *)spu.pSpuBuffer;
fb552464 920
3154bfab 921 if (spu_config.iTempo) {
650adfd2 922 if (!out_current->busy())
923 // cause more samples to be generated
924 // (and break some games because of bad sync)
3154bfab 925 spu.cycles_played -= 44100 / 60 / 2 * 768;
ef79bbde 926 }
650adfd2 927 }
ef79bbde
P
928}
929
930// SPU UPDATE... new epsxe func
931// 1 time every 32 hsync lines
932// (312/32)x50 in pal
933// (262/32)x60 in ntsc
934
935// since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
936// leave that func in the linux port, until epsxe linux is using
937// the async function as well
938
939void CALLBACK SPUupdate(void)
940{
ef79bbde
P
941}
942
943// XA AUDIO
944
945void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
946{
947 if(!xap) return;
948 if(!xap->freq) return; // no xa freq ? bye
949
950 FeedXA(xap); // call main XA feeder
951}
952
953// CDDA AUDIO
983a7cfd 954int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
ef79bbde 955{
983a7cfd 956 if (!pcm) return -1;
957 if (nbytes<=0) return -1;
ef79bbde 958
983a7cfd 959 return FeedCDDA((unsigned char *)pcm, nbytes);
ef79bbde
P
960}
961
6d75977b 962// to be called after state load
963void ClearWorkingState(void)
ef79bbde 964{
97ea4077 965 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
6d75977b 966 memset(iFMod,0,sizeof(iFMod));
3154bfab 967 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
ef79bbde
P
968}
969
970// SETUPSTREAMS: init most of the spu buffers
971void SetupStreams(void)
972{
973 int i;
974
3154bfab 975 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
976 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
977 memset(spu.sRVBStart,0,NSSIZE*2*4);
ef79bbde 978
3154bfab 979 spu.XAStart = // alloc xa buffer
ef79bbde 980 (uint32_t *)malloc(44100 * sizeof(uint32_t));
3154bfab 981 spu.XAEnd = spu.XAStart + 44100;
982 spu.XAPlay = spu.XAStart;
983 spu.XAFeed = spu.XAStart;
ef79bbde 984
3154bfab 985 spu.CDDAStart = // alloc cdda buffer
983a7cfd 986 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
3154bfab 987 spu.CDDAEnd = spu.CDDAStart + 16384;
988 spu.CDDAPlay = spu.CDDAStart;
989 spu.CDDAFeed = spu.CDDAStart;
ef79bbde
P
990
991 for(i=0;i<MAXCHAN;i++) // loop sound channels
992 {
6d866bb7 993 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
650adfd2 994 s_chan[i].ADSRX.SustainIncrease = 1;
3154bfab 995 s_chan[i].pLoop=spu.spuMemC;
996 s_chan[i].pCurr=spu.spuMemC;
ef79bbde
P
997 }
998
6d75977b 999 ClearWorkingState();
1000
3154bfab 1001 spu.bSpuInit=1; // flag: we are inited
ef79bbde
P
1002}
1003
1004// REMOVESTREAMS: free most buffer
1005void RemoveStreams(void)
1006{
3154bfab 1007 free(spu.pSpuBuffer); // free mixing buffer
1008 spu.pSpuBuffer = NULL;
1009 free(spu.sRVBStart); // free reverb buffer
1010 spu.sRVBStart = NULL;
1011 free(spu.XAStart); // free XA buffer
1012 spu.XAStart = NULL;
1013 free(spu.CDDAStart); // free CDDA buffer
1014 spu.CDDAStart = NULL;
ef79bbde
P
1015}
1016
1017// INIT/EXIT STUFF
1018
1019// SPUINIT: this func will be called first by the main emu
1020long CALLBACK SPUinit(void)
1021{
3154bfab 1022 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
ef79bbde
P
1023 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1024 InitADSR();
1025
3154bfab 1026 spu.spuAddr = 0xffffffff;
1027 spu.decode_pos = 0;
1028 memset((void *)s_chan, 0, sizeof(s_chan));
c4c66b22 1029 spu.pSpuIrq = spu.spuMemC;
ef79bbde 1030
ef79bbde
P
1031 SetupStreams(); // prepare streaming
1032
3154bfab 1033 if (spu_config.iVolume == 0)
1034 spu_config.iVolume = 768; // 1024 is 1.0
1035
ef79bbde
P
1036 return 0;
1037}
1038
1039// SPUOPEN: called by main emu after init
1040long CALLBACK SPUopen(void)
1041{
3154bfab 1042 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
ef79bbde
P
1043
1044 SetupSound(); // setup sound (before init!)
ef79bbde 1045
3154bfab 1046 spu.bSPUIsOpen = 1;
ef79bbde
P
1047
1048 return PSE_SPU_ERR_SUCCESS;
1049}
1050
1051// SPUCLOSE: called before shutdown
1052long CALLBACK SPUclose(void)
1053{
3154bfab 1054 if (!spu.bSPUIsOpen) return 0; // some security
ef79bbde 1055
3154bfab 1056 spu.bSPUIsOpen = 0; // no more open
ef79bbde 1057
07c13dfd 1058 out_current->finish(); // no more sound handling
ef79bbde
P
1059
1060 return 0;
1061}
1062
1063// SPUSHUTDOWN: called by main emu on final exit
1064long CALLBACK SPUshutdown(void)
1065{
1066 SPUclose();
1067 RemoveStreams(); // no more streaming
3154bfab 1068 spu.bSpuInit=0;
ef79bbde
P
1069
1070 return 0;
1071}
1072
1073// SPUTEST: we don't test, we are always fine ;)
1074long CALLBACK SPUtest(void)
1075{
1076 return 0;
1077}
1078
1079// SPUCONFIGURE: call config dialog
1080long CALLBACK SPUconfigure(void)
1081{
1082#ifdef _MACOSX
1083 DoConfiguration();
1084#else
ee849648 1085// StartCfgTool("CFG");
ef79bbde
P
1086#endif
1087 return 0;
1088}
1089
1090// SPUABOUT: show about window
1091void CALLBACK SPUabout(void)
1092{
1093#ifdef _MACOSX
1094 DoAbout();
1095#else
ee849648 1096// StartCfgTool("ABOUT");
ef79bbde
P
1097#endif
1098}
1099
1100// SETUP CALLBACKS
1101// this functions will be called once,
1102// passes a callback that should be called on SPU-IRQ/cdda volume change
1103void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1104{
3154bfab 1105 spu.irqCallback = callback;
ef79bbde
P
1106}
1107
1108void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1109{
3154bfab 1110 spu.cddavCallback = CDDAVcallback;
ef79bbde
P
1111}
1112
2b30c129 1113void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1114{
3154bfab 1115 spu.scheduleCallback = callback;
2b30c129 1116}
1117
ef79bbde 1118// COMMON PLUGIN INFO FUNCS
e906c010 1119/*
ef79bbde
P
1120char * CALLBACK PSEgetLibName(void)
1121{
1122 return _(libraryName);
1123}
1124
1125unsigned long CALLBACK PSEgetLibType(void)
1126{
1127 return PSE_LT_SPU;
1128}
1129
1130unsigned long CALLBACK PSEgetLibVersion(void)
1131{
1132 return (1 << 16) | (6 << 8);
1133}
1134
1135char * SPUgetLibInfos(void)
1136{
1137 return _(libraryInfo);
1138}
e906c010 1139*/
6d866bb7 1140
90f1d26c 1141// debug
174c454a 1142void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
90f1d26c 1143{
174c454a 1144 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
90f1d26c 1145
1146 for(;ch<MAXCHAN;ch++)
1147 {
3154bfab 1148 if (!(spu.dwChannelOn & (1<<ch)))
90f1d26c 1149 continue;
1150 if (s_chan[ch].bFMod == 2)
1151 fmod_chans |= 1 << ch;
1152 if (s_chan[ch].bNoise)
1153 noise_chans |= 1 << ch;
3154bfab 1154 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
174c454a 1155 irq_chans |= 1 << ch;
90f1d26c 1156 }
1157
3154bfab 1158 *chans_out = spu.dwChannelOn;
1159 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
90f1d26c 1160 *fmod_chans_out = fmod_chans;
1161 *noise_chans_out = noise_chans;
1162}
1163
6d866bb7 1164// vim:shiftwidth=1:expandtab