spu: remove all threading code
[pcsx_rearmed.git] / plugins / dfsound / spu.c
... / ...
CommitLineData
1/***************************************************************************
2 spu.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2011
9
10 ***************************************************************************/
11/***************************************************************************
12 * *
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. *
18 * *
19 ***************************************************************************/
20
21#include "stdafx.h"
22
23#define _IN_SPU
24
25#include "externals.h"
26#include "registers.h"
27#include "dsoundoss.h"
28
29#ifdef ENABLE_NLS
30#include <libintl.h>
31#include <locale.h>
32#define _(x) gettext(x)
33#define N_(x) (x)
34#else
35#define _(x) (x)
36#define N_(x) (x)
37#endif
38
39#ifdef __ARM_ARCH_7A__
40 #define ssat32_to_16(v) \
41 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
42#else
43 #define ssat32_to_16(v) do { \
44 if (v < -32768) v = -32768; \
45 else if (v > 32767) v = 32767; \
46 } while (0)
47#endif
48
49/*
50#if defined (USEMACOSX)
51static char * libraryName = N_("Mac OS X Sound");
52#elif defined (USEALSA)
53static char * libraryName = N_("ALSA Sound");
54#elif defined (USEOSS)
55static char * libraryName = N_("OSS Sound");
56#elif defined (USESDL)
57static char * libraryName = N_("SDL Sound");
58#elif defined (USEPULSEAUDIO)
59static char * libraryName = N_("PulseAudio Sound");
60#else
61static char * libraryName = N_("NULL Sound");
62#endif
63
64static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
65*/
66
67// globals
68
69// psx buffer / addresses
70
71unsigned short regArea[10000];
72unsigned short spuMem[256*1024];
73unsigned char * spuMemC;
74unsigned char * pSpuIrq=0;
75unsigned char * pSpuBuffer;
76unsigned char * pMixIrq=0;
77
78// user settings
79
80int iVolume=768; // 1024 is 1.0
81int iXAPitch=1;
82int iSPUIRQWait=1;
83int iDebugMode=0;
84int iRecordMode=0;
85int iUseReverb=2;
86int iUseInterpolation=2;
87
88// MAIN infos struct for each channel
89
90SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
91REVERBInfo rvb;
92
93unsigned int dwNoiseVal; // global noise generator
94unsigned int dwNoiseCount;
95int iSpuAsyncWait=0;
96
97unsigned short spuCtrl=0; // some vars to store psx reg infos
98unsigned short spuStat=0;
99unsigned short spuIrq=0;
100unsigned long spuAddr=0xffffffff; // address into spu mem
101int bSpuInit=0;
102int bSPUIsOpen=0;
103
104unsigned int dwNewChannel=0; // flags for faster testing, if new channel starts
105unsigned int dwChannelOn=0; // not silent channels
106unsigned int dwPendingChanOff=0;
107unsigned int dwChannelDead=0; // silent+not useful channels
108
109void (CALLBACK *irqCallback)(void)=0; // func of main emu, called on spu irq
110void (CALLBACK *cddavCallback)(unsigned short,unsigned short)=0;
111
112// certain globals (were local before, but with the new timeproc I need em global)
113
114static const int f[8][2] = { { 0, 0 },
115 { 60, 0 },
116 { 115, -52 },
117 { 98, -55 },
118 { 122, -60 } };
119int ChanBuf[NSSIZE+3];
120int SSumLR[(NSSIZE+3)*2];
121int iFMod[NSSIZE];
122int iCycle = 0;
123short * pS;
124
125int lastch=-1; // last channel processed on spu irq in timer mode
126static int lastns=0; // last ns pos
127static int iSecureStart=0; // secure start counter
128
129#define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
130
131////////////////////////////////////////////////////////////////////////
132// CODE AREA
133////////////////////////////////////////////////////////////////////////
134
135// dirty inline func includes
136
137#include "reverb.c"
138#include "adsr.c"
139
140////////////////////////////////////////////////////////////////////////
141// helpers for simple interpolation
142
143//
144// easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
145//
146// instead of having n equal sample values in a row like:
147// ____
148// |____
149//
150// we compare the current delta change with the next delta change.
151//
152// if curr_delta is positive,
153//
154// - and next delta is smaller (or changing direction):
155// \.
156// -__
157//
158// - and next delta significant (at least twice) bigger:
159// --_
160// \.
161//
162// - and next delta is nearly same:
163// \.
164// \.
165//
166//
167// if curr_delta is negative,
168//
169// - and next delta is smaller (or changing direction):
170// _--
171// /
172//
173// - and next delta significant (at least twice) bigger:
174// /
175// __-
176//
177// - and next delta is nearly same:
178// /
179// /
180//
181
182
183INLINE void InterpolateUp(int ch)
184{
185 if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
186 {
187 const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val
188 const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :)
189
190 s_chan[ch].SB[32]=0;
191
192 if(id1>0) // curr delta positive
193 {
194 if(id2<id1)
195 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
196 else
197 if(id2<(id1<<1))
198 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
199 else
200 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
201 }
202 else // curr delta negative
203 {
204 if(id2>id1)
205 {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;}
206 else
207 if(id2>(id1<<1))
208 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L;
209 else
210 s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L;
211 }
212 }
213 else
214 if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
215 {
216 s_chan[ch].SB[32]=0;
217
218 s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L;
219 //if(s_chan[ch].sinc<=0x8000)
220 // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1));
221 //else
222 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
223 }
224 else // no flags? add bigger val (if possible), calc smaller step, set flag1
225 s_chan[ch].SB[29]+=s_chan[ch].SB[28];
226}
227
228//
229// even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
230//
231
232INLINE void InterpolateDown(int ch)
233{
234 if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val?
235 {
236 s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight
237 if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals?
238 s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight
239 }
240}
241
242////////////////////////////////////////////////////////////////////////
243// helpers for gauss interpolation
244
245#define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos])
246#define gval(x) (((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3])
247
248#include "gauss_i.h"
249
250////////////////////////////////////////////////////////////////////////
251
252#include "xa.c"
253
254////////////////////////////////////////////////////////////////////////
255// START SOUND... called by main thread to setup a new sound on a channel
256////////////////////////////////////////////////////////////////////////
257
258INLINE void StartSound(int ch)
259{
260 StartADSR(ch);
261 StartREVERB(ch);
262
263 // fussy timing issues - do in VoiceOn
264 //s_chan[ch].pCurr=s_chan[ch].pStart; // set sample start
265 //s_chan[ch].bStop=0;
266 //s_chan[ch].bOn=1;
267
268 s_chan[ch].SB[26]=0; // init mixing vars
269 s_chan[ch].SB[27]=0;
270 s_chan[ch].iSBPos=28;
271
272 s_chan[ch].SB[29]=0; // init our interpolation helpers
273 s_chan[ch].SB[30]=0;
274
275 if(iUseInterpolation>=2) // gauss interpolation?
276 {s_chan[ch].spos=0x30000L;s_chan[ch].SB[28]=0;} // -> start with more decoding
277 else {s_chan[ch].spos=0x10000L;s_chan[ch].SB[31]=0;} // -> no/simple interpolation starts with one 44100 decoding
278
279 dwNewChannel&=~(1<<ch); // clear new channel bit
280}
281
282////////////////////////////////////////////////////////////////////////
283// ALL KIND OF HELPERS
284////////////////////////////////////////////////////////////////////////
285
286INLINE int FModChangeFrequency(int ch,int ns)
287{
288 unsigned int NP=s_chan[ch].iRawPitch;
289 int sinc;
290
291 NP=((32768L+iFMod[ns])*NP)/32768L;
292
293 if(NP>0x3fff) NP=0x3fff;
294 if(NP<0x1) NP=0x1;
295
296 sinc=NP<<4; // calc frequency
297 if(iUseInterpolation==1) // freq change in simple interpolation mode
298 s_chan[ch].SB[32]=1;
299 iFMod[ns]=0;
300
301 return sinc;
302}
303
304////////////////////////////////////////////////////////////////////////
305
306INLINE void StoreInterpolationVal(int ch,int fa)
307{
308 if(s_chan[ch].bFMod==2) // fmod freq channel
309 s_chan[ch].SB[29]=fa;
310 else
311 {
312 ssat32_to_16(fa);
313
314 if(iUseInterpolation>=2) // gauss/cubic interpolation
315 {
316 int gpos = s_chan[ch].SB[28];
317 gval0 = fa;
318 gpos = (gpos+1) & 3;
319 s_chan[ch].SB[28] = gpos;
320 }
321 else
322 if(iUseInterpolation==1) // simple interpolation
323 {
324 s_chan[ch].SB[28] = 0;
325 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'
326 s_chan[ch].SB[30] = s_chan[ch].SB[31];
327 s_chan[ch].SB[31] = fa;
328 s_chan[ch].SB[32] = 1; // -> flag: calc new interolation
329 }
330 else s_chan[ch].SB[29]=fa; // no interpolation
331 }
332}
333
334////////////////////////////////////////////////////////////////////////
335
336INLINE int iGetInterpolationVal(int ch, int spos)
337{
338 int fa;
339
340 if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29];
341
342 switch(iUseInterpolation)
343 {
344 //--------------------------------------------------//
345 case 3: // cubic interpolation
346 {
347 long xd;int gpos;
348 xd = (spos >> 1)+1;
349 gpos = s_chan[ch].SB[28];
350
351 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
352 fa *= (xd - (2<<15)) / 6;
353 fa >>= 15;
354 fa += gval(2) - gval(1) - gval(1) + gval0;
355 fa *= (xd - (1<<15)) >> 1;
356 fa >>= 15;
357 fa += gval(1) - gval0;
358 fa *= xd;
359 fa >>= 15;
360 fa = fa + gval0;
361
362 } break;
363 //--------------------------------------------------//
364 case 2: // gauss interpolation
365 {
366 int vl, vr;int gpos;
367 vl = (spos >> 6) & ~3;
368 gpos = s_chan[ch].SB[28];
369 vr=(gauss[vl]*gval0)&~2047;
370 vr+=(gauss[vl+1]*gval(1))&~2047;
371 vr+=(gauss[vl+2]*gval(2))&~2047;
372 vr+=(gauss[vl+3]*gval(3))&~2047;
373 fa = vr>>11;
374 } break;
375 //--------------------------------------------------//
376 case 1: // simple interpolation
377 {
378 if(s_chan[ch].sinc<0x10000L) // -> upsampling?
379 InterpolateUp(ch); // --> interpolate up
380 else InterpolateDown(ch); // --> else down
381 fa=s_chan[ch].SB[29];
382 } break;
383 //--------------------------------------------------//
384 default: // no interpolation
385 {
386 fa=s_chan[ch].SB[29];
387 } break;
388 //--------------------------------------------------//
389 }
390
391 return fa;
392}
393
394static void do_irq(void)
395{
396 //if(!(spuStat & STAT_IRQ))
397 {
398 spuStat |= STAT_IRQ; // asserted status?
399 if(irqCallback) irqCallback();
400 }
401}
402
403static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
404{
405 int nSample;
406 int fa, s_1, s_2, d, s;
407
408 s_1 = dest[27];
409 s_2 = dest[26];
410
411 for (nSample = 0; nSample < 28; src++)
412 {
413 d = (int)*src;
414 s = (int)(signed short)((d & 0x0f) << 12);
415
416 fa = s >> shift_factor;
417 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
418 s_2=s_1;s_1=fa;
419
420 dest[nSample++] = fa;
421
422 s = (int)(signed short)((d & 0xf0) << 8);
423 fa = s >> shift_factor;
424 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
425 s_2=s_1;s_1=fa;
426
427 dest[nSample++] = fa;
428 }
429}
430
431static int decode_block(int ch)
432{
433 unsigned char *start;
434 int predict_nr,shift_factor,flags;
435 int ret = 0;
436
437 start=s_chan[ch].pCurr; // set up the current pos
438 if(dwPendingChanOff&(1<<ch))
439 {
440 dwChannelOn&=~(1<<ch); // -> turn everything off
441 dwPendingChanOff&=~(1<<ch);
442 s_chan[ch].bStop=1;
443 s_chan[ch].ADSRX.EnvelopeVol=0;
444 }
445
446 //////////////////////////////////////////// irq check
447
448 if(spuCtrl&CTRL_IRQ)
449 {
450 if(pSpuIrq == start) // irq address reached?
451 {
452 do_irq(); // -> call main emu
453 ret = 1;
454 }
455 }
456
457 predict_nr=(int)start[0];
458 shift_factor=predict_nr&0xf;
459 predict_nr >>= 4;
460
461 decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor);
462
463 //////////////////////////////////////////// flag handler
464
465 flags=(int)start[1];
466 if(flags&4)
467 s_chan[ch].pLoop=start; // loop adress
468
469 start+=16;
470 if(flags&1) // 1: stop/loop
471 {
472 if(!(flags&2))
473 dwPendingChanOff|=1<<ch;
474
475 start = s_chan[ch].pLoop;
476 }
477
478 if (start - spuMemC >= 0x80000) {
479 // most likely wrong
480 start = spuMemC;
481 printf("ch%d oflow\n", ch);
482 }
483
484 s_chan[ch].pCurr = start; // store values for next cycle
485 s_chan[ch].bJump = flags & 1;
486
487 return ret;
488}
489
490// do block, but ignore sample data
491static int skip_block(int ch)
492{
493 unsigned char *start = s_chan[ch].pCurr;
494 int flags = start[1];
495 int ret = 0;
496
497 if(start == pSpuIrq)
498 {
499 do_irq();
500 ret = 1;
501 }
502
503 if(flags & 4)
504 s_chan[ch].pLoop = start;
505
506 s_chan[ch].pCurr += 16;
507
508 if(flags & 1)
509 s_chan[ch].pCurr = s_chan[ch].pLoop;
510
511 s_chan[ch].bJump = flags & 1;
512 return ret;
513}
514
515#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
516static int do_samples_##name(int ch, int ns, int ns_to) \
517{ \
518 int sinc = s_chan[ch].sinc; \
519 int spos = s_chan[ch].spos; \
520 int sbpos = s_chan[ch].iSBPos; \
521 int *SB = s_chan[ch].SB; \
522 int ret = -1; \
523 int d, fa; \
524 interp_start; \
525 \
526 for (; ns < ns_to; ns++) \
527 { \
528 fmod_code; \
529 \
530 while (spos >= 0x10000) \
531 { \
532 if(sbpos == 28) \
533 { \
534 sbpos = 0; \
535 d = decode_block(ch); \
536 if(d && iSPUIRQWait) \
537 { \
538 ret = ns; \
539 goto out; \
540 } \
541 } \
542 \
543 fa = SB[sbpos++]; \
544 interp1_code; \
545 spos -= 0x10000; \
546 } \
547 \
548 interp2_code; \
549 spos += sinc; \
550 } \
551 \
552out: \
553 s_chan[ch].sinc = sinc; \
554 s_chan[ch].spos = spos; \
555 s_chan[ch].iSBPos = sbpos; \
556 interp_end; \
557 \
558 return ret; \
559}
560
561#define fmod_recv_check \
562 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
563 sinc = FModChangeFrequency(ch,ns)
564
565make_do_samples(default, fmod_recv_check, ,
566 StoreInterpolationVal(ch, fa),
567 ChanBuf[ns] = iGetInterpolationVal(ch, spos), )
568make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa)
569
570#define simple_interp_store \
571 s_chan[ch].SB[28] = 0; \
572 s_chan[ch].SB[29] = s_chan[ch].SB[30]; \
573 s_chan[ch].SB[30] = s_chan[ch].SB[31]; \
574 s_chan[ch].SB[31] = fa; \
575 s_chan[ch].SB[32] = 1
576
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]
582
583make_do_samples(simple, , ,
584 simple_interp_store, simple_interp_get, )
585
586static int do_samples_noise(int ch, int ns, int ns_to)
587{
588 int level, shift, bit;
589
590 s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns);
591 while (s_chan[ch].spos >= 28*0x10000)
592 {
593 skip_block(ch);
594 s_chan[ch].spos -= 28*0x10000;
595 }
596
597 // modified from DrHell/shalma, no fraction
598 level = (spuCtrl >> 10) & 0x0f;
599 level = 0x8000 >> level;
600
601 for (; ns < ns_to; ns++)
602 {
603 dwNoiseCount += 2;
604 if (dwNoiseCount >= level)
605 {
606 dwNoiseCount -= level;
607 shift = (dwNoiseVal >> 10) & 0x1f;
608 bit = (0x69696969 >> shift) & 1;
609 if (dwNoiseVal & 0x8000)
610 bit ^= 1;
611 dwNoiseVal = (dwNoiseVal << 1) | bit;
612 }
613
614 ChanBuf[ns] = (signed short)dwNoiseVal;
615 }
616
617 return -1;
618}
619
620#ifdef __arm__
621// asm code; lv and rv must be 0-3fff
622extern void mix_chan(int start, int count, int lv, int rv);
623extern void mix_chan_rvb(int start, int count, int lv, int rv);
624#else
625static void mix_chan(int start, int count, int lv, int rv)
626{
627 int *dst = SSumLR + start * 2;
628 const int *src = ChanBuf + start;
629 int l, r;
630
631 while (count--)
632 {
633 int sval = *src++;
634
635 l = (sval * lv) >> 14;
636 r = (sval * rv) >> 14;
637 *dst++ += l;
638 *dst++ += r;
639 }
640}
641
642static void mix_chan_rvb(int start, int count, int lv, int rv)
643{
644 int *dst = SSumLR + start * 2;
645 int *drvb = sRVBStart + start * 2;
646 const int *src = ChanBuf + start;
647 int l, r;
648
649 while (count--)
650 {
651 int sval = *src++;
652
653 l = (sval * lv) >> 14;
654 r = (sval * rv) >> 14;
655 *dst++ += l;
656 *dst++ += r;
657 *drvb++ += l;
658 *drvb++ += r;
659 }
660}
661#endif
662
663////////////////////////////////////////////////////////////////////////
664// MAIN SPU FUNCTION
665// here is the main job handler...
666// basically the whole sound processing is done in this fat func!
667////////////////////////////////////////////////////////////////////////
668
669static int do_samples(void)
670{
671 int volmult = iVolume;
672 int ns,ns_from,ns_to;
673 int ch,d,silentch;
674 int bIRQReturn=0;
675
676 while(1)
677 {
678 // ok, at the beginning we are looking if there is
679 // enuff free place in the dsound/oss buffer to
680 // fill in new data, or if there is a new channel to start.
681 // if not, we wait (thread) or return (timer/spuasync)
682 // until enuff free place is available/a new channel gets
683 // started
684
685 if(dwNewChannel) // new channel should start immedately?
686 { // (at least one bit 0 ... MAXCHANNEL is set?)
687 iSecureStart++; // -> set iSecure
688 if(iSecureStart>5) iSecureStart=0; // (if it is set 5 times - that means on 5 tries a new samples has been started - in a row, we will reset it, to give the sound update a chance)
689 }
690 else iSecureStart=0; // 0: no new channel should start
691
692 if(!iSecureStart && // no new start?
693 (SoundGetBytesBuffered()>TESTSIZE)) // and still enuff data in sound buffer?
694 {
695 return 0;
696 }
697
698 //--------------------------------------------------// continue from irq handling in timer mode?
699
700 ns_from=0;
701 ns_to=NSSIZE;
702 ch=0;
703 if(lastch>=0) // will be -1 if no continue is pending
704 {
705 ch=lastch; ns_from=lastns; lastch=-1; // -> setup all kind of vars to continue
706 }
707
708 silentch=~(dwChannelOn|dwNewChannel);
709
710 //--------------------------------------------------//
711 //- main channel loop -//
712 //--------------------------------------------------//
713 {
714 for(;ch<MAXCHAN;ch++) // loop em all... we will collect 1 ms of sound of each playing channel
715 {
716 if(dwNewChannel&(1<<ch)) StartSound(ch); // start new sound
717 if(!(dwChannelOn&(1<<ch))) continue; // channel not playing? next
718
719 if(s_chan[ch].bNoise)
720 d=do_samples_noise(ch, ns_from, ns_to);
721 else if(s_chan[ch].bFMod==2 || (s_chan[ch].bFMod==0 && iUseInterpolation==0))
722 d=do_samples_noint(ch, ns_from, ns_to);
723 else if(s_chan[ch].bFMod==0 && iUseInterpolation==1)
724 d=do_samples_simple(ch, ns_from, ns_to);
725 else
726 d=do_samples_default(ch, ns_from, ns_to);
727 if(d>=0)
728 {
729 bIRQReturn=1;
730 lastch=ch;
731 lastns=ns_to=d;
732 if(d==0)
733 break;
734 }
735
736 MixADSR(ch, ns_from, ns_to);
737
738 if(s_chan[ch].bFMod==2) // fmod freq channel
739 memcpy(iFMod, ChanBuf, sizeof(iFMod));
740 else if(s_chan[ch].bRVBActive)
741 mix_chan_rvb(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
742 else
743 mix_chan(ns_from,ns_to-ns_from,s_chan[ch].iLeftVolume,s_chan[ch].iRightVolume);
744 }
745 }
746
747 // advance "stopped" channels that can cause irqs
748 // (all chans are always playing on the real thing..)
749 if(!bIRQReturn && (spuCtrl&CTRL_IRQ))
750 for(ch=0;ch<MAXCHAN;ch++)
751 {
752 if(!(silentch&(1<<ch))) continue; // already handled
753 if(dwChannelDead&(1<<ch)) continue;
754 if(s_chan[ch].pCurr > pSpuIrq && s_chan[ch].pLoop > pSpuIrq)
755 continue;
756
757 s_chan[ch].spos += s_chan[ch].sinc * NSSIZE;
758 while(s_chan[ch].spos >= 28 * 0x10000)
759 {
760 unsigned char *start = s_chan[ch].pCurr;
761
762 // no need for bIRQReturn since the channel is silent
763 iSpuAsyncWait |= skip_block(ch);
764 if(start == s_chan[ch].pCurr)
765 {
766 // looping on self
767 dwChannelDead |= 1<<ch;
768 s_chan[ch].spos = 0;
769 break;
770 }
771
772 s_chan[ch].spos -= 28 * 0x10000;
773 }
774 }
775
776 if(bIRQReturn && iSPUIRQWait) // special return for "spu irq - wait for cpu action"
777 {
778 iSpuAsyncWait=1;
779 bIRQReturn=0;
780 return 0;
781 }
782
783
784 //---------------------------------------------------//
785 //- here we have another 1 ms of sound data
786 //---------------------------------------------------//
787 // mix XA infos (if any)
788
789 MixXA();
790
791 ///////////////////////////////////////////////////////
792 // mix all channels (including reverb) into one buffer
793
794 if(iUseReverb)
795 REVERBDo();
796
797 if((spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
798 {
799 memset(pS, 0, NSSIZE * 2 * sizeof(pS[0]));
800 pS += NSSIZE*2;
801 }
802 else
803 for (ns = 0; ns < NSSIZE*2; )
804 {
805 d = SSumLR[ns]; SSumLR[ns] = 0;
806 d = d * volmult >> 10;
807 ssat32_to_16(d);
808 *pS++ = d;
809 ns++;
810
811 d = SSumLR[ns]; SSumLR[ns] = 0;
812 d = d * volmult >> 10;
813 ssat32_to_16(d);
814 *pS++ = d;
815 ns++;
816 }
817
818 //////////////////////////////////////////////////////
819 // special irq handling in the decode buffers (0x0000-0x1000)
820 // we know:
821 // the decode buffers are located in spu memory in the following way:
822 // 0x0000-0x03ff CD audio left
823 // 0x0400-0x07ff CD audio right
824 // 0x0800-0x0bff Voice 1
825 // 0x0c00-0x0fff Voice 3
826 // and decoded data is 16 bit for one sample
827 // we assume:
828 // even if voices 1/3 are off or no cd audio is playing, the internal
829 // play positions will move on and wrap after 0x400 bytes.
830 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
831 // increase this pointer on each sample by 2 bytes. If this pointer
832 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
833 // an IRQ. Only problem: the "wait for cpu" option is kinda hard to do here
834 // in some of Peops timer modes. So: we ignore this option here (for now).
835
836 if(pMixIrq)
837 {
838 for(ns=0;ns<NSSIZE;ns++)
839 {
840 if((spuCtrl&0x40) && pSpuIrq && pSpuIrq<spuMemC+0x1000)
841 {
842 for(ch=0;ch<4;ch++)
843 {
844 if(pSpuIrq>=pMixIrq+(ch*0x400) && pSpuIrq<pMixIrq+(ch*0x400)+2)
845 do_irq();
846 }
847 }
848 pMixIrq+=2;if(pMixIrq>spuMemC+0x3ff) pMixIrq=spuMemC;
849 }
850 }
851
852 InitREVERB();
853
854 // feed the sound
855 // wanna have around 1/60 sec (16.666 ms) updates
856 if (iCycle++ > 16/FRAG_MSECS)
857 {
858 SoundFeedStreamData((unsigned char *)pSpuBuffer,
859 ((unsigned char *)pS) - ((unsigned char *)pSpuBuffer));
860 pS = (short *)pSpuBuffer;
861 iCycle = 0;
862 }
863 }
864
865 return 0;
866}
867
868// SPU ASYNC... even newer epsxe func
869// 1 time every 'cycle' cycles... harhar
870
871// rearmed: called every 2ms now
872
873void CALLBACK SPUasync(unsigned long cycle)
874{
875 if(iSpuAsyncWait)
876 {
877 iSpuAsyncWait++;
878 if(iSpuAsyncWait<=16/FRAG_MSECS) return;
879 iSpuAsyncWait=0;
880 }
881
882 if(!bSpuInit) return; // -> no init, no call
883
884 do_samples();
885
886 // abuse iSpuAsyncWait mechanism to reduce calls to above function
887 // to make it do larger chunks
888 // note: doing it less often than once per frame causes skips
889 iSpuAsyncWait=1;
890}
891
892// SPU UPDATE... new epsxe func
893// 1 time every 32 hsync lines
894// (312/32)x50 in pal
895// (262/32)x60 in ntsc
896
897// since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
898// leave that func in the linux port, until epsxe linux is using
899// the async function as well
900
901void CALLBACK SPUupdate(void)
902{
903 SPUasync(0);
904}
905
906// XA AUDIO
907
908void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
909{
910 if(!xap) return;
911 if(!xap->freq) return; // no xa freq ? bye
912
913 FeedXA(xap); // call main XA feeder
914}
915
916// CDDA AUDIO
917int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
918{
919 if (!pcm) return -1;
920 if (nbytes<=0) return -1;
921
922 return FeedCDDA((unsigned char *)pcm, nbytes);
923}
924
925// to be called after state load
926void ClearWorkingState(void)
927{
928 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
929 memset(iFMod,0,sizeof(iFMod));
930 pS=(short *)pSpuBuffer; // setup soundbuffer pointer
931}
932
933// SETUPSTREAMS: init most of the spu buffers
934void SetupStreams(void)
935{
936 int i;
937
938 pSpuBuffer=(unsigned char *)malloc(32768); // alloc mixing buffer
939
940 if(iUseReverb==1) i=88200*2;
941 else i=NSSIZE*2;
942
943 sRVBStart = (int *)malloc(i*4); // alloc reverb buffer
944 memset(sRVBStart,0,i*4);
945 sRVBEnd = sRVBStart + i;
946 sRVBPlay = sRVBStart;
947
948 XAStart = // alloc xa buffer
949 (uint32_t *)malloc(44100 * sizeof(uint32_t));
950 XAEnd = XAStart + 44100;
951 XAPlay = XAStart;
952 XAFeed = XAStart;
953
954 CDDAStart = // alloc cdda buffer
955 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
956 CDDAEnd = CDDAStart + 16384;
957 CDDAPlay = CDDAStart;
958 CDDAFeed = CDDAStart;
959
960 for(i=0;i<MAXCHAN;i++) // loop sound channels
961 {
962// we don't use mutex sync... not needed, would only
963// slow us down:
964// s_chan[i].hMutex=CreateMutex(NULL,FALSE,NULL);
965 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
966 s_chan[i].pLoop=spuMemC;
967 s_chan[i].pCurr=spuMemC;
968 }
969
970 pMixIrq=spuMemC; // enable decoded buffer irqs by setting the address
971
972 ClearWorkingState();
973
974 bSpuInit=1; // flag: we are inited
975}
976
977// REMOVESTREAMS: free most buffer
978void RemoveStreams(void)
979{
980 free(pSpuBuffer); // free mixing buffer
981 pSpuBuffer = NULL;
982 free(sRVBStart); // free reverb buffer
983 sRVBStart = NULL;
984 free(XAStart); // free XA buffer
985 XAStart = NULL;
986 free(CDDAStart); // free CDDA buffer
987 CDDAStart = NULL;
988}
989
990// INIT/EXIT STUFF
991
992// SPUINIT: this func will be called first by the main emu
993long CALLBACK SPUinit(void)
994{
995 spuMemC = (unsigned char *)spuMem; // just small setup
996 memset((void *)&rvb, 0, sizeof(REVERBInfo));
997 InitADSR();
998
999 spuIrq = 0;
1000 spuAddr = 0xffffffff;
1001 spuMemC = (unsigned char *)spuMem;
1002 pMixIrq = 0;
1003 memset((void *)s_chan, 0, (MAXCHAN + 1) * sizeof(SPUCHAN));
1004 pSpuIrq = 0;
1005 //iSPUIRQWait = 0;
1006 lastch = -1;
1007
1008 SetupStreams(); // prepare streaming
1009
1010 return 0;
1011}
1012
1013// SPUOPEN: called by main emu after init
1014long CALLBACK SPUopen(void)
1015{
1016 if (bSPUIsOpen) return 0; // security for some stupid main emus
1017
1018 SetupSound(); // setup sound (before init!)
1019
1020 bSPUIsOpen = 1;
1021
1022 return PSE_SPU_ERR_SUCCESS;
1023}
1024
1025// SPUCLOSE: called before shutdown
1026long CALLBACK SPUclose(void)
1027{
1028 if (!bSPUIsOpen) return 0; // some security
1029
1030 bSPUIsOpen = 0; // no more open
1031
1032 RemoveSound(); // no more sound handling
1033
1034 return 0;
1035}
1036
1037// SPUSHUTDOWN: called by main emu on final exit
1038long CALLBACK SPUshutdown(void)
1039{
1040 SPUclose();
1041 RemoveStreams(); // no more streaming
1042 bSpuInit=0;
1043
1044 return 0;
1045}
1046
1047// SPUTEST: we don't test, we are always fine ;)
1048long CALLBACK SPUtest(void)
1049{
1050 return 0;
1051}
1052
1053// SPUCONFIGURE: call config dialog
1054long CALLBACK SPUconfigure(void)
1055{
1056#ifdef _MACOSX
1057 DoConfiguration();
1058#else
1059// StartCfgTool("CFG");
1060#endif
1061 return 0;
1062}
1063
1064// SPUABOUT: show about window
1065void CALLBACK SPUabout(void)
1066{
1067#ifdef _MACOSX
1068 DoAbout();
1069#else
1070// StartCfgTool("ABOUT");
1071#endif
1072}
1073
1074// SETUP CALLBACKS
1075// this functions will be called once,
1076// passes a callback that should be called on SPU-IRQ/cdda volume change
1077void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1078{
1079 irqCallback = callback;
1080}
1081
1082void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1083{
1084 cddavCallback = CDDAVcallback;
1085}
1086
1087// COMMON PLUGIN INFO FUNCS
1088/*
1089char * CALLBACK PSEgetLibName(void)
1090{
1091 return _(libraryName);
1092}
1093
1094unsigned long CALLBACK PSEgetLibType(void)
1095{
1096 return PSE_LT_SPU;
1097}
1098
1099unsigned long CALLBACK PSEgetLibVersion(void)
1100{
1101 return (1 << 16) | (6 << 8);
1102}
1103
1104char * SPUgetLibInfos(void)
1105{
1106 return _(libraryInfo);
1107}
1108*/
1109
1110// debug
1111void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1112{
1113 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1114
1115 for(;ch<MAXCHAN;ch++)
1116 {
1117 if (!(dwChannelOn & (1<<ch)))
1118 continue;
1119 if (s_chan[ch].bFMod == 2)
1120 fmod_chans |= 1 << ch;
1121 if (s_chan[ch].bNoise)
1122 noise_chans |= 1 << ch;
1123 if((spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= pSpuIrq && s_chan[ch].pLoop <= pSpuIrq)
1124 irq_chans |= 1 << ch;
1125 }
1126
1127 *chans_out = dwChannelOn;
1128 *run_chans = ~dwChannelOn & ~dwChannelDead & irq_chans;
1129 *fmod_chans_out = fmod_chans;
1130 *noise_chans_out = noise_chans;
1131}
1132
1133// vim:shiftwidth=1:expandtab