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-2011
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"
27 #include "dsoundoss.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;
78 unsigned char * pMixIrq=0;
82 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;
113 // certain globals (were local before, but with the new timeproc I need em global)
115 static const int f[8][2] = { { 0, 0 },
120 int ChanBuf[NSSIZE+3];
121 int SSumLR[(NSSIZE+3)*2];
127 int lastch=-1; // last channel processed on spu irq in timer mode
128 static int lastns=0; // last ns pos
129 static int cycles_since_update;
131 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
133 ////////////////////////////////////////////////////////////////////////
135 ////////////////////////////////////////////////////////////////////////
137 // dirty inline func includes
142 ////////////////////////////////////////////////////////////////////////
143 // helpers for simple interpolation
146 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
148 // instead of having n equal sample values in a row like:
152 // we compare the current delta change with the next delta change.
154 // if curr_delta is positive,
156 // - and next delta is smaller (or changing direction):
160 // - and next delta significant (at least twice) bigger:
164 // - and next delta is nearly same:
169 // if curr_delta is negative,
171 // - and next delta is smaller (or changing direction):
175 // - and next delta significant (at least twice) bigger:
179 // - and next delta is nearly same:
185 INLINE void InterpolateUp(int ch)
187 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
189 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
190 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
194 if(id1>0) // curr delta positive
197 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
200 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
202 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
204 else // curr delta negative
207 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
210 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
212 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
216 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
220 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
221 //if(s_chan[ch].sinc<=0x8000)
222 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
224 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
226 else // no flags? add bigger val (if possible), calc smaller step, set flag1
227 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
231 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
234 INLINE void InterpolateDown(int ch)
236 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
238 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
239 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
240 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
244 ////////////////////////////////////////////////////////////////////////
245 // helpers for gauss interpolation
247 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
248 #define gval(x) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
252 ////////////////////////////////////////////////////////////////////////
256 ////////////////////////////////////////////////////////////////////////
257 // START SOUND... called by main thread to setup a new sound on a channel
258 ////////////////////////////////////////////////////////////////////////
260 INLINE void StartSound(int ch)
265 // fussy timing issues - do in VoiceOn
266 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
267 //s_chan[ch].bStop=0;
270 s_chan[ch].SB[26]=0; // init mixing vars
272 s_chan[ch].iSBPos=28;
274 s_chan[ch].SB[29]=0; // init our interpolation helpers
277 if(iUseInterpolation>=2) // gauss interpolation?
278 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
279 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
281 dwNewChannel&=~(1<<ch); // clear new channel bit
284 ////////////////////////////////////////////////////////////////////////
285 // ALL KIND OF HELPERS
286 ////////////////////////////////////////////////////////////////////////
288 INLINE int FModChangeFrequency(int ch,int ns)
290 unsigned int NP=s_chan[ch].iRawPitch;
293 NP=((32768L+iFMod[ns])*NP)/32768L;
295 if(NP>0x3fff) NP=0x3fff;
298 sinc=NP<<4; // calc frequency
299 if(iUseInterpolation==1) // freq change in simple interpolation mode
306 ////////////////////////////////////////////////////////////////////////
308 INLINE void StoreInterpolationVal(int ch,int fa)
310 if(s_chan[ch].bFMod==2) // fmod freq channel
311 s_chan[ch].SB[29]=fa;
316 if(iUseInterpolation>=2) // gauss/cubic interpolation
318 int gpos = s_chan[ch].SB[28];
321 s_chan[ch].SB[28] = gpos;
324 if(iUseInterpolation==1) // simple interpolation
326 s_chan[ch].SB[28] = 0;
327 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'
328 s_chan[ch].SB[30] = s_chan[ch].SB[31];
329 s_chan[ch].SB[31] = fa;
330 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
332 else s_chan[ch].SB[29]=fa; // no interpolation
336 ////////////////////////////////////////////////////////////////////////
338 INLINE int iGetInterpolationVal(int ch, int spos)
342 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
344 switch(iUseInterpolation)
346 //--------------------------------------------------//
347 case 3: // cubic interpolation
351 gpos = s_chan[ch].SB[28];
353 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
354 fa *= (xd - (2<<15)) / 6;
356 fa += gval(2) - gval(1) - gval(1) + gval0;
357 fa *= (xd - (1<<15)) >> 1;
359 fa += gval(1) - gval0;
365 //--------------------------------------------------//
366 case 2: // gauss interpolation
369 vl = (spos >> 6) & ~3;
370 gpos = s_chan[ch].SB[28];
371 vr=(gauss[vl]*gval0)&~2047;
372 vr+=(gauss[vl+1]*gval(1))&~2047;
373 vr+=(gauss[vl+2]*gval(2))&~2047;
374 vr+=(gauss[vl+3]*gval(3))&~2047;
377 //--------------------------------------------------//
378 case 1: // simple interpolation
380 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
381 InterpolateUp(ch); // --> interpolate up
382 else InterpolateDown(ch); // --> else down
383 fa=s_chan[ch].SB[29];
385 //--------------------------------------------------//
386 default: // no interpolation
388 fa=s_chan[ch].SB[29];
390 //--------------------------------------------------//
396 static void do_irq(void)
398 //if(!(spuStat & STAT_IRQ))
400 spuStat |= STAT_IRQ; // asserted status?
401 if(irqCallback) irqCallback();
405 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
408 int fa, s_1, s_2, d, s;
413 for (nSample = 0; nSample < 28; src++)
416 s = (int)(signed short)((d & 0x0f) << 12);
418 fa = s >> shift_factor;
419 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
422 dest[nSample++] = fa;
424 s = (int)(signed short)((d & 0xf0) << 8);
425 fa = s >> shift_factor;
426 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
429 dest[nSample++] = fa;
433 static int decode_block(int ch)
435 unsigned char *start;
436 int predict_nr,shift_factor,flags;
439 start=s_chan[ch].pCurr; // set up the current pos
440 if(dwPendingChanOff&(1<<ch))
442 dwChannelOn&=~(1<<ch); // -> turn everything off
443 dwPendingChanOff&=~(1<<ch);
445 s_chan[ch].ADSRX.EnvelopeVol=0;
448 //////////////////////////////////////////// irq check
452 if(pSpuIrq == start) // irq address reached?
454 do_irq(); // -> call main emu
459 predict_nr=(int)start[0];
460 shift_factor=predict_nr&0xf;
463 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
465 //////////////////////////////////////////// flag handler
469 s_chan[ch].pLoop=start; // loop adress
472 if(flags&1) // 1: stop/loop
475 dwPendingChanOff|=1<<ch;
477 start = s_chan[ch].pLoop;
480 if (start - spuMemC >= 0x80000) {
483 printf("ch%d oflow\n", ch);
486 s_chan[ch].pCurr = start; // store values for next cycle
487 s_chan[ch].bJump = flags & 1;
492 // do block, but ignore sample data
493 static int skip_block(int ch)
495 unsigned char *start = s_chan[ch].pCurr;
496 int flags = start[1];
506 s_chan[ch].pLoop = start;
508 s_chan[ch].pCurr += 16;
511 s_chan[ch].pCurr = s_chan[ch].pLoop;
513 s_chan[ch].bJump = flags & 1;
517 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
518 static int do_samples_##name(int ch, int ns, int ns_to) \
520 int sinc = s_chan[ch].sinc; \
521 int spos = s_chan[ch].spos; \
522 int sbpos = s_chan[ch].iSBPos; \
523 int *SB = s_chan[ch].SB; \
528 for (; ns < ns_to; ns++) \
532 while (spos >= 0x10000) \
537 d = decode_block(ch); \
538 if(d && iSPUIRQWait) \
555 s_chan[ch].sinc = sinc; \
556 s_chan[ch].spos = spos; \
557 s_chan[ch].iSBPos = sbpos; \
563 #define fmod_recv_check \
564 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
565 sinc = FModChangeFrequency(ch,ns)
567 make_do_samples(default, fmod_recv_check, ,
568 StoreInterpolationVal(ch, fa),
569 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
570 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
572 #define simple_interp_store \
573 s_chan[ch].SB[28] = 0; \
574 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
575 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
576 s_chan[ch].SB[31] = fa; \
577 s_chan[ch].SB[32] = 1
579 #define simple_interp_get \
580 if(sinc<0x10000) /* -> upsampling? */ \
581 InterpolateUp(ch); /* --> interpolate up */ \
582 else InterpolateDown(ch); /* --> else down */ \
583 ChanBuf[ns] = s_chan[ch].SB[29]
585 make_do_samples(simple, , ,
586 simple_interp_store, simple_interp_get, )
588 static int do_samples_noise(int ch, int ns, int ns_to)
590 int level, shift, bit;
592 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
593 while (s_chan[ch].spos >= 28*0x10000)
596 s_chan[ch].spos -= 28*0x10000;
599 // modified from DrHell/shalma, no fraction
600 level = (spuCtrl >> 10) & 0x0f;
601 level = 0x8000 >> level;
603 for (; ns < ns_to; ns++)
606 if (dwNoiseCount >= level)
608 dwNoiseCount -= level;
609 shift = (dwNoiseVal >> 10) & 0x1f;
610 bit = (0x69696969 >> shift) & 1;
611 if (dwNoiseVal & 0x8000)
613 dwNoiseVal = (dwNoiseVal << 1) | bit;
616 ChanBuf[ns] = (signed short)dwNoiseVal;
623 // asm code; lv and rv must be 0-3fff
624 extern void mix_chan(int start, int count, int lv, int rv);
625 extern void mix_chan_rvb(int start, int count, int lv, int rv);
627 static void mix_chan(int start, int count, int lv, int rv)
629 int *dst = SSumLR + start * 2;
630 const int *src = ChanBuf + start;
637 l = (sval * lv) >> 14;
638 r = (sval * rv) >> 14;
644 static void mix_chan_rvb(int start, int count, int lv, int rv)
646 int *dst = SSumLR + start * 2;
647 int *drvb = sRVBStart + start * 2;
648 const int *src = ChanBuf + start;
655 l = (sval * lv) >> 14;
656 r = (sval * rv) >> 14;
665 ////////////////////////////////////////////////////////////////////////
667 // here is the main job handler...
668 // basically the whole sound processing is done in this fat func!
669 ////////////////////////////////////////////////////////////////////////
671 static int do_samples(int forced_updates)
673 int volmult = iVolume;
674 int ns,ns_from,ns_to;
680 // ok, at the beginning we are looking if there is
681 // enuff free place in the dsound/oss buffer to
682 // fill in new data, or if there is a new channel to start.
683 // if not, we wait (thread) or return (timer/spuasync)
684 // until enuff free place is available/a new channel gets
687 if(!forced_updates && SoundGetBytesBuffered()) // still enuff data in sound buffer?
692 cycles_since_update = 0;
693 if(forced_updates > 0)
696 //--------------------------------------------------// continue from irq handling in timer mode?
701 if(lastch>=0) // will be -1 if no continue is pending
703 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
706 silentch=~(dwChannelOn|dwNewChannel);
708 //--------------------------------------------------//
709 //- main channel loop -//
710 //--------------------------------------------------//
712 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
714 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
715 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
717 if(s_chan[ch].bNoise)
718 d=do_samples_noise(ch, ns_from, ns_to);
719 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
720 d=do_samples_noint(ch, ns_from, ns_to);
721 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
722 d=do_samples_simple(ch, ns_from, ns_to);
724 d=do_samples_default(ch, ns_from, ns_to);
734 MixADSR(ch, ns_from, ns_to);
736 if(s_chan[ch].bFMod==2) // fmod freq channel
737 memcpy(iFMod, ChanBuf, sizeof(iFMod));
738 else if(s_chan[ch].bRVBActive)
739 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
741 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
745 // advance "stopped" channels that can cause irqs
746 // (all chans are always playing on the real thing..)
747 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
748 for(ch=0;ch<MAXCHAN;ch++)
750 if(!(silentch&(1<<ch))) continue; // already handled
751 if(dwChannelDead&(1<<ch)) continue;
752 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
755 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
756 while(s_chan[ch].spos >= 28 * 0x10000)
758 unsigned char *start = s_chan[ch].pCurr;
760 // no need for bIRQReturn since the channel is silent
762 if(start == s_chan[ch].pCurr)
765 dwChannelDead |= 1<<ch;
770 s_chan[ch].spos -= 28 * 0x10000;
774 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
778 //---------------------------------------------------//
779 //- here we have another 1 ms of sound data
780 //---------------------------------------------------//
781 // mix XA infos (if any)
785 ///////////////////////////////////////////////////////
786 // mix all channels (including reverb) into one buffer
791 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
793 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
797 for (ns = 0; ns < NSSIZE*2; )
799 d = SSumLR[ns]; SSumLR[ns] = 0;
800 d = d * volmult >> 10;
805 d = SSumLR[ns]; SSumLR[ns] = 0;
806 d = d * volmult >> 10;
812 //////////////////////////////////////////////////////
813 // special irq handling in the decode buffers (0x0000-0x1000)
815 // the decode buffers are located in spu memory in the following way:
816 // 0x0000-0x03ff CD audio left
817 // 0x0400-0x07ff CD audio right
818 // 0x0800-0x0bff Voice 1
819 // 0x0c00-0x0fff Voice 3
820 // and decoded data is 16 bit for one sample
822 // even if voices 1/3 are off or no cd audio is playing, the internal
823 // play positions will move on and wrap after 0x400 bytes.
824 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
825 // increase this pointer on each sample by 2 bytes. If this pointer
826 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
827 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
828 // in some of Peops timer modes. So: we ignore this option here (for now).
832 for(ns=0;ns<NSSIZE;ns++)
834 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
838 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
842 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
849 // wanna have around 1/60 sec (16.666 ms) updates
850 if (iCycle++ > 16/FRAG_MSECS)
852 SoundFeedStreamData((unsigned char *)pSpuBuffer,
853 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
854 pS = (short *)pSpuBuffer;
862 // SPU ASYNC... even newer epsxe func
863 // 1 time every 'cycle' cycles... harhar
865 // rearmed: called every 2ms now
867 void CALLBACK SPUasync(unsigned long cycle)
869 int forced_updates = 0;
872 if(!bSpuInit) return; // -> no init, no call
874 cycles_since_update += cycle;
876 if(dwNewChannel || had_dma)
883 // once per frame should be fine (using a bit more because of BIAS)
884 if(cycles_since_update > PSXCLK/60 * 5/4)
888 do_samples(forced_updates);
891 // SPU UPDATE... new epsxe func
892 // 1 time every 32 hsync lines
893 // (312/32)x50 in pal
894 // (262/32)x60 in ntsc
896 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
897 // leave that func in the linux port, until epsxe linux is using
898 // the async function as well
900 void CALLBACK SPUupdate(void)
907 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
910 if(!xap->freq) return; // no xa freq ? bye
912 FeedXA(xap); // call main XA feeder
916 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
919 if (nbytes<=0) return -1;
921 return FeedCDDA((unsigned char *)pcm, nbytes);
924 // to be called after state load
925 void ClearWorkingState(void)
927 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
928 memset(iFMod,0,sizeof(iFMod));
929 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
932 // SETUPSTREAMS: init most of the spu buffers
933 void SetupStreams(void)
937 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
939 if(iUseReverb==1) i=88200*2;
942 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
943 memset(sRVBStart,0,i*4);
944 sRVBEnd = sRVBStart + i;
945 sRVBPlay = sRVBStart;
947 XAStart = // alloc xa buffer
948 (uint32_t *)malloc(44100 * sizeof(uint32_t));
949 XAEnd = XAStart + 44100;
953 CDDAStart = // alloc cdda buffer
954 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
955 CDDAEnd = CDDAStart + 16384;
956 CDDAPlay = CDDAStart;
957 CDDAFeed = CDDAStart;
959 for(i=0;i<MAXCHAN;i++) // loop sound channels
961 // we don't use mutex sync... not needed, would only
963 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
964 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
965 s_chan[i].pLoop=spuMemC;
966 s_chan[i].pCurr=spuMemC;
969 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
973 bSpuInit=1; // flag: we are inited
976 // REMOVESTREAMS: free most buffer
977 void RemoveStreams(void)
979 free(pSpuBuffer); // free mixing buffer
981 free(sRVBStart); // free reverb buffer
983 free(XAStart); // free XA buffer
985 free(CDDAStart); // free CDDA buffer
991 // SPUINIT: this func will be called first by the main emu
992 long CALLBACK SPUinit(void)
994 spuMemC = (unsigned char *)spuMem; // just small setup
995 memset((void *)&rvb, 0, sizeof(REVERBInfo));
999 spuAddr = 0xffffffff;
1000 spuMemC = (unsigned char *)spuMem;
1002 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1007 SetupStreams(); // prepare streaming
1012 // SPUOPEN: called by main emu after init
1013 long CALLBACK SPUopen(void)
1015 if (bSPUIsOpen) return 0; // security for some stupid main emus
1017 SetupSound(); // setup sound (before init!)
1021 return PSE_SPU_ERR_SUCCESS;
1024 // SPUCLOSE: called before shutdown
1025 long CALLBACK SPUclose(void)
1027 if (!bSPUIsOpen) return 0; // some security
1029 bSPUIsOpen = 0; // no more open
1031 RemoveSound(); // no more sound handling
1036 // SPUSHUTDOWN: called by main emu on final exit
1037 long CALLBACK SPUshutdown(void)
1040 RemoveStreams(); // no more streaming
1046 // SPUTEST: we don't test, we are always fine ;)
1047 long CALLBACK SPUtest(void)
1052 // SPUCONFIGURE: call config dialog
1053 long CALLBACK SPUconfigure(void)
1058 // StartCfgTool("CFG");
1063 // SPUABOUT: show about window
1064 void CALLBACK SPUabout(void)
1069 // StartCfgTool("ABOUT");
1074 // this functions will be called once,
1075 // passes a callback that should be called on SPU-IRQ/cdda volume change
1076 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1078 irqCallback = callback;
1081 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1083 cddavCallback = CDDAVcallback;
1086 // COMMON PLUGIN INFO FUNCS
1088 char * CALLBACK PSEgetLibName(void)
1090 return _(libraryName);
1093 unsigned long CALLBACK PSEgetLibType(void)
1098 unsigned long CALLBACK PSEgetLibVersion(void)
1100 return (1 << 16) | (6 << 8);
1103 char * SPUgetLibInfos(void)
1105 return _(libraryInfo);
1110 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1112 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1114 for(;ch<MAXCHAN;ch++)
1116 if (!(dwChannelOn & (1<<ch)))
1118 if (s_chan[ch].bFMod == 2)
1119 fmod_chans |= 1 << ch;
1120 if (s_chan[ch].bNoise)
1121 noise_chans |= 1 << ch;
1122 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1123 irq_chans |= 1 << ch;
1126 *chans_out = dwChannelOn;
1127 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1128 *fmod_chans_out = fmod_chans;
1129 *noise_chans_out = noise_chans;
1132 // vim:shiftwidth=1:expandtab