diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index b6601063..90eee6a9 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -2497,34 +2497,59 @@ SafeWriter* DivEngine::saveVGM() { loopTimer[i]-=(loopFreq[i]/44100.0)*(double)totalWait; } } + bool haveNegatives=false; for (int i=0; i=0) { if (loopTimer[i]<0) { - double waitTime=totalWait+(loopTimer[i]*(44100.0/loopFreq[i])); + haveNegatives=true; + } + } + } + while (haveNegatives) { + // finish all negatives + int nextToTouch=-1; + for (int i=0; i=0) { + if (loopTimer[i]<0) { + if (nextToTouch>=0) { + if (loopTimer[nextToTouch]>loopTimer[i]) nextToTouch=i; + } else { + nextToTouch=i; + } + } + } + } + if (nextToTouch>=0) { + double waitTime=totalWait+(loopTimer[nextToTouch]*(44100.0/loopFreq[nextToTouch])); + if (waitTime>0) { w->writeC(0x61); w->writeS(waitTime); printf("wait is: %f\n",waitTime); totalWait-=waitTime; tickCount+=waitTime; - if (loopSample[i]loopStart<(int)sample->rendLength) { - w->writeC(0x93); - w->writeC(i); - w->writeI(sample->rendOffContiguous+sample->loopStart); - w->writeC(0x81); - w->writeI(sample->rendLength-sample->loopStart); - } - } - loopSample[i]=-1; } + if (loopSample[nextToTouch]loopStart<(int)sample->rendLength) { + w->writeC(0x93); + w->writeC(nextToTouch); + w->writeI(sample->rendOffContiguous+sample->loopStart); + w->writeC(0x81); + w->writeI(sample->rendLength-sample->loopStart); + } + } + loopSample[nextToTouch]=-1; + } else { + haveNegatives=false; } } // write wait - w->writeC(0x61); - w->writeS(totalWait); - tickCount+=totalWait; + if (totalWait>0) { + w->writeC(0x61); + w->writeS(totalWait); + tickCount+=totalWait; + } } for (int i=0; i