+ if (s_chan->bFMod == 2) // fmod freq channel
+ memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
+ if (!(spu.spuCtrl & CTRL_MUTE))
+ ;
+ else if (s_chan->bRVBActive && do_rvb)
+ mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, RVB);
+ else
+ mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume);
+ }
+
+ MixCD(spu.SSumLR, RVB, ns_to, spu.decode_pos);
+
+ if (spu.rvb->StartAddr) {
+ if (do_rvb)
+ REVERBDo(spu.SSumLR, RVB, ns_to, spu.rvb->CurrAddr);
+
+ spu.rvb->CurrAddr += ns_to / 2;
+ while (spu.rvb->CurrAddr >= 0x40000)
+ spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
+ }
+}
+
+static void do_samples_finish(int *SSumLR, int ns_to,
+ int silentch, int decode_pos);
+
+// optional worker thread handling
+
+#if P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
+
+// worker thread state
+static struct spu_worker {
+ union {
+ struct {
+ unsigned int exit_thread;
+ unsigned int i_ready;
+ unsigned int i_reaped;
+ unsigned int last_boot_cnt; // dsp
+ unsigned int ram_dirty;
+ };
+ // aligning for C64X_DSP
+ unsigned int _pad0[128/4];
+ };
+ union {
+ struct {
+ unsigned int i_done;
+ unsigned int active; // dsp
+ unsigned int boot_cnt;
+ };
+ unsigned int _pad1[128/4];
+ };
+ struct work_item {
+ int ns_to;
+ int ctrl;
+ int decode_pos;
+ int rvb_addr;
+ unsigned int channels_new;
+ unsigned int channels_on;
+ unsigned int channels_silent;
+ struct {
+ int spos;
+ int sbpos;
+ int sinc;
+ int start;
+ int loop;
+ short vol_l;
+ short vol_r;
+ unsigned short ns_to;
+ unsigned short bNoise:1;
+ unsigned short bFMod:2;
+ unsigned short bRVBActive:1;
+ unsigned short bStarting:1;
+ ADSRInfoEx adsr;
+ } ch[24];
+ int SSumLR[NSSIZE * 2];
+ } i[4];
+} *worker;
+
+#define WORK_MAXCNT (sizeof(worker->i) / sizeof(worker->i[0]))
+#define WORK_I_MASK (WORK_MAXCNT - 1)
+
+static void thread_work_start(void);
+static void thread_work_wait_sync(struct work_item *work, int force);
+static void thread_sync_caches(void);
+static int thread_get_i_done(void);
+
+static int decode_block_work(void *context, int ch, int *SB)
+{
+ const unsigned char *ram = spu.spuMemC;
+ int predict_nr, shift_factor, flags;
+ struct work_item *work = context;
+ int start = work->ch[ch].start;
+ int loop = work->ch[ch].loop;
+
+ predict_nr = ram[start];
+ shift_factor = predict_nr & 0xf;
+ predict_nr >>= 4;
+
+ decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
+
+ flags = ram[start + 1];
+ if (flags & 4)
+ loop = start; // loop adress
+
+ start += 16;
+
+ if (flags & 1) // 1: stop/loop
+ start = loop;
+
+ work->ch[ch].start = start & 0x7ffff;
+ work->ch[ch].loop = loop;
+
+ return 0;
+}
+
+static void queue_channel_work(int ns_to, unsigned int silentch)
+{
+ int tmpFMod[NSSIZE];
+ struct work_item *work;
+ SPUCHAN *s_chan;
+ unsigned int mask;
+ int ch, d;
+
+ work = &worker->i[worker->i_ready & WORK_I_MASK];
+ work->ns_to = ns_to;
+ work->ctrl = spu.spuCtrl;
+ work->decode_pos = spu.decode_pos;
+ work->channels_silent = silentch;
+
+ mask = work->channels_new = spu.dwNewChannel & 0xffffff;
+ for (ch = 0; mask != 0; ch++, mask >>= 1) {
+ if (mask & 1)
+ StartSound(ch);
+ }
+
+ mask = work->channels_on = spu.dwChannelsAudible & 0xffffff;
+ spu.decode_dirty_ch |= mask & 0x0a;
+
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+
+ s_chan = &spu.s_chan[ch];
+ work->ch[ch].spos = s_chan->spos;
+ work->ch[ch].sbpos = s_chan->iSBPos;
+ work->ch[ch].sinc = s_chan->sinc;
+ work->ch[ch].adsr = s_chan->ADSRX;
+ work->ch[ch].vol_l = s_chan->iLeftVolume;
+ work->ch[ch].vol_r = s_chan->iRightVolume;
+ work->ch[ch].start = s_chan->pCurr - spu.spuMemC;
+ work->ch[ch].loop = s_chan->pLoop - spu.spuMemC;
+ work->ch[ch].bNoise = s_chan->bNoise;
+ work->ch[ch].bFMod = s_chan->bFMod;
+ work->ch[ch].bRVBActive = s_chan->bRVBActive;
+ work->ch[ch].bStarting = s_chan->bStarting;
+ if (s_chan->prevflags & 1)
+ work->ch[ch].start = work->ch[ch].loop;
+
+ if (unlikely(s_chan->bFMod == 2))
+ {
+ // sucks, have to do double work
+ assert(!s_chan->bNoise);
+ d = do_samples_gauss(tmpFMod, decode_block, NULL, ch, ns_to,
+ &spu.sb[ch], s_chan->sinc, &s_chan->spos, &s_chan->iSBPos);
+ if (!s_chan->bStarting) {
+ d = MixADSR(tmpFMod, &s_chan->ADSRX, d);
+ if (d < ns_to) {
+ spu.dwChannelsAudible &= ~(1 << ch);
+ s_chan->ADSRX.State = ADSR_RELEASE;
+ s_chan->ADSRX.EnvelopeVol = 0;
+ }
+ }
+ memset(&tmpFMod[d], 0, (ns_to - d) * sizeof(tmpFMod[d]));
+ work->ch[ch].ns_to = d;
+ continue;
+ }
+ if (unlikely(s_chan->bFMod))
+ d = do_samples_skip_fmod(ch, ns_to, tmpFMod);
+ else
+ d = do_samples_skip(ch, ns_to);
+ work->ch[ch].ns_to = d;
+
+ if (!s_chan->bStarting) {
+ // note: d is not accurate on skip
+ d = SkipADSR(&s_chan->ADSRX, d);
+ if (d < ns_to) {
+ spu.dwChannelsAudible &= ~(1 << ch);
+ s_chan->ADSRX.State = ADSR_RELEASE;
+ s_chan->ADSRX.EnvelopeVol = 0;
+ }
+ }
+ } // for (ch;;)
+
+ work->rvb_addr = 0;
+ if (spu.rvb->StartAddr) {
+ if (spu_config.iUseReverb)
+ work->rvb_addr = spu.rvb->CurrAddr;
+
+ spu.rvb->CurrAddr += ns_to / 2;
+ while (spu.rvb->CurrAddr >= 0x40000)
+ spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
+ }
+
+ worker->i_ready++;
+ thread_work_start();
+}
+
+static void do_channel_work(struct work_item *work)
+{
+ unsigned int mask;
+ int spos, sbpos;
+ int d, ch, ns_to;
+
+ ns_to = work->ns_to;
+
+ if (unlikely(spu.interpolation != spu_config.iUseInterpolation))
+ {
+ spu.interpolation = spu_config.iUseInterpolation;
+ mask = work->channels_on;
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ if (mask & 1)
+ ResetInterpolation(&spu.sb_thread[ch]);
+ }
+
+ if (work->rvb_addr)
+ memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2);
+
+ mask = work->channels_new;
+ for (ch = 0; mask != 0; ch++, mask >>= 1) {
+ if (mask & 1)
+ StartSoundSB(&spu.sb_thread[ch]);
+ }
+
+ mask = work->channels_on;
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+
+ d = work->ch[ch].ns_to;
+ spos = work->ch[ch].spos;
+ sbpos = work->ch[ch].sbpos;
+
+ if (work->ch[ch].bNoise)
+ do_lsfr_samples(ChanBuf, d, work->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
+ else
+ do_samples_adpcm(ChanBuf, decode_block_work, work, ch, d, work->ch[ch].bFMod,
+ &spu.sb_thread[ch], work->ch[ch].sinc, &spos, &sbpos);
+
+ d = MixADSR(ChanBuf, &work->ch[ch].adsr, d);
+ if (d < ns_to) {
+ work->ch[ch].adsr.EnvelopeVol = 0;
+ memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
+ }
+
+ if (ch == 1 || ch == 3)
+ do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos);
+
+ if (work->ch[ch].bFMod == 2) // fmod freq channel