From 76283d42b0dff47a0f61c541f10d5ca1ae7a2666 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 24 Feb 2022 22:07:06 -0500 Subject: [PATCH] fix crash when removing samples during playback issue #226 --- src/engine/engine.cpp | 5 +++++ src/engine/engine.h | 5 +++-- src/engine/platform/amiga.cpp | 8 ++++---- src/engine/platform/arcade.cpp | 12 ++++++------ src/engine/platform/genesis.cpp | 10 +++++----- src/engine/platform/nes.cpp | 6 +++--- src/engine/platform/pce.cpp | 6 +++--- src/engine/platform/ym2610.cpp | 2 +- src/engine/song.h | 1 + 9 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 06c57e914d..9de193e309 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -715,6 +715,11 @@ DivWavetable* DivEngine::getWave(int index) { return song.wave[index]; } +DivSample* DivEngine::getSample(int index) { + if (index<0 || index>=song.sampleLen) return &song.nullSample; + return song.sample[index]; +} + void DivEngine::setLoops(int loops) { remainingLoops=loops; } diff --git a/src/engine/engine.h b/src/engine/engine.h index 71b0de3035..3bddfb8e6d 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -37,8 +37,8 @@ warnings+=(String("\n")+x); \ } -#define DIV_VERSION "0.5.7" -#define DIV_ENGINE_VERSION 53 +#define DIV_VERSION "0.5.8" +#define DIV_ENGINE_VERSION 54 enum DivStatusView { DIV_STATUS_NOTHING=0, @@ -262,6 +262,7 @@ class DivEngine { void nextBuf(float** in, float** out, int inChans, int outChans, unsigned int size); DivInstrument* getIns(int index); DivWavetable* getWave(int index); + DivSample* getSample(int index); // start fresh void createNew(); // load a file. diff --git a/src/engine/platform/amiga.cpp b/src/engine/platform/amiga.cpp index 67cb857347..69277a462a 100644 --- a/src/engine/platform/amiga.cpp +++ b/src/engine/platform/amiga.cpp @@ -71,7 +71,7 @@ void DivPlatformAmiga::acquire(short* bufL, short* bufR, size_t start, size_t le if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { chan[i].audSub-=AMIGA_DIVIDER; if (chan[i].audSub<0) { - DivSample* s=parent->song.sample[chan[i].sample]; + DivSample* s=parent->getSample(chan[i].sample); if (s->rendLength>0) { if (s->depth==8) { chan[i].audDat=s->rendData[chan[i].audPos++]; @@ -121,7 +121,7 @@ void DivPlatformAmiga::tick() { } double off=1.0; if (chan[i].sample>=0 && chan[i].samplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[i].sample]; + DivSample* s=parent->getSample(chan[i].sample); if (s->centerRate<1) { off=1.0; } else { @@ -175,7 +175,7 @@ int DivPlatformAmiga::dispatch(DivCommand c) { chan[c.chan].sample=ins->amiga.initSample; double off=1.0; if (chan[c.chan].sample>=0 && chan[c.chan].samplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[c.chan].sample]; + DivSample* s=parent->getSample(chan[c.chan].sample); if (s->centerRate<1) { off=1.0; } else { @@ -262,7 +262,7 @@ int DivPlatformAmiga::dispatch(DivCommand c) { case DIV_CMD_LEGATO: { double off=1.0; if (chan[c.chan].sample>=0 && chan[c.chan].samplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[c.chan].sample]; + DivSample* s=parent->getSample(chan[c.chan].sample); if (s->centerRate<1) { off=1.0; } else { diff --git a/src/engine/platform/arcade.cpp b/src/engine/platform/arcade.cpp index ed665e576b..5838035571 100644 --- a/src/engine/platform/arcade.cpp +++ b/src/engine/platform/arcade.cpp @@ -167,7 +167,7 @@ void DivPlatformArcade::acquire_nuked(short* bufL, short* bufR, size_t start, si pcmL=0; pcmR=0; for (int i=8; i<13; i++) { if (chan[i].pcm.sample>=0) { - DivSample* s=parent->song.sample[chan[i].pcm.sample]; + DivSample* s=parent->getSample(chan[i].pcm.sample); if (s->rendLength<=0) { chan[i].pcm.sample=-1; continue; @@ -233,7 +233,7 @@ void DivPlatformArcade::acquire_ymfm(short* bufL, short* bufR, size_t start, siz pcmL=0; pcmR=0; for (int i=8; i<13; i++) { if (chan[i].pcm.sample>=0 && chan[i].pcm.samplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[i].pcm.sample]; + DivSample* s=parent->getSample(chan[i].pcm.sample); if (s->rendLength<=0) { chan[i].pcm.sample=-1; continue; @@ -471,7 +471,7 @@ void DivPlatformArcade::tick() { if (chan[i].furnacePCM) { double off=1.0; if (chan[i].pcm.sample>=0 && chan[i].pcm.samplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[i].pcm.sample]; + DivSample* s=parent->getSample(chan[i].pcm.sample); off=(double)s->centerRate/8363.0; } chan[i].pcm.freq=MIN(255,((off*parent->song.tuning*pow(2.0,double(chan[i].freq+256)/(64.0*12.0)))*255)/31250); @@ -516,7 +516,7 @@ int DivPlatformArcade::dispatch(DivCommand c) { chan[c.chan].freqChanged=true; chan[c.chan].furnacePCM=true; if (dumpWrites) { // Sega PCM writes - DivSample* s=parent->song.sample[chan[c.chan].pcm.sample]; + DivSample* s=parent->getSample(chan[c.chan].pcm.sample); addWrite(0x10086+(pcmChan<<3),3+((s->rendOffP>>16)<<3)); addWrite(0x10084+(pcmChan<<3),(s->rendOffP)&0xff); addWrite(0x10085+(pcmChan<<3),(s->rendOffP>>8)&0xff); @@ -543,10 +543,10 @@ int DivPlatformArcade::dispatch(DivCommand c) { break; } chan[c.chan].pcm.pos=0; - chan[c.chan].pcm.freq=MIN(255,(parent->song.sample[chan[c.chan].pcm.sample]->rate*255)/31250); + chan[c.chan].pcm.freq=MIN(255,(parent->getSample(chan[c.chan].pcm.sample)->rate*255)/31250); chan[c.chan].furnacePCM=false; if (dumpWrites) { // Sega PCM writes - DivSample* s=parent->song.sample[chan[c.chan].pcm.sample]; + DivSample* s=parent->getSample(chan[c.chan].pcm.sample); addWrite(0x10086+(pcmChan<<3),3+((s->rendOffP>>16)<<3)); addWrite(0x10084+(pcmChan<<3),(s->rendOffP)&0xff); addWrite(0x10085+(pcmChan<<3),(s->rendOffP>>8)&0xff); diff --git a/src/engine/platform/genesis.cpp b/src/engine/platform/genesis.cpp index 8117266259..6f9e8dfbff 100644 --- a/src/engine/platform/genesis.cpp +++ b/src/engine/platform/genesis.cpp @@ -89,7 +89,7 @@ void DivPlatformGenesis::acquire_nuked(short* bufL, short* bufR, size_t start, s if (dacMode && dacSample!=-1) { dacPeriod-=6; if (dacPeriod<1) { - DivSample* s=parent->song.sample[dacSample]; + DivSample* s=parent->getSample(dacSample); if (s->rendLength>0) { if (!isMuted[5]) { if (s->depth==8) { @@ -163,7 +163,7 @@ void DivPlatformGenesis::acquire_ymfm(short* bufL, short* bufR, size_t start, si if (dacMode && dacSample!=-1) { dacPeriod-=24; if (dacPeriod<1) { - DivSample* s=parent->song.sample[dacSample]; + DivSample* s=parent->getSample(dacSample); if (s->rendLength>0) { if (!isMuted[5]) { if (s->depth==8) { @@ -381,7 +381,7 @@ void DivPlatformGenesis::tick() { if (chan[i].furnaceDac && dacMode) { double off=1.0; if (dacSample>=0 && dacSamplesong.sampleLen) { - DivSample* s=parent->song.sample[dacSample]; + DivSample* s=parent->getSample(dacSample); if (s->centerRate<1) { off=1.0; } else { @@ -518,8 +518,8 @@ int DivPlatformGenesis::dispatch(DivCommand c) { } dacPos=0; dacPeriod=0; - dacRate=1280000/MAX(1,parent->song.sample[dacSample]->rate); - if (dumpWrites) addWrite(0xffff0001,parent->song.sample[dacSample]->rate); + dacRate=1280000/MAX(1,parent->getSample(dacSample)->rate); + if (dumpWrites) addWrite(0xffff0001,parent->getSample(dacSample)->rate); chan[c.chan].furnaceDac=false; } break; diff --git a/src/engine/platform/nes.cpp b/src/engine/platform/nes.cpp index 773a58f255..c877d025b0 100644 --- a/src/engine/platform/nes.cpp +++ b/src/engine/platform/nes.cpp @@ -75,7 +75,7 @@ void DivPlatformNES::acquire(short* bufL, short* bufR, size_t start, size_t len) if (dacSample!=-1) { dacPeriod+=dacRate; if (dacPeriod>=rate) { - DivSample* s=parent->song.sample[dacSample]; + DivSample* s=parent->getSample(dacSample); if (s->rendLength>0) { if (!isMuted[4]) { if (s->depth==8) { @@ -239,7 +239,7 @@ void DivPlatformNES::tick() { if (chan[4].furnaceDac) { double off=1.0; if (dacSample>=0 && dacSamplesong.sampleLen) { - DivSample* s=parent->song.sample[dacSample]; + DivSample* s=parent->getSample(dacSample); off=(double)s->centerRate/8363.0; } dacRate=MIN(chan[4].freq*off,32000); @@ -287,7 +287,7 @@ int DivPlatformNES::dispatch(DivCommand c) { } dacPos=0; dacPeriod=0; - dacRate=parent->song.sample[dacSample]->rate; + dacRate=parent->getSample(dacSample)->rate; if (dumpWrites) addWrite(0xffff0001,dacRate); chan[c.chan].furnaceDac=false; } diff --git a/src/engine/platform/pce.cpp b/src/engine/platform/pce.cpp index ede1bd667a..e37b7d0543 100644 --- a/src/engine/platform/pce.cpp +++ b/src/engine/platform/pce.cpp @@ -81,7 +81,7 @@ void DivPlatformPCE::acquire(short* bufL, short* bufR, size_t start, size_t len) if (chan[i].pcm && chan[i].dacSample!=-1) { chan[i].dacPeriod+=chan[i].dacRate; if (chan[i].dacPeriod>rate) { - DivSample* s=parent->song.sample[chan[i].dacSample]; + DivSample* s=parent->getSample(chan[i].dacSample); if (s->rendLength<=0) { chan[i].dacSample=-1; continue; @@ -205,7 +205,7 @@ void DivPlatformPCE::tick() { if (chan[i].furnaceDac) { double off=1.0; if (chan[i].dacSample>=0 && chan[i].dacSamplesong.sampleLen) { - DivSample* s=parent->song.sample[chan[i].dacSample]; + DivSample* s=parent->getSample(chan[i].dacSample); if (s->centerRate<1) { off=1.0; } else { @@ -285,7 +285,7 @@ int DivPlatformPCE::dispatch(DivCommand c) { } chan[c.chan].dacPos=0; chan[c.chan].dacPeriod=0; - chan[c.chan].dacRate=parent->song.sample[chan[c.chan].dacSample]->rate; + chan[c.chan].dacRate=parent->getSample(chan[c.chan].dacSample)->rate; if (dumpWrites) { chWrite(c.chan,0x04,0xdf); addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dacRate); diff --git a/src/engine/platform/ym2610.cpp b/src/engine/platform/ym2610.cpp index 378a7b9c4f..57cd03bdf4 100644 --- a/src/engine/platform/ym2610.cpp +++ b/src/engine/platform/ym2610.cpp @@ -436,7 +436,7 @@ int DivPlatformYM2610::dispatch(DivCommand c) { immWrite(0x128+c.chan-7,0); break; } - DivSample* s=parent->song.sample[12*sampleBank+c.value%12]; + DivSample* s=parent->getSample(12*sampleBank+c.value%12); immWrite(0x110+c.chan-7,(s->rendOff>>8)&0xff); immWrite(0x118+c.chan-7,s->rendOff>>16); int end=s->rendOff+s->adpcmRendLength-1; diff --git a/src/engine/song.h b/src/engine/song.h index 4582077e1c..29b0ec47f8 100644 --- a/src/engine/song.h +++ b/src/engine/song.h @@ -264,6 +264,7 @@ struct DivSong { DivInstrument nullIns; DivWavetable nullWave; + DivSample nullSample; void unload();