Skip to content

Commit

Permalink
Merge pull request #2 from agehama/bug/20220731_FixMidiImport
Browse files Browse the repository at this point in the history
開けないMIDIがある不具合を修正
  • Loading branch information
agehama authored Jul 31, 2022
2 parents 39fb92e + 2422253 commit 848853d
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions SFZ_MIDI_Player/source/MIDILoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,18 @@ namespace
}
default:
debugLog << U" unknown metaEvent: " << metaEventType;
return MetaEventData::Error();
debugLog << U" テキストとして解釈します";
const auto text = ReadText(reader);
debugLog << Unicode::FromUTF8(text);
return MetaEventData();
}
}
}

Optional<MidiData> LoadMidi(FilePathView path)
{
TextWriter debugLog(U"debugLog.txt");
TextWriter debugLog2(U"debugLog2.txt");
TextWriter debugLog(U"debug/debugLog.txt");
TextWriter debugLog2(U"debug/debugLog2.txt");
debugLog << U"open \"" << path << U"\"";
BinaryReader reader(path);

Expand All @@ -580,25 +583,18 @@ Optional<MidiData> LoadMidi(FilePathView path)
}

const uint16 format = ReadBytes<uint16>(reader);
if (format != 1)
if ((format != 0) && (format != 1))
{
debugLog << U"error: format != 1";
debugLog << U"error: (format != 0) && (format != 1)";
return none;
}
debugLog << U"format: " << format;

uint16 trackCount = ReadBytes<uint16>(reader);
debugLog << U"tracks: " << trackCount;
//midiData.tracks.resize(trackCount);

const uint16 resolution = ReadBytes<uint16>(reader);
debugLog << U"resolution: " << resolution;
//midiData.resolution = resolution;
/*{
uint8 bytes[2] = {};
reader.read(bytes, 2);
resolution = (bytes[0] << 8) + (bytes[1] << 0);
debugLog << U"resolution : " << resolution;
}*/

Array<TrackData> tracks;

Expand All @@ -616,11 +612,12 @@ Optional<MidiData> LoadMidi(FilePathView path)
debugLog << U"trackLength: " << trackBytesLength;

Array<MidiCode> trackData;
//TrackData trackData;

//debugLog2 << U"track " << trackIndex;

uint32 currentTick = 0;
uint8 prevOpCode = 0;

const int64 trackEndPos = reader.getPos() + trackBytesLength;

for (;;)
{
Expand Down Expand Up @@ -651,7 +648,16 @@ Optional<MidiData> LoadMidi(FilePathView path)
codeData.tick = currentTick;
//debugLog2 << U"measure: " << (tick / resolution + 1) << U", tick: " << tick % resolution << U" | " << tick;

const uint8 opcode = ReadBytes<uint8>(reader);
uint8 opcode = ReadBytes<uint8>(reader);

// ランニングステータス
if (opcode < 0x80)
{
opcode = prevOpCode;
reader.setPos(reader.getPos() - 1);
}

prevOpCode = opcode;

//if (0xFF != opcode)
{
Expand Down Expand Up @@ -741,6 +747,7 @@ Optional<MidiData> LoadMidi(FilePathView path)
if (result.isEndOfTrack())
{
trackData.push_back(codeData);
reader.setPos(trackEndPos);
break;
}
else if (result.isError())
Expand Down

0 comments on commit 848853d

Please sign in to comment.