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"
28 #include "dsoundoss.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; \
51 #if defined (USEMACOSX)
52 static char * libraryName = N_("Mac OS X Sound");
53 #elif defined (USEALSA)
54 static char * libraryName = N_("ALSA Sound");
55 #elif defined (USEOSS)
56 static char * libraryName = N_("OSS Sound");
57 #elif defined (USESDL)
58 static char * libraryName = N_("SDL Sound");
59 #elif defined (USEPULSEAUDIO)
60 static char * libraryName = N_("PulseAudio Sound");
62 static char * libraryName = N_("NULL Sound");
65 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
70 // psx buffer / addresses
72 unsigned short regArea[10000];
73 unsigned short spuMem[256*1024];
74 unsigned char * spuMemC;
75 unsigned char * pSpuIrq=0;
76 unsigned char * pSpuBuffer;
77 unsigned char * pMixIrq=0;
81 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;
99 unsigned short spuCtrl=0; // some vars to store psx reg infos
100 unsigned short spuStat=0;
101 unsigned short spuIrq=0;
102 unsigned long spuAddr=0xffffffff; // address into spu mem
103 int bEndThread=0; // thread handlers
108 static pthread_t thread = (pthread_t)-1; // thread id (linux)
110 unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
111 unsigned int dwChannelOn=0; // not silent channels
112 unsigned int dwPendingChanOff=0;
113 unsigned int dwChannelDead=0; // silent+not useful channels
115 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
116 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
118 // certain globals (were local before, but with the new timeproc I need em global)
120 static const int f[8][2] = { { 0, 0 },
125 int ChanBuf[NSSIZE+3];
126 int SSumLR[(NSSIZE+3)*2];
131 int lastch=-1; // last channel processed on spu irq in timer mode
132 static int lastns=0; // last ns pos
133 static int iSecureStart=0; // secure start counter
135 ////////////////////////////////////////////////////////////////////////
137 ////////////////////////////////////////////////////////////////////////
139 // dirty inline func includes
144 ////////////////////////////////////////////////////////////////////////
145 // helpers for simple interpolation
148 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
150 // instead of having n equal sample values in a row like:
154 // we compare the current delta change with the next delta change.
156 // if curr_delta is positive,
158 // - and next delta is smaller (or changing direction):
162 // - and next delta significant (at least twice) bigger:
166 // - and next delta is nearly same:
171 // if curr_delta is negative,
173 // - and next delta is smaller (or changing direction):
177 // - and next delta significant (at least twice) bigger:
181 // - and next delta is nearly same:
187 INLINE void InterpolateUp(int ch)
189 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
191 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
192 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
196 if(id1>0) // curr delta positive
199 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
202 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
204 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
206 else // curr delta negative
209 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
212 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
214 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
218 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
222 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
223 if(s_chan[ch].sinc<=0x8000)
224 s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
225 else s_chan[ch].SB[29]+=s_chan[ch].SB[28];
227 else // no flags? add bigger val (if possible), calc smaller step, set flag1
228 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
232 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
235 INLINE void InterpolateDown(int ch)
237 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
239 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
240 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
241 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
245 ////////////////////////////////////////////////////////////////////////
246 // helpers for gauss interpolation
248 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
249 #define gval(x) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
253 ////////////////////////////////////////////////////////////////////////
257 ////////////////////////////////////////////////////////////////////////
258 // START SOUND... called by main thread to setup a new sound on a channel
259 ////////////////////////////////////////////////////////////////////////
261 INLINE void StartSound(int ch)
266 // fussy timing issues - do in VoiceOn
267 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
268 //s_chan[ch].bStop=0;
271 s_chan[ch].SB[26]=0; // init mixing vars
273 s_chan[ch].iSBPos=28;
275 s_chan[ch].SB[29]=0; // init our interpolation helpers
278 if(iUseInterpolation>=2) // gauss interpolation?
279 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
280 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
282 dwNewChannel&=~(1<<ch); // clear new channel bit
285 ////////////////////////////////////////////////////////////////////////
286 // ALL KIND OF HELPERS
287 ////////////////////////////////////////////////////////////////////////
289 INLINE int FModChangeFrequency(int ch,int ns)
291 unsigned int NP=s_chan[ch].iRawPitch;
294 NP=((32768L+iFMod[ns])*NP)/32768L;
296 if(NP>0x3fff) NP=0x3fff;
299 sinc=NP<<4; // calc frequency
300 if(iUseInterpolation==1) // freq change in simple interpolation mode
307 ////////////////////////////////////////////////////////////////////////
309 INLINE void StoreInterpolationVal(int ch,int fa)
311 if(s_chan[ch].bFMod==2) // fmod freq channel
312 s_chan[ch].SB[29]=fa;
317 if(iUseInterpolation>=2) // gauss/cubic interpolation
319 int gpos = s_chan[ch].SB[28];
322 s_chan[ch].SB[28] = gpos;
325 if(iUseInterpolation==1) // simple interpolation
327 s_chan[ch].SB[28] = 0;
328 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'
329 s_chan[ch].SB[30] = s_chan[ch].SB[31];
330 s_chan[ch].SB[31] = fa;
331 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
333 else s_chan[ch].SB[29]=fa; // no interpolation
337 ////////////////////////////////////////////////////////////////////////
339 INLINE int iGetInterpolationVal(int ch)
343 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
345 switch(iUseInterpolation)
347 //--------------------------------------------------//
348 case 3: // cubic interpolation
351 xd = ((s_chan[ch].spos) >> 1)+1;
352 gpos = s_chan[ch].SB[28];
354 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
355 fa *= (xd - (2<<15)) / 6;
357 fa += gval(2) - gval(1) - gval(1) + gval0;
358 fa *= (xd - (1<<15)) >> 1;
360 fa += gval(1) - gval0;
366 //--------------------------------------------------//
367 case 2: // gauss interpolation
370 vl = (s_chan[ch].spos >> 6) & ~3;
371 gpos = s_chan[ch].SB[28];
372 vr=(gauss[vl]*gval0)&~2047;
373 vr+=(gauss[vl+1]*gval(1))&~2047;
374 vr+=(gauss[vl+2]*gval(2))&~2047;
375 vr+=(gauss[vl+3]*gval(3))&~2047;
378 //--------------------------------------------------//
379 case 1: // simple interpolation
381 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
382 InterpolateUp(ch); // --> interpolate up
383 else InterpolateDown(ch); // --> else down
384 fa=s_chan[ch].SB[29];
386 //--------------------------------------------------//
387 default: // no interpolation
389 fa=s_chan[ch].SB[29];
391 //--------------------------------------------------//
397 static void do_irq(void)
399 if(!(spuStat & STAT_IRQ))
402 if(irqCallback) irqCallback();
406 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
409 int fa, s_1, s_2, d, s;
414 for (nSample = 0; nSample < 28; src++)
417 s = (int)(signed short)((d & 0x0f) << 12);
419 fa = s >> shift_factor;
420 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
423 dest[nSample++] = fa;
425 s = (int)(signed short)((d & 0xf0) << 8);
426 fa = s >> shift_factor;
427 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
430 dest[nSample++] = fa;
434 static int decode_block(int ch)
436 unsigned char *start;
437 int predict_nr,shift_factor,flags;
440 start=s_chan[ch].pCurr; // set up the current pos
441 if(dwPendingChanOff&(1<<ch))
443 dwChannelOn&=~(1<<ch); // -> turn everything off
444 dwPendingChanOff&=~(1<<ch);
446 s_chan[ch].ADSRX.EnvelopeVol=0;
449 //////////////////////////////////////////// irq check
453 if(pSpuIrq == start) // irq address reached?
455 do_irq(); // -> call main emu
460 predict_nr=(int)start[0];
461 shift_factor=predict_nr&0xf;
464 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
466 //////////////////////////////////////////// flag handler
470 s_chan[ch].pLoop=start; // loop adress
473 if(flags&1) // 1: stop/loop
476 dwPendingChanOff|=1<<ch;
478 start = s_chan[ch].pLoop;
481 if (start - spuMemC >= 0x80000) {
484 printf("ch%d oflow\n", ch);
487 s_chan[ch].pCurr = start; // store values for next cycle
488 s_chan[ch].bJump = flags & 1;
493 // do block, but ignore sample data
494 static int skip_block(int ch)
496 unsigned char *start = s_chan[ch].pCurr;
497 int flags = start[1];
507 s_chan[ch].pLoop = start;
509 s_chan[ch].pCurr += 16;
512 s_chan[ch].pCurr = s_chan[ch].pLoop;
514 s_chan[ch].bJump = flags & 1;
518 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
519 static int do_samples_##name(int ch, int ns, int ns_to) \
521 int sinc = s_chan[ch].sinc; \
522 int spos = s_chan[ch].spos; \
523 int sbpos = s_chan[ch].iSBPos; \
524 int *SB = s_chan[ch].SB; \
529 for (; ns < ns_to; ns++) \
533 while (spos >= 0x10000) \
538 d = decode_block(ch); \
539 if(d && iSPUIRQWait) \
556 s_chan[ch].sinc = sinc; \
557 s_chan[ch].spos = spos; \
558 s_chan[ch].iSBPos = sbpos; \
564 #define fmod_recv_check \
565 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
566 sinc = FModChangeFrequency(ch,ns)
568 make_do_samples(default, fmod_recv_check, ,
569 StoreInterpolationVal(ch, fa),
570 ChanBuf[ns] = iGetInterpolationVal(ch), )
571 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
573 #define simple_interp_store \
574 s_chan[ch].SB[28] = 0; \
575 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
576 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
577 s_chan[ch].SB[31] = fa; \
578 s_chan[ch].SB[32] = 1
580 #define simple_interp_get \
581 if(sinc<0x10000) /* -> upsampling? */ \
582 InterpolateUp(ch); /* --> interpolate up */ \
583 else InterpolateDown(ch); /* --> else down */ \
584 ChanBuf[ns] = s_chan[ch].SB[29]
586 make_do_samples(simple, , ,
587 simple_interp_store, simple_interp_get, )
589 static int do_samples_noise(int ch, int ns, int ns_to)
591 int level, shift, bit;
593 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
594 while (s_chan[ch].spos >= 28*0x10000)
597 s_chan[ch].spos -= 28*0x10000;
600 // modified from DrHell/shalma, no fraction
601 level = (spuCtrl >> 10) & 0x0f;
602 level = 0x8000 >> level;
604 for (; ns < ns_to; ns++)
607 if (dwNoiseCount >= level)
609 dwNoiseCount -= level;
610 shift = (dwNoiseVal >> 10) & 0x1f;
611 bit = (0x69696969 >> shift) & 1;
612 if (dwNoiseVal & 0x8000)
614 dwNoiseVal = (dwNoiseVal << 1) | bit;
617 ChanBuf[ns] = (signed short)dwNoiseVal;
623 #ifdef __ARM_ARCH_7A__
625 extern void mix_chan(int start, int count, int lv, int rv);
626 extern void mix_chan_rvb(int start, int count, int lv, int rv);
628 static void mix_chan(int start, int count, int lv, int rv)
630 int *dst = SSumLR + start * 2;
631 const int *src = ChanBuf + start;
638 l = (sval * lv) >> 14;
639 r = (sval * rv) >> 14;
645 static void mix_chan_rvb(int start, int count, int lv, int rv)
647 int *dst = SSumLR + start * 2;
648 int *drvb = sRVBStart + start * 2;
649 const int *src = ChanBuf + start;
656 l = (sval * lv) >> 14;
657 r = (sval * rv) >> 14;
666 ////////////////////////////////////////////////////////////////////////
668 // here is the main job handler... thread, timer or direct func call
669 // basically the whole sound processing is done in this fat func!
670 ////////////////////////////////////////////////////////////////////////
672 // 5 ms waiting phase, if buffer is full and no new sound has to get started
673 // .. can be made smaller (smallest val: 1 ms), but bigger waits give
674 // better performance
679 ////////////////////////////////////////////////////////////////////////
681 static void *MAINThread(void *arg)
683 int volmult = iVolume;
684 int ns,ns_from,ns_to;
688 while(!bEndThread) // until we are shutting down
690 // ok, at the beginning we are looking if there is
691 // enuff free place in the dsound/oss buffer to
692 // fill in new data, or if there is a new channel to start.
693 // if not, we wait (thread) or return (timer/spuasync)
694 // until enuff free place is available/a new channel gets
697 if(dwNewChannel) // new channel should start immedately?
698 { // (at least one bit 0 ... MAXCHANNEL is set?)
699 iSecureStart++; // -> set iSecure
700 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)
702 else iSecureStart=0; // 0: no new channel should start
704 while(!iSecureStart && !bEndThread && // no new start? no thread end?
705 (SoundGetBytesBuffered()>TESTSIZE)) // and still enuff data in sound buffer?
707 iSecureStart=0; // reset secure
709 if(iUseTimer) return 0; // linux no-thread mode? bye
710 usleep(PAUSE_L); // else sleep for x ms (linux)
712 if(dwNewChannel) iSecureStart=1; // if a new channel kicks in (or, of course, sound buffer runs low), we will leave the loop
715 //--------------------------------------------------// continue from irq handling in timer mode?
720 if(lastch>=0) // will be -1 if no continue is pending
722 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
725 silentch=~(dwChannelOn|dwNewChannel);
727 //--------------------------------------------------//
728 //- main channel loop -//
729 //--------------------------------------------------//
731 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
733 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
734 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
736 if(s_chan[ch].bNoise)
737 d=do_samples_noise(ch, ns_from, ns_to);
738 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
739 d=do_samples_noint(ch, ns_from, ns_to);
740 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
741 d=do_samples_simple(ch, ns_from, ns_to);
743 d=do_samples_default(ch, ns_from, ns_to);
753 MixADSR(ch, ns_from, ns_to);
755 if(s_chan[ch].bFMod==2) // fmod freq channel
756 memcpy(iFMod, ChanBuf, sizeof(iFMod));
757 else if(s_chan[ch].bRVBActive)
758 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
760 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
764 // advance "stopped" channels that can cause irqs
765 // (all chans are always playing on the real thing..)
766 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
767 for(ch=0;ch<MAXCHAN;ch++)
769 if(!(silentch&(1<<ch))) continue; // already handled
770 if(dwChannelDead&(1<<ch)) continue;
771 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
774 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
775 while(s_chan[ch].spos >= 28 * 0x10000)
777 unsigned char *start = s_chan[ch].pCurr;
779 // no need for bIRQReturn since the channel is silent
780 iSpuAsyncWait |= skip_block(ch);
781 if(start == s_chan[ch].pCurr)
784 dwChannelDead |= 1<<ch;
789 s_chan[ch].spos -= 28 * 0x10000;
793 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
799 DWORD dwWatchTime=timeGetTime_spu()+2500;
801 while(iSpuAsyncWait && !bEndThread &&
802 timeGetTime_spu()<dwWatchTime)
813 //---------------------------------------------------//
814 //- here we have another 1 ms of sound data
815 //---------------------------------------------------//
816 // mix XA infos (if any)
820 ///////////////////////////////////////////////////////
821 // mix all channels (including reverb) into one buffer
826 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
828 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
832 for (ns = 0; ns < NSSIZE*2; )
834 d = SSumLR[ns]; SSumLR[ns] = 0;
835 d = d * volmult >> 10;
840 d = SSumLR[ns]; SSumLR[ns] = 0;
841 d = d * volmult >> 10;
847 //////////////////////////////////////////////////////
848 // special irq handling in the decode buffers (0x0000-0x1000)
850 // the decode buffers are located in spu memory in the following way:
851 // 0x0000-0x03ff CD audio left
852 // 0x0400-0x07ff CD audio right
853 // 0x0800-0x0bff Voice 1
854 // 0x0c00-0x0fff Voice 3
855 // and decoded data is 16 bit for one sample
857 // even if voices 1/3 are off or no cd audio is playing, the internal
858 // play positions will move on and wrap after 0x400 bytes.
859 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
860 // increase this pointer on each sample by 2 bytes. If this pointer
861 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
862 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
863 // in some of Peops timer modes. So: we ignore this option here (for now).
867 for(ns=0;ns<NSSIZE;ns++)
869 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
873 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
877 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
884 // wanna have around 1/60 sec (16.666 ms) updates
885 if (iCycle++ > 16/FRAG_MSECS)
887 SoundFeedStreamData((unsigned char *)pSpuBuffer,
888 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
889 pS = (short *)pSpuBuffer;
894 // end of big main loop...
901 // SPU ASYNC... even newer epsxe func
902 // 1 time every 'cycle' cycles... harhar
904 void CALLBACK SPUasync(unsigned long cycle)
909 if(iSpuAsyncWait<=16/FRAG_MSECS) return;
913 if(iUseTimer==2) // special mode, only used in Linux by this spu (or if you enable the experimental Windows mode)
915 if(!bSpuInit) return; // -> no init, no call
917 MAINThread(0); // -> linux high-compat mode
919 // abuse iSpuAsyncWait mechanism to reduce calls to above function
920 // to make it do larger chunks
921 // note: doing it less often than once per frame causes skips
926 // SPU UPDATE... new epsxe func
927 // 1 time every 32 hsync lines
928 // (312/32)x50 in pal
929 // (262/32)x60 in ntsc
931 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
932 // leave that func in the linux port, until epsxe linux is using
933 // the async function as well
935 void CALLBACK SPUupdate(void)
942 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
945 if(!xap->freq) return; // no xa freq ? bye
947 FeedXA(xap); // call main XA feeder
951 void CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
954 if (nbytes<=0) return;
956 FeedCDDA((unsigned char *)pcm, nbytes);
959 // SETUPTIMER: init of certain buffers and threads/timers
960 void SetupTimer(void)
962 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
963 memset(iFMod,0,NSSIZE*sizeof(int));
964 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
966 bEndThread=0; // init thread vars
968 bSpuInit=1; // flag: we are inited
970 if(!iUseTimer) // linux: use thread
972 pthread_create(&thread, NULL, MAINThread, NULL);
976 // REMOVETIMER: kill threads/timers
977 void RemoveTimer(void)
979 bEndThread=1; // raise flag to end thread
981 if(!iUseTimer) // linux tread?
984 while(!bThreadEnded && i<2000) {usleep(1000L);i++;} // -> wait until thread has ended
985 if(thread!=(pthread_t)-1) {pthread_cancel(thread);thread=(pthread_t)-1;} // -> cancel thread anyway
988 bThreadEnded=0; // no more spu is running
992 // SETUPSTREAMS: init most of the spu buffers
993 void SetupStreams(void)
997 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
999 if(iUseReverb==1) i=88200*2;
1002 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
1003 memset(sRVBStart,0,i*4);
1004 sRVBEnd = sRVBStart + i;
1005 sRVBPlay = sRVBStart;
1007 XAStart = // alloc xa buffer
1008 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1009 XAEnd = XAStart + 44100;
1013 CDDAStart = // alloc cdda buffer
1014 (uint32_t *)malloc(16384 * sizeof(uint32_t));
1015 CDDAEnd = CDDAStart + 16384;
1016 CDDAPlay = CDDAStart;
1017 CDDAFeed = CDDAStart;
1019 for(i=0;i<MAXCHAN;i++) // loop sound channels
1021 // we don't use mutex sync... not needed, would only
1023 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
1024 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1025 s_chan[i].pLoop=spuMemC;
1026 s_chan[i].pCurr=spuMemC;
1029 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
1032 // REMOVESTREAMS: free most buffer
1033 void RemoveStreams(void)
1035 free(pSpuBuffer); // free mixing buffer
1037 free(sRVBStart); // free reverb buffer
1039 free(XAStart); // free XA buffer
1041 free(CDDAStart); // free CDDA buffer
1047 // SPUINIT: this func will be called first by the main emu
1048 long CALLBACK SPUinit(void)
1050 spuMemC = (unsigned char *)spuMem; // just small setup
1051 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1055 spuAddr = 0xffffffff;
1058 spuMemC = (unsigned char *)spuMem;
1060 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1065 //ReadConfigSPU(); // read user stuff
1066 SetupStreams(); // prepare streaming
1071 // SPUOPEN: called by main emu after init
1072 long CALLBACK SPUopen(void)
1074 if (bSPUIsOpen) return 0; // security for some stupid main emus
1076 SetupSound(); // setup sound (before init!)
1077 SetupTimer(); // timer for feeding data
1081 return PSE_SPU_ERR_SUCCESS;
1084 // SPUCLOSE: called before shutdown
1085 long CALLBACK SPUclose(void)
1087 if (!bSPUIsOpen) return 0; // some security
1089 bSPUIsOpen = 0; // no more open
1091 RemoveTimer(); // no more feeding
1092 RemoveSound(); // no more sound handling
1097 // SPUSHUTDOWN: called by main emu on final exit
1098 long CALLBACK SPUshutdown(void)
1101 RemoveStreams(); // no more streaming
1106 // SPUTEST: we don't test, we are always fine ;)
1107 long CALLBACK SPUtest(void)
1112 // SPUCONFIGURE: call config dialog
1113 long CALLBACK SPUconfigure(void)
1118 // StartCfgTool("CFG");
1123 // SPUABOUT: show about window
1124 void CALLBACK SPUabout(void)
1129 // StartCfgTool("ABOUT");
1134 // this functions will be called once,
1135 // passes a callback that should be called on SPU-IRQ/cdda volume change
1136 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1138 irqCallback = callback;
1141 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1143 cddavCallback = CDDAVcallback;
1146 // COMMON PLUGIN INFO FUNCS
1148 char * CALLBACK PSEgetLibName(void)
1150 return _(libraryName);
1153 unsigned long CALLBACK PSEgetLibType(void)
1158 unsigned long CALLBACK PSEgetLibVersion(void)
1160 return (1 << 16) | (6 << 8);
1163 char * SPUgetLibInfos(void)
1165 return _(libraryInfo);
1170 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1172 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1174 for(;ch<MAXCHAN;ch++)
1176 if (!(dwChannelOn & (1<<ch)))
1178 if (s_chan[ch].bFMod == 2)
1179 fmod_chans |= 1 << ch;
1180 if (s_chan[ch].bNoise)
1181 noise_chans |= 1 << ch;
1182 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1183 irq_chans |= 1 << ch;
1186 *chans_out = dwChannelOn;
1187 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1188 *fmod_chans_out = fmod_chans;
1189 *noise_chans_out = noise_chans;
1192 // vim:shiftwidth=1:expandtab