gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

commit f9573bfe09524bb1abedb211014ac9d5e81e34b7
parent 369be558fbd8a3a796deb9d328381dedad7ae27a
Author: dsp56300 <dsp56300@users.noreply.github.com>
Date:   Sat, 26 Feb 2022 17:27:32 +0100

be more tolerant to broken midi files

Diffstat:
Msource/synthLib/midiToSysex.cpp | 36++++++++++++++++++++++++++++++------
1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/source/synthLib/midiToSysex.cpp b/source/synthLib/midiToSysex.cpp @@ -49,21 +49,39 @@ namespace synthLib return false; } - const uint8_t event = getc(hFile); - switch (event) + const auto ch = getc(hFile); + + if (ch == EOF) + break; + + const auto ev = static_cast<uint8_t>(ch); + + switch (ev) { case 0xf0: // that's what we're searching for, sysex data { const auto sysExLen = readVarLen(hFile, &numBytesRead); std::vector<uint8_t> sysex; - sysex.resize(sysExLen + 1); + sysex.reserve(sysExLen + 1); + + sysex.push_back(0xf0); - sysex[0] = 0xf0; + // we ignore the provided sysex length as I've seen files that do not have the length encoded properly + while(true) + { + const auto c = getc(hFile); + sysex.push_back(static_cast<uint8_t>(c)); - fread(&sysex[1], sysExLen, 1, hFile); + if(c == 0xf7) + { + _sysexMessages.insert(_sysexMessages.end(), sysex.begin(), sysex.end()); + break; + } - _sysexMessages.insert(_sysexMessages.end(), sysex.begin(), sysex.end()); + if (feof(hFile)) + break; + } } break; @@ -135,6 +153,12 @@ namespace synthLib int32_t MidiToSysex::readVarLen(FILE* hFile, int* _pNumBytesRead) { + if (feof(hFile)) + { + *_pNumBytesRead = 0; + return -1; + } + uint32_t value; uint8_t c;