1 /***************************************************************************
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012
10 ***************************************************************************/
11 /***************************************************************************
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. *
19 ***************************************************************************/
25 #include "externals.h"
26 #include "registers.h"
28 #include "arm_features.h"
33 #define _(x) gettext(x)
40 #ifdef __ARM_ARCH_7A__
41 #define ssat32_to_16(v) \
42 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
44 #define ssat32_to_16(v) do { \
45 if (v < -32768) v = -32768; \
46 else if (v > 32767) v = 32767; \
50 #define PSXCLK 33868800 /* 33.8688 MHz */
52 // intended to be ~1 frame
53 #define IRQ_NEAR_BLOCKS 32
56 #if defined (USEMACOSX)
57 static char * libraryName = N_("Mac OS X Sound");
58 #elif defined (USEALSA)
59 static char * libraryName = N_("ALSA Sound");
60 #elif defined (USEOSS)
61 static char * libraryName = N_("OSS Sound");
62 #elif defined (USESDL)
63 static char * libraryName = N_("SDL Sound");
64 #elif defined (USEPULSEAUDIO)
65 static char * libraryName = N_("PulseAudio Sound");
67 static char * libraryName = N_("NULL Sound");
70 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
75 // psx buffer / addresses
77 unsigned short regArea[10000];
78 unsigned short spuMem[256*1024];
79 unsigned char * spuMemC;
80 unsigned char * pSpuIrq=0;
81 unsigned char * pSpuBuffer;
85 int iVolume=768; // 1024 is 1.0
88 int iUseInterpolation=2;
90 // MAIN infos struct for each channel
92 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
95 unsigned int dwNoiseVal; // global noise generator
96 unsigned int dwNoiseCount;
98 unsigned short spuCtrl=0; // some vars to store psx reg infos
99 unsigned short spuStat=0;
100 unsigned short spuIrq=0;
101 unsigned long spuAddr=0xffffffff; // address into spu mem
105 unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
106 unsigned int dwChannelOn=0; // not silent channels
107 unsigned int dwPendingChanOff=0;
108 unsigned int dwChannelDead=0; // silent+not useful channels
110 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
111 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
112 void (CALLBACK *scheduleCallback)(unsigned int)=0;
114 // certain globals (were local before, but with the new timeproc I need em global)
116 static const int f[8][2] = { { 0, 0 },
121 int ChanBuf[NSSIZE+3];
122 int SSumLR[(NSSIZE+3)*2];
126 static int decode_dirty_ch;
128 unsigned int cycles_played;
130 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
132 ////////////////////////////////////////////////////////////////////////
134 ////////////////////////////////////////////////////////////////////////
136 // dirty inline func includes
141 ////////////////////////////////////////////////////////////////////////
142 // helpers for simple interpolation
145 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
147 // instead of having n equal sample values in a row like:
151 // we compare the current delta change with the next delta change.
153 // if curr_delta is positive,
155 // - and next delta is smaller (or changing direction):
159 // - and next delta significant (at least twice) bigger:
163 // - and next delta is nearly same:
168 // if curr_delta is negative,
170 // - and next delta is smaller (or changing direction):
174 // - and next delta significant (at least twice) bigger:
178 // - and next delta is nearly same:
184 INLINE void InterpolateUp(int ch)
186 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
188 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
189 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
193 if(id1>0) // curr delta positive
196 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
199 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
201 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
203 else // curr delta negative
206 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
209 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
211 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
215 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
219 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
220 //if(s_chan[ch].sinc<=0x8000)
221 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
223 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
225 else // no flags? add bigger val (if possible), calc smaller step, set flag1
226 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
230 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
233 INLINE void InterpolateDown(int ch)
235 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
237 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
238 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
239 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
243 ////////////////////////////////////////////////////////////////////////
244 // helpers for gauss interpolation
246 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
247 #define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
251 ////////////////////////////////////////////////////////////////////////
255 static void do_irq(void)
257 //if(!(spuStat & STAT_IRQ))
259 spuStat |= STAT_IRQ; // asserted status?
260 if(irqCallback) irqCallback();
264 static int check_irq(int ch, unsigned char *pos)
266 if((spuCtrl & CTRL_IRQ) && pos == pSpuIrq)
268 //printf("ch%d irq %04x\n", ch, pos - spuMemC);
275 ////////////////////////////////////////////////////////////////////////
276 // START SOUND... called by main thread to setup a new sound on a channel
277 ////////////////////////////////////////////////////////////////////////
279 INLINE void StartSound(int ch)
284 // fussy timing issues - do in VoiceOn
285 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
286 //s_chan[ch].bStop=0;
289 s_chan[ch].SB[26]=0; // init mixing vars
291 s_chan[ch].iSBPos=28;
293 s_chan[ch].SB[29]=0; // init our interpolation helpers
296 if(iUseInterpolation>=2) // gauss interpolation?
297 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
298 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
300 dwNewChannel&=~(1<<ch); // clear new channel bit
303 ////////////////////////////////////////////////////////////////////////
304 // ALL KIND OF HELPERS
305 ////////////////////////////////////////////////////////////////////////
307 INLINE int FModChangeFrequency(int ch,int ns)
309 unsigned int NP=s_chan[ch].iRawPitch;
312 NP=((32768L+iFMod[ns])*NP)/32768L;
314 if(NP>0x3fff) NP=0x3fff;
317 sinc=NP<<4; // calc frequency
318 if(iUseInterpolation==1) // freq change in simple interpolation mode
325 ////////////////////////////////////////////////////////////////////////
327 INLINE void StoreInterpolationVal(int ch,int fa)
329 if(s_chan[ch].bFMod==2) // fmod freq channel
330 s_chan[ch].SB[29]=fa;
335 if(iUseInterpolation>=2) // gauss/cubic interpolation
337 int gpos = s_chan[ch].SB[28];
340 s_chan[ch].SB[28] = gpos;
343 if(iUseInterpolation==1) // simple interpolation
345 s_chan[ch].SB[28] = 0;
346 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'
347 s_chan[ch].SB[30] = s_chan[ch].SB[31];
348 s_chan[ch].SB[31] = fa;
349 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
351 else s_chan[ch].SB[29]=fa; // no interpolation
355 ////////////////////////////////////////////////////////////////////////
357 INLINE int iGetInterpolationVal(int ch, int spos)
361 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
363 switch(iUseInterpolation)
365 //--------------------------------------------------//
366 case 3: // cubic interpolation
370 gpos = s_chan[ch].SB[28];
372 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
373 fa *= (xd - (2<<15)) / 6;
375 fa += gval(2) - gval(1) - gval(1) + gval0;
376 fa *= (xd - (1<<15)) >> 1;
378 fa += gval(1) - gval0;
384 //--------------------------------------------------//
385 case 2: // gauss interpolation
388 vl = (spos >> 6) & ~3;
389 gpos = s_chan[ch].SB[28];
390 vr=(gauss[vl]*(int)gval0)&~2047;
391 vr+=(gauss[vl+1]*gval(1))&~2047;
392 vr+=(gauss[vl+2]*gval(2))&~2047;
393 vr+=(gauss[vl+3]*gval(3))&~2047;
396 //--------------------------------------------------//
397 case 1: // simple interpolation
399 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
400 InterpolateUp(ch); // --> interpolate up
401 else InterpolateDown(ch); // --> else down
402 fa=s_chan[ch].SB[29];
404 //--------------------------------------------------//
405 default: // no interpolation
407 fa=s_chan[ch].SB[29];
409 //--------------------------------------------------//
415 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
418 int fa, s_1, s_2, d, s;
423 for (nSample = 0; nSample < 28; src++)
426 s = (int)(signed short)((d & 0x0f) << 12);
428 fa = s >> shift_factor;
429 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
432 dest[nSample++] = fa;
434 s = (int)(signed short)((d & 0xf0) << 8);
435 fa = s >> shift_factor;
436 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
439 dest[nSample++] = fa;
443 static int decode_block(int ch)
445 unsigned char *start;
446 int predict_nr,shift_factor,flags;
450 start = s_chan[ch].pCurr; // set up the current pos
451 if(start == spuMemC) // ?
454 if(s_chan[ch].prevflags&1) // 1: stop/loop
456 if(!(s_chan[ch].prevflags&2))
459 start = s_chan[ch].pLoop;
462 ret = check_irq(ch, start); // hack, see check_irq below..
466 dwChannelOn &= ~(1<<ch); // -> turn everything off
467 s_chan[ch].bStop = 1;
468 s_chan[ch].ADSRX.EnvelopeVol = 0;
471 predict_nr=(int)start[0];
472 shift_factor=predict_nr&0xf;
475 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
479 s_chan[ch].pLoop=start; // loop adress
483 if(flags&1) { // 1: stop/loop
484 start = s_chan[ch].pLoop;
485 ret |= check_irq(ch, start); // hack.. :(
488 if (start - spuMemC >= 0x80000)
491 s_chan[ch].pCurr = start; // store values for next cycle
492 s_chan[ch].prevflags = flags;
497 // do block, but ignore sample data
498 static int skip_block(int ch)
500 unsigned char *start = s_chan[ch].pCurr;
501 int flags = start[1];
502 int ret = check_irq(ch, start);
504 if(s_chan[ch].prevflags & 1)
505 start = s_chan[ch].pLoop;
508 s_chan[ch].pLoop = start;
513 start = s_chan[ch].pLoop;
515 s_chan[ch].pCurr = start;
516 s_chan[ch].prevflags = flags;
521 // if irq is going to trigger sooner than in upd_samples, set upd_samples
522 static void scan_for_irq(int ch, unsigned int *upd_samples)
524 int pos, sinc, sinc_inv, end;
525 unsigned char *block;
528 block = s_chan[ch].pCurr;
529 pos = s_chan[ch].spos;
530 sinc = s_chan[ch].sinc;
531 end = pos + *upd_samples * sinc;
533 pos += (28 - s_chan[ch].iSBPos) << 16;
536 if (block == pSpuIrq)
540 if (flags & 1) { // 1: stop/loop
541 block = s_chan[ch].pLoop;
542 if (block == pSpuIrq) // hack.. (see decode_block)
550 sinc_inv = s_chan[ch].sinc_inv;
552 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
554 pos -= s_chan[ch].spos;
555 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
556 //xprintf("ch%02d: irq sched: %3d %03d\n",
557 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
561 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
562 static noinline int do_samples_##name(int ch, int ns, int ns_to) \
564 int sinc = s_chan[ch].sinc; \
565 int spos = s_chan[ch].spos; \
566 int sbpos = s_chan[ch].iSBPos; \
567 int *SB = s_chan[ch].SB; \
572 for (; ns < ns_to; ns++) \
577 while (spos >= 0x10000) \
583 d = decode_block(ch); \
585 ret = /*ns_to =*/ ns + 1; \
595 s_chan[ch].sinc = sinc; \
596 s_chan[ch].spos = spos; \
597 s_chan[ch].iSBPos = sbpos; \
603 #define fmod_recv_check \
604 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
605 sinc = FModChangeFrequency(ch,ns)
607 make_do_samples(default, fmod_recv_check, ,
608 StoreInterpolationVal(ch, fa),
609 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
610 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
612 #define simple_interp_store \
613 s_chan[ch].SB[28] = 0; \
614 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
615 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
616 s_chan[ch].SB[31] = fa; \
617 s_chan[ch].SB[32] = 1
619 #define simple_interp_get \
620 if(sinc<0x10000) /* -> upsampling? */ \
621 InterpolateUp(ch); /* --> interpolate up */ \
622 else InterpolateDown(ch); /* --> else down */ \
623 ChanBuf[ns] = s_chan[ch].SB[29]
625 make_do_samples(simple, , ,
626 simple_interp_store, simple_interp_get, )
628 static noinline int do_samples_noise(int ch, int ns, int ns_to)
630 int level, shift, bit;
633 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
634 while (s_chan[ch].spos >= 28*0x10000)
639 s_chan[ch].spos -= 28*0x10000;
642 // modified from DrHell/shalma, no fraction
643 level = (spuCtrl >> 10) & 0x0f;
644 level = 0x8000 >> level;
646 for (; ns < ns_to; ns++)
649 if (dwNoiseCount >= level)
651 dwNoiseCount -= level;
652 shift = (dwNoiseVal >> 10) & 0x1f;
653 bit = (0x69696969 >> shift) & 1;
654 if (dwNoiseVal & 0x8000)
656 dwNoiseVal = (dwNoiseVal << 1) | bit;
659 ChanBuf[ns] = (signed short)dwNoiseVal;
666 // asm code; lv and rv must be 0-3fff
667 extern void mix_chan(int start, int count, int lv, int rv);
668 extern void mix_chan_rvb(int start, int count, int lv, int rv);
670 static void mix_chan(int start, int count, int lv, int rv)
672 int *dst = SSumLR + start * 2;
673 const int *src = ChanBuf + start;
680 l = (sval * lv) >> 14;
681 r = (sval * rv) >> 14;
687 static void mix_chan_rvb(int start, int count, int lv, int rv)
689 int *dst = SSumLR + start * 2;
690 int *drvb = sRVBStart + start * 2;
691 const int *src = ChanBuf + start;
698 l = (sval * lv) >> 14;
699 r = (sval * rv) >> 14;
708 // 0x0800-0x0bff Voice 1
709 // 0x0c00-0x0fff Voice 3
710 static noinline void do_decode_bufs(int which, int start, int count)
712 const int *src = ChanBuf + start;
713 unsigned short *dst = &spuMem[0x800/2 + which*0x400/2];
714 int cursor = decode_pos + start;
719 dst[cursor] = *src++;
723 // decode_pos is updated and irqs are checked later, after voice loop
726 ////////////////////////////////////////////////////////////////////////
728 // here is the main job handler...
729 // basically the whole sound processing is done in this fat func!
730 ////////////////////////////////////////////////////////////////////////
732 void do_samples(unsigned int cycles_to)
734 const int ns_from = 0;
736 int volmult = iVolume;
740 cycle_diff = cycles_to - cycles_played;
741 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
743 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
744 cycles_played = cycles_to;
748 if (cycle_diff < 2 * 768)
751 ns_to = (cycle_diff / 768 + 1) & ~1;
752 if (ns_to > NSSIZE) {
753 // should never happen
754 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
758 //////////////////////////////////////////////////////
759 // special irq handling in the decode buffers (0x0000-0x1000)
761 // the decode buffers are located in spu memory in the following way:
762 // 0x0000-0x03ff CD audio left
763 // 0x0400-0x07ff CD audio right
764 // 0x0800-0x0bff Voice 1
765 // 0x0c00-0x0fff Voice 3
766 // and decoded data is 16 bit for one sample
768 // even if voices 1/3 are off or no cd audio is playing, the internal
769 // play positions will move on and wrap after 0x400 bytes.
770 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
771 // increase this pointer on each sample by 2 bytes. If this pointer
772 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
775 if (unlikely((spuCtrl & CTRL_IRQ) && pSpuIrq && pSpuIrq < spuMemC+0x1000))
777 int irq_pos = (pSpuIrq - spuMemC) / 2 & 0x1ff;
778 int left = (irq_pos - decode_pos) & 0x1ff;
779 if (0 < left && left <= ns_to)
781 //xprintf("decoder irq %x\n", decode_pos);
790 silentch=~(dwChannelOn|dwNewChannel);
792 //--------------------------------------------------//
793 //- main channel loop -//
794 //--------------------------------------------------//
796 for(ch=0;ch<MAXCHAN;ch++) // loop em all...
798 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
799 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
801 if(s_chan[ch].bNoise)
802 do_samples_noise(ch, ns_from, ns_to);
803 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
804 do_samples_noint(ch, ns_from, ns_to);
805 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
806 do_samples_simple(ch, ns_from, ns_to);
808 do_samples_default(ch, ns_from, ns_to);
810 ns_len = ns_to - ns_from;
812 MixADSR(ch, ns_from, ns_to);
816 do_decode_bufs(ch/2, ns_from, ns_len);
817 decode_dirty_ch |= 1<<ch;
820 if(s_chan[ch].bFMod==2) // fmod freq channel
821 memcpy(&iFMod[ns_from], &ChanBuf[ns_from], ns_len * sizeof(iFMod[0]));
822 if(s_chan[ch].bRVBActive)
823 mix_chan_rvb(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
825 mix_chan(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
829 // advance "stopped" channels that can cause irqs
830 // (all chans are always playing on the real thing..)
832 for(ch=0;ch<MAXCHAN;ch++)
834 if(!(silentch&(1<<ch))) continue; // already handled
835 if(dwChannelDead&(1<<ch)) continue;
836 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
839 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
840 while(s_chan[ch].spos >= 28 * 0x10000)
842 unsigned char *start = s_chan[ch].pCurr;
845 if(start == s_chan[ch].pCurr || start - spuMemC < 0x1000)
847 // looping on self or stopped(?)
848 dwChannelDead |= 1<<ch;
853 s_chan[ch].spos -= 28 * 0x10000;
857 if(unlikely(silentch & decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
859 memset(&spuMem[0x800/2], 0, 0x400);
860 decode_dirty_ch &= ~(1<<1);
862 if(unlikely(silentch & decode_dirty_ch & (1<<3)))
864 memset(&spuMem[0xc00/2], 0, 0x400);
865 decode_dirty_ch &= ~(1<<3);
868 //---------------------------------------------------//
869 //- here we have another 1 ms of sound data
870 //---------------------------------------------------//
871 // mix XA infos (if any)
875 ///////////////////////////////////////////////////////
876 // mix all channels (including reverb) into one buffer
881 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
883 memset(pS, 0, ns_to * 2 * sizeof(pS[0]));
887 for (ns = 0; ns < ns_to * 2; )
889 d = SSumLR[ns]; SSumLR[ns] = 0;
890 d = d * volmult >> 10;
895 d = SSumLR[ns]; SSumLR[ns] = 0;
896 d = d * volmult >> 10;
902 cycles_played += ns_to * 768;
904 decode_pos = (decode_pos + ns_to) & 0x1ff;
908 void schedule_next_irq(void)
910 unsigned int upd_samples;
913 if (scheduleCallback == NULL)
916 upd_samples = 44100 / 50;
918 for (ch = 0; ch < MAXCHAN; ch++)
920 if (dwChannelDead & (1 << ch))
922 if ((unsigned long)(pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
923 && (unsigned long)(pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
926 scan_for_irq(ch, &upd_samples);
929 if (unlikely(pSpuIrq < spuMemC + 0x1000))
931 int irq_pos = (pSpuIrq - spuMemC) / 2 & 0x1ff;
932 int left = (irq_pos - decode_pos) & 0x1ff;
933 if (0 < left && left < upd_samples) {
934 //xprintf("decode: %3d (%3d/%3d)\n", left, decode_pos, irq_pos);
939 if (upd_samples < 44100 / 50)
940 scheduleCallback(upd_samples * 768);
943 // SPU ASYNC... even newer epsxe func
944 // 1 time every 'cycle' cycles... harhar
946 // rearmed: called dynamically now
948 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
952 if (spuCtrl & CTRL_IRQ)
956 out_current->feed(pSpuBuffer, (unsigned char *)pS - pSpuBuffer);
957 pS = (short *)pSpuBuffer;
960 if (!out_current->busy())
961 // cause more samples to be generated
962 // (and break some games because of bad sync)
963 cycles_played -= 44100 / 60 / 2 * 768;
968 // SPU UPDATE... new epsxe func
969 // 1 time every 32 hsync lines
970 // (312/32)x50 in pal
971 // (262/32)x60 in ntsc
973 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
974 // leave that func in the linux port, until epsxe linux is using
975 // the async function as well
977 void CALLBACK SPUupdate(void)
983 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
986 if(!xap->freq) return; // no xa freq ? bye
988 FeedXA(xap); // call main XA feeder
992 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
995 if (nbytes<=0) return -1;
997 return FeedCDDA((unsigned char *)pcm, nbytes);
1000 // to be called after state load
1001 void ClearWorkingState(void)
1003 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1004 memset(iFMod,0,sizeof(iFMod));
1005 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
1008 // SETUPSTREAMS: init most of the spu buffers
1009 void SetupStreams(void)
1013 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
1015 if(iUseReverb==1) i=88200*2;
1018 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
1019 memset(sRVBStart,0,i*4);
1020 sRVBEnd = sRVBStart + i;
1021 sRVBPlay = sRVBStart;
1023 XAStart = // alloc xa buffer
1024 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1025 XAEnd = XAStart + 44100;
1029 CDDAStart = // alloc cdda buffer
1030 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1031 CDDAEnd = CDDAStart + 16384;
1032 CDDAPlay = CDDAStart;
1033 CDDAFeed = CDDAStart;
1035 for(i=0;i<MAXCHAN;i++) // loop sound channels
1037 // we don't use mutex sync... not needed, would only
1039 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
1040 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1041 s_chan[i].ADSRX.SustainIncrease = 1;
1042 s_chan[i].pLoop=spuMemC;
1043 s_chan[i].pCurr=spuMemC;
1046 ClearWorkingState();
1048 bSpuInit=1; // flag: we are inited
1051 // REMOVESTREAMS: free most buffer
1052 void RemoveStreams(void)
1054 free(pSpuBuffer); // free mixing buffer
1056 free(sRVBStart); // free reverb buffer
1058 free(XAStart); // free XA buffer
1060 free(CDDAStart); // free CDDA buffer
1066 // SPUINIT: this func will be called first by the main emu
1067 long CALLBACK SPUinit(void)
1069 spuMemC = (unsigned char *)spuMem; // just small setup
1070 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1074 spuAddr = 0xffffffff;
1075 spuMemC = (unsigned char *)spuMem;
1077 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1080 SetupStreams(); // prepare streaming
1085 // SPUOPEN: called by main emu after init
1086 long CALLBACK SPUopen(void)
1088 if (bSPUIsOpen) return 0; // security for some stupid main emus
1090 SetupSound(); // setup sound (before init!)
1094 return PSE_SPU_ERR_SUCCESS;
1097 // SPUCLOSE: called before shutdown
1098 long CALLBACK SPUclose(void)
1100 if (!bSPUIsOpen) return 0; // some security
1102 bSPUIsOpen = 0; // no more open
1104 out_current->finish(); // no more sound handling
1109 // SPUSHUTDOWN: called by main emu on final exit
1110 long CALLBACK SPUshutdown(void)
1113 RemoveStreams(); // no more streaming
1119 // SPUTEST: we don't test, we are always fine ;)
1120 long CALLBACK SPUtest(void)
1125 // SPUCONFIGURE: call config dialog
1126 long CALLBACK SPUconfigure(void)
1131 // StartCfgTool("CFG");
1136 // SPUABOUT: show about window
1137 void CALLBACK SPUabout(void)
1142 // StartCfgTool("ABOUT");
1147 // this functions will be called once,
1148 // passes a callback that should be called on SPU-IRQ/cdda volume change
1149 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1151 irqCallback = callback;
1154 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1156 cddavCallback = CDDAVcallback;
1159 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1161 scheduleCallback = callback;
1164 // COMMON PLUGIN INFO FUNCS
1166 char * CALLBACK PSEgetLibName(void)
1168 return _(libraryName);
1171 unsigned long CALLBACK PSEgetLibType(void)
1176 unsigned long CALLBACK PSEgetLibVersion(void)
1178 return (1 << 16) | (6 << 8);
1181 char * SPUgetLibInfos(void)
1183 return _(libraryInfo);
1188 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1190 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1192 for(;ch<MAXCHAN;ch++)
1194 if (!(dwChannelOn & (1<<ch)))
1196 if (s_chan[ch].bFMod == 2)
1197 fmod_chans |= 1 << ch;
1198 if (s_chan[ch].bNoise)
1199 noise_chans |= 1 << ch;
1200 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1201 irq_chans |= 1 << ch;
1204 *chans_out = dwChannelOn;
1205 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1206 *fmod_chans_out = fmod_chans;
1207 *noise_chans_out = noise_chans;
1210 // vim:shiftwidth=1:expandtab