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"
34 #define _(x) gettext(x)
42 #define ssat32_to_16(v) \
43 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
45 #define ssat32_to_16(v) do { \
46 if (v < -32768) v = -32768; \
47 else if (v > 32767) v = 32767; \
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;
89 int iUseInterpolation=2;
91 // MAIN infos struct for each channel
93 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
96 unsigned long dwNoiseVal=1; // global noise generator
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 long dwNewChannel=0; // flags for faster testing, if new channel starts
111 unsigned long dwChannelOn=0;
112 unsigned long dwPendingChanOff=0;
114 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
115 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
117 // certain globals (were local before, but with the new timeproc I need em global)
119 static const int f[8][2] = { { 0, 0 },
125 int SSumLR[NSSIZE*2];
130 int lastch=-1; // last channel processed on spu irq in timer mode
131 static int lastns=0; // last ns pos
132 static int iSecureStart=0; // secure start counter
134 ////////////////////////////////////////////////////////////////////////
136 ////////////////////////////////////////////////////////////////////////
138 // dirty inline func includes
143 ////////////////////////////////////////////////////////////////////////
144 // helpers for simple interpolation
147 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
149 // instead of having n equal sample values in a row like:
153 // we compare the current delta change with the next delta change.
155 // if curr_delta is positive,
157 // - and next delta is smaller (or changing direction):
161 // - and next delta significant (at least twice) bigger:
165 // - and next delta is nearly same:
170 // if curr_delta is negative,
172 // - and next delta is smaller (or changing direction):
176 // - and next delta significant (at least twice) bigger:
180 // - and next delta is nearly same:
186 INLINE void InterpolateUp(int ch)
188 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
190 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
191 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
195 if(id1>0) // curr delta positive
198 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
201 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
203 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
205 else // curr delta negative
208 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
211 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
213 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
217 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
221 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
222 if(s_chan[ch].sinc<=0x8000)
223 s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
224 else 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].s_1=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 void VoiceChangeFrequency(int ch)
290 s_chan[ch].iUsedFreq=s_chan[ch].iActFreq; // -> take it and calc steps
291 s_chan[ch].sinc=s_chan[ch].iRawPitch<<4;
292 if(!s_chan[ch].sinc) s_chan[ch].sinc=1;
293 if(iUseInterpolation==1) s_chan[ch].SB[32]=1; // -> freq change in simle imterpolation mode: set flag
296 ////////////////////////////////////////////////////////////////////////
298 INLINE int FModChangeFrequency(int ch,int ns)
300 int NP=s_chan[ch].iRawPitch;
303 NP=((32768L+iFMod[ns])*NP)/32768L;
305 if(NP>0x3fff) NP=0x3fff;
308 NP=(44100L*NP)/(4096L); // calc frequency
310 s_chan[ch].iActFreq=NP;
311 s_chan[ch].iUsedFreq=NP;
312 sinc=(((NP/10)<<16)/4410);
314 if(iUseInterpolation==1) // freq change in simple interpolation mode
321 ////////////////////////////////////////////////////////////////////////
323 // noise handler... just produces some noise data
324 // surely wrong... and no noise frequency (spuCtrl&0x3f00) will be used...
325 // and sometimes the noise will be used as fmod modulation... pfff
327 INLINE int iGetNoiseVal(int ch)
331 if((dwNoiseVal<<=1)&0x80000000L)
333 dwNoiseVal^=0x0040001L;
334 fa=((dwNoiseVal>>2)&0x7fff);
337 else fa=(dwNoiseVal>>2)&0x7fff;
339 // mmm... depending on the noise freq we allow bigger/smaller changes to the previous val
340 fa=s_chan[ch].iOldNoise+((fa-s_chan[ch].iOldNoise)/((0x001f-((spuCtrl&0x3f00)>>9))+1));
341 if(fa>32767L) fa=32767L;
342 if(fa<-32767L) fa=-32767L;
343 s_chan[ch].iOldNoise=fa;
345 if(iUseInterpolation<2) // no gauss/cubic interpolation?
346 s_chan[ch].SB[29] = fa; // -> store noise val in "current sample" slot
350 ////////////////////////////////////////////////////////////////////////
352 INLINE void StoreInterpolationVal(int ch,int fa)
354 if(s_chan[ch].bFMod==2) // fmod freq channel
355 s_chan[ch].SB[29]=fa;
358 if((spuCtrl&0x4000)==0) fa=0; // muted?
361 if(fa>32767L) fa=32767L;
362 if(fa<-32767L) fa=-32767L;
365 if(iUseInterpolation>=2) // gauss/cubic interpolation
367 int gpos = s_chan[ch].SB[28];
370 s_chan[ch].SB[28] = gpos;
373 if(iUseInterpolation==1) // simple interpolation
375 s_chan[ch].SB[28] = 0;
376 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'
377 s_chan[ch].SB[30] = s_chan[ch].SB[31];
378 s_chan[ch].SB[31] = fa;
379 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
381 else s_chan[ch].SB[29]=fa; // no interpolation
385 ////////////////////////////////////////////////////////////////////////
387 INLINE int iGetInterpolationVal(int ch)
391 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
393 switch(iUseInterpolation)
395 //--------------------------------------------------//
396 case 3: // cubic interpolation
399 xd = ((s_chan[ch].spos) >> 1)+1;
400 gpos = s_chan[ch].SB[28];
402 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
403 fa *= (xd - (2<<15)) / 6;
405 fa += gval(2) - gval(1) - gval(1) + gval0;
406 fa *= (xd - (1<<15)) >> 1;
408 fa += gval(1) - gval0;
414 //--------------------------------------------------//
415 case 2: // gauss interpolation
418 vl = (s_chan[ch].spos >> 6) & ~3;
419 gpos = s_chan[ch].SB[28];
420 vr=(gauss[vl]*gval0)&~2047;
421 vr+=(gauss[vl+1]*gval(1))&~2047;
422 vr+=(gauss[vl+2]*gval(2))&~2047;
423 vr+=(gauss[vl+3]*gval(3))&~2047;
426 //--------------------------------------------------//
427 case 1: // simple interpolation
429 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
430 InterpolateUp(ch); // --> interpolate up
431 else InterpolateDown(ch); // --> else down
432 fa=s_chan[ch].SB[29];
434 //--------------------------------------------------//
435 default: // no interpolation
437 fa=s_chan[ch].SB[29];
439 //--------------------------------------------------//
445 static void do_irq(void)
447 if(!(spuStat & STAT_IRQ))
450 if(irqCallback) irqCallback();
454 static int decode_block(int ch)
456 unsigned char *start;
457 unsigned int nSample;
458 int predict_nr,shift_factor,flags,d,s;
464 start=s_chan[ch].pCurr; // set up the current pos
465 if(start == (unsigned char*)-1 || // special "stop" sign
466 (dwPendingChanOff&(1<<ch)))
468 dwChannelOn&=~(1<<ch); // -> turn everything off
469 dwPendingChanOff&=~(1<<ch);
471 s_chan[ch].ADSRX.EnvelopeVol=0;
472 return 0; // -> and done for this channel
475 //////////////////////////////////////////// irq check
479 if(pSpuIrq == start) // irq address reached?
481 do_irq(); // -> call main emu
489 predict_nr=(int)*start;start++;
490 shift_factor=predict_nr&0xf;
492 flags=(int)*start;start++;
494 // -------------------------------------- //
496 for (nSample=0;nSample<28;start++)
500 if(s&0x8000) s|=0xffff0000;
502 fa=(s >> shift_factor);
503 fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
507 s_chan[ch].SB[nSample++]=fa;
509 if(s&0x8000) s|=0xffff0000;
510 fa=(s>>shift_factor);
511 fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
514 s_chan[ch].SB[nSample++]=fa;
517 //////////////////////////////////////////// flag handler
519 if((flags&4) && (!s_chan[ch].bIgnoreLoop))
520 s_chan[ch].pLoop=start-16; // loop adress
522 if(flags&1) // 1: stop/loop
525 dwPendingChanOff|=1<<ch;
527 start = s_chan[ch].pLoop;
530 if (start - spuMemC >= 0x80000)
531 start = (unsigned char*)-1;
533 s_chan[ch].pCurr=start; // store values for next cycle
540 // do block, but ignore sample data
541 static int skip_block(int ch)
543 unsigned char *start = s_chan[ch].pCurr;
544 int flags = start[1];
547 // Tron Bonne hack, probably wrong (could be wrong memory contents..)
548 if(flags & ~7) flags = 0;
556 if((flags & 4) && !s_chan[ch].bIgnoreLoop)
557 s_chan[ch].pLoop=start;
559 s_chan[ch].pCurr += 16;
562 s_chan[ch].pCurr = s_chan[ch].pLoop;
567 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
568 static int do_samples_##name(int ch, int ns, int ns_to) \
570 int sinc = s_chan[ch].sinc; \
571 int spos = s_chan[ch].spos; \
576 for (; ns < ns_to; ns++) \
580 while (spos >= 0x10000) \
582 if(s_chan[ch].iSBPos == 28) \
584 d = decode_block(ch); \
585 if(d && iSPUIRQWait) \
592 fa = s_chan[ch].SB[s_chan[ch].iSBPos++]; \
602 s_chan[ch].sinc = sinc; \
603 s_chan[ch].spos = spos; \
609 #define fmod_recv_check \
610 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
611 sinc = FModChangeFrequency(ch,ns)
613 make_do_samples(default, fmod_recv_check, ,
614 StoreInterpolationVal(ch, fa),
615 ChanBuf[ns] = iGetInterpolationVal(ch), )
616 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
618 #define simple_interp_store \
619 s_chan[ch].SB[28] = 0; \
620 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
621 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
622 s_chan[ch].SB[31] = fa; \
623 s_chan[ch].SB[32] = 1
625 #define simple_interp_get \
626 if(sinc<0x10000) /* -> upsampling? */ \
627 InterpolateUp(ch); /* --> interpolate up */ \
628 else InterpolateDown(ch); /* --> else down */ \
629 ChanBuf[ns] = s_chan[ch].SB[29]
631 make_do_samples(simple, , ,
632 simple_interp_store, simple_interp_get, )
634 static int do_samples_noise(int ch, int ns, int ns_to)
636 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
637 while (s_chan[ch].spos >= 28*0x10000)
640 s_chan[ch].spos -= 28*0x10000;
643 for (; ns < ns_to; ns++)
644 ChanBuf[ns] = iGetNoiseVal(ch);
649 ////////////////////////////////////////////////////////////////////////
651 // here is the main job handler... thread, timer or direct func call
652 // basically the whole sound processing is done in this fat func!
653 ////////////////////////////////////////////////////////////////////////
655 // 5 ms waiting phase, if buffer is full and no new sound has to get started
656 // .. can be made smaller (smallest val: 1 ms), but bigger waits give
657 // better performance
662 ////////////////////////////////////////////////////////////////////////
664 static void *MAINThread(void *arg)
666 int ns,ns_from,ns_to;
667 #if !defined(_MACOSX) && !defined(__arm__)
668 int voldiv = iVolume;
670 const int voldiv = 2;
675 while(!bEndThread) // until we are shutting down
677 // ok, at the beginning we are looking if there is
678 // enuff free place in the dsound/oss buffer to
679 // fill in new data, or if there is a new channel to start.
680 // if not, we wait (thread) or return (timer/spuasync)
681 // until enuff free place is available/a new channel gets
684 if(dwNewChannel) // new channel should start immedately?
685 { // (at least one bit 0 ... MAXCHANNEL is set?)
686 iSecureStart++; // -> set iSecure
687 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)
689 else iSecureStart=0; // 0: no new channel should start
691 while(!iSecureStart && !bEndThread && // no new start? no thread end?
692 (SoundGetBytesBuffered()>TESTSIZE)) // and still enuff data in sound buffer?
694 iSecureStart=0; // reset secure
696 if(iUseTimer) return 0; // linux no-thread mode? bye
697 usleep(PAUSE_L); // else sleep for x ms (linux)
699 if(dwNewChannel) iSecureStart=1; // if a new channel kicks in (or, of course, sound buffer runs low), we will leave the loop
702 //--------------------------------------------------// continue from irq handling in timer mode?
707 if(lastch>=0) // will be -1 if no continue is pending
709 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
712 //--------------------------------------------------//
713 //- main channel loop -//
714 //--------------------------------------------------//
716 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
718 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
719 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
721 if(s_chan[ch].iActFreq!=s_chan[ch].iUsedFreq) // new psx frequency?
722 VoiceChangeFrequency(ch);
724 if(s_chan[ch].bNoise)
725 d=do_samples_noise(ch, ns_from, ns_to);
726 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
727 d=do_samples_noint(ch, ns_from, ns_to);
728 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
729 d=do_samples_simple(ch, ns_from, ns_to);
731 d=do_samples_default(ch, ns_from, ns_to);
739 MixADSR(ch, ns_from, ns_to);
741 if(s_chan[ch].bFMod==2) // fmod freq channel
742 memcpy(iFMod, ChanBuf, sizeof(iFMod));
745 int lv=s_chan[ch].iLeftVolume;
746 int rv=s_chan[ch].iRightVolume;
748 for(ns=ns_from;ns<ns_to;ns++)
750 int sval = ChanBuf[ns];
753 //////////////////////////////////////////////
754 // ok, left/right sound volume (psx volume goes from 0 ... 0x3fff)
761 //////////////////////////////////////////////
762 // now let us store sound data for reverb
764 if(s_chan[ch].bRVBActive) StoreREVERB(ch,ns,l,r);
770 // advance "stopped" channels that can cause irqs
771 // (all chans are always playing on the real thing..)
772 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
773 for(ch=0;ch<MAXCHAN;ch++)
775 if(dwChannelOn&(1<<ch)) continue; // already handled
776 if(s_chan[ch].pCurr == (unsigned char *)-1)
778 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
781 if(s_chan[ch].iActFreq!=s_chan[ch].iUsedFreq) // new psx frequency?
782 VoiceChangeFrequency(ch);
784 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
785 while(s_chan[ch].spos >= 28 * 0x10000)
787 unsigned char *start=s_chan[ch].pCurr;
789 bIRQReturn |= skip_block(ch);
790 if(start == s_chan[ch].pCurr)
793 s_chan[ch].pCurr=(unsigned char *)-1;
797 s_chan[ch].spos -= 28 * 0x10000;
801 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
807 DWORD dwWatchTime=timeGetTime_spu()+2500;
809 while(iSpuAsyncWait && !bEndThread &&
810 timeGetTime_spu()<dwWatchTime)
821 //---------------------------------------------------//
822 //- here we have another 1 ms of sound data
823 //---------------------------------------------------//
824 // mix XA infos (if any)
828 ///////////////////////////////////////////////////////
829 // mix all channels (including reverb) into one buffer
834 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
836 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
840 for (ns = 0; ns < NSSIZE*2; )
842 d = SSumLR[ns] / voldiv; SSumLR[ns] = 0;
847 d = SSumLR[ns] / voldiv; SSumLR[ns] = 0;
853 //////////////////////////////////////////////////////
854 // special irq handling in the decode buffers (0x0000-0x1000)
856 // the decode buffers are located in spu memory in the following way:
857 // 0x0000-0x03ff CD audio left
858 // 0x0400-0x07ff CD audio right
859 // 0x0800-0x0bff Voice 1
860 // 0x0c00-0x0fff Voice 3
861 // and decoded data is 16 bit for one sample
863 // even if voices 1/3 are off or no cd audio is playing, the internal
864 // play positions will move on and wrap after 0x400 bytes.
865 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
866 // increase this pointer on each sample by 2 bytes. If this pointer
867 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
868 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
869 // in some of Peops timer modes. So: we ignore this option here (for now).
873 for(ns=0;ns<NSSIZE;ns++)
875 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
879 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
883 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
890 // wanna have around 1/60 sec (16.666 ms) updates
893 SoundFeedStreamData((unsigned char *)pSpuBuffer,
894 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
895 pS = (short *)pSpuBuffer;
900 // end of big main loop...
907 // SPU ASYNC... even newer epsxe func
908 // 1 time every 'cycle' cycles... harhar
910 void CALLBACK SPUasync(unsigned long cycle)
915 if(iSpuAsyncWait<=16) return;
919 if(iUseTimer==2) // special mode, only used in Linux by this spu (or if you enable the experimental Windows mode)
921 if(!bSpuInit) return; // -> no init, no call
923 MAINThread(0); // -> linux high-compat mode
925 // abuse iSpuAsyncWait mechanism to reduce calls to above function
926 // to make it do larger chunks
927 // note: doing it less often than once per frame causes skips
932 // SPU UPDATE... new epsxe func
933 // 1 time every 32 hsync lines
934 // (312/32)x50 in pal
935 // (262/32)x60 in ntsc
937 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
938 // leave that func in the linux port, until epsxe linux is using
939 // the async function as well
941 void CALLBACK SPUupdate(void)
948 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
951 if(!xap->freq) return; // no xa freq ? bye
953 FeedXA(xap); // call main XA feeder
957 void CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
960 if (nbytes<=0) return;
962 FeedCDDA((unsigned char *)pcm, nbytes);
965 // SETUPTIMER: init of certain buffers and threads/timers
966 void SetupTimer(void)
968 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
969 memset(iFMod,0,NSSIZE*sizeof(int));
970 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
972 bEndThread=0; // init thread vars
974 bSpuInit=1; // flag: we are inited
976 if(!iUseTimer) // linux: use thread
978 pthread_create(&thread, NULL, MAINThread, NULL);
982 // REMOVETIMER: kill threads/timers
983 void RemoveTimer(void)
985 bEndThread=1; // raise flag to end thread
987 if(!iUseTimer) // linux tread?
990 while(!bThreadEnded && i<2000) {usleep(1000L);i++;} // -> wait until thread has ended
991 if(thread!=(pthread_t)-1) {pthread_cancel(thread);thread=(pthread_t)-1;} // -> cancel thread anyway
994 bThreadEnded=0; // no more spu is running
998 // SETUPSTREAMS: init most of the spu buffers
999 void SetupStreams(void)
1003 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
1005 if(iUseReverb==1) i=88200*2;
1008 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
1009 memset(sRVBStart,0,i*4);
1010 sRVBEnd = sRVBStart + i;
1011 sRVBPlay = sRVBStart;
1013 XAStart = // alloc xa buffer
1014 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1015 XAEnd = XAStart + 44100;
1019 CDDAStart = // alloc cdda buffer
1020 (uint32_t *)malloc(16384 * sizeof(uint32_t));
1021 CDDAEnd = CDDAStart + 16384;
1022 CDDAPlay = CDDAStart;
1023 CDDAFeed = CDDAStart;
1025 for(i=0;i<MAXCHAN;i++) // loop sound channels
1027 // we don't use mutex sync... not needed, would only
1029 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
1030 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1031 s_chan[i].pLoop=spuMemC;
1032 s_chan[i].pStart=spuMemC;
1033 s_chan[i].pCurr=spuMemC;
1036 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
1039 // REMOVESTREAMS: free most buffer
1040 void RemoveStreams(void)
1042 free(pSpuBuffer); // free mixing buffer
1044 free(sRVBStart); // free reverb buffer
1046 free(XAStart); // free XA buffer
1048 free(CDDAStart); // free CDDA buffer
1054 // SPUINIT: this func will be called first by the main emu
1055 long CALLBACK SPUinit(void)
1057 spuMemC = (unsigned char *)spuMem; // just small setup
1058 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1063 spuAddr = 0xffffffff;
1066 spuMemC = (unsigned char *)spuMem;
1068 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1073 //ReadConfigSPU(); // read user stuff
1074 SetupStreams(); // prepare streaming
1079 // SPUOPEN: called by main emu after init
1080 long CALLBACK SPUopen(void)
1082 if (bSPUIsOpen) return 0; // security for some stupid main emus
1084 SetupSound(); // setup sound (before init!)
1085 SetupTimer(); // timer for feeding data
1089 return PSE_SPU_ERR_SUCCESS;
1092 // SPUCLOSE: called before shutdown
1093 long CALLBACK SPUclose(void)
1095 if (!bSPUIsOpen) return 0; // some security
1097 bSPUIsOpen = 0; // no more open
1099 RemoveTimer(); // no more feeding
1100 RemoveSound(); // no more sound handling
1105 // SPUSHUTDOWN: called by main emu on final exit
1106 long CALLBACK SPUshutdown(void)
1109 RemoveStreams(); // no more streaming
1114 // SPUTEST: we don't test, we are always fine ;)
1115 long CALLBACK SPUtest(void)
1120 // SPUCONFIGURE: call config dialog
1121 long CALLBACK SPUconfigure(void)
1126 // StartCfgTool("CFG");
1131 // SPUABOUT: show about window
1132 void CALLBACK SPUabout(void)
1137 // StartCfgTool("ABOUT");
1142 // this functions will be called once,
1143 // passes a callback that should be called on SPU-IRQ/cdda volume change
1144 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1146 irqCallback = callback;
1149 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1151 cddavCallback = CDDAVcallback;
1154 // COMMON PLUGIN INFO FUNCS
1156 char * CALLBACK PSEgetLibName(void)
1158 return _(libraryName);
1161 unsigned long CALLBACK PSEgetLibType(void)
1166 unsigned long CALLBACK PSEgetLibVersion(void)
1168 return (1 << 16) | (6 << 8);
1171 char * SPUgetLibInfos(void)
1173 return _(libraryInfo);
1178 void spu_get_debug_info(int *chans_out, int *fmod_chans_out, int *noise_chans_out)
1180 int ch = 0, fmod_chans = 0, noise_chans = 0;
1182 for(;ch<MAXCHAN;ch++)
1184 if (!(dwChannelOn & (1<<ch)))
1186 if (s_chan[ch].bFMod == 2)
1187 fmod_chans |= 1 << ch;
1188 if (s_chan[ch].bNoise)
1189 noise_chans |= 1 << ch;
1192 *chans_out = dwChannelOn;
1193 *fmod_chans_out = fmod_chans;
1194 *noise_chans_out = noise_chans;
1197 // vim:shiftwidth=1:expandtab