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-2012
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 "arm_features.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; \
50 #define PSXCLK 33868800 /* 33.8688 MHz */
53 #if defined (USEMACOSX)
54 static char * libraryName = N_("Mac OS X Sound");
55 #elif defined (USEALSA)
56 static char * libraryName = N_("ALSA Sound");
57 #elif defined (USEOSS)
58 static char * libraryName = N_("OSS Sound");
59 #elif defined (USESDL)
60 static char * libraryName = N_("SDL Sound");
61 #elif defined (USEPULSEAUDIO)
62 static char * libraryName = N_("PulseAudio Sound");
64 static char * libraryName = N_("NULL Sound");
67 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
72 // psx buffer / addresses
74 unsigned short regArea[10000];
75 unsigned short spuMem[256*1024];
76 unsigned char * spuMemC;
77 unsigned char * pSpuIrq=0;
78 unsigned char * pSpuBuffer;
82 int iVolume=768; // 1024 is 1.0
85 int iUseInterpolation=2;
87 // MAIN infos struct for each channel
89 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
92 unsigned int dwNoiseVal; // global noise generator
93 unsigned int dwNoiseCount;
95 unsigned short spuCtrl=0; // some vars to store psx reg infos
96 unsigned short spuStat=0;
97 unsigned short spuIrq=0;
98 unsigned long spuAddr=0xffffffff; // address into spu mem
102 unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
103 unsigned int dwChannelOn=0; // not silent channels
104 unsigned int dwPendingChanOff=0;
105 unsigned int dwChannelDead=0; // silent+not useful channels
107 void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
108 void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
110 // certain globals (were local before, but with the new timeproc I need em global)
112 static const int f[8][2] = { { 0, 0 },
117 int ChanBuf[NSSIZE+3];
118 int SSumLR[(NSSIZE+3)*2];
123 static int decode_dirty_ch;
126 int lastch=-1; // last channel processed on spu irq in timer mode
127 static int lastns=0; // last ns pos
128 static int cycles_since_update;
130 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
132 ////////////////////////////////////////////////////////////////////////
134 ////////////////////////////////////////////////////////////////////////
136 // dirty inline func includes
141 ////////////////////////////////////////////////////////////////////////
142 // helpers for simple interpolation
145 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
147 // instead of having n equal sample values in a row like:
151 // we compare the current delta change with the next delta change.
153 // if curr_delta is positive,
155 // - and next delta is smaller (or changing direction):
159 // - and next delta significant (at least twice) bigger:
163 // - and next delta is nearly same:
168 // if curr_delta is negative,
170 // - and next delta is smaller (or changing direction):
174 // - and next delta significant (at least twice) bigger:
178 // - and next delta is nearly same:
184 INLINE void InterpolateUp(int ch)
186 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
188 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
189 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
193 if(id1>0) // curr delta positive
196 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
199 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
201 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
203 else // curr delta negative
206 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
209 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
211 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
215 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
219 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
220 //if(s_chan[ch].sinc<=0x8000)
221 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
223 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
225 else // no flags? add bigger val (if possible), calc smaller step, set flag1
226 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
230 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
233 INLINE void InterpolateDown(int ch)
235 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
237 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
238 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
239 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
243 ////////////////////////////////////////////////////////////////////////
244 // helpers for gauss interpolation
246 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
247 #define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
251 ////////////////////////////////////////////////////////////////////////
255 static void do_irq(void)
257 //if(!(spuStat & STAT_IRQ))
259 spuStat |= STAT_IRQ; // asserted status?
260 if(irqCallback) irqCallback();
264 static int check_irq(int ch, unsigned char *pos)
266 if((spuCtrl & CTRL_IRQ) && pos == pSpuIrq)
268 //printf("ch%d irq %04x\n", ch, pos - spuMemC);
275 ////////////////////////////////////////////////////////////////////////
276 // START SOUND... called by main thread to setup a new sound on a channel
277 ////////////////////////////////////////////////////////////////////////
279 INLINE void StartSound(int ch)
284 // fussy timing issues - do in VoiceOn
285 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
286 //s_chan[ch].bStop=0;
289 s_chan[ch].SB[26]=0; // init mixing vars
291 s_chan[ch].iSBPos=28;
293 s_chan[ch].SB[29]=0; // init our interpolation helpers
296 if(iUseInterpolation>=2) // gauss interpolation?
297 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
298 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
300 dwNewChannel&=~(1<<ch); // clear new channel bit
303 ////////////////////////////////////////////////////////////////////////
304 // ALL KIND OF HELPERS
305 ////////////////////////////////////////////////////////////////////////
307 INLINE int FModChangeFrequency(int ch,int ns)
309 unsigned int NP=s_chan[ch].iRawPitch;
312 NP=((32768L+iFMod[ns])*NP)/32768L;
314 if(NP>0x3fff) NP=0x3fff;
317 sinc=NP<<4; // calc frequency
318 if(iUseInterpolation==1) // freq change in simple interpolation mode
325 ////////////////////////////////////////////////////////////////////////
327 INLINE void StoreInterpolationVal(int ch,int fa)
329 if(s_chan[ch].bFMod==2) // fmod freq channel
330 s_chan[ch].SB[29]=fa;
335 if(iUseInterpolation>=2) // gauss/cubic interpolation
337 int gpos = s_chan[ch].SB[28];
340 s_chan[ch].SB[28] = gpos;
343 if(iUseInterpolation==1) // simple interpolation
345 s_chan[ch].SB[28] = 0;
346 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'
347 s_chan[ch].SB[30] = s_chan[ch].SB[31];
348 s_chan[ch].SB[31] = fa;
349 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
351 else s_chan[ch].SB[29]=fa; // no interpolation
355 ////////////////////////////////////////////////////////////////////////
357 INLINE int iGetInterpolationVal(int ch, int spos)
361 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
363 switch(iUseInterpolation)
365 //--------------------------------------------------//
366 case 3: // cubic interpolation
370 gpos = s_chan[ch].SB[28];
372 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
373 fa *= (xd - (2<<15)) / 6;
375 fa += gval(2) - gval(1) - gval(1) + gval0;
376 fa *= (xd - (1<<15)) >> 1;
378 fa += gval(1) - gval0;
384 //--------------------------------------------------//
385 case 2: // gauss interpolation
388 vl = (spos >> 6) & ~3;
389 gpos = s_chan[ch].SB[28];
390 vr=(gauss[vl]*(int)gval0)&~2047;
391 vr+=(gauss[vl+1]*gval(1))&~2047;
392 vr+=(gauss[vl+2]*gval(2))&~2047;
393 vr+=(gauss[vl+3]*gval(3))&~2047;
396 //--------------------------------------------------//
397 case 1: // simple interpolation
399 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
400 InterpolateUp(ch); // --> interpolate up
401 else InterpolateDown(ch); // --> else down
402 fa=s_chan[ch].SB[29];
404 //--------------------------------------------------//
405 default: // no interpolation
407 fa=s_chan[ch].SB[29];
409 //--------------------------------------------------//
415 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
418 int fa, s_1, s_2, d, s;
423 for (nSample = 0; nSample < 28; src++)
426 s = (int)(signed short)((d & 0x0f) << 12);
428 fa = s >> shift_factor;
429 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
432 dest[nSample++] = fa;
434 s = (int)(signed short)((d & 0xf0) << 8);
435 fa = s >> shift_factor;
436 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
439 dest[nSample++] = fa;
443 static int decode_block(int ch)
445 unsigned char *start;
446 int predict_nr,shift_factor,flags;
449 start=s_chan[ch].pCurr; // set up the current pos
451 if(s_chan[ch].prevflags&1) // 1: stop/loop
453 if(!(s_chan[ch].prevflags&2))
455 dwChannelOn&=~(1<<ch); // -> turn everything off
457 s_chan[ch].ADSRX.EnvelopeVol=0;
460 start = s_chan[ch].pLoop;
463 ret = check_irq(ch, start); // hack, see check_irq below..
465 predict_nr=(int)start[0];
466 shift_factor=predict_nr&0xf;
469 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
473 s_chan[ch].pLoop=start; // loop adress
477 if(flags&1) { // 1: stop/loop
478 start = s_chan[ch].pLoop;
479 ret |= check_irq(ch, start); // hack.. :(
482 if (start - spuMemC >= 0x80000)
485 s_chan[ch].pCurr = start; // store values for next cycle
486 s_chan[ch].prevflags = flags;
491 // do block, but ignore sample data
492 static int skip_block(int ch)
494 unsigned char *start = s_chan[ch].pCurr;
495 int flags = start[1];
496 int ret = check_irq(ch, start);
498 if(s_chan[ch].prevflags & 1)
499 start = s_chan[ch].pLoop;
502 s_chan[ch].pLoop = start;
507 start = s_chan[ch].pLoop;
509 s_chan[ch].pCurr = start;
510 s_chan[ch].prevflags = flags;
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); \
537 ret = ns_to = ns + 1; \
549 s_chan[ch].sinc = sinc; \
550 s_chan[ch].spos = spos; \
551 s_chan[ch].iSBPos = sbpos; \
557 #define fmod_recv_check \
558 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
559 sinc = FModChangeFrequency(ch,ns)
561 make_do_samples(default, fmod_recv_check, ,
562 StoreInterpolationVal(ch, fa),
563 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
564 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
566 #define simple_interp_store \
567 s_chan[ch].SB[28] = 0; \
568 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
569 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
570 s_chan[ch].SB[31] = fa; \
571 s_chan[ch].SB[32] = 1
573 #define simple_interp_get \
574 if(sinc<0x10000) /* -> upsampling? */ \
575 InterpolateUp(ch); /* --> interpolate up */ \
576 else InterpolateDown(ch); /* --> else down */ \
577 ChanBuf[ns] = s_chan[ch].SB[29]
579 make_do_samples(simple, , ,
580 simple_interp_store, simple_interp_get, )
582 static int do_samples_noise(int ch, int ns, int ns_to)
584 int level, shift, bit;
587 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
588 while (s_chan[ch].spos >= 28*0x10000)
593 s_chan[ch].spos -= 28*0x10000;
596 // modified from DrHell/shalma, no fraction
597 level = (spuCtrl >> 10) & 0x0f;
598 level = 0x8000 >> level;
600 for (; ns < ns_to; ns++)
603 if (dwNoiseCount >= level)
605 dwNoiseCount -= level;
606 shift = (dwNoiseVal >> 10) & 0x1f;
607 bit = (0x69696969 >> shift) & 1;
608 if (dwNoiseVal & 0x8000)
610 dwNoiseVal = (dwNoiseVal << 1) | bit;
613 ChanBuf[ns] = (signed short)dwNoiseVal;
620 // asm code; lv and rv must be 0-3fff
621 extern void mix_chan(int start, int count, int lv, int rv);
622 extern void mix_chan_rvb(int start, int count, int lv, int rv);
624 static void mix_chan(int start, int count, int lv, int rv)
626 int *dst = SSumLR + start * 2;
627 const int *src = ChanBuf + start;
634 l = (sval * lv) >> 14;
635 r = (sval * rv) >> 14;
641 static void mix_chan_rvb(int start, int count, int lv, int rv)
643 int *dst = SSumLR + start * 2;
644 int *drvb = sRVBStart + start * 2;
645 const int *src = ChanBuf + start;
652 l = (sval * lv) >> 14;
653 r = (sval * rv) >> 14;
662 // 0x0800-0x0bff Voice 1
663 // 0x0c00-0x0fff Voice 3
664 static void noinline do_decode_bufs(int which, int start, int count)
666 const int *src = ChanBuf + start;
667 unsigned short *dst = &spuMem[0x800/2 + which*0x400/2];
668 int cursor = decode_pos;
672 dst[cursor] = *src++;
673 cursor = (cursor + 1) & 0x1ff;
676 // decode_pos is updated and irqs are checked later, after voice loop
679 ////////////////////////////////////////////////////////////////////////
681 // here is the main job handler...
682 // basically the whole sound processing is done in this fat func!
683 ////////////////////////////////////////////////////////////////////////
685 static int do_samples(int forced_updates)
687 int volmult = iVolume;
688 int ns,ns_from,ns_to;
694 // ok, at the beginning we are looking if there is
695 // enuff free place in the dsound/oss buffer to
696 // fill in new data, or if there is a new channel to start.
697 // if not, we wait (thread) or return (timer/spuasync)
698 // until enuff free place is available/a new channel gets
701 if(!forced_updates && out_current->busy()) // still enuff data in sound buffer?
706 cycles_since_update = 0;
707 if(forced_updates > 0)
710 //--------------------------------------------------// continue from irq handling in timer mode?
715 if(lastch>=0) // will be -1 if no continue is pending
717 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
720 silentch=~(dwChannelOn|dwNewChannel);
722 //--------------------------------------------------//
723 //- main channel loop -//
724 //--------------------------------------------------//
726 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
728 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
729 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
731 if(s_chan[ch].bNoise)
732 d=do_samples_noise(ch, ns_from, ns_to);
733 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
734 d=do_samples_noint(ch, ns_from, ns_to);
735 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
736 d=do_samples_simple(ch, ns_from, ns_to);
738 d=do_samples_default(ch, ns_from, ns_to);
746 MixADSR(ch, ns_from, ns_to);
750 do_decode_bufs(ch/2, ns_from, ns_to-ns_from);
751 decode_dirty_ch |= 1<<ch;
754 if(s_chan[ch].bFMod==2) // fmod freq channel
755 memcpy(iFMod, ChanBuf, sizeof(iFMod));
756 else if(s_chan[ch].bRVBActive)
757 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
759 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
763 // advance "stopped" channels that can cause irqs
764 // (all chans are always playing on the real thing..)
766 for(ch=0;ch<MAXCHAN;ch++)
768 if(!(silentch&(1<<ch))) continue; // already handled
769 if(dwChannelDead&(1<<ch)) continue;
770 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
773 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
774 while(s_chan[ch].spos >= 28 * 0x10000)
776 unsigned char *start = s_chan[ch].pCurr;
778 // no need for bIRQReturn since the channel is silent
780 if(start == s_chan[ch].pCurr)
783 dwChannelDead |= 1<<ch;
788 s_chan[ch].spos -= 28 * 0x10000;
792 if(bIRQReturn) // special return for "spu irq - wait for cpu action"
795 if(unlikely(silentch & decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
797 memset(&spuMem[0x800/2], 0, 0x400);
798 decode_dirty_ch &= ~(1<<1);
800 if(unlikely(silentch & decode_dirty_ch & (1<<3)))
802 memset(&spuMem[0xc00/2], 0, 0x400);
803 decode_dirty_ch &= ~(1<<3);
806 //---------------------------------------------------//
807 //- here we have another 1 ms of sound data
808 //---------------------------------------------------//
809 // mix XA infos (if any)
813 ///////////////////////////////////////////////////////
814 // mix all channels (including reverb) into one buffer
819 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
821 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
825 for (ns = 0; ns < NSSIZE*2; )
827 d = SSumLR[ns]; SSumLR[ns] = 0;
828 d = d * volmult >> 10;
833 d = SSumLR[ns]; SSumLR[ns] = 0;
834 d = d * volmult >> 10;
840 //////////////////////////////////////////////////////
841 // special irq handling in the decode buffers (0x0000-0x1000)
843 // the decode buffers are located in spu memory in the following way:
844 // 0x0000-0x03ff CD audio left
845 // 0x0400-0x07ff CD audio right
846 // 0x0800-0x0bff Voice 1
847 // 0x0c00-0x0fff Voice 3
848 // and decoded data is 16 bit for one sample
850 // even if voices 1/3 are off or no cd audio is playing, the internal
851 // play positions will move on and wrap after 0x400 bytes.
852 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
853 // increase this pointer on each sample by 2 bytes. If this pointer
854 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
855 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
856 // in some of Peops timer modes. So: we ignore this option here (for now).
858 if(unlikely((spuCtrl&CTRL_IRQ) && pSpuIrq && pSpuIrq<spuMemC+0x1000))
860 int irq_pos=(pSpuIrq-spuMemC)/2 & 0x1ff;
861 if((decode_pos <= irq_pos && irq_pos < decode_pos+NSSIZE)
862 || (decode_pos+NSSIZE > 0x200 && irq_pos < ((decode_pos+NSSIZE) & 0x1ff)))
864 //printf("decoder irq %x\n", decode_pos);
868 decode_pos = (decode_pos + NSSIZE) & 0x1ff;
873 // wanna have around 1/60 sec (16.666 ms) updates
874 if (iCycle++ > 16/FRAG_MSECS)
876 out_current->feed(pSpuBuffer,
877 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
878 pS = (short *)pSpuBuffer;
886 // SPU ASYNC... even newer epsxe func
887 // 1 time every 'cycle' cycles... harhar
889 // rearmed: called every 2ms now
891 void CALLBACK SPUasync(unsigned long cycle)
894 int forced_updates = 0;
897 if(!bSpuInit) return; // -> no init, no call
899 cycles_since_update += cycle;
901 if(dwNewChannel || had_dma)
908 if((spuCtrl&CTRL_IRQ) && (((spuCtrl^old_ctrl)&CTRL_IRQ) // irq was enabled
909 || cycles_since_update > PSXCLK/60 / 4)) {
911 forced_updates = cycles_since_update / (PSXCLK/44100) / NSSIZE;
913 // with no irqs, once per frame should be fine (using a bit more because of BIAS)
914 else if(cycles_since_update > PSXCLK/60 * 5/4)
920 do_samples(forced_updates);
923 // SPU UPDATE... new epsxe func
924 // 1 time every 32 hsync lines
925 // (312/32)x50 in pal
926 // (262/32)x60 in ntsc
928 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
929 // leave that func in the linux port, until epsxe linux is using
930 // the async function as well
932 void CALLBACK SPUupdate(void)
939 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
942 if(!xap->freq) return; // no xa freq ? bye
944 FeedXA(xap); // call main XA feeder
948 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
951 if (nbytes<=0) return -1;
953 return FeedCDDA((unsigned char *)pcm, nbytes);
956 // to be called after state load
957 void ClearWorkingState(void)
959 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
960 memset(iFMod,0,sizeof(iFMod));
961 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
964 // SETUPSTREAMS: init most of the spu buffers
965 void SetupStreams(void)
969 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
971 if(iUseReverb==1) i=88200*2;
974 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
975 memset(sRVBStart,0,i*4);
976 sRVBEnd = sRVBStart + i;
977 sRVBPlay = sRVBStart;
979 XAStart = // alloc xa buffer
980 (uint32_t *)malloc(44100 * sizeof(uint32_t));
981 XAEnd = XAStart + 44100;
985 CDDAStart = // alloc cdda buffer
986 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
987 CDDAEnd = CDDAStart + 16384;
988 CDDAPlay = CDDAStart;
989 CDDAFeed = CDDAStart;
991 for(i=0;i<MAXCHAN;i++) // loop sound channels
993 // we don't use mutex sync... not needed, would only
995 // s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
996 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
997 s_chan[i].pLoop=spuMemC;
998 s_chan[i].pCurr=spuMemC;
1001 ClearWorkingState();
1003 bSpuInit=1; // flag: we are inited
1006 // REMOVESTREAMS: free most buffer
1007 void RemoveStreams(void)
1009 free(pSpuBuffer); // free mixing buffer
1011 free(sRVBStart); // free reverb buffer
1013 free(XAStart); // free XA buffer
1015 free(CDDAStart); // free CDDA buffer
1021 // SPUINIT: this func will be called first by the main emu
1022 long CALLBACK SPUinit(void)
1024 spuMemC = (unsigned char *)spuMem; // just small setup
1025 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1029 spuAddr = 0xffffffff;
1030 spuMemC = (unsigned char *)spuMem;
1032 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1036 SetupStreams(); // prepare streaming
1041 // SPUOPEN: called by main emu after init
1042 long CALLBACK SPUopen(void)
1044 if (bSPUIsOpen) return 0; // security for some stupid main emus
1046 SetupSound(); // setup sound (before init!)
1050 return PSE_SPU_ERR_SUCCESS;
1053 // SPUCLOSE: called before shutdown
1054 long CALLBACK SPUclose(void)
1056 if (!bSPUIsOpen) return 0; // some security
1058 bSPUIsOpen = 0; // no more open
1060 out_current->finish(); // no more sound handling
1065 // SPUSHUTDOWN: called by main emu on final exit
1066 long CALLBACK SPUshutdown(void)
1069 RemoveStreams(); // no more streaming
1075 // SPUTEST: we don't test, we are always fine ;)
1076 long CALLBACK SPUtest(void)
1081 // SPUCONFIGURE: call config dialog
1082 long CALLBACK SPUconfigure(void)
1087 // StartCfgTool("CFG");
1092 // SPUABOUT: show about window
1093 void CALLBACK SPUabout(void)
1098 // StartCfgTool("ABOUT");
1103 // this functions will be called once,
1104 // passes a callback that should be called on SPU-IRQ/cdda volume change
1105 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1107 irqCallback = callback;
1110 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1112 cddavCallback = CDDAVcallback;
1115 // COMMON PLUGIN INFO FUNCS
1117 char * CALLBACK PSEgetLibName(void)
1119 return _(libraryName);
1122 unsigned long CALLBACK PSEgetLibType(void)
1127 unsigned long CALLBACK PSEgetLibVersion(void)
1129 return (1 << 16) | (6 << 8);
1132 char * SPUgetLibInfos(void)
1134 return _(libraryInfo);
1139 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1141 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1143 for(;ch<MAXCHAN;ch++)
1145 if (!(dwChannelOn & (1<<ch)))
1147 if (s_chan[ch].bFMod == 2)
1148 fmod_chans |= 1 << ch;
1149 if (s_chan[ch].bNoise)
1150 noise_chans |= 1 << ch;
1151 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1152 irq_chans |= 1 << ch;
1155 *chans_out = dwChannelOn;
1156 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1157 *fmod_chans_out = fmod_chans;
1158 *noise_chans_out = noise_chans;
1161 // vim:shiftwidth=1:expandtab