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