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