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 s_chan[ch].prevflags=2;
243 s_chan[ch].SB[26]=0; // init mixing vars
245 s_chan[ch].iSBPos=27;
248 s_chan[ch].SB[29]=0; // init our interpolation helpers
253 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
254 spu.dwChannelOn|=1<<ch;
255 spu.dwChannelDead&=~(1<<ch);
258 ////////////////////////////////////////////////////////////////////////
259 // ALL KIND OF HELPERS
260 ////////////////////////////////////////////////////////////////////////
262 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
264 unsigned int NP=pitch;
267 NP=((32768L+iFMod[ns])*NP)>>15;
269 if(NP>0x3fff) NP=0x3fff;
272 sinc=NP<<4; // calc frequency
273 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
280 ////////////////////////////////////////////////////////////////////////
282 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
284 if(fmod_freq) // fmod freq channel
290 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
298 if(spu_config.iUseInterpolation==1) // simple interpolation
301 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'
304 SB[32] = 1; // -> flag: calc new interolation
306 else SB[29]=fa; // no interpolation
310 ////////////////////////////////////////////////////////////////////////
312 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
316 if(fmod_freq) return SB[29];
318 switch(spu_config.iUseInterpolation)
320 //--------------------------------------------------//
321 case 3: // cubic interpolation
327 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
328 fa *= (xd - (2<<15)) / 6;
330 fa += gval(2) - gval(1) - gval(1) + gval0;
331 fa *= (xd - (1<<15)) >> 1;
333 fa += gval(1) - gval0;
339 //--------------------------------------------------//
340 case 2: // gauss interpolation
343 vl = (spos >> 6) & ~3;
345 vr=(gauss[vl]*(int)gval0)&~2047;
346 vr+=(gauss[vl+1]*gval(1))&~2047;
347 vr+=(gauss[vl+2]*gval(2))&~2047;
348 vr+=(gauss[vl+3]*gval(3))&~2047;
351 //--------------------------------------------------//
352 case 1: // simple interpolation
354 if(sinc<0x10000L) // -> upsampling?
355 InterpolateUp(SB, sinc); // --> interpolate up
356 else InterpolateDown(SB, sinc); // --> else down
359 //--------------------------------------------------//
360 default: // no interpolation
364 //--------------------------------------------------//
370 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
373 int fa, s_1, s_2, d, s;
378 for (nSample = 0; nSample < 28; src++)
381 s = (int)(signed short)((d & 0x0f) << 12);
383 fa = s >> shift_factor;
384 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
387 dest[nSample++] = fa;
389 s = (int)(signed short)((d & 0xf0) << 8);
390 fa = s >> shift_factor;
391 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
394 dest[nSample++] = fa;
398 static int decode_block(int ch, int *SB)
400 unsigned char *start;
401 int predict_nr, shift_factor, flags;
404 start = s_chan[ch].pCurr; // set up the current pos
405 if (start == spu.spuMemC) // ?
408 if (s_chan[ch].prevflags & 1) // 1: stop/loop
410 if (!(s_chan[ch].prevflags & 2))
413 start = s_chan[ch].pLoop;
416 check_irq(ch, start); // hack, see check_irq below..
418 predict_nr = (int)start[0];
419 shift_factor = predict_nr & 0xf;
422 decode_block_data(SB, start + 2, predict_nr, shift_factor);
426 s_chan[ch].pLoop = start; // loop adress
430 if (flags & 1) { // 1: stop/loop
431 start = s_chan[ch].pLoop;
432 check_irq(ch, start); // hack.. :(
435 if (start - spu.spuMemC >= 0x80000)
438 s_chan[ch].pCurr = start; // store values for next cycle
439 s_chan[ch].prevflags = flags;
444 // do block, but ignore sample data
445 static int skip_block(int ch)
447 unsigned char *start = s_chan[ch].pCurr;
451 if (s_chan[ch].prevflags & 1) {
452 if (!(s_chan[ch].prevflags & 2))
455 start = s_chan[ch].pLoop;
458 check_irq(ch, start);
462 s_chan[ch].pLoop = start;
467 start = s_chan[ch].pLoop;
468 check_irq(ch, start);
471 s_chan[ch].pCurr = start;
472 s_chan[ch].prevflags = flags;
477 // if irq is going to trigger sooner than in upd_samples, set upd_samples
478 static void scan_for_irq(int ch, unsigned int *upd_samples)
480 int pos, sinc, sinc_inv, end;
481 unsigned char *block;
484 block = s_chan[ch].pCurr;
485 pos = s_chan[ch].spos;
486 sinc = s_chan[ch].sinc;
487 end = pos + *upd_samples * sinc;
489 pos += (28 - s_chan[ch].iSBPos) << 16;
492 if (block == spu.pSpuIrq)
496 if (flags & 1) { // 1: stop/loop
497 block = s_chan[ch].pLoop;
498 if (block == spu.pSpuIrq) // hack.. (see decode_block)
506 sinc_inv = s_chan[ch].sinc_inv;
508 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
510 pos -= s_chan[ch].spos;
511 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
512 //xprintf("ch%02d: irq sched: %3d %03d\n",
513 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
517 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
518 static noinline int do_samples_##name(int ch, int ns_to) \
520 int sinc = s_chan[ch].sinc; \
521 int spos = s_chan[ch].spos; \
522 int sbpos = s_chan[ch].iSBPos; \
523 int *SB = s_chan[ch].SB; \
528 for (ns = 0; ns < ns_to; ns++) \
533 while (spos >= 0x10000) \
539 d = decode_block(ch, SB); \
551 s_chan[ch].sinc = sinc; \
552 s_chan[ch].spos = spos; \
553 s_chan[ch].iSBPos = sbpos; \
559 #define fmod_recv_check \
560 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
561 sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns)
563 make_do_samples(default, fmod_recv_check, ,
564 StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2),
565 ChanBuf[ns] = iGetInterpolationVal(SB, sinc, spos, s_chan[ch].bFMod==2), )
566 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
568 #define simple_interp_store \
575 #define simple_interp_get \
576 if(sinc<0x10000) /* -> upsampling? */ \
577 InterpolateUp(SB, sinc); /* --> interpolate up */ \
578 else InterpolateDown(SB, sinc); /* --> else down */ \
581 make_do_samples(simple, , ,
582 simple_interp_store, simple_interp_get, )
584 static int do_samples_skip(int ch, int ns_to)
586 int ret = ns_to, ns, d;
588 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
590 for (ns = 0; ns < ns_to; ns++)
592 s_chan[ch].spos += s_chan[ch].sinc;
593 while (s_chan[ch].spos >= 28*0x10000)
598 s_chan[ch].spos -= 28*0x10000;
602 s_chan[ch].iSBPos = s_chan[ch].spos >> 16;
603 s_chan[ch].spos &= 0xffff;
608 static void do_lsfr_samples(int ns_to, int ctrl,
609 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
611 unsigned int counter = *dwNoiseCount;
612 unsigned int val = *dwNoiseVal;
613 unsigned int level, shift, bit;
616 // modified from DrHell/shalma, no fraction
617 level = (ctrl >> 10) & 0x0f;
618 level = 0x8000 >> level;
620 for (ns = 0; ns < ns_to; ns++)
623 if (counter >= level)
626 shift = (val >> 10) & 0x1f;
627 bit = (0x69696969 >> shift) & 1;
628 bit ^= (val >> 15) & 1;
629 val = (val << 1) | bit;
632 ChanBuf[ns] = (signed short)val;
635 *dwNoiseCount = counter;
639 static int do_samples_noise(int ch, int ns_to)
643 ret = do_samples_skip(ch, ns_to);
645 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
651 // asm code; lv and rv must be 0-3fff
652 extern void mix_chan(int start, int count, int lv, int rv);
653 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
655 static void mix_chan(int start, int count, int lv, int rv)
657 int *dst = SSumLR + start * 2;
658 const int *src = ChanBuf + start;
665 l = (sval * lv) >> 14;
666 r = (sval * rv) >> 14;
672 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
674 int *dst = SSumLR + start * 2;
675 int *drvb = rvb + start * 2;
676 const int *src = ChanBuf + start;
683 l = (sval * lv) >> 14;
684 r = (sval * rv) >> 14;
693 // 0x0800-0x0bff Voice 1
694 // 0x0c00-0x0fff Voice 3
695 static noinline void do_decode_bufs(unsigned short *mem, int which,
696 int count, int decode_pos)
698 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
699 const int *src = ChanBuf;
700 int cursor = decode_pos;
705 dst[cursor] = *src++;
709 // decode_pos is updated and irqs are checked later, after voice loop
712 static void do_silent_chans(int ns_to, int silentch)
716 for (ch = 0; ch < MAXCHAN; ch++)
718 if (!(silentch & (1<<ch))) continue; // already handled
719 if (spu.dwChannelDead & (1<<ch)) continue;
720 if (s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
723 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
724 s_chan[ch].iSBPos = 0;
726 s_chan[ch].spos += s_chan[ch].sinc * ns_to;
727 while (s_chan[ch].spos >= 28 * 0x10000)
729 unsigned char *start = s_chan[ch].pCurr;
732 if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
734 // looping on self or stopped(?)
735 spu.dwChannelDead |= 1<<ch;
740 s_chan[ch].spos -= 28 * 0x10000;
745 static void do_channels(int ns_to)
752 mask = spu.dwChannelOn & 0xffffff;
753 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
755 if (!(mask & 1)) continue; // channel not playing? next
757 if (s_chan[ch].bNoise)
758 d = do_samples_noise(ch, ns_to);
759 else if (s_chan[ch].bFMod == 2
760 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
761 d = do_samples_noint(ch, ns_to);
762 else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
763 d = do_samples_simple(ch, ns_to);
765 d = do_samples_default(ch, ns_to);
767 d = MixADSR(&s_chan[ch].ADSRX, d);
769 spu.dwChannelOn &= ~(1 << ch);
770 s_chan[ch].ADSRX.EnvelopeVol = 0;
771 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
774 if (ch == 1 || ch == 3)
776 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
777 spu.decode_dirty_ch |= 1 << ch;
780 if (s_chan[ch].bFMod == 2) // fmod freq channel
781 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
782 if (s_chan[ch].bRVBActive)
783 mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
785 mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
789 ////////////////////////////////////////////////////////////////////////
791 // here is the main job handler...
792 ////////////////////////////////////////////////////////////////////////
794 void do_samples_finish(int ns_to, int silentch);
796 void do_samples(unsigned int cycles_to)
803 cycle_diff = cycles_to - spu.cycles_played;
804 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
806 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
807 spu.cycles_played = cycles_to;
811 if (cycle_diff < 2 * 768)
814 ns_to = (cycle_diff / 768 + 1) & ~1;
815 if (ns_to > NSSIZE) {
816 // should never happen
817 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
821 //////////////////////////////////////////////////////
822 // special irq handling in the decode buffers (0x0000-0x1000)
824 // the decode buffers are located in spu memory in the following way:
825 // 0x0000-0x03ff CD audio left
826 // 0x0400-0x07ff CD audio right
827 // 0x0800-0x0bff Voice 1
828 // 0x0c00-0x0fff Voice 3
829 // and decoded data is 16 bit for one sample
831 // even if voices 1/3 are off or no cd audio is playing, the internal
832 // play positions will move on and wrap after 0x400 bytes.
833 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
834 // increase this pointer on each sample by 2 bytes. If this pointer
835 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
838 if (unlikely((spu.spuCtrl & CTRL_IRQ)
839 && spu.pSpuIrq < spu.spuMemC+0x1000))
841 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
842 int left = (irq_pos - spu.decode_pos) & 0x1ff;
843 if (0 < left && left <= ns_to)
845 //xprintf("decoder irq %x\n", spu.decode_pos);
850 silentch = ~(spu.dwChannelOn|spu.dwNewChannel);
852 mask = spu.dwNewChannel & 0xffffff;
853 for (ch = 0; mask != 0; ch++, mask >>= 1) {
858 if (spu.dwChannelOn == 0)
864 do_samples_finish(ns_to, silentch);
866 // advance "stopped" channels that can cause irqs
867 // (all chans are always playing on the real thing..)
868 if (spu.spuCtrl & CTRL_IRQ)
869 do_silent_chans(ns_to, silentch);
871 spu.cycles_played += ns_to * 768;
872 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
875 void do_samples_finish(int ns_to, int silentch)
877 int volmult = spu_config.iVolume;
881 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
883 memset(&spu.spuMem[0x800/2], 0, 0x400);
884 spu.decode_dirty_ch &= ~(1<<1);
886 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
888 memset(&spu.spuMem[0xc00/2], 0, 0x400);
889 spu.decode_dirty_ch &= ~(1<<3);
892 //---------------------------------------------------//
893 // mix XA infos (if any)
895 MixXA(ns_to, spu.decode_pos);
897 ///////////////////////////////////////////////////////
898 // mix all channels (including reverb) into one buffer
900 if(spu_config.iUseReverb)
903 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
905 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
909 for (ns = 0; ns < ns_to * 2; )
911 d = SSumLR[ns]; SSumLR[ns] = 0;
912 d = d * volmult >> 10;
917 d = SSumLR[ns]; SSumLR[ns] = 0;
918 d = d * volmult >> 10;
925 void schedule_next_irq(void)
927 unsigned int upd_samples;
930 if (spu.scheduleCallback == NULL)
933 upd_samples = 44100 / 50;
935 for (ch = 0; ch < MAXCHAN; ch++)
937 if (spu.dwChannelDead & (1 << ch))
939 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
940 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
943 scan_for_irq(ch, &upd_samples);
946 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
948 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
949 int left = (irq_pos - spu.decode_pos) & 0x1ff;
950 if (0 < left && left < upd_samples) {
951 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
956 if (upd_samples < 44100 / 50)
957 spu.scheduleCallback(upd_samples * 768);
960 // SPU ASYNC... even newer epsxe func
961 // 1 time every 'cycle' cycles... harhar
963 // rearmed: called dynamically now
965 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
969 if (spu.spuCtrl & CTRL_IRQ)
973 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
974 spu.pS = (short *)spu.pSpuBuffer;
976 if (spu_config.iTempo) {
977 if (!out_current->busy())
978 // cause more samples to be generated
979 // (and break some games because of bad sync)
980 spu.cycles_played -= 44100 / 60 / 2 * 768;
985 // SPU UPDATE... new epsxe func
986 // 1 time every 32 hsync lines
987 // (312/32)x50 in pal
988 // (262/32)x60 in ntsc
990 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
991 // leave that func in the linux port, until epsxe linux is using
992 // the async function as well
994 void CALLBACK SPUupdate(void)
1000 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1003 if(!xap->freq) return; // no xa freq ? bye
1005 FeedXA(xap); // call main XA feeder
1009 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1011 if (!pcm) return -1;
1012 if (nbytes<=0) return -1;
1014 return FeedCDDA((unsigned char *)pcm, nbytes);
1017 // to be called after state load
1018 void ClearWorkingState(void)
1020 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1021 memset(iFMod,0,sizeof(iFMod));
1022 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1025 // SETUPSTREAMS: init most of the spu buffers
1026 void SetupStreams(void)
1030 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1031 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
1032 memset(spu.sRVBStart,0,NSSIZE*2*4);
1034 spu.XAStart = // alloc xa buffer
1035 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1036 spu.XAEnd = spu.XAStart + 44100;
1037 spu.XAPlay = spu.XAStart;
1038 spu.XAFeed = spu.XAStart;
1040 spu.CDDAStart = // alloc cdda buffer
1041 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1042 spu.CDDAEnd = spu.CDDAStart + 16384;
1043 spu.CDDAPlay = spu.CDDAStart;
1044 spu.CDDAFeed = spu.CDDAStart;
1046 for(i=0;i<MAXCHAN;i++) // loop sound channels
1048 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1049 s_chan[i].ADSRX.SustainIncrease = 1;
1050 s_chan[i].pLoop=spu.spuMemC;
1051 s_chan[i].pCurr=spu.spuMemC;
1054 ClearWorkingState();
1056 spu.bSpuInit=1; // flag: we are inited
1059 // REMOVESTREAMS: free most buffer
1060 void RemoveStreams(void)
1062 free(spu.pSpuBuffer); // free mixing buffer
1063 spu.pSpuBuffer = NULL;
1064 free(spu.sRVBStart); // free reverb buffer
1065 spu.sRVBStart = NULL;
1066 free(spu.XAStart); // free XA buffer
1068 free(spu.CDDAStart); // free CDDA buffer
1069 spu.CDDAStart = NULL;
1074 // SPUINIT: this func will be called first by the main emu
1075 long CALLBACK SPUinit(void)
1077 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1078 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1081 spu.spuAddr = 0xffffffff;
1083 memset((void *)s_chan, 0, sizeof(s_chan));
1084 spu.pSpuIrq = spu.spuMemC;
1086 SetupStreams(); // prepare streaming
1088 if (spu_config.iVolume == 0)
1089 spu_config.iVolume = 768; // 1024 is 1.0
1094 // SPUOPEN: called by main emu after init
1095 long CALLBACK SPUopen(void)
1097 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1099 SetupSound(); // setup sound (before init!)
1103 return PSE_SPU_ERR_SUCCESS;
1106 // SPUCLOSE: called before shutdown
1107 long CALLBACK SPUclose(void)
1109 if (!spu.bSPUIsOpen) return 0; // some security
1111 spu.bSPUIsOpen = 0; // no more open
1113 out_current->finish(); // no more sound handling
1118 // SPUSHUTDOWN: called by main emu on final exit
1119 long CALLBACK SPUshutdown(void)
1122 RemoveStreams(); // no more streaming
1128 // SPUTEST: we don't test, we are always fine ;)
1129 long CALLBACK SPUtest(void)
1134 // SPUCONFIGURE: call config dialog
1135 long CALLBACK SPUconfigure(void)
1140 // StartCfgTool("CFG");
1145 // SPUABOUT: show about window
1146 void CALLBACK SPUabout(void)
1151 // StartCfgTool("ABOUT");
1156 // this functions will be called once,
1157 // passes a callback that should be called on SPU-IRQ/cdda volume change
1158 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1160 spu.irqCallback = callback;
1163 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1165 spu.cddavCallback = CDDAVcallback;
1168 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1170 spu.scheduleCallback = callback;
1173 // COMMON PLUGIN INFO FUNCS
1175 char * CALLBACK PSEgetLibName(void)
1177 return _(libraryName);
1180 unsigned long CALLBACK PSEgetLibType(void)
1185 unsigned long CALLBACK PSEgetLibVersion(void)
1187 return (1 << 16) | (6 << 8);
1190 char * SPUgetLibInfos(void)
1192 return _(libraryInfo);
1197 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1199 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1201 for(;ch<MAXCHAN;ch++)
1203 if (!(spu.dwChannelOn & (1<<ch)))
1205 if (s_chan[ch].bFMod == 2)
1206 fmod_chans |= 1 << ch;
1207 if (s_chan[ch].bNoise)
1208 noise_chans |= 1 << ch;
1209 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1210 irq_chans |= 1 << ch;
1213 *chans_out = spu.dwChannelOn;
1214 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1215 *fmod_chans_out = fmod_chans;
1216 *noise_chans_out = noise_chans;
1219 // vim:shiftwidth=1:expandtab