Skip to content

Commit

Permalink
fix crash when removing samples during playback
Browse files Browse the repository at this point in the history
issue #226
  • Loading branch information
tildearrow committed Feb 25, 2022
1 parent 721445c commit 76283d4
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/engine/platform/amiga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void DivPlatformAmiga::acquire(short* bufL, short* bufR, size_t start, size_t le
if (chan[i].sample>=0 && chan[i].sample<parent->song.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++];
Expand Down Expand Up @@ -121,7 +121,7 @@ void DivPlatformAmiga::tick() {
}
double off=1.0;
if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
DivSample* s=parent->song.sample[chan[i].sample];
DivSample* s=parent->getSample(chan[i].sample);
if (s->centerRate<1) {
off=1.0;
} else {
Expand Down Expand Up @@ -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].sample<parent->song.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 {
Expand Down Expand Up @@ -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].sample<parent->song.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 {
Expand Down
12 changes: 6 additions & 6 deletions src/engine/platform/arcade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.sample<parent->song.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;
Expand Down Expand Up @@ -471,7 +471,7 @@ void DivPlatformArcade::tick() {
if (chan[i].furnacePCM) {
double off=1.0;
if (chan[i].pcm.sample>=0 && chan[i].pcm.sample<parent->song.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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/engine/platform/genesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -381,7 +381,7 @@ void DivPlatformGenesis::tick() {
if (chan[i].furnaceDac && dacMode) {
double off=1.0;
if (dacSample>=0 && dacSample<parent->song.sampleLen) {
DivSample* s=parent->song.sample[dacSample];
DivSample* s=parent->getSample(dacSample);
if (s->centerRate<1) {
off=1.0;
} else {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/platform/nes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -239,7 +239,7 @@ void DivPlatformNES::tick() {
if (chan[4].furnaceDac) {
double off=1.0;
if (dacSample>=0 && dacSample<parent->song.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);
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/engine/platform/pce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -205,7 +205,7 @@ void DivPlatformPCE::tick() {
if (chan[i].furnaceDac) {
double off=1.0;
if (chan[i].dacSample>=0 && chan[i].dacSample<parent->song.sampleLen) {
DivSample* s=parent->song.sample[chan[i].dacSample];
DivSample* s=parent->getSample(chan[i].dacSample);
if (s->centerRate<1) {
off=1.0;
} else {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/platform/ym2610.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/engine/song.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ struct DivSong {

DivInstrument nullIns;
DivWavetable nullWave;
DivSample nullSample;

void unload();

Expand Down

0 comments on commit 76283d4

Please sign in to comment.