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"
29 #include "spu_config.h"
34 #define _(x) gettext(x)
41 #ifdef __ARM_ARCH_7A__
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; \
51 #define PSXCLK 33868800 /* 33.8688 MHz */
53 // intended to be ~1 frame
54 #define IRQ_NEAR_BLOCKS 32
57 #if defined (USEMACOSX)
58 static char * libraryName = N_("Mac OS X Sound");
59 #elif defined (USEALSA)
60 static char * libraryName = N_("ALSA Sound");
61 #elif defined (USEOSS)
62 static char * libraryName = N_("OSS Sound");
63 #elif defined (USESDL)
64 static char * libraryName = N_("SDL Sound");
65 #elif defined (USEPULSEAUDIO)
66 static char * libraryName = N_("PulseAudio Sound");
68 static char * libraryName = N_("NULL Sound");
71 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
79 // MAIN infos struct for each channel
81 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
84 // certain globals (were local before, but with the new timeproc I need em global)
86 static const int f[8][2] = { { 0, 0 },
95 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
97 ////////////////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////////////////
101 // dirty inline func includes
106 ////////////////////////////////////////////////////////////////////////
107 // helpers for simple interpolation
110 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
112 // instead of having n equal sample values in a row like:
116 // we compare the current delta change with the next delta change.
118 // if curr_delta is positive,
120 // - and next delta is smaller (or changing direction):
124 // - and next delta significant (at least twice) bigger:
128 // - and next delta is nearly same:
133 // if curr_delta is negative,
135 // - and next delta is smaller (or changing direction):
139 // - and next delta significant (at least twice) bigger:
143 // - and next delta is nearly same:
149 INLINE void InterpolateUp(int ch)
151 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
153 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
154 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
158 if(id1>0) // curr delta positive
161 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
164 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
166 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
168 else // curr delta negative
171 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
174 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
176 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
180 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
184 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
185 //if(s_chan[ch].sinc<=0x8000)
186 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
188 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
190 else // no flags? add bigger val (if possible), calc smaller step, set flag1
191 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
195 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
198 INLINE void InterpolateDown(int ch)
200 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
202 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
203 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
204 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
208 ////////////////////////////////////////////////////////////////////////
209 // helpers for gauss interpolation
211 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos&3])
212 #define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
216 ////////////////////////////////////////////////////////////////////////
220 static void do_irq(void)
222 //if(!(spu.spuStat & STAT_IRQ))
224 spu.spuStat |= STAT_IRQ; // asserted status?
225 if(spu.irqCallback) spu.irqCallback();
229 static int check_irq(int ch, unsigned char *pos)
231 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
233 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
240 ////////////////////////////////////////////////////////////////////////
241 // START SOUND... called by main thread to setup a new sound on a channel
242 ////////////////////////////////////////////////////////////////////////
244 INLINE void StartSound(int ch)
249 // fussy timing issues - do in VoiceOn
250 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
251 //s_chan[ch].bStop=0;
254 s_chan[ch].SB[26]=0; // init mixing vars
256 s_chan[ch].iSBPos=28;
258 s_chan[ch].SB[29]=0; // init our interpolation helpers
261 if(spu_config.iUseInterpolation>=2) // gauss interpolation?
262 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
263 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
265 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
268 ////////////////////////////////////////////////////////////////////////
269 // ALL KIND OF HELPERS
270 ////////////////////////////////////////////////////////////////////////
272 INLINE int FModChangeFrequency(int ch,int ns)
274 unsigned int NP=s_chan[ch].iRawPitch;
277 NP=((32768L+iFMod[ns])*NP)/32768L;
279 if(NP>0x3fff) NP=0x3fff;
282 sinc=NP<<4; // calc frequency
283 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
290 ////////////////////////////////////////////////////////////////////////
292 INLINE void StoreInterpolationVal(int ch,int fa)
294 if(s_chan[ch].bFMod==2) // fmod freq channel
295 s_chan[ch].SB[29]=fa;
300 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
302 int gpos = s_chan[ch].SB[28];
305 s_chan[ch].SB[28] = gpos;
308 if(spu_config.iUseInterpolation==1) // simple interpolation
310 s_chan[ch].SB[28] = 0;
311 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'
312 s_chan[ch].SB[30] = s_chan[ch].SB[31];
313 s_chan[ch].SB[31] = fa;
314 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
316 else s_chan[ch].SB[29]=fa; // no interpolation
320 ////////////////////////////////////////////////////////////////////////
322 INLINE int iGetInterpolationVal(int ch, int spos)
326 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
328 switch(spu_config.iUseInterpolation)
330 //--------------------------------------------------//
331 case 3: // cubic interpolation
335 gpos = s_chan[ch].SB[28];
337 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
338 fa *= (xd - (2<<15)) / 6;
340 fa += gval(2) - gval(1) - gval(1) + gval0;
341 fa *= (xd - (1<<15)) >> 1;
343 fa += gval(1) - gval0;
349 //--------------------------------------------------//
350 case 2: // gauss interpolation
353 vl = (spos >> 6) & ~3;
354 gpos = s_chan[ch].SB[28];
355 vr=(gauss[vl]*(int)gval0)&~2047;
356 vr+=(gauss[vl+1]*gval(1))&~2047;
357 vr+=(gauss[vl+2]*gval(2))&~2047;
358 vr+=(gauss[vl+3]*gval(3))&~2047;
361 //--------------------------------------------------//
362 case 1: // simple interpolation
364 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
365 InterpolateUp(ch); // --> interpolate up
366 else InterpolateDown(ch); // --> else down
367 fa=s_chan[ch].SB[29];
369 //--------------------------------------------------//
370 default: // no interpolation
372 fa=s_chan[ch].SB[29];
374 //--------------------------------------------------//
380 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
383 int fa, s_1, s_2, d, s;
388 for (nSample = 0; nSample < 28; src++)
391 s = (int)(signed short)((d & 0x0f) << 12);
393 fa = s >> shift_factor;
394 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
397 dest[nSample++] = fa;
399 s = (int)(signed short)((d & 0xf0) << 8);
400 fa = s >> shift_factor;
401 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
404 dest[nSample++] = fa;
408 static int decode_block(int ch)
410 unsigned char *start;
411 int predict_nr,shift_factor,flags;
415 start = s_chan[ch].pCurr; // set up the current pos
416 if(start == spu.spuMemC) // ?
419 if(s_chan[ch].prevflags&1) // 1: stop/loop
421 if(!(s_chan[ch].prevflags&2))
424 start = s_chan[ch].pLoop;
427 ret = check_irq(ch, start); // hack, see check_irq below..
431 spu.dwChannelOn &= ~(1<<ch); // -> turn everything off
432 s_chan[ch].bStop = 1;
433 s_chan[ch].ADSRX.EnvelopeVol = 0;
436 predict_nr=(int)start[0];
437 shift_factor=predict_nr&0xf;
440 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
444 s_chan[ch].pLoop=start; // loop adress
448 if(flags&1) { // 1: stop/loop
449 start = s_chan[ch].pLoop;
450 ret |= check_irq(ch, start); // hack.. :(
453 if (start - spu.spuMemC >= 0x80000)
456 s_chan[ch].pCurr = start; // store values for next cycle
457 s_chan[ch].prevflags = flags;
462 // do block, but ignore sample data
463 static int skip_block(int ch)
465 unsigned char *start = s_chan[ch].pCurr;
466 int flags = start[1];
467 int ret = check_irq(ch, start);
469 if(s_chan[ch].prevflags & 1)
470 start = s_chan[ch].pLoop;
473 s_chan[ch].pLoop = start;
478 start = s_chan[ch].pLoop;
480 s_chan[ch].pCurr = start;
481 s_chan[ch].prevflags = flags;
486 // if irq is going to trigger sooner than in upd_samples, set upd_samples
487 static void scan_for_irq(int ch, unsigned int *upd_samples)
489 int pos, sinc, sinc_inv, end;
490 unsigned char *block;
493 block = s_chan[ch].pCurr;
494 pos = s_chan[ch].spos;
495 sinc = s_chan[ch].sinc;
496 end = pos + *upd_samples * sinc;
498 pos += (28 - s_chan[ch].iSBPos) << 16;
501 if (block == spu.pSpuIrq)
505 if (flags & 1) { // 1: stop/loop
506 block = s_chan[ch].pLoop;
507 if (block == spu.pSpuIrq) // hack.. (see decode_block)
515 sinc_inv = s_chan[ch].sinc_inv;
517 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
519 pos -= s_chan[ch].spos;
520 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
521 //xprintf("ch%02d: irq sched: %3d %03d\n",
522 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
526 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
527 static noinline int do_samples_##name(int ch, int ns, int ns_to) \
529 int sinc = s_chan[ch].sinc; \
530 int spos = s_chan[ch].spos; \
531 int sbpos = s_chan[ch].iSBPos; \
532 int *SB = s_chan[ch].SB; \
537 for (; ns < ns_to; ns++) \
542 while (spos >= 0x10000) \
548 d = decode_block(ch); \
550 ret = /*ns_to =*/ ns + 1; \
560 s_chan[ch].sinc = sinc; \
561 s_chan[ch].spos = spos; \
562 s_chan[ch].iSBPos = sbpos; \
568 #define fmod_recv_check \
569 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
570 sinc = FModChangeFrequency(ch,ns)
572 make_do_samples(default, fmod_recv_check, ,
573 StoreInterpolationVal(ch, fa),
574 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
575 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
577 #define simple_interp_store \
578 s_chan[ch].SB[28] = 0; \
579 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
580 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
581 s_chan[ch].SB[31] = fa; \
582 s_chan[ch].SB[32] = 1
584 #define simple_interp_get \
585 if(sinc<0x10000) /* -> upsampling? */ \
586 InterpolateUp(ch); /* --> interpolate up */ \
587 else InterpolateDown(ch); /* --> else down */ \
588 ChanBuf[ns] = s_chan[ch].SB[29]
590 make_do_samples(simple, , ,
591 simple_interp_store, simple_interp_get, )
593 static noinline int do_samples_noise(int ch, int ns, int ns_to)
595 int level, shift, bit;
598 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
599 while (s_chan[ch].spos >= 28*0x10000)
604 s_chan[ch].spos -= 28*0x10000;
607 // modified from DrHell/shalma, no fraction
608 level = (spu.spuCtrl >> 10) & 0x0f;
609 level = 0x8000 >> level;
611 for (; ns < ns_to; ns++)
613 spu.dwNoiseCount += 2;
614 if (spu.dwNoiseCount >= level)
616 spu.dwNoiseCount -= level;
617 shift = (spu.dwNoiseVal >> 10) & 0x1f;
618 bit = (0x69696969 >> shift) & 1;
619 if (spu.dwNoiseVal & 0x8000)
621 spu.dwNoiseVal = (spu.dwNoiseVal << 1) | bit;
624 ChanBuf[ns] = (signed short)spu.dwNoiseVal;
631 // asm code; lv and rv must be 0-3fff
632 extern void mix_chan(int start, int count, int lv, int rv);
633 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
635 static void mix_chan(int start, int count, int lv, int rv)
637 int *dst = SSumLR + start * 2;
638 const int *src = ChanBuf + start;
645 l = (sval * lv) >> 14;
646 r = (sval * rv) >> 14;
652 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
654 int *dst = SSumLR + start * 2;
655 int *drvb = rvb + start * 2;
656 const int *src = ChanBuf + start;
663 l = (sval * lv) >> 14;
664 r = (sval * rv) >> 14;
673 // 0x0800-0x0bff Voice 1
674 // 0x0c00-0x0fff Voice 3
675 static noinline void do_decode_bufs(int which, int start, int count)
677 const int *src = ChanBuf + start;
678 unsigned short *dst = &spu.spuMem[0x800/2 + which*0x400/2];
679 int cursor = spu.decode_pos + start;
684 dst[cursor] = *src++;
688 // decode_pos is updated and irqs are checked later, after voice loop
691 ////////////////////////////////////////////////////////////////////////
693 // here is the main job handler...
694 // basically the whole sound processing is done in this fat func!
695 ////////////////////////////////////////////////////////////////////////
697 void do_samples(unsigned int cycles_to)
699 const int ns_from = 0;
701 int volmult = spu_config.iVolume;
705 cycle_diff = cycles_to - spu.cycles_played;
706 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
708 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
709 spu.cycles_played = cycles_to;
713 if (cycle_diff < 2 * 768)
716 ns_to = (cycle_diff / 768 + 1) & ~1;
717 if (ns_to > NSSIZE) {
718 // should never happen
719 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
723 //////////////////////////////////////////////////////
724 // special irq handling in the decode buffers (0x0000-0x1000)
726 // the decode buffers are located in spu memory in the following way:
727 // 0x0000-0x03ff CD audio left
728 // 0x0400-0x07ff CD audio right
729 // 0x0800-0x0bff Voice 1
730 // 0x0c00-0x0fff Voice 3
731 // and decoded data is 16 bit for one sample
733 // even if voices 1/3 are off or no cd audio is playing, the internal
734 // play positions will move on and wrap after 0x400 bytes.
735 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
736 // increase this pointer on each sample by 2 bytes. If this pointer
737 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
740 if (unlikely((spu.spuCtrl & CTRL_IRQ)
741 && spu.pSpuIrq && spu.pSpuIrq < spu.spuMemC+0x1000))
743 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
744 int left = (irq_pos - spu.decode_pos) & 0x1ff;
745 if (0 < left && left <= ns_to)
747 //xprintf("decoder irq %x\n", spu.decode_pos);
755 silentch=~(spu.dwChannelOn|spu.dwNewChannel);
757 //--------------------------------------------------//
758 //- main channel loop -//
759 //--------------------------------------------------//
761 for(ch=0;ch<MAXCHAN;ch++) // loop em all...
763 if(spu.dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
764 if(!(spu.dwChannelOn&(1<<ch))) continue; // channel not playing? next
766 if(s_chan[ch].bNoise)
767 do_samples_noise(ch, ns_from, ns_to);
768 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && spu_config.iUseInterpolation==0))
769 do_samples_noint(ch, ns_from, ns_to);
770 else if(s_chan[ch].bFMod==0 && spu_config.iUseInterpolation==1)
771 do_samples_simple(ch, ns_from, ns_to);
773 do_samples_default(ch, ns_from, ns_to);
775 ns_len = ns_to - ns_from;
777 MixADSR(ch, ns_from, ns_to);
781 do_decode_bufs(ch/2, ns_from, ns_len);
782 spu.decode_dirty_ch |= 1<<ch;
785 if(s_chan[ch].bFMod==2) // fmod freq channel
786 memcpy(&iFMod[ns_from], &ChanBuf[ns_from], ns_len * sizeof(iFMod[0]));
787 if(s_chan[ch].bRVBActive)
788 mix_chan_rvb(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
790 mix_chan(ns_from, ns_len, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
794 // advance "stopped" channels that can cause irqs
795 // (all chans are always playing on the real thing..)
796 if(spu.spuCtrl&CTRL_IRQ)
797 for(ch=0;ch<MAXCHAN;ch++)
799 if(!(silentch&(1<<ch))) continue; // already handled
800 if(spu.dwChannelDead&(1<<ch)) continue;
801 if(s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
804 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from);
805 while(s_chan[ch].spos >= 28 * 0x10000)
807 unsigned char *start = s_chan[ch].pCurr;
810 if(start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
812 // looping on self or stopped(?)
813 spu.dwChannelDead |= 1<<ch;
818 s_chan[ch].spos -= 28 * 0x10000;
822 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
824 memset(&spu.spuMem[0x800/2], 0, 0x400);
825 spu.decode_dirty_ch &= ~(1<<1);
827 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
829 memset(&spu.spuMem[0xc00/2], 0, 0x400);
830 spu.decode_dirty_ch &= ~(1<<3);
833 //---------------------------------------------------//
834 // mix XA infos (if any)
838 ///////////////////////////////////////////////////////
839 // mix all channels (including reverb) into one buffer
841 if(spu_config.iUseReverb)
844 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
846 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
850 for (ns = 0; ns < ns_to * 2; )
852 d = SSumLR[ns]; SSumLR[ns] = 0;
853 d = d * volmult >> 10;
858 d = SSumLR[ns]; SSumLR[ns] = 0;
859 d = d * volmult >> 10;
865 spu.cycles_played += ns_to * 768;
867 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
871 void schedule_next_irq(void)
873 unsigned int upd_samples;
876 if (spu.scheduleCallback == NULL)
879 upd_samples = 44100 / 50;
881 for (ch = 0; ch < MAXCHAN; ch++)
883 if (spu.dwChannelDead & (1 << ch))
885 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
886 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
889 scan_for_irq(ch, &upd_samples);
892 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
894 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
895 int left = (irq_pos - spu.decode_pos) & 0x1ff;
896 if (0 < left && left < upd_samples) {
897 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
902 if (upd_samples < 44100 / 50)
903 spu.scheduleCallback(upd_samples * 768);
906 // SPU ASYNC... even newer epsxe func
907 // 1 time every 'cycle' cycles... harhar
909 // rearmed: called dynamically now
911 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
915 if (spu.spuCtrl & CTRL_IRQ)
919 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
920 spu.pS = (short *)spu.pSpuBuffer;
922 if (spu_config.iTempo) {
923 if (!out_current->busy())
924 // cause more samples to be generated
925 // (and break some games because of bad sync)
926 spu.cycles_played -= 44100 / 60 / 2 * 768;
931 // SPU UPDATE... new epsxe func
932 // 1 time every 32 hsync lines
933 // (312/32)x50 in pal
934 // (262/32)x60 in ntsc
936 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
937 // leave that func in the linux port, until epsxe linux is using
938 // the async function as well
940 void CALLBACK SPUupdate(void)
946 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
949 if(!xap->freq) return; // no xa freq ? bye
951 FeedXA(xap); // call main XA feeder
955 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
958 if (nbytes<=0) return -1;
960 return FeedCDDA((unsigned char *)pcm, nbytes);
963 // to be called after state load
964 void ClearWorkingState(void)
966 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
967 memset(iFMod,0,sizeof(iFMod));
968 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
971 // SETUPSTREAMS: init most of the spu buffers
972 void SetupStreams(void)
976 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
977 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
978 memset(spu.sRVBStart,0,NSSIZE*2*4);
980 spu.XAStart = // alloc xa buffer
981 (uint32_t *)malloc(44100 * sizeof(uint32_t));
982 spu.XAEnd = spu.XAStart + 44100;
983 spu.XAPlay = spu.XAStart;
984 spu.XAFeed = spu.XAStart;
986 spu.CDDAStart = // alloc cdda buffer
987 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
988 spu.CDDAEnd = spu.CDDAStart + 16384;
989 spu.CDDAPlay = spu.CDDAStart;
990 spu.CDDAFeed = spu.CDDAStart;
992 for(i=0;i<MAXCHAN;i++) // loop sound channels
994 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
995 s_chan[i].ADSRX.SustainIncrease = 1;
996 s_chan[i].pLoop=spu.spuMemC;
997 s_chan[i].pCurr=spu.spuMemC;
1000 ClearWorkingState();
1002 spu.bSpuInit=1; // flag: we are inited
1005 // REMOVESTREAMS: free most buffer
1006 void RemoveStreams(void)
1008 free(spu.pSpuBuffer); // free mixing buffer
1009 spu.pSpuBuffer = NULL;
1010 free(spu.sRVBStart); // free reverb buffer
1011 spu.sRVBStart = NULL;
1012 free(spu.XAStart); // free XA buffer
1014 free(spu.CDDAStart); // free CDDA buffer
1015 spu.CDDAStart = NULL;
1020 // SPUINIT: this func will be called first by the main emu
1021 long CALLBACK SPUinit(void)
1023 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1024 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1027 spu.spuAddr = 0xffffffff;
1029 memset((void *)s_chan, 0, sizeof(s_chan));
1032 SetupStreams(); // prepare streaming
1034 if (spu_config.iVolume == 0)
1035 spu_config.iVolume = 768; // 1024 is 1.0
1040 // SPUOPEN: called by main emu after init
1041 long CALLBACK SPUopen(void)
1043 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1045 SetupSound(); // setup sound (before init!)
1049 return PSE_SPU_ERR_SUCCESS;
1052 // SPUCLOSE: called before shutdown
1053 long CALLBACK SPUclose(void)
1055 if (!spu.bSPUIsOpen) return 0; // some security
1057 spu.bSPUIsOpen = 0; // no more open
1059 out_current->finish(); // no more sound handling
1064 // SPUSHUTDOWN: called by main emu on final exit
1065 long CALLBACK SPUshutdown(void)
1068 RemoveStreams(); // no more streaming
1074 // SPUTEST: we don't test, we are always fine ;)
1075 long CALLBACK SPUtest(void)
1080 // SPUCONFIGURE: call config dialog
1081 long CALLBACK SPUconfigure(void)
1086 // StartCfgTool("CFG");
1091 // SPUABOUT: show about window
1092 void CALLBACK SPUabout(void)
1097 // StartCfgTool("ABOUT");
1102 // this functions will be called once,
1103 // passes a callback that should be called on SPU-IRQ/cdda volume change
1104 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1106 spu.irqCallback = callback;
1109 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1111 spu.cddavCallback = CDDAVcallback;
1114 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1116 spu.scheduleCallback = callback;
1119 // COMMON PLUGIN INFO FUNCS
1121 char * CALLBACK PSEgetLibName(void)
1123 return _(libraryName);
1126 unsigned long CALLBACK PSEgetLibType(void)
1131 unsigned long CALLBACK PSEgetLibVersion(void)
1133 return (1 << 16) | (6 << 8);
1136 char * SPUgetLibInfos(void)
1138 return _(libraryInfo);
1143 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1145 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1147 for(;ch<MAXCHAN;ch++)
1149 if (!(spu.dwChannelOn & (1<<ch)))
1151 if (s_chan[ch].bFMod == 2)
1152 fmod_chans |= 1 << ch;
1153 if (s_chan[ch].bNoise)
1154 noise_chans |= 1 << ch;
1155 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1156 irq_chans |= 1 << ch;
1159 *chans_out = spu.dwChannelOn;
1160 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1161 *fmod_chans_out = fmod_chans;
1162 *noise_chans_out = noise_chans;
1165 // vim:shiftwidth=1:expandtab