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