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