diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 43b1f8cf6..77ee45a0d 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1666,6 +1666,19 @@ void DivEngine::setFilePlayerSync(bool doSync) { filePlayerSync=doSync; } +void DivEngine::syncFilePlayer() { + if (curFilePlayer==NULL) return; + int finalSeconds=totalSeconds+filePlayerCueSeconds; + int finalMicros=totalTicks+filePlayerCueMicros; + + while (finalMicros>=1000000) { + finalMicros-=1000000; + finalSeconds++; + } + + curFilePlayer->setPosSeconds(finalSeconds,finalMicros); +} + void DivEngine::playSub(bool preserveDrift, int goalRow) { logV("playSub() called"); std::chrono::high_resolution_clock::time_point timeStart=std::chrono::high_resolution_clock::now(); @@ -2020,7 +2033,7 @@ bool DivEngine::play() { bool didItPlay=playing; if (didItPlay) { if (curFilePlayer && filePlayerSync) { - curFilePlayer->setPosSeconds(totalSeconds,totalTicks); + syncFilePlayer(); curFilePlayer->play(); } } @@ -2042,7 +2055,7 @@ bool DivEngine::playToRow(int row) { bool didItPlay=playing; if (didItPlay) { if (curFilePlayer && filePlayerSync) { - curFilePlayer->setPosSeconds(totalSeconds,totalTicks); + syncFilePlayer(); curFilePlayer->play(); } } @@ -2055,6 +2068,9 @@ void DivEngine::stepOne(int row) { BUSY_BEGIN_SOFT; freelance=false; playSub(false,row); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + } for (int i=0; iplay(); + } } } BUSY_END; @@ -3188,6 +3208,10 @@ void DivEngine::deepCloneOrder(int pos, bool where) { if (pos<=curOrder) curOrder++; if (playing && !freelance) { playSub(false); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + curFilePlayer->play(); + } } } BUSY_END; @@ -3208,6 +3232,10 @@ void DivEngine::deleteOrder(int pos) { if (curOrder>=curSubSong->ordersLen) curOrder=curSubSong->ordersLen-1; if (playing && !freelance) { playSub(false); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + curFilePlayer->play(); + } } BUSY_END; } @@ -3231,6 +3259,10 @@ void DivEngine::moveOrderUp(int& pos) { pos--; if (playing && !freelance) { playSub(false); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + curFilePlayer->play(); + } } BUSY_END; } @@ -3254,6 +3286,10 @@ void DivEngine::moveOrderDown(int& pos) { pos++; if (playing && !freelance) { playSub(false); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + curFilePlayer->play(); + } } BUSY_END; } @@ -3674,7 +3710,7 @@ void DivEngine::setOrder(unsigned char order) { playSub(false); if (curFilePlayer && filePlayerSync) { - curFilePlayer->setPosSeconds(totalSeconds,totalTicks); + syncFilePlayer(); curFilePlayer->play(); } } @@ -3697,6 +3733,10 @@ void DivEngine::updateSysFlags(int system, bool restart, bool render) { if (restart) { if (isPlaying()) { playSub(false); + if (curFilePlayer && filePlayerSync) { + syncFilePlayer(); + curFilePlayer->play(); + } } else if (freelance) { reset(); } diff --git a/src/engine/engine.h b/src/engine/engine.h index 07f07f10e..6d3c54363 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -603,13 +603,16 @@ class DivEngine { DivFilePlayer* curFilePlayer; bool filePlayerSync; ssize_t filePlayerCueSeconds; - unsigned int filePlayerCueMillis; + unsigned int filePlayerCueMicros; size_t totalProcessed; unsigned int renderPoolThreads; DivWorkPool* renderPool; + // song timestamps + DivSongTimestamps* songTimestamps; + // MIDI stuff std::function midiCallback=[](const TAMidiMessage&) -> int {return -3;}; @@ -630,6 +633,8 @@ class DivEngine { void runMidiTime(int totalCycles=1); bool shallSwitchCores(); + void syncFilePlayer(); + void testFunction(); bool loadDMF(unsigned char* file, size_t len); @@ -874,6 +879,9 @@ class DivEngine { // find song length in rows (up to specified loop point), and find length of every order void findSongLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector& orders, int& length); + // calculate all song timestamps + void calcSongTimestamps(); + // play (returns whether successful) bool play(); @@ -1573,7 +1581,7 @@ class DivEngine { curFilePlayer(NULL), filePlayerSync(true), filePlayerCueSeconds(0), - filePlayerCueMillis(0), + filePlayerCueMicros(0), totalProcessed(0), renderPoolThreads(0), renderPool(NULL), diff --git a/src/engine/song.h b/src/engine/song.h index 30121a6ac..3ebe7a831 100644 --- a/src/engine/song.h +++ b/src/engine/song.h @@ -245,6 +245,13 @@ struct DivEffectStorage { storageLen(0) {} }; +struct DivSongTimestamps { + int totalSeconds; + int totalMicros; + + // TODO +}; + struct DivSong { unsigned short version; bool isDMF;