spu: move more things to StartSound
[pcsx_rearmed.git] / plugins / dfsound / spu.c
... / ...
CommitLineData
1/***************************************************************************
2 spu.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014
9
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#ifndef _WIN32
22#include <sys/time.h> // gettimeofday in xa.c
23#endif
24#include "stdafx.h"
25
26#define _IN_SPU
27
28#include "externals.h"
29#include "registers.h"
30#include "out.h"
31#include "arm_features.h"
32#include "spu_config.h"
33
34#ifdef __ARM_ARCH_7A__
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
44#define PSXCLK 33868800 /* 33.8688 MHz */
45
46// intended to be ~1 frame
47#define IRQ_NEAR_BLOCKS 32
48
49/*
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");
65*/
66
67// globals
68
69SPUInfo spu;
70SPUConfig spu_config;
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
77// certain globals (were local before, but with the new timeproc I need em global)
78
79static const int f[8][2] = { { 0, 0 },
80 { 60, 0 },
81 { 115, -52 },
82 { 98, -55 },
83 { 122, -60 } };
84int ChanBuf[NSSIZE];
85int SSumLR[NSSIZE*2];
86int iFMod[NSSIZE];
87
88#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
89
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
141static void InterpolateUp(int *SB, int sinc)
142{
143 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
144 {
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 :)
147
148 SB[32]=0;
149
150 if(id1>0) // curr delta positive
151 {
152 if(id2<id1)
153 {SB[28]=id1;SB[32]=2;}
154 else
155 if(id2<(id1<<1))
156 SB[28]=(id1*sinc)>>16;
157 else
158 SB[28]=(id1*sinc)>>17;
159 }
160 else // curr delta negative
161 {
162 if(id2>id1)
163 {SB[28]=id1;SB[32]=2;}
164 else
165 if(id2>(id1<<1))
166 SB[28]=(id1*sinc)>>16;
167 else
168 SB[28]=(id1*sinc)>>17;
169 }
170 }
171 else
172 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
173 {
174 SB[32]=0;
175
176 SB[28]=(SB[28]*sinc)>>17;
177 //if(sinc<=0x8000)
178 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
179 //else
180 SB[29]+=SB[28];
181 }
182 else // no flags? add bigger val (if possible), calc smaller step, set flag1
183 SB[29]+=SB[28];
184}
185
186//
187// even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
188//
189
190static void InterpolateDown(int *SB, int sinc)
191{
192 if(sinc>=0x20000L) // we would skip at least one val?
193 {
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
197 }
198}
199
200////////////////////////////////////////////////////////////////////////
201// helpers for gauss interpolation
202
203#define gval0 (((short*)(&SB[29]))[gpos&3])
204#define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
205
206#include "gauss_i.h"
207
208////////////////////////////////////////////////////////////////////////
209
210#include "xa.c"
211
212static void do_irq(void)
213{
214 //if(!(spu.spuStat & STAT_IRQ))
215 {
216 spu.spuStat |= STAT_IRQ; // asserted status?
217 if(spu.irqCallback) spu.irqCallback();
218 }
219}
220
221static int check_irq(int ch, unsigned char *pos)
222{
223 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
224 {
225 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
226 do_irq();
227 return 1;
228 }
229 return 0;
230}
231
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
241 s_chan[ch].bStop=0;
242 s_chan[ch].prevflags=2;
243
244 s_chan[ch].SB[26]=0; // init mixing vars
245 s_chan[ch].SB[27]=0;
246 s_chan[ch].iSBPos=27;
247
248 s_chan[ch].SB[28]=0;
249 s_chan[ch].SB[29]=0; // init our interpolation helpers
250 s_chan[ch].SB[30]=0;
251 s_chan[ch].SB[31]=0;
252 s_chan[ch].spos=0;
253
254 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
255 spu.dwChannelOn|=1<<ch;
256 spu.dwChannelDead&=~(1<<ch);
257}
258
259////////////////////////////////////////////////////////////////////////
260// ALL KIND OF HELPERS
261////////////////////////////////////////////////////////////////////////
262
263INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
264{
265 unsigned int NP=pitch;
266 int sinc;
267
268 NP=((32768L+iFMod[ns])*NP)>>15;
269
270 if(NP>0x3fff) NP=0x3fff;
271 if(NP<0x1) NP=0x1;
272
273 sinc=NP<<4; // calc frequency
274 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
275 SB[32]=1;
276 iFMod[ns]=0;
277
278 return sinc;
279}
280
281////////////////////////////////////////////////////////////////////////
282
283INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
284{
285 if(fmod_freq) // fmod freq channel
286 SB[29]=fa;
287 else
288 {
289 ssat32_to_16(fa);
290
291 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
292 {
293 int gpos = SB[28];
294 gval0 = fa;
295 gpos = (gpos+1) & 3;
296 SB[28] = gpos;
297 }
298 else
299 if(spu_config.iUseInterpolation==1) // simple interpolation
300 {
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
306 }
307 else SB[29]=fa; // no interpolation
308 }
309}
310
311////////////////////////////////////////////////////////////////////////
312
313INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
314{
315 int fa;
316
317 if(fmod_freq) return SB[29];
318
319 switch(spu_config.iUseInterpolation)
320 {
321 //--------------------------------------------------//
322 case 3: // cubic interpolation
323 {
324 long xd;int gpos;
325 xd = (spos >> 1)+1;
326 gpos = SB[28];
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;
344 vl = (spos >> 6) & ~3;
345 gpos = SB[28];
346 vr=(gauss[vl]*(int)gval0)&~2047;
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 {
355 if(sinc<0x10000L) // -> upsampling?
356 InterpolateUp(SB, sinc); // --> interpolate up
357 else InterpolateDown(SB, sinc); // --> else down
358 fa=SB[29];
359 } break;
360 //--------------------------------------------------//
361 default: // no interpolation
362 {
363 fa=SB[29];
364 } break;
365 //--------------------------------------------------//
366 }
367
368 return fa;
369}
370
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
399static int decode_block(int ch, int *SB)
400{
401 unsigned char *start;
402 int predict_nr, shift_factor, flags;
403 int ret = 0;
404
405 start = s_chan[ch].pCurr; // set up the current pos
406 if (start == spu.spuMemC) // ?
407 ret = 1;
408
409 if (s_chan[ch].prevflags & 1) // 1: stop/loop
410 {
411 if (!(s_chan[ch].prevflags & 2))
412 ret = 1;
413
414 start = s_chan[ch].pLoop;
415 }
416 else
417 check_irq(ch, start); // hack, see check_irq below..
418
419 predict_nr = (int)start[0];
420 shift_factor = predict_nr & 0xf;
421 predict_nr >>= 4;
422
423 decode_block_data(SB, start + 2, predict_nr, shift_factor);
424
425 flags = start[1];
426 if (flags & 4)
427 s_chan[ch].pLoop = start; // loop adress
428
429 start += 16;
430
431 if (flags & 1) { // 1: stop/loop
432 start = s_chan[ch].pLoop;
433 check_irq(ch, start); // hack.. :(
434 }
435
436 if (start - spu.spuMemC >= 0x80000)
437 start = spu.spuMemC;
438
439 s_chan[ch].pCurr = start; // store values for next cycle
440 s_chan[ch].prevflags = flags;
441
442 return ret;
443}
444
445// do block, but ignore sample data
446static int skip_block(int ch)
447{
448 unsigned char *start = s_chan[ch].pCurr;
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;
455
456 start = s_chan[ch].pLoop;
457 }
458 else
459 check_irq(ch, start);
460
461 flags = start[1];
462 if (flags & 4)
463 s_chan[ch].pLoop = start;
464
465 start += 16;
466
467 if (flags & 1) {
468 start = s_chan[ch].pLoop;
469 check_irq(ch, start);
470 }
471
472 s_chan[ch].pCurr = start;
473 s_chan[ch].prevflags = flags;
474
475 return ret;
476}
477
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 {
493 if (block == spu.pSpuIrq)
494 break;
495 flags = block[1];
496 block += 16;
497 if (flags & 1) { // 1: stop/loop
498 block = s_chan[ch].pLoop;
499 if (block == spu.pSpuIrq) // hack.. (see decode_block)
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
518#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
519static noinline int do_samples_##name(int ch, int ns_to) \
520{ \
521 int sinc = s_chan[ch].sinc; \
522 int spos = s_chan[ch].spos; \
523 int sbpos = s_chan[ch].iSBPos; \
524 int *SB = s_chan[ch].SB; \
525 int ns, d, fa; \
526 int ret = ns_to; \
527 interp_start; \
528 \
529 for (ns = 0; ns < ns_to; ns++) \
530 { \
531 fmod_code; \
532 \
533 spos += sinc; \
534 while (spos >= 0x10000) \
535 { \
536 fa = SB[sbpos++]; \
537 if (sbpos >= 28) \
538 { \
539 sbpos = 0; \
540 d = decode_block(ch, SB); \
541 if (d && ns < ret) \
542 ret = ns; \
543 } \
544 \
545 interp1_code; \
546 spos -= 0x10000; \
547 } \
548 \
549 interp2_code; \
550 } \
551 \
552 s_chan[ch].sinc = sinc; \
553 s_chan[ch].spos = spos; \
554 s_chan[ch].iSBPos = sbpos; \
555 interp_end; \
556 \
557 return ret; \
558}
559
560#define fmod_recv_check \
561 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
562 sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns)
563
564make_do_samples(default, fmod_recv_check, ,
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)
568
569#define simple_interp_store \
570 SB[28] = 0; \
571 SB[29] = SB[30]; \
572 SB[30] = SB[31]; \
573 SB[31] = fa; \
574 SB[32] = 1
575
576#define simple_interp_get \
577 if(sinc<0x10000) /* -> upsampling? */ \
578 InterpolateUp(SB, sinc); /* --> interpolate up */ \
579 else InterpolateDown(SB, sinc); /* --> else down */ \
580 ChanBuf[ns] = SB[29]
581
582make_do_samples(simple, , ,
583 simple_interp_store, simple_interp_get, )
584
585static int do_samples_skip(int ch, int ns_to)
586{
587 int ret = ns_to, ns, d;
588
589 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
590
591 for (ns = 0; ns < ns_to; ns++)
592 {
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 }
601 }
602
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
617 // modified from DrHell/shalma, no fraction
618 level = (ctrl >> 10) & 0x0f;
619 level = 0x8000 >> level;
620
621 for (ns = 0; ns < ns_to; ns++)
622 {
623 counter += 2;
624 if (counter >= level)
625 {
626 counter -= level;
627 shift = (val >> 10) & 0x1f;
628 bit = (0x69696969 >> shift) & 1;
629 bit ^= (val >> 15) & 1;
630 val = (val << 1) | bit;
631 }
632
633 ChanBuf[ns] = (signed short)val;
634 }
635
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
648 return ret;
649}
650
651#ifdef HAVE_ARMV5
652// asm code; lv and rv must be 0-3fff
653extern void mix_chan(int start, int count, int lv, int rv);
654extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
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
673static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
674{
675 int *dst = SSumLR + start * 2;
676 int *drvb = rvb + start * 2;
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
694// 0x0800-0x0bff Voice 1
695// 0x0c00-0x0fff Voice 3
696static noinline void do_decode_bufs(unsigned short *mem, int which,
697 int count, int decode_pos)
698{
699 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
700 const int *src = ChanBuf;
701 int cursor = decode_pos;
702
703 while (count-- > 0)
704 {
705 cursor &= 0x1ff;
706 dst[cursor] = *src++;
707 cursor++;
708 }
709
710 // decode_pos is updated and irqs are checked later, after voice loop
711}
712
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
791////////////////////////////////////////////////////////////////////////
792// MAIN SPU FUNCTION
793// here is the main job handler...
794////////////////////////////////////////////////////////////////////////
795
796void do_samples_finish(int ns_to, int silentch);
797
798void do_samples(unsigned int cycles_to)
799{
800 unsigned int mask;
801 int ch, ns_to;
802 int silentch;
803 int cycle_diff;
804
805 cycle_diff = cycles_to - spu.cycles_played;
806 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
807 {
808 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
809 spu.cycles_played = cycles_to;
810 return;
811 }
812
813 if (cycle_diff < 2 * 768)
814 return;
815
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
840 if (unlikely((spu.spuCtrl & CTRL_IRQ)
841 && spu.pSpuIrq < spu.spuMemC+0x1000))
842 {
843 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
844 int left = (irq_pos - spu.decode_pos) & 0x1ff;
845 if (0 < left && left <= ns_to)
846 {
847 //xprintf("decoder irq %x\n", spu.decode_pos);
848 do_irq();
849 }
850 }
851
852 silentch = ~(spu.dwChannelOn|spu.dwNewChannel);
853
854 mask = spu.dwNewChannel & 0xffffff;
855 for (ch = 0; mask != 0; ch++, mask >>= 1) {
856 if (mask & 1)
857 StartSound(ch);
858 }
859
860 if (spu.dwChannelOn == 0)
861 InitREVERB(ns_to);
862 else {
863 do_channels(ns_to);
864 }
865
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;
882
883 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
884 {
885 memset(&spu.spuMem[0x800/2], 0, 0x400);
886 spu.decode_dirty_ch &= ~(1<<1);
887 }
888 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
889 {
890 memset(&spu.spuMem[0xc00/2], 0, 0x400);
891 spu.decode_dirty_ch &= ~(1<<3);
892 }
893
894 //---------------------------------------------------//
895 // mix XA infos (if any)
896
897 MixXA(ns_to, spu.decode_pos);
898
899 ///////////////////////////////////////////////////////
900 // mix all channels (including reverb) into one buffer
901
902 if(spu_config.iUseReverb)
903 REVERBDo(ns_to);
904
905 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
906 {
907 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
908 spu.pS += ns_to * 2;
909 }
910 else
911 for (ns = 0; ns < ns_to * 2; )
912 {
913 d = SSumLR[ns]; SSumLR[ns] = 0;
914 d = d * volmult >> 10;
915 ssat32_to_16(d);
916 *spu.pS++ = d;
917 ns++;
918
919 d = SSumLR[ns]; SSumLR[ns] = 0;
920 d = d * volmult >> 10;
921 ssat32_to_16(d);
922 *spu.pS++ = d;
923 ns++;
924 }
925}
926
927void schedule_next_irq(void)
928{
929 unsigned int upd_samples;
930 int ch;
931
932 if (spu.scheduleCallback == NULL)
933 return;
934
935 upd_samples = 44100 / 50;
936
937 for (ch = 0; ch < MAXCHAN; ch++)
938 {
939 if (spu.dwChannelDead & (1 << ch))
940 continue;
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)
943 continue;
944
945 scan_for_irq(ch, &upd_samples);
946 }
947
948 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
949 {
950 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
951 int left = (irq_pos - spu.decode_pos) & 0x1ff;
952 if (0 < left && left < upd_samples) {
953 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
954 upd_samples = left;
955 }
956 }
957
958 if (upd_samples < 44100 / 50)
959 spu.scheduleCallback(upd_samples * 768);
960}
961
962// SPU ASYNC... even newer epsxe func
963// 1 time every 'cycle' cycles... harhar
964
965// rearmed: called dynamically now
966
967void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
968{
969 do_samples(cycle);
970
971 if (spu.spuCtrl & CTRL_IRQ)
972 schedule_next_irq();
973
974 if (flags & 1) {
975 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
976 spu.pS = (short *)spu.pSpuBuffer;
977
978 if (spu_config.iTempo) {
979 if (!out_current->busy())
980 // cause more samples to be generated
981 // (and break some games because of bad sync)
982 spu.cycles_played -= 44100 / 60 / 2 * 768;
983 }
984 }
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{
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
1011int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1012{
1013 if (!pcm) return -1;
1014 if (nbytes<=0) return -1;
1015
1016 return FeedCDDA((unsigned char *)pcm, nbytes);
1017}
1018
1019// to be called after state load
1020void ClearWorkingState(void)
1021{
1022 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1023 memset(iFMod,0,sizeof(iFMod));
1024 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1025}
1026
1027// SETUPSTREAMS: init most of the spu buffers
1028void SetupStreams(void)
1029{
1030 int i;
1031
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);
1035
1036 spu.XAStart = // alloc xa buffer
1037 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1038 spu.XAEnd = spu.XAStart + 44100;
1039 spu.XAPlay = spu.XAStart;
1040 spu.XAFeed = spu.XAStart;
1041
1042 spu.CDDAStart = // alloc cdda buffer
1043 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1044 spu.CDDAEnd = spu.CDDAStart + 16384;
1045 spu.CDDAPlay = spu.CDDAStart;
1046 spu.CDDAFeed = spu.CDDAStart;
1047
1048 for(i=0;i<MAXCHAN;i++) // loop sound channels
1049 {
1050 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1051 s_chan[i].ADSRX.SustainIncrease = 1;
1052 s_chan[i].pLoop=spu.spuMemC;
1053 s_chan[i].pCurr=spu.spuMemC;
1054 }
1055
1056 ClearWorkingState();
1057
1058 spu.bSpuInit=1; // flag: we are inited
1059}
1060
1061// REMOVESTREAMS: free most buffer
1062void RemoveStreams(void)
1063{
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;
1072}
1073
1074// INIT/EXIT STUFF
1075
1076// SPUINIT: this func will be called first by the main emu
1077long CALLBACK SPUinit(void)
1078{
1079 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1080 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1081 InitADSR();
1082
1083 spu.spuAddr = 0xffffffff;
1084 spu.decode_pos = 0;
1085 memset((void *)s_chan, 0, sizeof(s_chan));
1086 spu.pSpuIrq = spu.spuMemC;
1087
1088 SetupStreams(); // prepare streaming
1089
1090 if (spu_config.iVolume == 0)
1091 spu_config.iVolume = 768; // 1024 is 1.0
1092
1093 return 0;
1094}
1095
1096// SPUOPEN: called by main emu after init
1097long CALLBACK SPUopen(void)
1098{
1099 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1100
1101 SetupSound(); // setup sound (before init!)
1102
1103 spu.bSPUIsOpen = 1;
1104
1105 return PSE_SPU_ERR_SUCCESS;
1106}
1107
1108// SPUCLOSE: called before shutdown
1109long CALLBACK SPUclose(void)
1110{
1111 if (!spu.bSPUIsOpen) return 0; // some security
1112
1113 spu.bSPUIsOpen = 0; // no more open
1114
1115 out_current->finish(); // no more sound handling
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
1125 spu.bSpuInit=0;
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
1142// StartCfgTool("CFG");
1143#endif
1144 return 0;
1145}
1146
1147// SPUABOUT: show about window
1148void CALLBACK SPUabout(void)
1149{
1150#ifdef _MACOSX
1151 DoAbout();
1152#else
1153// StartCfgTool("ABOUT");
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{
1162 spu.irqCallback = callback;
1163}
1164
1165void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1166{
1167 spu.cddavCallback = CDDAVcallback;
1168}
1169
1170void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1171{
1172 spu.scheduleCallback = callback;
1173}
1174
1175// COMMON PLUGIN INFO FUNCS
1176/*
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}
1196*/
1197
1198// debug
1199void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1200{
1201 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1202
1203 for(;ch<MAXCHAN;ch++)
1204 {
1205 if (!(spu.dwChannelOn & (1<<ch)))
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;
1211 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1212 irq_chans |= 1 << ch;
1213 }
1214
1215 *chans_out = spu.dwChannelOn;
1216 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1217 *fmod_chans_out = fmod_chans;
1218 *noise_chans_out = noise_chans;
1219}
1220
1221// vim:shiftwidth=1:expandtab