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 #if defined (USEMACOSX)
43 static char * libraryName = N_("Mac OS X Sound");
44 #elif defined (USEALSA)
45 static char * libraryName = N_("ALSA Sound");
46 #elif defined (USEOSS)
47 static char * libraryName = N_("OSS Sound");
48 #elif defined (USESDL)
49 static char * libraryName = N_("SDL Sound");
50 #elif defined (USEPULSEAUDIO)
51 static char * libraryName = N_("PulseAudio Sound");
53 static char * libraryName = N_("NULL Sound");
56 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
61 // psx buffer / addresses
63 unsigned short regArea[10000];
64 unsigned short spuMem[256*1024];
65 unsigned char * spuMemC;
66 unsigned char * pSpuIrq=0;
67 unsigned char * pSpuBuffer;
68 unsigned char * pMixIrq=0;
79 int iUseInterpolation=2;
81 // MAIN infos struct for each channel
83 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
86 unsigned long dwNoiseVal=1; // global noise generator
89 unsigned short spuCtrl=0; // some vars to store psx reg infos
90 unsigned short spuStat=0;
91 unsigned short spuIrq=0;
92 unsigned long spuAddr=0xffffffff; // address into spu mem
93 int bEndThread=0; // thread handlers
98 static pthread_t thread = (pthread_t)-1; // thread id (linux)
100 unsigned long dwNewChannel=0; // flags for faster testing, if new channel starts
101 unsigned long dwChannelOn=0;
102 unsigned long dwPendingChanOff=0;
104 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
105 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
107 // certain globals (were local before, but with the new timeproc I need em global)
109 static const int f[8][2] = { { 0, 0 },
115 int SSumLR[NSSIZE*2];
120 int lastch=-1; // last channel processed on spu irq in timer mode
121 static int lastns=0; // last ns pos
122 static int iSecureStart=0; // secure start counter
124 ////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////
128 // dirty inline func includes
133 ////////////////////////////////////////////////////////////////////////
134 // helpers for simple interpolation
137 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
139 // instead of having n equal sample values in a row like:
143 // we compare the current delta change with the next delta change.
145 // if curr_delta is positive,
147 // - and next delta is smaller (or changing direction):
151 // - and next delta significant (at least twice) bigger:
155 // - and next delta is nearly same:
160 // if curr_delta is negative,
162 // - and next delta is smaller (or changing direction):
166 // - and next delta significant (at least twice) bigger:
170 // - and next delta is nearly same:
176 INLINE void InterpolateUp(int ch)
178 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
180 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
181 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
185 if(id1>0) // curr delta positive
188 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
191 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
193 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
195 else // curr delta negative
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;
207 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
211 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
212 if(s_chan[ch].sinc<=0x8000)
213 s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
214 else s_chan[ch].SB[29]+=s_chan[ch].SB[28];
216 else // no flags? add bigger val (if possible), calc smaller step, set flag1
217 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
221 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
224 INLINE void InterpolateDown(int ch)
226 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
228 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
229 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
230 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
234 ////////////////////////////////////////////////////////////////////////
235 // helpers for gauss interpolation
237 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
238 #define gval(x) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
242 ////////////////////////////////////////////////////////////////////////
246 ////////////////////////////////////////////////////////////////////////
247 // START SOUND... called by main thread to setup a new sound on a channel
248 ////////////////////////////////////////////////////////////////////////
250 INLINE void StartSound(int ch)
255 // fussy timing issues - do in VoiceOn
256 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
257 //s_chan[ch].bStop=0;
260 s_chan[ch].s_1=0; // init mixing vars
262 s_chan[ch].iSBPos=28;
264 s_chan[ch].SB[29]=0; // init our interpolation helpers
267 if(iUseInterpolation>=2) // gauss interpolation?
268 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
269 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
271 dwNewChannel&=~(1<<ch); // clear new channel bit
274 ////////////////////////////////////////////////////////////////////////
275 // ALL KIND OF HELPERS
276 ////////////////////////////////////////////////////////////////////////
278 INLINE void VoiceChangeFrequency(int ch)
280 s_chan[ch].iUsedFreq=s_chan[ch].iActFreq; // -> take it and calc steps
281 s_chan[ch].sinc=s_chan[ch].iRawPitch<<4;
282 if(!s_chan[ch].sinc) s_chan[ch].sinc=1;
283 if(iUseInterpolation==1) s_chan[ch].SB[32]=1; // -> freq change in simle imterpolation mode: set flag
286 ////////////////////////////////////////////////////////////////////////
288 INLINE int FModChangeFrequency(int ch,int ns)
290 int NP=s_chan[ch].iRawPitch;
293 NP=((32768L+iFMod[ns])*NP)/32768L;
295 if(NP>0x3fff) NP=0x3fff;
298 NP=(44100L*NP)/(4096L); // calc frequency
300 s_chan[ch].iActFreq=NP;
301 s_chan[ch].iUsedFreq=NP;
302 sinc=(((NP/10)<<16)/4410);
304 if(iUseInterpolation==1) // freq change in simple interpolation mode
311 ////////////////////////////////////////////////////////////////////////
313 // noise handler... just produces some noise data
314 // surely wrong... and no noise frequency (spuCtrl&0x3f00) will be used...
315 // and sometimes the noise will be used as fmod modulation... pfff
317 INLINE int iGetNoiseVal(int ch)
321 if((dwNoiseVal<<=1)&0x80000000L)
323 dwNoiseVal^=0x0040001L;
324 fa=((dwNoiseVal>>2)&0x7fff);
327 else fa=(dwNoiseVal>>2)&0x7fff;
329 // mmm... depending on the noise freq we allow bigger/smaller changes to the previous val
330 fa=s_chan[ch].iOldNoise+((fa-s_chan[ch].iOldNoise)/((0x001f-((spuCtrl&0x3f00)>>9))+1));
331 if(fa>32767L) fa=32767L;
332 if(fa<-32767L) fa=-32767L;
333 s_chan[ch].iOldNoise=fa;
335 if(iUseInterpolation<2) // no gauss/cubic interpolation?
336 s_chan[ch].SB[29] = fa; // -> store noise val in "current sample" slot
340 ////////////////////////////////////////////////////////////////////////
342 INLINE void StoreInterpolationVal(int ch,int fa)
344 if(s_chan[ch].bFMod==2) // fmod freq channel
345 s_chan[ch].SB[29]=fa;
348 if((spuCtrl&0x4000)==0) fa=0; // muted?
351 if(fa>32767L) fa=32767L;
352 if(fa<-32767L) fa=-32767L;
355 if(iUseInterpolation>=2) // gauss/cubic interpolation
357 int gpos = s_chan[ch].SB[28];
360 s_chan[ch].SB[28] = gpos;
363 if(iUseInterpolation==1) // simple interpolation
365 s_chan[ch].SB[28] = 0;
366 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'
367 s_chan[ch].SB[30] = s_chan[ch].SB[31];
368 s_chan[ch].SB[31] = fa;
369 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
371 else s_chan[ch].SB[29]=fa; // no interpolation
375 ////////////////////////////////////////////////////////////////////////
377 INLINE int iGetInterpolationVal(int ch)
381 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
383 switch(iUseInterpolation)
385 //--------------------------------------------------//
386 case 3: // cubic interpolation
389 xd = ((s_chan[ch].spos) >> 1)+1;
390 gpos = s_chan[ch].SB[28];
392 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
393 fa *= (xd - (2<<15)) / 6;
395 fa += gval(2) - gval(1) - gval(1) + gval0;
396 fa *= (xd - (1<<15)) >> 1;
398 fa += gval(1) - gval0;
404 //--------------------------------------------------//
405 case 2: // gauss interpolation
408 vl = (s_chan[ch].spos >> 6) & ~3;
409 gpos = s_chan[ch].SB[28];
410 vr=(gauss[vl]*gval0)&~2047;
411 vr+=(gauss[vl+1]*gval(1))&~2047;
412 vr+=(gauss[vl+2]*gval(2))&~2047;
413 vr+=(gauss[vl+3]*gval(3))&~2047;
416 //--------------------------------------------------//
417 case 1: // simple interpolation
419 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
420 InterpolateUp(ch); // --> interpolate up
421 else InterpolateDown(ch); // --> else down
422 fa=s_chan[ch].SB[29];
424 //--------------------------------------------------//
425 default: // no interpolation
427 fa=s_chan[ch].SB[29];
429 //--------------------------------------------------//
435 static void do_irq(void)
437 if(!(spuStat & STAT_IRQ))
440 if(irqCallback) irqCallback();
444 static int decode_block(int ch)
446 unsigned char *start;
447 unsigned int nSample;
448 int predict_nr,shift_factor,flags,d,s;
454 start=s_chan[ch].pCurr; // set up the current pos
455 if(start == (unsigned char*)-1 || // special "stop" sign
456 (dwPendingChanOff&(1<<ch)))
458 dwChannelOn&=~(1<<ch); // -> turn everything off
459 dwPendingChanOff&=~(1<<ch);
461 s_chan[ch].ADSRX.EnvelopeVol=0;
462 return 0; // -> and done for this channel
465 //////////////////////////////////////////// irq check
469 if(pSpuIrq == start) // irq address reached?
471 do_irq(); // -> call main emu
479 predict_nr=(int)*start;start++;
480 shift_factor=predict_nr&0xf;
482 flags=(int)*start;start++;
484 // -------------------------------------- //
486 for (nSample=0;nSample<28;start++)
490 if(s&0x8000) s|=0xffff0000;
492 fa=(s >> shift_factor);
493 fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
497 s_chan[ch].SB[nSample++]=fa;
499 if(s&0x8000) s|=0xffff0000;
500 fa=(s>>shift_factor);
501 fa=fa + ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
504 s_chan[ch].SB[nSample++]=fa;
507 //////////////////////////////////////////// flag handler
509 if((flags&4) && (!s_chan[ch].bIgnoreLoop))
510 s_chan[ch].pLoop=start-16; // loop adress
512 if(flags&1) // 1: stop/loop
515 dwPendingChanOff|=1<<ch;
517 start = s_chan[ch].pLoop;
520 if (start - spuMemC >= 0x80000)
521 start = (unsigned char*)-1;
523 s_chan[ch].pCurr=start; // store values for next cycle
530 // do block, but ignore sample data
531 static int skip_block(int ch)
533 unsigned char *start = s_chan[ch].pCurr;
534 int flags = start[1];
537 // Tron Bonne hack, probably wrong (could be wrong memory contents..)
538 if(flags & ~7) flags = 0;
546 if((flags & 4) && !s_chan[ch].bIgnoreLoop)
547 s_chan[ch].pLoop=start;
549 s_chan[ch].pCurr += 16;
552 s_chan[ch].pCurr = s_chan[ch].pLoop;
557 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
558 static int do_samples_##name(int ch, int ns, int ns_to) \
560 int sinc = s_chan[ch].sinc; \
561 int spos = s_chan[ch].spos; \
566 for (; ns < ns_to; ns++) \
570 while (spos >= 0x10000) \
572 if(s_chan[ch].iSBPos == 28) \
574 d = decode_block(ch); \
575 if(d && iSPUIRQWait) \
582 fa = s_chan[ch].SB[s_chan[ch].iSBPos++]; \
592 s_chan[ch].sinc = sinc; \
593 s_chan[ch].spos = spos; \
599 #define fmod_recv_check \
600 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
601 sinc = FModChangeFrequency(ch,ns)
603 make_do_samples(default, fmod_recv_check, ,
604 StoreInterpolationVal(ch, fa),
605 ChanBuf[ns] = iGetInterpolationVal(ch), )
606 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
608 #define simple_interp_store \
609 s_chan[ch].SB[28] = 0; \
610 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
611 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
612 s_chan[ch].SB[31] = fa; \
613 s_chan[ch].SB[32] = 1
615 #define simple_interp_get \
616 if(sinc<0x10000) /* -> upsampling? */ \
617 InterpolateUp(ch); /* --> interpolate up */ \
618 else InterpolateDown(ch); /* --> else down */ \
619 ChanBuf[ns] = s_chan[ch].SB[29]
621 make_do_samples(simple, , ,
622 simple_interp_store, simple_interp_get, )
624 static int do_samples_noise(int ch, int ns, int ns_to)
626 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
627 while (s_chan[ch].spos >= 28*0x10000)
630 s_chan[ch].spos -= 28*0x10000;
633 for (; ns < ns_to; ns++)
634 ChanBuf[ns] = iGetNoiseVal(ch);
639 ////////////////////////////////////////////////////////////////////////
641 // here is the main job handler... thread, timer or direct func call
642 // basically the whole sound processing is done in this fat func!
643 ////////////////////////////////////////////////////////////////////////
645 // 5 ms waiting phase, if buffer is full and no new sound has to get started
646 // .. can be made smaller (smallest val: 1 ms), but bigger waits give
647 // better performance
652 ////////////////////////////////////////////////////////////////////////
654 static void *MAINThread(void *arg)
656 int ns,ns_from,ns_to;
657 #if !defined(_MACOSX) && !defined(__arm__)
658 int voldiv = iVolume;
660 const int voldiv = 2;
665 while(!bEndThread) // until we are shutting down
667 // ok, at the beginning we are looking if there is
668 // enuff free place in the dsound/oss buffer to
669 // fill in new data, or if there is a new channel to start.
670 // if not, we wait (thread) or return (timer/spuasync)
671 // until enuff free place is available/a new channel gets
674 if(dwNewChannel) // new channel should start immedately?
675 { // (at least one bit 0 ... MAXCHANNEL is set?)
676 iSecureStart++; // -> set iSecure
677 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)
679 else iSecureStart=0; // 0: no new channel should start
681 while(!iSecureStart && !bEndThread && // no new start? no thread end?
682 (SoundGetBytesBuffered()>TESTSIZE)) // and still enuff data in sound buffer?
684 iSecureStart=0; // reset secure
686 if(iUseTimer) return 0; // linux no-thread mode? bye
687 usleep(PAUSE_L); // else sleep for x ms (linux)
689 if(dwNewChannel) iSecureStart=1; // if a new channel kicks in (or, of course, sound buffer runs low), we will leave the loop
692 //--------------------------------------------------// continue from irq handling in timer mode?
697 if(lastch>=0) // will be -1 if no continue is pending
699 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
702 //--------------------------------------------------//
703 //- main channel loop -//
704 //--------------------------------------------------//
706 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
708 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
709 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
711 if(s_chan[ch].iActFreq!=s_chan[ch].iUsedFreq) // new psx frequency?
712 VoiceChangeFrequency(ch);
714 if(s_chan[ch].bNoise)
715 d=do_samples_noise(ch, ns_from, ns_to);
716 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
717 d=do_samples_noint(ch, ns_from, ns_to);
718 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
719 d=do_samples_simple(ch, ns_from, ns_to);
721 d=do_samples_default(ch, ns_from, ns_to);
729 MixADSR(ch, ns_from, ns_to);
731 if(s_chan[ch].bFMod==2) // fmod freq channel
732 memcpy(iFMod, ChanBuf, sizeof(iFMod));
733 else for(ns=ns_from;ns<ns_to;ns++)
735 int sval = ChanBuf[ns];
738 //////////////////////////////////////////////
739 // ok, left/right sound volume (psx volume goes from 0 ... 0x3fff)
741 SSumLR[ns*2] +=(sval*s_chan[ch].iLeftVolume)/0x4000L;
742 SSumLR[ns*2+1]+=(sval*s_chan[ch].iRightVolume)/0x4000L;
744 //////////////////////////////////////////////
745 // now let us store sound data for reverb
747 if(s_chan[ch].bRVBActive) StoreREVERB(ch,ns,sval);
753 // advance "stopped" channels that can cause irqs
754 // (all chans are always playing on the real thing..)
755 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
756 for(ch=0;ch<MAXCHAN;ch++)
758 if(dwChannelOn&(1<<ch)) continue; // already handled
759 if(s_chan[ch].pCurr == (unsigned char *)-1)
761 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
764 if(s_chan[ch].iActFreq!=s_chan[ch].iUsedFreq) // new psx frequency?
765 VoiceChangeFrequency(ch);
767 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
768 while(s_chan[ch].spos >= 28 * 0x10000)
770 unsigned char *start=s_chan[ch].pCurr;
772 bIRQReturn |= skip_block(ch);
773 if(start == s_chan[ch].pCurr)
776 s_chan[ch].pCurr=(unsigned char *)-1;
780 s_chan[ch].spos -= 28 * 0x10000;
784 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
790 DWORD dwWatchTime=timeGetTime_spu()+2500;
792 while(iSpuAsyncWait && !bEndThread &&
793 timeGetTime_spu()<dwWatchTime)
804 //---------------------------------------------------//
805 //- here we have another 1 ms of sound data
806 //---------------------------------------------------//
807 // mix XA infos (if any)
811 ///////////////////////////////////////////////////////
812 // mix all channels (including reverb) into one buffer
814 for (ns = 0; ns < NSSIZE*2; )
816 SSumLR[ns] += MixREVERBLeft(ns/2);
818 d = SSumLR[ns] / voldiv; SSumLR[ns] = 0;
819 if (d < -32767) d = -32767; if (d > 32767) d = 32767;
823 SSumLR[ns] += MixREVERBRight();
825 d = SSumLR[ns] / voldiv; SSumLR[ns] = 0;
826 if(d < -32767) d = -32767; if(d > 32767) d = 32767;
831 //////////////////////////////////////////////////////
832 // special irq handling in the decode buffers (0x0000-0x1000)
834 // the decode buffers are located in spu memory in the following way:
835 // 0x0000-0x03ff CD audio left
836 // 0x0400-0x07ff CD audio right
837 // 0x0800-0x0bff Voice 1
838 // 0x0c00-0x0fff Voice 3
839 // and decoded data is 16 bit for one sample
841 // even if voices 1/3 are off or no cd audio is playing, the internal
842 // play positions will move on and wrap after 0x400 bytes.
843 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
844 // increase this pointer on each sample by 2 bytes. If this pointer
845 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
846 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
847 // in some of Peops timer modes. So: we ignore this option here (for now).
851 for(ns=0;ns<NSSIZE;ns++)
853 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
857 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
861 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
868 // wanna have around 1/60 sec (16.666 ms) updates
871 SoundFeedStreamData((unsigned char *)pSpuBuffer,
872 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
873 pS = (short *)pSpuBuffer;
878 // end of big main loop...
885 // SPU ASYNC... even newer epsxe func
886 // 1 time every 'cycle' cycles... harhar
888 void CALLBACK SPUasync(unsigned long cycle)
893 if(iSpuAsyncWait<=16) return;
897 if(iUseTimer==2) // special mode, only used in Linux by this spu (or if you enable the experimental Windows mode)
899 if(!bSpuInit) return; // -> no init, no call
901 MAINThread(0); // -> linux high-compat mode
903 // abuse iSpuAsyncWait mechanism to reduce calls to above function
904 // to make it do larger chunks
905 // note: doing it less often than once per frame causes skips
910 // SPU UPDATE... new epsxe func
911 // 1 time every 32 hsync lines
912 // (312/32)x50 in pal
913 // (262/32)x60 in ntsc
915 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
916 // leave that func in the linux port, until epsxe linux is using
917 // the async function as well
919 void CALLBACK SPUupdate(void)
926 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
929 if(!xap->freq) return; // no xa freq ? bye
931 FeedXA(xap); // call main XA feeder
935 void CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
938 if (nbytes<=0) return;
940 FeedCDDA((unsigned char *)pcm, nbytes);
943 // SETUPTIMER: init of certain buffers and threads/timers
944 void SetupTimer(void)
946 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
947 memset(iFMod,0,NSSIZE*sizeof(int));
948 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
950 bEndThread=0; // init thread vars
952 bSpuInit=1; // flag: we are inited
954 if(!iUseTimer) // linux: use thread
956 pthread_create(&thread, NULL, MAINThread, NULL);
960 // REMOVETIMER: kill threads/timers
961 void RemoveTimer(void)
963 bEndThread=1; // raise flag to end thread
965 if(!iUseTimer) // linux tread?
968 while(!bThreadEnded && i<2000) {usleep(1000L);i++;} // -> wait until thread has ended
969 if(thread!=(pthread_t)-1) {pthread_cancel(thread);thread=(pthread_t)-1;} // -> cancel thread anyway
972 bThreadEnded=0; // no more spu is running
976 // SETUPSTREAMS: init most of the spu buffers
977 void SetupStreams(void)
981 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
983 if(iUseReverb==1) i=88200*2;
986 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
987 memset(sRVBStart,0,i*4);
988 sRVBEnd = sRVBStart + i;
989 sRVBPlay = sRVBStart;
991 XAStart = // alloc xa buffer
992 (uint32_t *)malloc(44100 * sizeof(uint32_t));
993 XAEnd = XAStart + 44100;
997 CDDAStart = // alloc cdda buffer
998 (uint32_t *)malloc(16384 * sizeof(uint32_t));
999 CDDAEnd = CDDAStart + 16384;
1000 CDDAPlay = CDDAStart;
1001 CDDAFeed = CDDAStart;
1003 for(i=0;i<MAXCHAN;i++) // loop sound channels
1005 // we don't use mutex sync... not needed, would only
1007 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
1008 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1009 s_chan[i].pLoop=spuMemC;
1010 s_chan[i].pStart=spuMemC;
1011 s_chan[i].pCurr=spuMemC;
1014 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
1017 // REMOVESTREAMS: free most buffer
1018 void RemoveStreams(void)
1020 free(pSpuBuffer); // free mixing buffer
1022 free(sRVBStart); // free reverb buffer
1024 free(XAStart); // free XA buffer
1026 free(CDDAStart); // free CDDA buffer
1032 // SPUINIT: this func will be called first by the main emu
1033 long CALLBACK SPUinit(void)
1035 spuMemC = (unsigned char *)spuMem; // just small setup
1036 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1042 spuAddr = 0xffffffff;
1045 spuMemC = (unsigned char *)spuMem;
1047 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1052 //ReadConfigSPU(); // read user stuff
1053 SetupStreams(); // prepare streaming
1058 // SPUOPEN: called by main emu after init
1059 long CALLBACK SPUopen(void)
1061 if (bSPUIsOpen) return 0; // security for some stupid main emus
1063 SetupSound(); // setup sound (before init!)
1064 SetupTimer(); // timer for feeding data
1068 return PSE_SPU_ERR_SUCCESS;
1071 // SPUCLOSE: called before shutdown
1072 long CALLBACK SPUclose(void)
1074 if (!bSPUIsOpen) return 0; // some security
1076 bSPUIsOpen = 0; // no more open
1078 RemoveTimer(); // no more feeding
1079 RemoveSound(); // no more sound handling
1084 // SPUSHUTDOWN: called by main emu on final exit
1085 long CALLBACK SPUshutdown(void)
1088 RemoveStreams(); // no more streaming
1093 // SPUTEST: we don't test, we are always fine ;)
1094 long CALLBACK SPUtest(void)
1099 // SPUCONFIGURE: call config dialog
1100 long CALLBACK SPUconfigure(void)
1105 // StartCfgTool("CFG");
1110 // SPUABOUT: show about window
1111 void CALLBACK SPUabout(void)
1116 // StartCfgTool("ABOUT");
1121 // this functions will be called once,
1122 // passes a callback that should be called on SPU-IRQ/cdda volume change
1123 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1125 irqCallback = callback;
1128 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1130 cddavCallback = CDDAVcallback;
1133 // COMMON PLUGIN INFO FUNCS
1135 char * CALLBACK PSEgetLibName(void)
1137 return _(libraryName);
1140 unsigned long CALLBACK PSEgetLibType(void)
1145 unsigned long CALLBACK PSEgetLibVersion(void)
1147 return (1 << 16) | (6 << 8);
1150 char * SPUgetLibInfos(void)
1152 return _(libraryInfo);
1157 void spu_get_debug_info(int *chans_out, int *fmod_chans_out, int *noise_chans_out)
1159 int ch = 0, fmod_chans = 0, noise_chans = 0;
1161 for(;ch<MAXCHAN;ch++)
1163 if (!(dwChannelOn & (1<<ch)))
1165 if (s_chan[ch].bFMod == 2)
1166 fmod_chans |= 1 << ch;
1167 if (s_chan[ch].bNoise)
1168 noise_chans |= 1 << ch;
1171 *chans_out = dwChannelOn;
1172 *fmod_chans_out = fmod_chans;
1173 *noise_chans_out = noise_chans;
1176 // vim:shiftwidth=1:expandtab