diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index c062488bc..b9d94c773 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -995,7 +995,32 @@ void DivEngine::delUnusedSamples() { } bool DivEngine::sysChanCountChange(int firstChan, int before, int after) { + if (song.chans-before+after>DIV_MAX_CHANS) { + return false; + } + int chanMovement=after-before; + int lastChan=firstChan+before; + if (chanMovement!=0) { + if (chanMovement>0) { + // add channels + for (int i=song.chans+chanMovement-1; i>=lastChan+chanMovement; i--) { + swapChannels(i,i-chanMovement); + } + for (int i=lastChan; i0) { - // add channels - for (int i=chanCount+chanMovement-1; i>=lastChan+chanMovement; i--) { - swapChannels(i,i-chanMovement); - } - for (int i=lastChan; i=song.systemLen) { + lastError=_("invalid index"); + return false; + } + if (song.chans-song.systemChans[index]+ch>DIV_MAX_CHANS) { + lastError=fmt::sprintf(_("max number of total channels is %d"),DIV_MAX_CHANS); + return false; + } + + quitDispatch(); + BUSY_BEGIN; + saveLock.lock(); + + if (!preserveOrder) { + int firstChan=0; + while (song.dispatchOfChan[firstChan]!=index) firstChan++; + if (!sysChanCountChange(firstChan,song.systemChans[index],ch)) { + logE("it should not be failing here!"); + abort(); + } + } + + song.systemChans[index]=ch; + song.systemFlags[index].clear(); + song.recalcChans(); + saveLock.unlock(); + BUSY_END; + initDispatch(); + BUSY_BEGIN; + renderSamples(); + reset(); + BUSY_END; + + return true; +} + bool DivEngine::addSystem(DivSystem which) { if (song.systemLen>=DIV_MAX_CHIPS) { lastError=fmt::sprintf(_("max number of systems is %d"),DIV_MAX_CHIPS);