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"
32 #define _(x) gettext(x)
39 #ifdef __ARM_ARCH_7A__
40 #define ssat32_to_16(v) \
41 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
43 #define ssat32_to_16(v) do { \
44 if (v < -32768) v = -32768; \
45 else if (v > 32767) v = 32767; \
49 #define PSXCLK 33868800 /* 33.8688 MHz */
52 #if defined (USEMACOSX)
53 static char * libraryName = N_("Mac OS X Sound");
54 #elif defined (USEALSA)
55 static char * libraryName = N_("ALSA Sound");
56 #elif defined (USEOSS)
57 static char * libraryName = N_("OSS Sound");
58 #elif defined (USESDL)
59 static char * libraryName = N_("SDL Sound");
60 #elif defined (USEPULSEAUDIO)
61 static char * libraryName = N_("PulseAudio Sound");
63 static char * libraryName = N_("NULL Sound");
66 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
71 // psx buffer / addresses
73 unsigned short regArea[10000];
74 unsigned short spuMem[256*1024];
75 unsigned char * spuMemC;
76 unsigned char * pSpuIrq=0;
77 unsigned char * pSpuBuffer;
81 int iVolume=768; // 1024 is 1.0
84 int iUseInterpolation=2;
86 // MAIN infos struct for each channel
88 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
91 unsigned int dwNoiseVal; // global noise generator
92 unsigned int dwNoiseCount;
94 unsigned short spuCtrl=0; // some vars to store psx reg infos
95 unsigned short spuStat=0;
96 unsigned short spuIrq=0;
97 unsigned long spuAddr=0xffffffff; // address into spu mem
101 unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
102 unsigned int dwChannelOn=0; // not silent channels
103 unsigned int dwPendingChanOff=0;
104 unsigned int dwChannelDead=0; // silent+not useful channels
106 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
107 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
109 // certain globals (were local before, but with the new timeproc I need em global)
111 static const int f[8][2] = { { 0, 0 },
116 int ChanBuf[NSSIZE+3];
117 int SSumLR[(NSSIZE+3)*2];
122 static int decode_dirty_ch;
125 int lastch=-1; // last channel processed on spu irq in timer mode
126 static int lastns=0; // last ns pos
127 static int cycles_since_update;
129 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
131 ////////////////////////////////////////////////////////////////////////
133 ////////////////////////////////////////////////////////////////////////
135 // dirty inline func includes
140 ////////////////////////////////////////////////////////////////////////
141 // helpers for simple interpolation
144 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
146 // instead of having n equal sample values in a row like:
150 // we compare the current delta change with the next delta change.
152 // if curr_delta is positive,
154 // - and next delta is smaller (or changing direction):
158 // - and next delta significant (at least twice) bigger:
162 // - and next delta is nearly same:
167 // if curr_delta is negative,
169 // - and next delta is smaller (or changing direction):
173 // - and next delta significant (at least twice) bigger:
177 // - and next delta is nearly same:
183 INLINE void InterpolateUp(int ch)
185 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
187 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
188 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
192 if(id1>0) // curr delta positive
195 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
198 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
200 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
202 else // curr delta negative
205 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
208 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
210 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
214 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
218 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
219 //if(s_chan[ch].sinc<=0x8000)
220 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
222 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
224 else // no flags? add bigger val (if possible), calc smaller step, set flag1
225 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
229 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
232 INLINE void InterpolateDown(int ch)
234 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
236 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
237 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
238 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
242 ////////////////////////////////////////////////////////////////////////
243 // helpers for gauss interpolation
245 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
246 #define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
250 ////////////////////////////////////////////////////////////////////////
254 static void do_irq(void)
256 //if(!(spuStat & STAT_IRQ))
258 spuStat |= STAT_IRQ; // asserted status?
259 if(irqCallback) irqCallback();
263 static int check_irq(int ch, unsigned char *pos)
265 if((spuCtrl & CTRL_IRQ) && pos == pSpuIrq)
267 //printf("ch%d irq %04x\n", ch, pos - spuMemC);
274 ////////////////////////////////////////////////////////////////////////
275 // START SOUND... called by main thread to setup a new sound on a channel
276 ////////////////////////////////////////////////////////////////////////
278 INLINE void StartSound(int ch)
283 // fussy timing issues - do in VoiceOn
284 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
285 //s_chan[ch].bStop=0;
288 s_chan[ch].SB[26]=0; // init mixing vars
290 s_chan[ch].iSBPos=28;
292 s_chan[ch].SB[29]=0; // init our interpolation helpers
295 if(iUseInterpolation>=2) // gauss interpolation?
296 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
297 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
299 dwNewChannel&=~(1<<ch); // clear new channel bit
302 ////////////////////////////////////////////////////////////////////////
303 // ALL KIND OF HELPERS
304 ////////////////////////////////////////////////////////////////////////
306 INLINE int FModChangeFrequency(int ch,int ns)
308 unsigned int NP=s_chan[ch].iRawPitch;
311 NP=((32768L+iFMod[ns])*NP)/32768L;
313 if(NP>0x3fff) NP=0x3fff;
316 sinc=NP<<4; // calc frequency
317 if(iUseInterpolation==1) // freq change in simple interpolation mode
324 ////////////////////////////////////////////////////////////////////////
326 INLINE void StoreInterpolationVal(int ch,int fa)
328 if(s_chan[ch].bFMod==2) // fmod freq channel
329 s_chan[ch].SB[29]=fa;
334 if(iUseInterpolation>=2) // gauss/cubic interpolation
336 int gpos = s_chan[ch].SB[28];
339 s_chan[ch].SB[28] = gpos;
342 if(iUseInterpolation==1) // simple interpolation
344 s_chan[ch].SB[28] = 0;
345 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'
346 s_chan[ch].SB[30] = s_chan[ch].SB[31];
347 s_chan[ch].SB[31] = fa;
348 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
350 else s_chan[ch].SB[29]=fa; // no interpolation
354 ////////////////////////////////////////////////////////////////////////
356 INLINE int iGetInterpolationVal(int ch, int spos)
360 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
362 switch(iUseInterpolation)
364 //--------------------------------------------------//
365 case 3: // cubic interpolation
369 gpos = s_chan[ch].SB[28];
371 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
372 fa *= (xd - (2<<15)) / 6;
374 fa += gval(2) - gval(1) - gval(1) + gval0;
375 fa *= (xd - (1<<15)) >> 1;
377 fa += gval(1) - gval0;
383 //--------------------------------------------------//
384 case 2: // gauss interpolation
387 vl = (spos >> 6) & ~3;
388 gpos = s_chan[ch].SB[28];
389 vr=(gauss[vl]*(int)gval0)&~2047;
390 vr+=(gauss[vl+1]*gval(1))&~2047;
391 vr+=(gauss[vl+2]*gval(2))&~2047;
392 vr+=(gauss[vl+3]*gval(3))&~2047;
395 //--------------------------------------------------//
396 case 1: // simple interpolation
398 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
399 InterpolateUp(ch); // --> interpolate up
400 else InterpolateDown(ch); // --> else down
401 fa=s_chan[ch].SB[29];
403 //--------------------------------------------------//
404 default: // no interpolation
406 fa=s_chan[ch].SB[29];
408 //--------------------------------------------------//
414 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
417 int fa, s_1, s_2, d, s;
422 for (nSample = 0; nSample < 28; src++)
425 s = (int)(signed short)((d & 0x0f) << 12);
427 fa = s >> shift_factor;
428 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
431 dest[nSample++] = fa;
433 s = (int)(signed short)((d & 0xf0) << 8);
434 fa = s >> shift_factor;
435 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
438 dest[nSample++] = fa;
442 static int decode_block(int ch)
444 unsigned char *start;
445 int predict_nr,shift_factor,flags;
448 start=s_chan[ch].pCurr; // set up the current pos
450 if(s_chan[ch].prevflags&1) // 1: stop/loop
452 if(!(s_chan[ch].prevflags&2))
454 dwChannelOn&=~(1<<ch); // -> turn everything off
456 s_chan[ch].ADSRX.EnvelopeVol=0;
459 start = s_chan[ch].pLoop;
462 ret = check_irq(ch, start); // hack, see check_irq below..
464 predict_nr=(int)start[0];
465 shift_factor=predict_nr&0xf;
468 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
472 s_chan[ch].pLoop=start; // loop adress
476 if(flags&1) { // 1: stop/loop
477 start = s_chan[ch].pLoop;
478 ret |= check_irq(ch, start); // hack.. :(
481 if (start - spuMemC >= 0x80000)
484 s_chan[ch].pCurr = start; // store values for next cycle
485 s_chan[ch].prevflags = flags;
490 // do block, but ignore sample data
491 static int skip_block(int ch)
493 unsigned char *start = s_chan[ch].pCurr;
494 int flags = start[1];
495 int ret = check_irq(ch, start);
497 if(s_chan[ch].prevflags & 1)
498 start = s_chan[ch].pLoop;
501 s_chan[ch].pLoop = start;
506 start = s_chan[ch].pLoop;
508 s_chan[ch].pCurr = start;
509 s_chan[ch].prevflags = flags;
514 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
515 static int do_samples_##name(int ch, int ns, int ns_to) \
517 int sinc = s_chan[ch].sinc; \
518 int spos = s_chan[ch].spos; \
519 int sbpos = s_chan[ch].iSBPos; \
520 int *SB = s_chan[ch].SB; \
525 for (; ns < ns_to; ns++) \
529 while (spos >= 0x10000) \
534 d = decode_block(ch); \
536 ret = ns_to = ns + 1; \
548 s_chan[ch].sinc = sinc; \
549 s_chan[ch].spos = spos; \
550 s_chan[ch].iSBPos = sbpos; \
556 #define fmod_recv_check \
557 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
558 sinc = FModChangeFrequency(ch,ns)
560 make_do_samples(default, fmod_recv_check, ,
561 StoreInterpolationVal(ch, fa),
562 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
563 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
565 #define simple_interp_store \
566 s_chan[ch].SB[28] = 0; \
567 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
568 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
569 s_chan[ch].SB[31] = fa; \
570 s_chan[ch].SB[32] = 1
572 #define simple_interp_get \
573 if(sinc<0x10000) /* -> upsampling? */ \
574 InterpolateUp(ch); /* --> interpolate up */ \
575 else InterpolateDown(ch); /* --> else down */ \
576 ChanBuf[ns] = s_chan[ch].SB[29]
578 make_do_samples(simple, , ,
579 simple_interp_store, simple_interp_get, )
581 static int do_samples_noise(int ch, int ns, int ns_to)
583 int level, shift, bit;
586 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
587 while (s_chan[ch].spos >= 28*0x10000)
592 s_chan[ch].spos -= 28*0x10000;
595 // modified from DrHell/shalma, no fraction
596 level = (spuCtrl >> 10) & 0x0f;
597 level = 0x8000 >> level;
599 for (; ns < ns_to; ns++)
602 if (dwNoiseCount >= level)
604 dwNoiseCount -= level;
605 shift = (dwNoiseVal >> 10) & 0x1f;
606 bit = (0x69696969 >> shift) & 1;
607 if (dwNoiseVal & 0x8000)
609 dwNoiseVal = (dwNoiseVal << 1) | bit;
612 ChanBuf[ns] = (signed short)dwNoiseVal;
619 // asm code; lv and rv must be 0-3fff
620 extern void mix_chan(int start, int count, int lv, int rv);
621 extern void mix_chan_rvb(int start, int count, int lv, int rv);
623 static void mix_chan(int start, int count, int lv, int rv)
625 int *dst = SSumLR + start * 2;
626 const int *src = ChanBuf + start;
633 l = (sval * lv) >> 14;
634 r = (sval * rv) >> 14;
640 static void mix_chan_rvb(int start, int count, int lv, int rv)
642 int *dst = SSumLR + start * 2;
643 int *drvb = sRVBStart + start * 2;
644 const int *src = ChanBuf + start;
651 l = (sval * lv) >> 14;
652 r = (sval * rv) >> 14;
661 // 0x0800-0x0bff Voice 1
662 // 0x0c00-0x0fff Voice 3
663 static void noinline do_decode_bufs(int which, int start, int count)
665 const int *src = ChanBuf + start;
666 unsigned short *dst = &spuMem[0x800/2 + which*0x400/2];
667 int cursor = decode_pos;
671 dst[cursor] = *src++;
672 cursor = (cursor + 1) & 0x1ff;
675 // decode_pos is updated and irqs are checked later, after voice loop
678 ////////////////////////////////////////////////////////////////////////
680 // here is the main job handler...
681 // basically the whole sound processing is done in this fat func!
682 ////////////////////////////////////////////////////////////////////////
684 static int do_samples(int forced_updates)
686 int volmult = iVolume;
687 int ns,ns_from,ns_to;
693 // ok, at the beginning we are looking if there is
694 // enuff free place in the dsound/oss buffer to
695 // fill in new data, or if there is a new channel to start.
696 // if not, we wait (thread) or return (timer/spuasync)
697 // until enuff free place is available/a new channel gets
700 if(!forced_updates && out_current->busy()) // still enuff data in sound buffer?
705 cycles_since_update = 0;
706 if(forced_updates > 0)
709 //--------------------------------------------------// continue from irq handling in timer mode?
714 if(lastch>=0) // will be -1 if no continue is pending
716 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
719 silentch=~(dwChannelOn|dwNewChannel);
721 //--------------------------------------------------//
722 //- main channel loop -//
723 //--------------------------------------------------//
725 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
727 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
728 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
730 if(s_chan[ch].bNoise)
731 d=do_samples_noise(ch, ns_from, ns_to);
732 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
733 d=do_samples_noint(ch, ns_from, ns_to);
734 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
735 d=do_samples_simple(ch, ns_from, ns_to);
737 d=do_samples_default(ch, ns_from, ns_to);
745 MixADSR(ch, ns_from, ns_to);
749 do_decode_bufs(ch/2, ns_from, ns_to-ns_from);
750 decode_dirty_ch |= 1<<ch;
753 if(s_chan[ch].bFMod==2) // fmod freq channel
754 memcpy(iFMod, ChanBuf, sizeof(iFMod));
755 else if(s_chan[ch].bRVBActive)
756 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
758 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
762 // advance "stopped" channels that can cause irqs
763 // (all chans are always playing on the real thing..)
765 for(ch=0;ch<MAXCHAN;ch++)
767 if(!(silentch&(1<<ch))) continue; // already handled
768 if(dwChannelDead&(1<<ch)) continue;
769 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
772 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
773 while(s_chan[ch].spos >= 28 * 0x10000)
775 unsigned char *start = s_chan[ch].pCurr;
777 // no need for bIRQReturn since the channel is silent
779 if(start == s_chan[ch].pCurr)
782 dwChannelDead |= 1<<ch;
787 s_chan[ch].spos -= 28 * 0x10000;
791 if(bIRQReturn) // special return for "spu irq - wait for cpu action"
794 if(unlikely(silentch & decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
796 memset(&spuMem[0x800/2], 0, 0x400);
797 decode_dirty_ch &= ~(1<<1);
799 if(unlikely(silentch & decode_dirty_ch & (1<<3)))
801 memset(&spuMem[0xc00/2], 0, 0x400);
802 decode_dirty_ch &= ~(1<<3);
805 //---------------------------------------------------//
806 //- here we have another 1 ms of sound data
807 //---------------------------------------------------//
808 // mix XA infos (if any)
812 ///////////////////////////////////////////////////////
813 // mix all channels (including reverb) into one buffer
818 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
820 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
824 for (ns = 0; ns < NSSIZE*2; )
826 d = SSumLR[ns]; SSumLR[ns] = 0;
827 d = d * volmult >> 10;
832 d = SSumLR[ns]; SSumLR[ns] = 0;
833 d = d * volmult >> 10;
839 //////////////////////////////////////////////////////
840 // special irq handling in the decode buffers (0x0000-0x1000)
842 // the decode buffers are located in spu memory in the following way:
843 // 0x0000-0x03ff CD audio left
844 // 0x0400-0x07ff CD audio right
845 // 0x0800-0x0bff Voice 1
846 // 0x0c00-0x0fff Voice 3
847 // and decoded data is 16 bit for one sample
849 // even if voices 1/3 are off or no cd audio is playing, the internal
850 // play positions will move on and wrap after 0x400 bytes.
851 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
852 // increase this pointer on each sample by 2 bytes. If this pointer
853 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
854 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
855 // in some of Peops timer modes. So: we ignore this option here (for now).
857 if(unlikely((spuCtrl&CTRL_IRQ) && pSpuIrq && pSpuIrq<spuMemC+0x1000))
859 int irq_pos=(pSpuIrq-spuMemC)/2 & 0x1ff;
860 if((decode_pos <= irq_pos && irq_pos < decode_pos+NSSIZE)
861 || (decode_pos+NSSIZE > 0x200 && irq_pos < ((decode_pos+NSSIZE) & 0x1ff)))
863 //printf("decoder irq %x\n", decode_pos);
867 decode_pos = (decode_pos + NSSIZE) & 0x1ff;
872 // wanna have around 1/60 sec (16.666 ms) updates
873 if (iCycle++ > 16/FRAG_MSECS)
875 out_current->feed(pSpuBuffer,
876 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
877 pS = (short *)pSpuBuffer;
885 // SPU ASYNC... even newer epsxe func
886 // 1 time every 'cycle' cycles... harhar
888 // rearmed: called every 2ms now
890 void CALLBACK SPUasync(unsigned long cycle)
893 int forced_updates = 0;
896 if(!bSpuInit) return; // -> no init, no call
898 cycles_since_update += cycle;
900 if(dwNewChannel || had_dma)
907 if((spuCtrl&CTRL_IRQ) && (((spuCtrl^old_ctrl)&CTRL_IRQ) // irq was enabled
908 || cycles_since_update > PSXCLK/60 / 4)) {
910 forced_updates = cycles_since_update / (PSXCLK/44100) / NSSIZE;
912 // with no irqs, once per frame should be fine (using a bit more because of BIAS)
913 else if(cycles_since_update > PSXCLK/60 * 5/4)
919 do_samples(forced_updates);
922 // SPU UPDATE... new epsxe func
923 // 1 time every 32 hsync lines
924 // (312/32)x50 in pal
925 // (262/32)x60 in ntsc
927 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
928 // leave that func in the linux port, until epsxe linux is using
929 // the async function as well
931 void CALLBACK SPUupdate(void)
938 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
941 if(!xap->freq) return; // no xa freq ? bye
943 FeedXA(xap); // call main XA feeder
947 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
950 if (nbytes<=0) return -1;
952 return FeedCDDA((unsigned char *)pcm, nbytes);
955 // to be called after state load
956 void ClearWorkingState(void)
958 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
959 memset(iFMod,0,sizeof(iFMod));
960 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
963 // SETUPSTREAMS: init most of the spu buffers
964 void SetupStreams(void)
968 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
970 if(iUseReverb==1) i=88200*2;
973 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
974 memset(sRVBStart,0,i*4);
975 sRVBEnd = sRVBStart + i;
976 sRVBPlay = sRVBStart;
978 XAStart = // alloc xa buffer
979 (uint32_t *)malloc(44100 * sizeof(uint32_t));
980 XAEnd = XAStart + 44100;
984 CDDAStart = // alloc cdda buffer
985 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
986 CDDAEnd = CDDAStart + 16384;
987 CDDAPlay = CDDAStart;
988 CDDAFeed = CDDAStart;
990 for(i=0;i<MAXCHAN;i++) // loop sound channels
992 // we don't use mutex sync... not needed, would only
994 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
995 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
996 s_chan[i].pLoop=spuMemC;
997 s_chan[i].pCurr=spuMemC;
1000 ClearWorkingState();
1002 bSpuInit=1; // flag: we are inited
1005 // REMOVESTREAMS: free most buffer
1006 void RemoveStreams(void)
1008 free(pSpuBuffer); // free mixing buffer
1010 free(sRVBStart); // free reverb buffer
1012 free(XAStart); // free XA buffer
1014 free(CDDAStart); // free CDDA buffer
1020 // SPUINIT: this func will be called first by the main emu
1021 long CALLBACK SPUinit(void)
1023 spuMemC = (unsigned char *)spuMem; // just small setup
1024 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1028 spuAddr = 0xffffffff;
1029 spuMemC = (unsigned char *)spuMem;
1031 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1035 SetupStreams(); // prepare streaming
1040 // SPUOPEN: called by main emu after init
1041 long CALLBACK SPUopen(void)
1043 if (bSPUIsOpen) return 0; // security for some stupid main emus
1045 SetupSound(); // setup sound (before init!)
1049 return PSE_SPU_ERR_SUCCESS;
1052 // SPUCLOSE: called before shutdown
1053 long CALLBACK SPUclose(void)
1055 if (!bSPUIsOpen) return 0; // some security
1057 bSPUIsOpen = 0; // no more open
1059 out_current->finish(); // no more sound handling
1064 // SPUSHUTDOWN: called by main emu on final exit
1065 long CALLBACK SPUshutdown(void)
1068 RemoveStreams(); // no more streaming
1074 // SPUTEST: we don't test, we are always fine ;)
1075 long CALLBACK SPUtest(void)
1080 // SPUCONFIGURE: call config dialog
1081 long CALLBACK SPUconfigure(void)
1086 // StartCfgTool("CFG");
1091 // SPUABOUT: show about window
1092 void CALLBACK SPUabout(void)
1097 // StartCfgTool("ABOUT");
1102 // this functions will be called once,
1103 // passes a callback that should be called on SPU-IRQ/cdda volume change
1104 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1106 irqCallback = callback;
1109 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1111 cddavCallback = CDDAVcallback;
1114 // COMMON PLUGIN INFO FUNCS
1116 char * CALLBACK PSEgetLibName(void)
1118 return _(libraryName);
1121 unsigned long CALLBACK PSEgetLibType(void)
1126 unsigned long CALLBACK PSEgetLibVersion(void)
1128 return (1 << 16) | (6 << 8);
1131 char * SPUgetLibInfos(void)
1133 return _(libraryInfo);
1138 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1140 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1142 for(;ch<MAXCHAN;ch++)
1144 if (!(dwChannelOn & (1<<ch)))
1146 if (s_chan[ch].bFMod == 2)
1147 fmod_chans |= 1 << ch;
1148 if (s_chan[ch].bNoise)
1149 noise_chans |= 1 << ch;
1150 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1151 irq_chans |= 1 << ch;
1154 *chans_out = dwChannelOn;
1155 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1156 *fmod_chans_out = fmod_chans;
1157 *noise_chans_out = noise_chans;
1160 // vim:shiftwidth=1:expandtab