Skip to content

Commit

Permalink
slightly more robust midi parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
khang06 committed Aug 29, 2020
1 parent 4813b05 commit 2a8905c
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Chikara/Midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,29 @@ void Midi::loadMidi()

while(file_stream.tellg() != file_end)
{
assertText("MTrk");
length = parseInt();
size_t pos = file_stream.tellg();

if(length + pos > file_end) length = file_end - pos;
try {
assertText("MTrk");
length = parseInt();
size_t pos = file_stream.tellg();

if (length + pos > file_end) {
printf("warning: track runs past the end of the midi\n");
length = file_end - pos;
}

file_stream.seekg(pos + length, std::ios::beg);
file_stream.seekg(pos + length, std::ios::beg);

MidiChunk chunk;
chunk.start = pos;
chunk.length = length;
tracks.push_back(chunk);
count++;
MidiChunk chunk;
chunk.start = pos;
chunk.length = length;
tracks.push_back(chunk);
count++;
}
catch (const char* e) {
int track_pos = file_stream.tellg();
printf("broken track, not parsing further! pos: %d\n", track_pos);
break;
}
}

track_count = count;
Expand Down Expand Up @@ -231,6 +241,7 @@ void Midi::loadMidi()

} catch(const char* e)
{
printf("%s\n", e);
MessageBoxA(NULL, "This MIDI doesn't appear to be valid.", "Fatal Error", MB_ICONERROR);
exit(1);
}
Expand Down Expand Up @@ -409,7 +420,7 @@ void BufferedReader::updateBuffer()
if ((pos + read) > (start + length))
read = start + length - pos;

if (read == 0)
if (read == 0 && buffer_size != 0)
throw "\nOutside the buffer";

mtx->lock();
Expand Down

0 comments on commit 2a8905c

Please sign in to comment.