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,2014
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 ***************************************************************************/
22 #include <sys/time.h> // gettimeofday in xa.c
28 #include "externals.h"
29 #include "registers.h"
31 #include "arm_features.h"
32 #include "spu_config.h"
34 #ifdef __ARM_ARCH_7A__
35 #define ssat32_to_16(v) \
36 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
38 #define ssat32_to_16(v) do { \
39 if (v < -32768) v = -32768; \
40 else if (v > 32767) v = 32767; \
44 #define PSXCLK 33868800 /* 33.8688 MHz */
46 // intended to be ~1 frame
47 #define IRQ_NEAR_BLOCKS 32
50 #if defined (USEMACOSX)
51 static char * libraryName = N_("Mac OS X Sound");
52 #elif defined (USEALSA)
53 static char * libraryName = N_("ALSA Sound");
54 #elif defined (USEOSS)
55 static char * libraryName = N_("OSS Sound");
56 #elif defined (USESDL)
57 static char * libraryName = N_("SDL Sound");
58 #elif defined (USEPULSEAUDIO)
59 static char * libraryName = N_("PulseAudio Sound");
61 static char * libraryName = N_("NULL Sound");
64 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 // MAIN infos struct for each channel
74 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
77 // certain globals (were local before, but with the new timeproc I need em global)
79 static const int f[8][2] = { { 0, 0 },
88 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
90 ////////////////////////////////////////////////////////////////////////
92 ////////////////////////////////////////////////////////////////////////
94 // dirty inline func includes
99 ////////////////////////////////////////////////////////////////////////
100 // helpers for simple interpolation
103 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
105 // instead of having n equal sample values in a row like:
109 // we compare the current delta change with the next delta change.
111 // if curr_delta is positive,
113 // - and next delta is smaller (or changing direction):
117 // - and next delta significant (at least twice) bigger:
121 // - and next delta is nearly same:
126 // if curr_delta is negative,
128 // - and next delta is smaller (or changing direction):
132 // - and next delta significant (at least twice) bigger:
136 // - and next delta is nearly same:
141 static void InterpolateUp(int *SB, int sinc)
143 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
145 const int id1=SB[30]-SB[29]; // curr delta to next val
146 const int id2=SB[31]-SB[30]; // and next delta to next-next val :)
150 if(id1>0) // curr delta positive
153 {SB[28]=id1;SB[32]=2;}
156 SB[28]=(id1*sinc)>>16;
158 SB[28]=(id1*sinc)>>17;
160 else // curr delta negative
163 {SB[28]=id1;SB[32]=2;}
166 SB[28]=(id1*sinc)>>16;
168 SB[28]=(id1*sinc)>>17;
172 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
176 SB[28]=(SB[28]*sinc)>>17;
178 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
182 else // no flags? add bigger val (if possible), calc smaller step, set flag1
187 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
190 static void InterpolateDown(int *SB, int sinc)
192 if(sinc>=0x20000L) // we would skip at least one val?
194 SB[29]+=(SB[30]-SB[29])/2; // add easy weight
195 if(sinc>=0x30000L) // we would skip even more vals?
196 SB[29]+=(SB[31]-SB[30])/2; // add additional next weight
200 ////////////////////////////////////////////////////////////////////////
201 // helpers for gauss interpolation
203 #define gval0 (((short*)(&SB[29]))[gpos&3])
204 #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
208 ////////////////////////////////////////////////////////////////////////
212 static void do_irq(void)
214 //if(!(spu.spuStat & STAT_IRQ))
216 spu.spuStat |= STAT_IRQ; // asserted status?
217 if(spu.irqCallback) spu.irqCallback();
221 static int check_irq(int ch, unsigned char *pos)
223 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
225 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
232 ////////////////////////////////////////////////////////////////////////
233 // START SOUND... called by main thread to setup a new sound on a channel
234 ////////////////////////////////////////////////////////////////////////
236 INLINE void StartSound(int ch)
241 // fussy timing issues - do in VoiceOn
242 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
243 //s_chan[ch].bStop=0;
246 s_chan[ch].SB[26]=0; // init mixing vars
248 s_chan[ch].iSBPos=27;
251 s_chan[ch].SB[29]=0; // init our interpolation helpers
256 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
259 ////////////////////////////////////////////////////////////////////////
260 // ALL KIND OF HELPERS
261 ////////////////////////////////////////////////////////////////////////
263 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
265 unsigned int NP=pitch;
268 NP=((32768L+iFMod[ns])*NP)>>15;
270 if(NP>0x3fff) NP=0x3fff;
273 sinc=NP<<4; // calc frequency
274 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
281 ////////////////////////////////////////////////////////////////////////
283 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
285 if(fmod_freq) // fmod freq channel
291 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
299 if(spu_config.iUseInterpolation==1) // simple interpolation
302 SB[29] = 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'
305 SB[32] = 1; // -> flag: calc new interolation
307 else SB[29]=fa; // no interpolation
311 ////////////////////////////////////////////////////////////////////////
313 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
317 if(fmod_freq) return SB[29];
319 switch(spu_config.iUseInterpolation)
321 //--------------------------------------------------//
322 case 3: // cubic interpolation
328 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
329 fa *= (xd - (2<<15)) / 6;
331 fa += gval(2) - gval(1) - gval(1) + gval0;
332 fa *= (xd - (1<<15)) >> 1;
334 fa += gval(1) - gval0;
340 //--------------------------------------------------//
341 case 2: // gauss interpolation
344 vl = (spos >> 6) & ~3;
346 vr=(gauss[vl]*(int)gval0)&~2047;
347 vr+=(gauss[vl+1]*gval(1))&~2047;
348 vr+=(gauss[vl+2]*gval(2))&~2047;
349 vr+=(gauss[vl+3]*gval(3))&~2047;
352 //--------------------------------------------------//
353 case 1: // simple interpolation
355 if(sinc<0x10000L) // -> upsampling?
356 InterpolateUp(SB, sinc); // --> interpolate up
357 else InterpolateDown(SB, sinc); // --> else down
360 //--------------------------------------------------//
361 default: // no interpolation
365 //--------------------------------------------------//
371 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
374 int fa, s_1, s_2, d, s;
379 for (nSample = 0; nSample < 28; src++)
382 s = (int)(signed short)((d & 0x0f) << 12);
384 fa = s >> shift_factor;
385 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
388 dest[nSample++] = fa;
390 s = (int)(signed short)((d & 0xf0) << 8);
391 fa = s >> shift_factor;
392 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
395 dest[nSample++] = fa;
399 static int decode_block(int ch, int *SB)
401 unsigned char *start;
402 int predict_nr, shift_factor, flags;
405 start = s_chan[ch].pCurr; // set up the current pos
406 if (start == spu.spuMemC) // ?
409 if (s_chan[ch].prevflags & 1) // 1: stop/loop
411 if (!(s_chan[ch].prevflags & 2))
414 start = s_chan[ch].pLoop;
417 check_irq(ch, start); // hack, see check_irq below..
419 predict_nr = (int)start[0];
420 shift_factor = predict_nr & 0xf;
423 decode_block_data(SB, start + 2, predict_nr, shift_factor);
427 s_chan[ch].pLoop = start; // loop adress
431 if (flags & 1) { // 1: stop/loop
432 start = s_chan[ch].pLoop;
433 check_irq(ch, start); // hack.. :(
436 if (start - spu.spuMemC >= 0x80000)
439 s_chan[ch].pCurr = start; // store values for next cycle
440 s_chan[ch].prevflags = flags;
445 // do block, but ignore sample data
446 static int skip_block(int ch)
448 unsigned char *start = s_chan[ch].pCurr;
452 if (s_chan[ch].prevflags & 1) {
453 if (!(s_chan[ch].prevflags & 2))
456 start = s_chan[ch].pLoop;
459 check_irq(ch, start);
463 s_chan[ch].pLoop = start;
468 start = s_chan[ch].pLoop;
469 check_irq(ch, start);
472 s_chan[ch].pCurr = start;
473 s_chan[ch].prevflags = flags;
478 // if irq is going to trigger sooner than in upd_samples, set upd_samples
479 static void scan_for_irq(int ch, unsigned int *upd_samples)
481 int pos, sinc, sinc_inv, end;
482 unsigned char *block;
485 block = s_chan[ch].pCurr;
486 pos = s_chan[ch].spos;
487 sinc = s_chan[ch].sinc;
488 end = pos + *upd_samples * sinc;
490 pos += (28 - s_chan[ch].iSBPos) << 16;
493 if (block == spu.pSpuIrq)
497 if (flags & 1) { // 1: stop/loop
498 block = s_chan[ch].pLoop;
499 if (block == spu.pSpuIrq) // hack.. (see decode_block)
507 sinc_inv = s_chan[ch].sinc_inv;
509 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
511 pos -= s_chan[ch].spos;
512 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
513 //xprintf("ch%02d: irq sched: %3d %03d\n",
514 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
518 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
519 static noinline int do_samples_##name(int ch, int ns_to) \
521 int sinc = s_chan[ch].sinc; \
522 int spos = s_chan[ch].spos; \
523 int sbpos = s_chan[ch].iSBPos; \
524 int *SB = s_chan[ch].SB; \
529 for (ns = 0; ns < ns_to; ns++) \
534 while (spos >= 0x10000) \
540 d = decode_block(ch, SB); \
552 s_chan[ch].sinc = sinc; \
553 s_chan[ch].spos = spos; \
554 s_chan[ch].iSBPos = sbpos; \
560 #define fmod_recv_check \
561 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
562 sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns)
564 make_do_samples(default, fmod_recv_check, ,
565 StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2),
566 ChanBuf[ns] = iGetInterpolationVal(SB, sinc, spos, s_chan[ch].bFMod==2), )
567 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
569 #define simple_interp_store \
576 #define simple_interp_get \
577 if(sinc<0x10000) /* -> upsampling? */ \
578 InterpolateUp(SB, sinc); /* --> interpolate up */ \
579 else InterpolateDown(SB, sinc); /* --> else down */ \
582 make_do_samples(simple, , ,
583 simple_interp_store, simple_interp_get, )
585 static int do_samples_skip(int ch, int ns_to)
587 int ret = ns_to, ns, d;
589 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
591 for (ns = 0; ns < ns_to; ns++)
593 s_chan[ch].spos += s_chan[ch].sinc;
594 while (s_chan[ch].spos >= 28*0x10000)
599 s_chan[ch].spos -= 28*0x10000;
603 s_chan[ch].iSBPos = s_chan[ch].spos >> 16;
604 s_chan[ch].spos &= 0xffff;
609 static void do_lsfr_samples(int ns_to, int ctrl,
610 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
612 unsigned int counter = *dwNoiseCount;
613 unsigned int val = *dwNoiseVal;
614 unsigned int level, shift, bit;
617 // modified from DrHell/shalma, no fraction
618 level = (ctrl >> 10) & 0x0f;
619 level = 0x8000 >> level;
621 for (ns = 0; ns < ns_to; ns++)
624 if (counter >= level)
627 shift = (val >> 10) & 0x1f;
628 bit = (0x69696969 >> shift) & 1;
629 bit ^= (val >> 15) & 1;
630 val = (val << 1) | bit;
633 ChanBuf[ns] = (signed short)val;
636 *dwNoiseCount = counter;
640 static int do_samples_noise(int ch, int ns_to)
644 ret = do_samples_skip(ch, ns_to);
646 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
652 // asm code; lv and rv must be 0-3fff
653 extern void mix_chan(int start, int count, int lv, int rv);
654 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
656 static void mix_chan(int start, int count, int lv, int rv)
658 int *dst = SSumLR + start * 2;
659 const int *src = ChanBuf + start;
666 l = (sval * lv) >> 14;
667 r = (sval * rv) >> 14;
673 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
675 int *dst = SSumLR + start * 2;
676 int *drvb = rvb + start * 2;
677 const int *src = ChanBuf + start;
684 l = (sval * lv) >> 14;
685 r = (sval * rv) >> 14;
694 // 0x0800-0x0bff Voice 1
695 // 0x0c00-0x0fff Voice 3
696 static noinline void do_decode_bufs(unsigned short *mem, int which,
697 int count, int decode_pos)
699 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
700 const int *src = ChanBuf;
701 int cursor = decode_pos;
706 dst[cursor] = *src++;
710 // decode_pos is updated and irqs are checked later, after voice loop
713 static void do_silent_chans(int ns_to, int silentch)
717 for (ch = 0; ch < MAXCHAN; ch++)
719 if (!(silentch & (1<<ch))) continue; // already handled
720 if (spu.dwChannelDead & (1<<ch)) continue;
721 if (s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
724 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
725 s_chan[ch].iSBPos = 0;
727 s_chan[ch].spos += s_chan[ch].sinc * ns_to;
728 while (s_chan[ch].spos >= 28 * 0x10000)
730 unsigned char *start = s_chan[ch].pCurr;
733 if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
735 // looping on self or stopped(?)
736 spu.dwChannelDead |= 1<<ch;
741 s_chan[ch].spos -= 28 * 0x10000;
746 static void do_channels(int ns_to)
753 mask = spu.dwChannelOn & 0xffffff;
754 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
756 if (!(mask & 1)) continue; // channel not playing? next
758 if (s_chan[ch].bNoise)
759 d = do_samples_noise(ch, ns_to);
760 else if (s_chan[ch].bFMod == 2
761 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
762 d = do_samples_noint(ch, ns_to);
763 else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
764 d = do_samples_simple(ch, ns_to);
766 d = do_samples_default(ch, ns_to);
770 spu.dwChannelOn &= ~(1 << ch);
771 s_chan[ch].bStop = 1;
772 s_chan[ch].ADSRX.EnvelopeVol = 0;
773 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
776 if (ch == 1 || ch == 3)
778 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
779 spu.decode_dirty_ch |= 1 << ch;
782 if (s_chan[ch].bFMod == 2) // fmod freq channel
783 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
784 if (s_chan[ch].bRVBActive)
785 mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
787 mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
791 ////////////////////////////////////////////////////////////////////////
793 // here is the main job handler...
794 ////////////////////////////////////////////////////////////////////////
796 void do_samples_finish(int ns_to, int silentch);
798 void do_samples(unsigned int cycles_to)
805 cycle_diff = cycles_to - spu.cycles_played;
806 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
808 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
809 spu.cycles_played = cycles_to;
813 if (cycle_diff < 2 * 768)
816 ns_to = (cycle_diff / 768 + 1) & ~1;
817 if (ns_to > NSSIZE) {
818 // should never happen
819 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
823 //////////////////////////////////////////////////////
824 // special irq handling in the decode buffers (0x0000-0x1000)
826 // the decode buffers are located in spu memory in the following way:
827 // 0x0000-0x03ff CD audio left
828 // 0x0400-0x07ff CD audio right
829 // 0x0800-0x0bff Voice 1
830 // 0x0c00-0x0fff Voice 3
831 // and decoded data is 16 bit for one sample
833 // even if voices 1/3 are off or no cd audio is playing, the internal
834 // play positions will move on and wrap after 0x400 bytes.
835 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
836 // increase this pointer on each sample by 2 bytes. If this pointer
837 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
840 if (unlikely((spu.spuCtrl & CTRL_IRQ)
841 && spu.pSpuIrq < spu.spuMemC+0x1000))
843 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
844 int left = (irq_pos - spu.decode_pos) & 0x1ff;
845 if (0 < left && left <= ns_to)
847 //xprintf("decoder irq %x\n", spu.decode_pos);
852 silentch = ~(spu.dwChannelOn|spu.dwNewChannel);
854 mask = spu.dwNewChannel & 0xffffff;
855 for (ch = 0; mask != 0; ch++, mask >>= 1) {
860 if (spu.dwChannelOn == 0)
866 do_samples_finish(ns_to, silentch);
868 // advance "stopped" channels that can cause irqs
869 // (all chans are always playing on the real thing..)
870 if (spu.spuCtrl & CTRL_IRQ)
871 do_silent_chans(ns_to, silentch);
873 spu.cycles_played += ns_to * 768;
874 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
877 void do_samples_finish(int ns_to, int silentch)
879 int volmult = spu_config.iVolume;
883 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
885 memset(&spu.spuMem[0x800/2], 0, 0x400);
886 spu.decode_dirty_ch &= ~(1<<1);
888 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
890 memset(&spu.spuMem[0xc00/2], 0, 0x400);
891 spu.decode_dirty_ch &= ~(1<<3);
894 //---------------------------------------------------//
895 // mix XA infos (if any)
897 MixXA(ns_to, spu.decode_pos);
899 ///////////////////////////////////////////////////////
900 // mix all channels (including reverb) into one buffer
902 if(spu_config.iUseReverb)
905 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
907 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
911 for (ns = 0; ns < ns_to * 2; )
913 d = SSumLR[ns]; SSumLR[ns] = 0;
914 d = d * volmult >> 10;
919 d = SSumLR[ns]; SSumLR[ns] = 0;
920 d = d * volmult >> 10;
927 void schedule_next_irq(void)
929 unsigned int upd_samples;
932 if (spu.scheduleCallback == NULL)
935 upd_samples = 44100 / 50;
937 for (ch = 0; ch < MAXCHAN; ch++)
939 if (spu.dwChannelDead & (1 << ch))
941 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
942 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
945 scan_for_irq(ch, &upd_samples);
948 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
950 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
951 int left = (irq_pos - spu.decode_pos) & 0x1ff;
952 if (0 < left && left < upd_samples) {
953 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
958 if (upd_samples < 44100 / 50)
959 spu.scheduleCallback(upd_samples * 768);
962 // SPU ASYNC... even newer epsxe func
963 // 1 time every 'cycle' cycles... harhar
965 // rearmed: called dynamically now
967 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
971 if (spu.spuCtrl & CTRL_IRQ)
975 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
976 spu.pS = (short *)spu.pSpuBuffer;
978 if (spu_config.iTempo) {
979 if (!out_current->busy())
980 // cause more samples to be generated
981 // (and break some games because of bad sync)
982 spu.cycles_played -= 44100 / 60 / 2 * 768;
987 // SPU UPDATE... new epsxe func
988 // 1 time every 32 hsync lines
989 // (312/32)x50 in pal
990 // (262/32)x60 in ntsc
992 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
993 // leave that func in the linux port, until epsxe linux is using
994 // the async function as well
996 void CALLBACK SPUupdate(void)
1002 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1005 if(!xap->freq) return; // no xa freq ? bye
1007 FeedXA(xap); // call main XA feeder
1011 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1013 if (!pcm) return -1;
1014 if (nbytes<=0) return -1;
1016 return FeedCDDA((unsigned char *)pcm, nbytes);
1019 // to be called after state load
1020 void ClearWorkingState(void)
1022 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1023 memset(iFMod,0,sizeof(iFMod));
1024 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1027 // SETUPSTREAMS: init most of the spu buffers
1028 void SetupStreams(void)
1032 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1033 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
1034 memset(spu.sRVBStart,0,NSSIZE*2*4);
1036 spu.XAStart = // alloc xa buffer
1037 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1038 spu.XAEnd = spu.XAStart + 44100;
1039 spu.XAPlay = spu.XAStart;
1040 spu.XAFeed = spu.XAStart;
1042 spu.CDDAStart = // alloc cdda buffer
1043 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1044 spu.CDDAEnd = spu.CDDAStart + 16384;
1045 spu.CDDAPlay = spu.CDDAStart;
1046 spu.CDDAFeed = spu.CDDAStart;
1048 for(i=0;i<MAXCHAN;i++) // loop sound channels
1050 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1051 s_chan[i].ADSRX.SustainIncrease = 1;
1052 s_chan[i].pLoop=spu.spuMemC;
1053 s_chan[i].pCurr=spu.spuMemC;
1056 ClearWorkingState();
1058 spu.bSpuInit=1; // flag: we are inited
1061 // REMOVESTREAMS: free most buffer
1062 void RemoveStreams(void)
1064 free(spu.pSpuBuffer); // free mixing buffer
1065 spu.pSpuBuffer = NULL;
1066 free(spu.sRVBStart); // free reverb buffer
1067 spu.sRVBStart = NULL;
1068 free(spu.XAStart); // free XA buffer
1070 free(spu.CDDAStart); // free CDDA buffer
1071 spu.CDDAStart = NULL;
1076 // SPUINIT: this func will be called first by the main emu
1077 long CALLBACK SPUinit(void)
1079 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1080 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1083 spu.spuAddr = 0xffffffff;
1085 memset((void *)s_chan, 0, sizeof(s_chan));
1086 spu.pSpuIrq = spu.spuMemC;
1088 SetupStreams(); // prepare streaming
1090 if (spu_config.iVolume == 0)
1091 spu_config.iVolume = 768; // 1024 is 1.0
1096 // SPUOPEN: called by main emu after init
1097 long CALLBACK SPUopen(void)
1099 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1101 SetupSound(); // setup sound (before init!)
1105 return PSE_SPU_ERR_SUCCESS;
1108 // SPUCLOSE: called before shutdown
1109 long CALLBACK SPUclose(void)
1111 if (!spu.bSPUIsOpen) return 0; // some security
1113 spu.bSPUIsOpen = 0; // no more open
1115 out_current->finish(); // no more sound handling
1120 // SPUSHUTDOWN: called by main emu on final exit
1121 long CALLBACK SPUshutdown(void)
1124 RemoveStreams(); // no more streaming
1130 // SPUTEST: we don't test, we are always fine ;)
1131 long CALLBACK SPUtest(void)
1136 // SPUCONFIGURE: call config dialog
1137 long CALLBACK SPUconfigure(void)
1142 // StartCfgTool("CFG");
1147 // SPUABOUT: show about window
1148 void CALLBACK SPUabout(void)
1153 // StartCfgTool("ABOUT");
1158 // this functions will be called once,
1159 // passes a callback that should be called on SPU-IRQ/cdda volume change
1160 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1162 spu.irqCallback = callback;
1165 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1167 spu.cddavCallback = CDDAVcallback;
1170 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1172 spu.scheduleCallback = callback;
1175 // COMMON PLUGIN INFO FUNCS
1177 char * CALLBACK PSEgetLibName(void)
1179 return _(libraryName);
1182 unsigned long CALLBACK PSEgetLibType(void)
1187 unsigned long CALLBACK PSEgetLibVersion(void)
1189 return (1 << 16) | (6 << 8);
1192 char * SPUgetLibInfos(void)
1194 return _(libraryInfo);
1199 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1201 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1203 for(;ch<MAXCHAN;ch++)
1205 if (!(spu.dwChannelOn & (1<<ch)))
1207 if (s_chan[ch].bFMod == 2)
1208 fmod_chans |= 1 << ch;
1209 if (s_chan[ch].bNoise)
1210 noise_chans |= 1 << ch;
1211 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1212 irq_chans |= 1 << ch;
1215 *chans_out = spu.dwChannelOn;
1216 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1217 *fmod_chans_out = fmod_chans;
1218 *noise_chans_out = noise_chans;
1221 // vim:shiftwidth=1:expandtab