From 5378974b9603802a987ca0b3561ec2e7b5e6ffee Mon Sep 17 00:00:00 2001 From: Ian Karlsson Date: Fri, 27 May 2022 21:40:26 +0200 Subject: [PATCH] Save the size of each block in a Furnace module --- papers/format.md | 12 ++++++------ src/engine/fileOps.cpp | 26 +++++++++++++++++++++++++- src/engine/instrument.cpp | 8 ++++++++ src/engine/wavetable.cpp | 8 ++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/papers/format.md b/papers/format.md index 013f2c5d..c06b99b9 100644 --- a/papers/format.md +++ b/papers/format.md @@ -130,7 +130,7 @@ size | description size | description -----|------------------------------------ 4 | "INFO" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field 1 | time base (of first song) 1 | speed 1 (of first song) 1 | speed 2 (of first song) @@ -332,7 +332,7 @@ the way it's currently done is really weird, but it provides for some backwards size | description -----|------------------------------------ 4 | "SONG" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field 1 | time base 1 | speed 1 1 | speed 2 @@ -386,7 +386,7 @@ notes: size | description -----|------------------------------------ 4 | "INST" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field 2 | format version (see header) 1 | instrument type | - 0: standard @@ -795,7 +795,7 @@ size | description size | description -----|------------------------------------ 4 | "WAVE" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field STR | wavetable name 4 | wavetable size 4 | wavetable min @@ -809,7 +809,7 @@ size | description size | description -----|------------------------------------ 4 | "SMPL" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field STR | sample name 4 | length 4 | rate @@ -841,7 +841,7 @@ size | description size | description -----|------------------------------------ 4 | "PATR" block ID - 4 | reserved + 4 | size of the block excluding the ID and this field 2 | channel 2 | pattern index 2 | subsong (>=95) or reserved diff --git a/src/engine/fileOps.cpp b/src/engine/fileOps.cpp index 637f7b24..7a4fb8f1 100644 --- a/src/engine/fileOps.cpp +++ b/src/engine/fileOps.cpp @@ -2696,7 +2696,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { std::vector wavePtr; std::vector samplePtr; std::vector patPtr; - size_t ptrSeek, subSongPtrSeek; + size_t ptrSeek, subSongPtrSeek, blockStartSeek, blockEndSeek; size_t subSongIndex=0; DivSubSong* subSong=song.subsong[subSongIndex]; warnings=""; @@ -2775,6 +2775,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { /// SONG INFO w->write("INFO",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeC(subSong->timeBase); @@ -2932,11 +2933,17 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { w->writeI(0); } + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); + /// SUBSONGS for (subSongIndex=1; subSongIndextell()); w->write("SONG",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeC(subSong->timeBase); @@ -2979,6 +2986,11 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { for (int i=0; iwriteString(subSong->chanShortName[i],false); } + + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); } /// INSTRUMENT @@ -3000,6 +3012,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { DivSample* sample=song.sample[i]; samplePtr.push_back(w->tell()); w->write("SMPL",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeString(sample->name,false); @@ -3012,6 +3025,11 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { w->writeI(sample->loopStart); w->write(sample->getCurBuf(),sample->getCurBufLen()); + + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); } /// PATTERN @@ -3019,6 +3037,7 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { DivPattern* pat=song.subsong[i.subsong]->pat[i.chan].getPattern(i.pat,false); patPtr.push_back(w->tell()); w->write("PATR",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeS(i.chan); @@ -3036,6 +3055,11 @@ SafeWriter* DivEngine::saveFur(bool notPrimary) { } w->writeString(pat->name,false); + + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); } /// POINTERS diff --git a/src/engine/instrument.cpp b/src/engine/instrument.cpp index 8b8a1fb0..8e32e1e8 100644 --- a/src/engine/instrument.cpp +++ b/src/engine/instrument.cpp @@ -24,7 +24,10 @@ #include "../fileutils.h" void DivInstrument::putInsData(SafeWriter* w) { + size_t blockStartSeek, blockEndSeek; + w->write("INST",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeS(DIV_ENGINE_VERSION); @@ -520,6 +523,11 @@ void DivInstrument::putInsData(SafeWriter* w) { for (int j=0; j<23; j++) { // reserved w->writeC(0); } + + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); } DivDataErrors DivInstrument::readInsData(SafeReader& reader, short version) { diff --git a/src/engine/wavetable.cpp b/src/engine/wavetable.cpp index db6c33b2..953400ee 100644 --- a/src/engine/wavetable.cpp +++ b/src/engine/wavetable.cpp @@ -24,7 +24,10 @@ #include "../fileutils.h" void DivWavetable::putWaveData(SafeWriter* w) { + size_t blockStartSeek, blockEndSeek; + w->write("WAVE",4); + blockStartSeek=w->tell(); w->writeI(0); w->writeC(0); // name @@ -34,6 +37,11 @@ void DivWavetable::putWaveData(SafeWriter* w) { for (int j=0; jwriteI(data[j]); } + + blockEndSeek=w->tell(); + w->seek(blockStartSeek,SEEK_SET); + w->writeI(blockEndSeek-blockStartSeek-4); + w->seek(0,SEEK_END); } DivDataErrors DivWavetable::readWaveData(SafeReader& reader, short version) {