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; \
50 #if defined (USEMACOSX)
51 static char * libraryName = N_("Mac OS X Sound");
52 #elif defined (USEALSA)
53 static char * libraryName = N_("ALSA Sound");
54 #elif defined (USEOSS)
55 static char * libraryName = N_("OSS Sound");
56 #elif defined (USESDL)
57 static char * libraryName = N_("SDL Sound");
58 #elif defined (USEPULSEAUDIO)
59 static char * libraryName = N_("PulseAudio Sound");
61 static char * libraryName = N_("NULL Sound");
64 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
69 // psx buffer / addresses
71 unsigned short regArea[10000];
72 unsigned short spuMem[256*1024];
73 unsigned char * spuMemC;
74 unsigned char * pSpuIrq=0;
75 unsigned char * pSpuBuffer;
76 unsigned char * pMixIrq=0;
80 int iVolume=768; // 1024 is 1.0
86 int iUseInterpolation=2;
88 // MAIN infos struct for each channel
90 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
93 unsigned int dwNoiseVal; // global noise generator
94 unsigned int dwNoiseCount;
97 unsigned short spuCtrl=0; // some vars to store psx reg infos
98 unsigned short spuStat=0;
99 unsigned short spuIrq=0;
100 unsigned long spuAddr=0xffffffff; // address into spu mem
104 unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
105 unsigned int dwChannelOn=0; // not silent channels
106 unsigned int dwPendingChanOff=0;
107 unsigned int dwChannelDead=0; // silent+not useful channels
109 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
110 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
112 // certain globals (were local before, but with the new timeproc I need em global)
114 static const int f[8][2] = { { 0, 0 },
119 int ChanBuf[NSSIZE+3];
120 int SSumLR[(NSSIZE+3)*2];
125 int lastch=-1; // last channel processed on spu irq in timer mode
126 static int lastns=0; // last ns pos
127 static int iSecureStart=0; // secure start counter
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) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
250 ////////////////////////////////////////////////////////////////////////
254 ////////////////////////////////////////////////////////////////////////
255 // START SOUND... called by main thread to setup a new sound on a channel
256 ////////////////////////////////////////////////////////////////////////
258 INLINE void StartSound(int ch)
263 // fussy timing issues - do in VoiceOn
264 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
265 //s_chan[ch].bStop=0;
268 s_chan[ch].SB[26]=0; // init mixing vars
270 s_chan[ch].iSBPos=28;
272 s_chan[ch].SB[29]=0; // init our interpolation helpers
275 if(iUseInterpolation>=2) // gauss interpolation?
276 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
277 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
279 dwNewChannel&=~(1<<ch); // clear new channel bit
282 ////////////////////////////////////////////////////////////////////////
283 // ALL KIND OF HELPERS
284 ////////////////////////////////////////////////////////////////////////
286 INLINE int FModChangeFrequency(int ch,int ns)
288 unsigned int NP=s_chan[ch].iRawPitch;
291 NP=((32768L+iFMod[ns])*NP)/32768L;
293 if(NP>0x3fff) NP=0x3fff;
296 sinc=NP<<4; // calc frequency
297 if(iUseInterpolation==1) // freq change in simple interpolation mode
304 ////////////////////////////////////////////////////////////////////////
306 INLINE void StoreInterpolationVal(int ch,int fa)
308 if(s_chan[ch].bFMod==2) // fmod freq channel
309 s_chan[ch].SB[29]=fa;
314 if(iUseInterpolation>=2) // gauss/cubic interpolation
316 int gpos = s_chan[ch].SB[28];
319 s_chan[ch].SB[28] = gpos;
322 if(iUseInterpolation==1) // simple interpolation
324 s_chan[ch].SB[28] = 0;
325 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'
326 s_chan[ch].SB[30] = s_chan[ch].SB[31];
327 s_chan[ch].SB[31] = fa;
328 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
330 else s_chan[ch].SB[29]=fa; // no interpolation
334 ////////////////////////////////////////////////////////////////////////
336 INLINE int iGetInterpolationVal(int ch, int spos)
340 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
342 switch(iUseInterpolation)
344 //--------------------------------------------------//
345 case 3: // cubic interpolation
349 gpos = s_chan[ch].SB[28];
351 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
352 fa *= (xd - (2<<15)) / 6;
354 fa += gval(2) - gval(1) - gval(1) + gval0;
355 fa *= (xd - (1<<15)) >> 1;
357 fa += gval(1) - gval0;
363 //--------------------------------------------------//
364 case 2: // gauss interpolation
367 vl = (spos >> 6) & ~3;
368 gpos = s_chan[ch].SB[28];
369 vr=(gauss[vl]*gval0)&~2047;
370 vr+=(gauss[vl+1]*gval(1))&~2047;
371 vr+=(gauss[vl+2]*gval(2))&~2047;
372 vr+=(gauss[vl+3]*gval(3))&~2047;
375 //--------------------------------------------------//
376 case 1: // simple interpolation
378 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
379 InterpolateUp(ch); // --> interpolate up
380 else InterpolateDown(ch); // --> else down
381 fa=s_chan[ch].SB[29];
383 //--------------------------------------------------//
384 default: // no interpolation
386 fa=s_chan[ch].SB[29];
388 //--------------------------------------------------//
394 static void do_irq(void)
396 //if(!(spuStat & STAT_IRQ))
398 spuStat |= STAT_IRQ; // asserted status?
399 if(irqCallback) irqCallback();
403 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
406 int fa, s_1, s_2, d, s;
411 for (nSample = 0; nSample < 28; src++)
414 s = (int)(signed short)((d & 0x0f) << 12);
416 fa = s >> shift_factor;
417 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
420 dest[nSample++] = fa;
422 s = (int)(signed short)((d & 0xf0) << 8);
423 fa = s >> shift_factor;
424 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
427 dest[nSample++] = fa;
431 static int decode_block(int ch)
433 unsigned char *start;
434 int predict_nr,shift_factor,flags;
437 start=s_chan[ch].pCurr; // set up the current pos
438 if(dwPendingChanOff&(1<<ch))
440 dwChannelOn&=~(1<<ch); // -> turn everything off
441 dwPendingChanOff&=~(1<<ch);
443 s_chan[ch].ADSRX.EnvelopeVol=0;
446 //////////////////////////////////////////// irq check
450 if(pSpuIrq == start) // irq address reached?
452 do_irq(); // -> call main emu
457 predict_nr=(int)start[0];
458 shift_factor=predict_nr&0xf;
461 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
463 //////////////////////////////////////////// flag handler
467 s_chan[ch].pLoop=start; // loop adress
470 if(flags&1) // 1: stop/loop
473 dwPendingChanOff|=1<<ch;
475 start = s_chan[ch].pLoop;
478 if (start - spuMemC >= 0x80000) {
481 printf("ch%d oflow\n", ch);
484 s_chan[ch].pCurr = start; // store values for next cycle
485 s_chan[ch].bJump = flags & 1;
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];
504 s_chan[ch].pLoop = start;
506 s_chan[ch].pCurr += 16;
509 s_chan[ch].pCurr = s_chan[ch].pLoop;
511 s_chan[ch].bJump = flags & 1;
515 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
516 static int do_samples_##name(int ch, int ns, int ns_to) \
518 int sinc = s_chan[ch].sinc; \
519 int spos = s_chan[ch].spos; \
520 int sbpos = s_chan[ch].iSBPos; \
521 int *SB = s_chan[ch].SB; \
526 for (; ns < ns_to; ns++) \
530 while (spos >= 0x10000) \
535 d = decode_block(ch); \
536 if(d && iSPUIRQWait) \
553 s_chan[ch].sinc = sinc; \
554 s_chan[ch].spos = spos; \
555 s_chan[ch].iSBPos = sbpos; \
561 #define fmod_recv_check \
562 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
563 sinc = FModChangeFrequency(ch,ns)
565 make_do_samples(default, fmod_recv_check, ,
566 StoreInterpolationVal(ch, fa),
567 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
568 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
570 #define simple_interp_store \
571 s_chan[ch].SB[28] = 0; \
572 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
573 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
574 s_chan[ch].SB[31] = fa; \
575 s_chan[ch].SB[32] = 1
577 #define simple_interp_get \
578 if(sinc<0x10000) /* -> upsampling? */ \
579 InterpolateUp(ch); /* --> interpolate up */ \
580 else InterpolateDown(ch); /* --> else down */ \
581 ChanBuf[ns] = s_chan[ch].SB[29]
583 make_do_samples(simple, , ,
584 simple_interp_store, simple_interp_get, )
586 static int do_samples_noise(int ch, int ns, int ns_to)
588 int level, shift, bit;
590 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
591 while (s_chan[ch].spos >= 28*0x10000)
594 s_chan[ch].spos -= 28*0x10000;
597 // modified from DrHell/shalma, no fraction
598 level = (spuCtrl >> 10) & 0x0f;
599 level = 0x8000 >> level;
601 for (; ns < ns_to; ns++)
604 if (dwNoiseCount >= level)
606 dwNoiseCount -= level;
607 shift = (dwNoiseVal >> 10) & 0x1f;
608 bit = (0x69696969 >> shift) & 1;
609 if (dwNoiseVal & 0x8000)
611 dwNoiseVal = (dwNoiseVal << 1) | bit;
614 ChanBuf[ns] = (signed short)dwNoiseVal;
621 // asm code; lv and rv must be 0-3fff
622 extern void mix_chan(int start, int count, int lv, int rv);
623 extern void mix_chan_rvb(int start, int count, int lv, int rv);
625 static void mix_chan(int start, int count, int lv, int rv)
627 int *dst = SSumLR + start * 2;
628 const int *src = ChanBuf + start;
635 l = (sval * lv) >> 14;
636 r = (sval * rv) >> 14;
642 static void mix_chan_rvb(int start, int count, int lv, int rv)
644 int *dst = SSumLR + start * 2;
645 int *drvb = sRVBStart + start * 2;
646 const int *src = ChanBuf + start;
653 l = (sval * lv) >> 14;
654 r = (sval * rv) >> 14;
663 ////////////////////////////////////////////////////////////////////////
665 // here is the main job handler...
666 // basically the whole sound processing is done in this fat func!
667 ////////////////////////////////////////////////////////////////////////
669 static int do_samples(void)
671 int volmult = iVolume;
672 int ns,ns_from,ns_to;
678 // ok, at the beginning we are looking if there is
679 // enuff free place in the dsound/oss buffer to
680 // fill in new data, or if there is a new channel to start.
681 // if not, we wait (thread) or return (timer/spuasync)
682 // until enuff free place is available/a new channel gets
685 if(dwNewChannel) // new channel should start immedately?
686 { // (at least one bit 0 ... MAXCHANNEL is set?)
687 iSecureStart++; // -> set iSecure
688 if(iSecureStart>5) iSecureStart=0; // (if it is set 5 times - that means on 5 tries a new samples has been started - in a row, we will reset it, to give the sound update a chance)
690 else iSecureStart=0; // 0: no new channel should start
692 if(!iSecureStart && // no new start?
693 (SoundGetBytesBuffered()>TESTSIZE)) // and still enuff data in sound buffer?
698 //--------------------------------------------------// continue from irq handling in timer mode?
703 if(lastch>=0) // will be -1 if no continue is pending
705 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
708 silentch=~(dwChannelOn|dwNewChannel);
710 //--------------------------------------------------//
711 //- main channel loop -//
712 //--------------------------------------------------//
714 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
716 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
717 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
719 if(s_chan[ch].bNoise)
720 d=do_samples_noise(ch, ns_from, ns_to);
721 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
722 d=do_samples_noint(ch, ns_from, ns_to);
723 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
724 d=do_samples_simple(ch, ns_from, ns_to);
726 d=do_samples_default(ch, ns_from, ns_to);
736 MixADSR(ch, ns_from, ns_to);
738 if(s_chan[ch].bFMod==2) // fmod freq channel
739 memcpy(iFMod, ChanBuf, sizeof(iFMod));
740 else if(s_chan[ch].bRVBActive)
741 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
743 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
747 // advance "stopped" channels that can cause irqs
748 // (all chans are always playing on the real thing..)
749 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
750 for(ch=0;ch<MAXCHAN;ch++)
752 if(!(silentch&(1<<ch))) continue; // already handled
753 if(dwChannelDead&(1<<ch)) continue;
754 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
757 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
758 while(s_chan[ch].spos >= 28 * 0x10000)
760 unsigned char *start = s_chan[ch].pCurr;
762 // no need for bIRQReturn since the channel is silent
763 iSpuAsyncWait |= skip_block(ch);
764 if(start == s_chan[ch].pCurr)
767 dwChannelDead |= 1<<ch;
772 s_chan[ch].spos -= 28 * 0x10000;
776 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
784 //---------------------------------------------------//
785 //- here we have another 1 ms of sound data
786 //---------------------------------------------------//
787 // mix XA infos (if any)
791 ///////////////////////////////////////////////////////
792 // mix all channels (including reverb) into one buffer
797 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
799 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
803 for (ns = 0; ns < NSSIZE*2; )
805 d = SSumLR[ns]; SSumLR[ns] = 0;
806 d = d * volmult >> 10;
811 d = SSumLR[ns]; SSumLR[ns] = 0;
812 d = d * volmult >> 10;
818 //////////////////////////////////////////////////////
819 // special irq handling in the decode buffers (0x0000-0x1000)
821 // the decode buffers are located in spu memory in the following way:
822 // 0x0000-0x03ff CD audio left
823 // 0x0400-0x07ff CD audio right
824 // 0x0800-0x0bff Voice 1
825 // 0x0c00-0x0fff Voice 3
826 // and decoded data is 16 bit for one sample
828 // even if voices 1/3 are off or no cd audio is playing, the internal
829 // play positions will move on and wrap after 0x400 bytes.
830 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
831 // increase this pointer on each sample by 2 bytes. If this pointer
832 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
833 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
834 // in some of Peops timer modes. So: we ignore this option here (for now).
838 for(ns=0;ns<NSSIZE;ns++)
840 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
844 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
848 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
855 // wanna have around 1/60 sec (16.666 ms) updates
856 if (iCycle++ > 16/FRAG_MSECS)
858 SoundFeedStreamData((unsigned char *)pSpuBuffer,
859 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
860 pS = (short *)pSpuBuffer;
868 // SPU ASYNC... even newer epsxe func
869 // 1 time every 'cycle' cycles... harhar
871 // rearmed: called every 2ms now
873 void CALLBACK SPUasync(unsigned long cycle)
878 if(iSpuAsyncWait<=16/FRAG_MSECS) return;
882 if(!bSpuInit) return; // -> no init, no call
886 // abuse iSpuAsyncWait mechanism to reduce calls to above function
887 // to make it do larger chunks
888 // note: doing it less often than once per frame causes skips
892 // SPU UPDATE... new epsxe func
893 // 1 time every 32 hsync lines
894 // (312/32)x50 in pal
895 // (262/32)x60 in ntsc
897 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
898 // leave that func in the linux port, until epsxe linux is using
899 // the async function as well
901 void CALLBACK SPUupdate(void)
908 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
911 if(!xap->freq) return; // no xa freq ? bye
913 FeedXA(xap); // call main XA feeder
917 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
920 if (nbytes<=0) return -1;
922 return FeedCDDA((unsigned char *)pcm, nbytes);
925 // to be called after state load
926 void ClearWorkingState(void)
928 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
929 memset(iFMod,0,sizeof(iFMod));
930 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
933 // SETUPSTREAMS: init most of the spu buffers
934 void SetupStreams(void)
938 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
940 if(iUseReverb==1) i=88200*2;
943 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
944 memset(sRVBStart,0,i*4);
945 sRVBEnd = sRVBStart + i;
946 sRVBPlay = sRVBStart;
948 XAStart = // alloc xa buffer
949 (uint32_t *)malloc(44100 * sizeof(uint32_t));
950 XAEnd = XAStart + 44100;
954 CDDAStart = // alloc cdda buffer
955 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
956 CDDAEnd = CDDAStart + 16384;
957 CDDAPlay = CDDAStart;
958 CDDAFeed = CDDAStart;
960 for(i=0;i<MAXCHAN;i++) // loop sound channels
962 // we don't use mutex sync... not needed, would only
964 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
965 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
966 s_chan[i].pLoop=spuMemC;
967 s_chan[i].pCurr=spuMemC;
970 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
974 bSpuInit=1; // flag: we are inited
977 // REMOVESTREAMS: free most buffer
978 void RemoveStreams(void)
980 free(pSpuBuffer); // free mixing buffer
982 free(sRVBStart); // free reverb buffer
984 free(XAStart); // free XA buffer
986 free(CDDAStart); // free CDDA buffer
992 // SPUINIT: this func will be called first by the main emu
993 long CALLBACK SPUinit(void)
995 spuMemC = (unsigned char *)spuMem; // just small setup
996 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1000 spuAddr = 0xffffffff;
1001 spuMemC = (unsigned char *)spuMem;
1003 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1008 SetupStreams(); // prepare streaming
1013 // SPUOPEN: called by main emu after init
1014 long CALLBACK SPUopen(void)
1016 if (bSPUIsOpen) return 0; // security for some stupid main emus
1018 SetupSound(); // setup sound (before init!)
1022 return PSE_SPU_ERR_SUCCESS;
1025 // SPUCLOSE: called before shutdown
1026 long CALLBACK SPUclose(void)
1028 if (!bSPUIsOpen) return 0; // some security
1030 bSPUIsOpen = 0; // no more open
1032 RemoveSound(); // no more sound handling
1037 // SPUSHUTDOWN: called by main emu on final exit
1038 long CALLBACK SPUshutdown(void)
1041 RemoveStreams(); // no more streaming
1047 // SPUTEST: we don't test, we are always fine ;)
1048 long CALLBACK SPUtest(void)
1053 // SPUCONFIGURE: call config dialog
1054 long CALLBACK SPUconfigure(void)
1059 // StartCfgTool("CFG");
1064 // SPUABOUT: show about window
1065 void CALLBACK SPUabout(void)
1070 // StartCfgTool("ABOUT");
1075 // this functions will be called once,
1076 // passes a callback that should be called on SPU-IRQ/cdda volume change
1077 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1079 irqCallback = callback;
1082 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1084 cddavCallback = CDDAVcallback;
1087 // COMMON PLUGIN INFO FUNCS
1089 char * CALLBACK PSEgetLibName(void)
1091 return _(libraryName);
1094 unsigned long CALLBACK PSEgetLibType(void)
1099 unsigned long CALLBACK PSEgetLibVersion(void)
1101 return (1 << 16) | (6 << 8);
1104 char * SPUgetLibInfos(void)
1106 return _(libraryInfo);
1111 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1113 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1115 for(;ch<MAXCHAN;ch++)
1117 if (!(dwChannelOn & (1<<ch)))
1119 if (s_chan[ch].bFMod == 2)
1120 fmod_chans |= 1 << ch;
1121 if (s_chan[ch].bNoise)
1122 noise_chans |= 1 << ch;
1123 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1124 irq_chans |= 1 << ch;
1127 *chans_out = dwChannelOn;
1128 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1129 *fmod_chans_out = fmod_chans;
1130 *noise_chans_out = noise_chans;
1133 // vim:shiftwidth=1:expandtab