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:
142 INLINE void InterpolateUp(int ch)
144 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
146 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
147 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
151 if(id1>0) // curr delta positive
154 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
157 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
159 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
161 else // curr delta negative
164 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
167 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
169 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
173 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
177 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
178 //if(s_chan[ch].sinc<=0x8000)
179 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
181 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
183 else // no flags? add bigger val (if possible), calc smaller step, set flag1
184 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
188 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
191 INLINE void InterpolateDown(int ch)
193 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
195 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
196 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
197 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
201 ////////////////////////////////////////////////////////////////////////
202 // helpers for gauss interpolation
204 #define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos&3])
205 #define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
209 ////////////////////////////////////////////////////////////////////////
213 static void do_irq(void)
215 //if(!(spu.spuStat & STAT_IRQ))
217 spu.spuStat |= STAT_IRQ; // asserted status?
218 if(spu.irqCallback) spu.irqCallback();
222 static int check_irq(int ch, unsigned char *pos)
224 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
226 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
233 ////////////////////////////////////////////////////////////////////////
234 // START SOUND... called by main thread to setup a new sound on a channel
235 ////////////////////////////////////////////////////////////////////////
237 INLINE void StartSound(int ch)
242 // fussy timing issues - do in VoiceOn
243 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
244 //s_chan[ch].bStop=0;
247 s_chan[ch].SB[26]=0; // init mixing vars
249 s_chan[ch].iSBPos=27;
252 s_chan[ch].SB[29]=0; // init our interpolation helpers
257 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
260 ////////////////////////////////////////////////////////////////////////
261 // ALL KIND OF HELPERS
262 ////////////////////////////////////////////////////////////////////////
264 INLINE int FModChangeFrequency(int ch,int ns)
266 unsigned int NP=s_chan[ch].iRawPitch;
269 NP=((32768L+iFMod[ns])*NP)/32768L;
271 if(NP>0x3fff) NP=0x3fff;
274 sinc=NP<<4; // calc frequency
275 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
282 ////////////////////////////////////////////////////////////////////////
284 INLINE void StoreInterpolationVal(int ch,int fa)
286 if(s_chan[ch].bFMod==2) // fmod freq channel
287 s_chan[ch].SB[29]=fa;
292 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
294 int gpos = s_chan[ch].SB[28];
297 s_chan[ch].SB[28] = gpos;
300 if(spu_config.iUseInterpolation==1) // simple interpolation
302 s_chan[ch].SB[28] = 0;
303 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'
304 s_chan[ch].SB[30] = s_chan[ch].SB[31];
305 s_chan[ch].SB[31] = fa;
306 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
308 else s_chan[ch].SB[29]=fa; // no interpolation
312 ////////////////////////////////////////////////////////////////////////
314 INLINE int iGetInterpolationVal(int ch, int spos)
318 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
320 switch(spu_config.iUseInterpolation)
322 //--------------------------------------------------//
323 case 3: // cubic interpolation
327 gpos = s_chan[ch].SB[28];
329 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
330 fa *= (xd - (2<<15)) / 6;
332 fa += gval(2) - gval(1) - gval(1) + gval0;
333 fa *= (xd - (1<<15)) >> 1;
335 fa += gval(1) - gval0;
341 //--------------------------------------------------//
342 case 2: // gauss interpolation
345 vl = (spos >> 6) & ~3;
346 gpos = s_chan[ch].SB[28];
347 vr=(gauss[vl]*(int)gval0)&~2047;
348 vr+=(gauss[vl+1]*gval(1))&~2047;
349 vr+=(gauss[vl+2]*gval(2))&~2047;
350 vr+=(gauss[vl+3]*gval(3))&~2047;
353 //--------------------------------------------------//
354 case 1: // simple interpolation
356 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
357 InterpolateUp(ch); // --> interpolate up
358 else InterpolateDown(ch); // --> else down
359 fa=s_chan[ch].SB[29];
361 //--------------------------------------------------//
362 default: // no interpolation
364 fa=s_chan[ch].SB[29];
366 //--------------------------------------------------//
372 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
375 int fa, s_1, s_2, d, s;
380 for (nSample = 0; nSample < 28; src++)
383 s = (int)(signed short)((d & 0x0f) << 12);
385 fa = s >> shift_factor;
386 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
389 dest[nSample++] = fa;
391 s = (int)(signed short)((d & 0xf0) << 8);
392 fa = s >> shift_factor;
393 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
396 dest[nSample++] = fa;
400 static int decode_block(int ch)
402 unsigned char *start;
403 int predict_nr, shift_factor, flags;
406 start = s_chan[ch].pCurr; // set up the current pos
407 if (start == spu.spuMemC) // ?
410 if (s_chan[ch].prevflags & 1) // 1: stop/loop
412 if (!(s_chan[ch].prevflags & 2))
415 start = s_chan[ch].pLoop;
418 check_irq(ch, start); // hack, see check_irq below..
420 predict_nr = (int)start[0];
421 shift_factor = predict_nr & 0xf;
424 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
428 s_chan[ch].pLoop = start; // loop adress
432 if (flags & 1) { // 1: stop/loop
433 start = s_chan[ch].pLoop;
434 check_irq(ch, start); // hack.. :(
437 if (start - spu.spuMemC >= 0x80000)
440 s_chan[ch].pCurr = start; // store values for next cycle
441 s_chan[ch].prevflags = flags;
446 // do block, but ignore sample data
447 static int skip_block(int ch)
449 unsigned char *start = s_chan[ch].pCurr;
453 if (s_chan[ch].prevflags & 1) {
454 if (!(s_chan[ch].prevflags & 2))
457 start = s_chan[ch].pLoop;
460 check_irq(ch, start);
464 s_chan[ch].pLoop = start;
469 start = s_chan[ch].pLoop;
470 check_irq(ch, start);
473 s_chan[ch].pCurr = start;
474 s_chan[ch].prevflags = flags;
479 // if irq is going to trigger sooner than in upd_samples, set upd_samples
480 static void scan_for_irq(int ch, unsigned int *upd_samples)
482 int pos, sinc, sinc_inv, end;
483 unsigned char *block;
486 block = s_chan[ch].pCurr;
487 pos = s_chan[ch].spos;
488 sinc = s_chan[ch].sinc;
489 end = pos + *upd_samples * sinc;
491 pos += (28 - s_chan[ch].iSBPos) << 16;
494 if (block == spu.pSpuIrq)
498 if (flags & 1) { // 1: stop/loop
499 block = s_chan[ch].pLoop;
500 if (block == spu.pSpuIrq) // hack.. (see decode_block)
508 sinc_inv = s_chan[ch].sinc_inv;
510 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
512 pos -= s_chan[ch].spos;
513 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
514 //xprintf("ch%02d: irq sched: %3d %03d\n",
515 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
519 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
520 static noinline int do_samples_##name(int ch, int ns_to) \
522 int sinc = s_chan[ch].sinc; \
523 int spos = s_chan[ch].spos; \
524 int sbpos = s_chan[ch].iSBPos; \
525 int *SB = s_chan[ch].SB; \
530 for (ns = 0; ns < ns_to; ns++) \
535 while (spos >= 0x10000) \
541 d = decode_block(ch); \
553 s_chan[ch].sinc = sinc; \
554 s_chan[ch].spos = spos; \
555 s_chan[ch].iSBPos = sbpos; \
561 #define fmod_recv_check \
562 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
563 sinc = FModChangeFrequency(ch,ns)
565 make_do_samples(default, fmod_recv_check, ,
566 StoreInterpolationVal(ch, fa),
567 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
568 make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
570 #define simple_interp_store \
577 #define simple_interp_get \
578 if(sinc<0x10000) /* -> upsampling? */ \
579 InterpolateUp(ch); /* --> interpolate up */ \
580 else InterpolateDown(ch); /* --> else down */ \
581 ChanBuf[ns] = s_chan[ch].SB[29]
583 make_do_samples(simple, , ,
584 simple_interp_store, simple_interp_get, )
586 static int do_samples_skip(int ch, int ns_to)
588 int ret = ns_to, ns, d;
590 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
592 for (ns = 0; ns < ns_to; ns++)
594 s_chan[ch].spos += s_chan[ch].sinc;
595 while (s_chan[ch].spos >= 28*0x10000)
600 s_chan[ch].spos -= 28*0x10000;
604 s_chan[ch].iSBPos = s_chan[ch].spos >> 16;
605 s_chan[ch].spos &= 0xffff;
610 static void do_lsfr_samples(int ns_to, int ctrl,
611 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
613 unsigned int counter = *dwNoiseCount;
614 unsigned int val = *dwNoiseVal;
615 unsigned int level, shift, bit;
618 // modified from DrHell/shalma, no fraction
619 level = (ctrl >> 10) & 0x0f;
620 level = 0x8000 >> level;
622 for (ns = 0; ns < ns_to; ns++)
625 if (counter >= level)
628 shift = (val >> 10) & 0x1f;
629 bit = (0x69696969 >> shift) & 1;
630 bit ^= (val >> 15) & 1;
631 val = (val << 1) | bit;
634 ChanBuf[ns] = (signed short)val;
637 *dwNoiseCount = counter;
641 static int do_samples_noise(int ch, int ns_to)
645 ret = do_samples_skip(ch, ns_to);
647 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
653 // asm code; lv and rv must be 0-3fff
654 extern void mix_chan(int start, int count, int lv, int rv);
655 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
657 static void mix_chan(int start, int count, int lv, int rv)
659 int *dst = SSumLR + start * 2;
660 const int *src = ChanBuf + start;
667 l = (sval * lv) >> 14;
668 r = (sval * rv) >> 14;
674 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
676 int *dst = SSumLR + start * 2;
677 int *drvb = rvb + start * 2;
678 const int *src = ChanBuf + start;
685 l = (sval * lv) >> 14;
686 r = (sval * rv) >> 14;
695 // 0x0800-0x0bff Voice 1
696 // 0x0c00-0x0fff Voice 3
697 static noinline void do_decode_bufs(unsigned short *mem, int which,
698 int count, int decode_pos)
700 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
701 const int *src = ChanBuf;
702 int cursor = decode_pos;
707 dst[cursor] = *src++;
711 // decode_pos is updated and irqs are checked later, after voice loop
714 static void do_silent_chans(int ns_to, int silentch)
718 for (ch = 0; ch < MAXCHAN; ch++)
720 if (!(silentch & (1<<ch))) continue; // already handled
721 if (spu.dwChannelDead & (1<<ch)) continue;
722 if (s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
725 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
726 s_chan[ch].iSBPos = 0;
728 s_chan[ch].spos += s_chan[ch].sinc * ns_to;
729 while (s_chan[ch].spos >= 28 * 0x10000)
731 unsigned char *start = s_chan[ch].pCurr;
734 if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
736 // looping on self or stopped(?)
737 spu.dwChannelDead |= 1<<ch;
742 s_chan[ch].spos -= 28 * 0x10000;
747 static void do_channels(int ns_to)
754 mask = spu.dwChannelOn & 0xffffff;
755 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
757 if (!(mask & 1)) continue; // channel not playing? next
759 if (s_chan[ch].bNoise)
760 d = do_samples_noise(ch, ns_to);
761 else if (s_chan[ch].bFMod == 2
762 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
763 d = do_samples_noint(ch, ns_to);
764 else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
765 d = do_samples_simple(ch, ns_to);
767 d = do_samples_default(ch, ns_to);
771 spu.dwChannelOn &= ~(1 << ch);
772 s_chan[ch].bStop = 1;
773 s_chan[ch].ADSRX.EnvelopeVol = 0;
774 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
777 if (ch == 1 || ch == 3)
779 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
780 spu.decode_dirty_ch |= 1 << ch;
783 if (s_chan[ch].bFMod == 2) // fmod freq channel
784 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
785 if (s_chan[ch].bRVBActive)
786 mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
788 mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
792 ////////////////////////////////////////////////////////////////////////
794 // here is the main job handler...
795 ////////////////////////////////////////////////////////////////////////
797 void do_samples_finish(int ns_to, int silentch);
799 void do_samples(unsigned int cycles_to)
806 cycle_diff = cycles_to - spu.cycles_played;
807 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
809 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
810 spu.cycles_played = cycles_to;
814 if (cycle_diff < 2 * 768)
817 ns_to = (cycle_diff / 768 + 1) & ~1;
818 if (ns_to > NSSIZE) {
819 // should never happen
820 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
824 //////////////////////////////////////////////////////
825 // special irq handling in the decode buffers (0x0000-0x1000)
827 // the decode buffers are located in spu memory in the following way:
828 // 0x0000-0x03ff CD audio left
829 // 0x0400-0x07ff CD audio right
830 // 0x0800-0x0bff Voice 1
831 // 0x0c00-0x0fff Voice 3
832 // and decoded data is 16 bit for one sample
834 // even if voices 1/3 are off or no cd audio is playing, the internal
835 // play positions will move on and wrap after 0x400 bytes.
836 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
837 // increase this pointer on each sample by 2 bytes. If this pointer
838 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
841 if (unlikely((spu.spuCtrl & CTRL_IRQ)
842 && spu.pSpuIrq < spu.spuMemC+0x1000))
844 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
845 int left = (irq_pos - spu.decode_pos) & 0x1ff;
846 if (0 < left && left <= ns_to)
848 //xprintf("decoder irq %x\n", spu.decode_pos);
853 silentch = ~(spu.dwChannelOn|spu.dwNewChannel);
855 mask = spu.dwNewChannel & 0xffffff;
856 for (ch = 0; mask != 0; ch++, mask >>= 1) {
861 if (spu.dwChannelOn == 0)
867 do_samples_finish(ns_to, silentch);
869 // advance "stopped" channels that can cause irqs
870 // (all chans are always playing on the real thing..)
871 if (spu.spuCtrl & CTRL_IRQ)
872 do_silent_chans(ns_to, silentch);
874 spu.cycles_played += ns_to * 768;
875 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
878 void do_samples_finish(int ns_to, int silentch)
880 int volmult = spu_config.iVolume;
884 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
886 memset(&spu.spuMem[0x800/2], 0, 0x400);
887 spu.decode_dirty_ch &= ~(1<<1);
889 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
891 memset(&spu.spuMem[0xc00/2], 0, 0x400);
892 spu.decode_dirty_ch &= ~(1<<3);
895 //---------------------------------------------------//
896 // mix XA infos (if any)
898 MixXA(ns_to, spu.decode_pos);
900 ///////////////////////////////////////////////////////
901 // mix all channels (including reverb) into one buffer
903 if(spu_config.iUseReverb)
906 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
908 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
912 for (ns = 0; ns < ns_to * 2; )
914 d = SSumLR[ns]; SSumLR[ns] = 0;
915 d = d * volmult >> 10;
920 d = SSumLR[ns]; SSumLR[ns] = 0;
921 d = d * volmult >> 10;
928 void schedule_next_irq(void)
930 unsigned int upd_samples;
933 if (spu.scheduleCallback == NULL)
936 upd_samples = 44100 / 50;
938 for (ch = 0; ch < MAXCHAN; ch++)
940 if (spu.dwChannelDead & (1 << ch))
942 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
943 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
946 scan_for_irq(ch, &upd_samples);
949 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
951 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
952 int left = (irq_pos - spu.decode_pos) & 0x1ff;
953 if (0 < left && left < upd_samples) {
954 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
959 if (upd_samples < 44100 / 50)
960 spu.scheduleCallback(upd_samples * 768);
963 // SPU ASYNC... even newer epsxe func
964 // 1 time every 'cycle' cycles... harhar
966 // rearmed: called dynamically now
968 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
972 if (spu.spuCtrl & CTRL_IRQ)
976 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
977 spu.pS = (short *)spu.pSpuBuffer;
979 if (spu_config.iTempo) {
980 if (!out_current->busy())
981 // cause more samples to be generated
982 // (and break some games because of bad sync)
983 spu.cycles_played -= 44100 / 60 / 2 * 768;
988 // SPU UPDATE... new epsxe func
989 // 1 time every 32 hsync lines
990 // (312/32)x50 in pal
991 // (262/32)x60 in ntsc
993 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
994 // leave that func in the linux port, until epsxe linux is using
995 // the async function as well
997 void CALLBACK SPUupdate(void)
1003 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1006 if(!xap->freq) return; // no xa freq ? bye
1008 FeedXA(xap); // call main XA feeder
1012 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1014 if (!pcm) return -1;
1015 if (nbytes<=0) return -1;
1017 return FeedCDDA((unsigned char *)pcm, nbytes);
1020 // to be called after state load
1021 void ClearWorkingState(void)
1023 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1024 memset(iFMod,0,sizeof(iFMod));
1025 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1028 // SETUPSTREAMS: init most of the spu buffers
1029 void SetupStreams(void)
1033 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1034 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
1035 memset(spu.sRVBStart,0,NSSIZE*2*4);
1037 spu.XAStart = // alloc xa buffer
1038 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1039 spu.XAEnd = spu.XAStart + 44100;
1040 spu.XAPlay = spu.XAStart;
1041 spu.XAFeed = spu.XAStart;
1043 spu.CDDAStart = // alloc cdda buffer
1044 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1045 spu.CDDAEnd = spu.CDDAStart + 16384;
1046 spu.CDDAPlay = spu.CDDAStart;
1047 spu.CDDAFeed = spu.CDDAStart;
1049 for(i=0;i<MAXCHAN;i++) // loop sound channels
1051 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1052 s_chan[i].ADSRX.SustainIncrease = 1;
1053 s_chan[i].pLoop=spu.spuMemC;
1054 s_chan[i].pCurr=spu.spuMemC;
1057 ClearWorkingState();
1059 spu.bSpuInit=1; // flag: we are inited
1062 // REMOVESTREAMS: free most buffer
1063 void RemoveStreams(void)
1065 free(spu.pSpuBuffer); // free mixing buffer
1066 spu.pSpuBuffer = NULL;
1067 free(spu.sRVBStart); // free reverb buffer
1068 spu.sRVBStart = NULL;
1069 free(spu.XAStart); // free XA buffer
1071 free(spu.CDDAStart); // free CDDA buffer
1072 spu.CDDAStart = NULL;
1077 // SPUINIT: this func will be called first by the main emu
1078 long CALLBACK SPUinit(void)
1080 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1081 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1084 spu.spuAddr = 0xffffffff;
1086 memset((void *)s_chan, 0, sizeof(s_chan));
1087 spu.pSpuIrq = spu.spuMemC;
1089 SetupStreams(); // prepare streaming
1091 if (spu_config.iVolume == 0)
1092 spu_config.iVolume = 768; // 1024 is 1.0
1097 // SPUOPEN: called by main emu after init
1098 long CALLBACK SPUopen(void)
1100 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1102 SetupSound(); // setup sound (before init!)
1106 return PSE_SPU_ERR_SUCCESS;
1109 // SPUCLOSE: called before shutdown
1110 long CALLBACK SPUclose(void)
1112 if (!spu.bSPUIsOpen) return 0; // some security
1114 spu.bSPUIsOpen = 0; // no more open
1116 out_current->finish(); // no more sound handling
1121 // SPUSHUTDOWN: called by main emu on final exit
1122 long CALLBACK SPUshutdown(void)
1125 RemoveStreams(); // no more streaming
1131 // SPUTEST: we don't test, we are always fine ;)
1132 long CALLBACK SPUtest(void)
1137 // SPUCONFIGURE: call config dialog
1138 long CALLBACK SPUconfigure(void)
1143 // StartCfgTool("CFG");
1148 // SPUABOUT: show about window
1149 void CALLBACK SPUabout(void)
1154 // StartCfgTool("ABOUT");
1159 // this functions will be called once,
1160 // passes a callback that should be called on SPU-IRQ/cdda volume change
1161 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1163 spu.irqCallback = callback;
1166 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1168 spu.cddavCallback = CDDAVcallback;
1171 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1173 spu.scheduleCallback = callback;
1176 // COMMON PLUGIN INFO FUNCS
1178 char * CALLBACK PSEgetLibName(void)
1180 return _(libraryName);
1183 unsigned long CALLBACK PSEgetLibType(void)
1188 unsigned long CALLBACK PSEgetLibVersion(void)
1190 return (1 << 16) | (6 << 8);
1193 char * SPUgetLibInfos(void)
1195 return _(libraryInfo);
1200 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1202 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1204 for(;ch<MAXCHAN;ch++)
1206 if (!(spu.dwChannelOn & (1<<ch)))
1208 if (s_chan[ch].bFMod == 2)
1209 fmod_chans |= 1 << ch;
1210 if (s_chan[ch].bNoise)
1211 noise_chans |= 1 << ch;
1212 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1213 irq_chans |= 1 << ch;
1216 *chans_out = spu.dwChannelOn;
1217 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1218 *fmod_chans_out = fmod_chans;
1219 *noise_chans_out = noise_chans;
1222 // vim:shiftwidth=1:expandtab