From e8be47abb880a06090f36a7305ab0e4f59f0a701 Mon Sep 17 00:00:00 2001 From: Diegoisawesome Date: Fri, 14 Sep 2018 22:35:04 -0500 Subject: [PATCH 1/2] Add running status to mid2agb --- tools/mid2agb/main.cpp | 14 +++++++++++++- tools/mid2agb/midi.cpp | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tools/mid2agb/main.cpp b/tools/mid2agb/main.cpp index 9b883fba5b..5659e49b17 100644 --- a/tools/mid2agb/main.cpp +++ b/tools/mid2agb/main.cpp @@ -73,6 +73,18 @@ static std::string StripExtension(std::string s) return s; } +static std::string StripPathAndExtension(std::string s) +{ + std::size_t pos = s.find_last_of("/\\"); + + if (pos > 0 && pos != std::string::npos) + { + s = s.substr(pos + 1, pos); + } + + return StripExtension(s); +} + static std::string GetExtension(std::string s) { std::size_t pos = s.find_last_of('.'); @@ -206,7 +218,7 @@ int main(int argc, char** argv) RaiseError("output filename extension is not \"s\""); if (g_asmLabel.empty()) - g_asmLabel = StripExtension(outputFilename); + g_asmLabel = StripPathAndExtension(outputFilename); g_inputFile = std::fopen(inputFilename.c_str(), "rb"); diff --git a/tools/mid2agb/midi.cpp b/tools/mid2agb/midi.cpp index ba5dd654ae..7c05c52374 100644 --- a/tools/mid2agb/midi.cpp +++ b/tools/mid2agb/midi.cpp @@ -52,6 +52,7 @@ static std::int32_t s_absoluteTime; static int s_blockCount = 0; static int s_minNote; static int s_maxNote; +static int s_runningStatus = 0; void Seek(long offset) { @@ -181,19 +182,31 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz { typeChan = ReadInt8(); + if (typeChan < 0x80 && s_runningStatus != 0) + { + typeChan = s_runningStatus; + Skip(-1); + } + if (typeChan == 0xFF) { category = MidiEventCategory::Meta; size = 0; } + else if (typeChan >= 0xF8) + { + category = MidiEventCategory::Invalid; + } else if (typeChan >= 0xF0) { category = MidiEventCategory::SysEx; size = 0; + s_runningStatus = 0; } else if (typeChan >= 0x80) { category = MidiEventCategory::Control; + s_runningStatus = typeChan; switch (typeChan >> 4) { From 3028fdc353447be5b36a5bee526ffe3ee696a62a Mon Sep 17 00:00:00 2001 From: Diegoisawesome Date: Sat, 15 Sep 2018 15:49:31 -0500 Subject: [PATCH 2/2] Update mid2agb source Credit to @camthesaxman --- tools/mid2agb/Makefile | 7 ++- tools/mid2agb/main.cpp | 128 ++++++++++++++++++----------------------- tools/mid2agb/midi.cpp | 21 ++++--- 3 files changed, 74 insertions(+), 82 deletions(-) diff --git a/tools/mid2agb/Makefile b/tools/mid2agb/Makefile index 7fd5d5fc83..4dc2f123fc 100644 --- a/tools/mid2agb/Makefile +++ b/tools/mid2agb/Makefile @@ -1,12 +1,15 @@ CXX := g++ -CXXFLAGS := -std=c++11 -O2 -Wall -Wno-switch -Werror +CXXFLAGS := -std=c++11 -O2 -s -Wall -Wno-switch -Werror SRCS := agb.cpp error.cpp main.cpp midi.cpp tables.cpp HEADERS := agb.h error.h main.h midi.h tables.h -.PHONY: clean +.PHONY: all clean + +all: mid2agb + @: mid2agb: $(SRCS) $(HEADERS) $(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS) diff --git a/tools/mid2agb/main.cpp b/tools/mid2agb/main.cpp index 5659e49b17..03feabdd8f 100644 --- a/tools/mid2agb/main.cpp +++ b/tools/mid2agb/main.cpp @@ -50,7 +50,8 @@ bool g_compressionEnabled = true; " input_file filename(.mid) of MIDI file\n" " output_file filename(.s) for AGB file (default:input_file)\n" "\n" - "options -V??? master volume (default:127)\n" + "options -L??? label for assembler (default:output_file)\n" + " -V??? master volume (default:127)\n" " -G??? voice group number (default:0)\n" " -P??? priority (default:0)\n" " -R??? reverb (default:off)\n" @@ -73,18 +74,6 @@ static std::string StripExtension(std::string s) return s; } -static std::string StripPathAndExtension(std::string s) -{ - std::size_t pos = s.find_last_of("/\\"); - - if (pos > 0 && pos != std::string::npos) - { - s = s.substr(pos + 1, pos); - } - - return StripExtension(s); -} - static std::string GetExtension(std::string s) { std::size_t pos = s.find_last_of('.'); @@ -97,57 +86,45 @@ static std::string GetExtension(std::string s) return ""; } -struct Option +static std::string BaseName(std::string s) { - char letter = 0; - const char *arg = NULL; -}; + std::size_t posAfterSlash = s.find_last_of("/\\"); -static Option ParseOption(int& index, const int argc, char** argv) + if (posAfterSlash == std::string::npos) + posAfterSlash = 0; + else + posAfterSlash++; + + std::size_t dotPos = s.find_first_of('.', posAfterSlash); + if (dotPos > posAfterSlash && dotPos != std::string::npos) + s = s.substr(posAfterSlash, dotPos - posAfterSlash); + + return s; +} + +static const char *GetArgument(int argc, char **argv, int& index) { - static std::set optionsWithArg = { 'L', 'V', 'G', 'P', 'R' }; - static std::set optionsWithoutArg = { 'X', 'E', 'N' }; - assert(index >= 0 && index < argc); - const char *opt = argv[index]; + const char *option = argv[index]; - assert(opt[0] == '-'); - assert(std::strlen(opt) == 2); + assert(option != nullptr); + assert(option[0] == '-'); - char letter = std::toupper(opt[1]); + // If there is text following the letter, return that. + if (std::strlen(option) >= 3) + return option + 2; - bool isOption = false; - bool hasArg = false; - - if (optionsWithArg.count(letter) != 0) - { - isOption = true; - hasArg = true; - } - else if (optionsWithoutArg.count(letter) != 0) - { - isOption = true; - } - - if (!isOption) - PrintUsage(); - - Option retVal; - - retVal.letter = letter; - - if (hasArg) + // Otherwise, try to get the next arg. + if (index + 1 < argc) { index++; - - if (index >= argc) - RaiseError("missing argument for \"-%c\"", letter); - - retVal.arg = argv[index]; + return argv[index]; + } + else + { + return nullptr; } - - return retVal; } int main(int argc, char** argv) @@ -157,51 +134,60 @@ int main(int argc, char** argv) for (int i = 1; i < argc; i++) { - if (argv[i][0] == '-' && std::strlen(argv[i]) == 2) - { - Option option = ParseOption(i, argc, argv); + const char *option = argv[i]; - switch (option.letter) + if (option[0] == '-' && option[1] != '\0') + { + const char *arg = GetArgument(argc, argv, i); + + switch (std::toupper(option[1])) { case 'E': g_exactGateTime = true; break; case 'G': - g_voiceGroup = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_voiceGroup = std::stoi(arg); break; case 'L': - g_asmLabel = option.arg; + if (arg == nullptr) + PrintUsage(); + g_asmLabel = std::stoi(arg); break; case 'N': g_compressionEnabled = false; break; case 'P': - g_priority = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_priority = std::stoi(arg); break; case 'R': - g_reverb = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_reverb = std::stoi(arg); break; case 'V': - g_masterVolume = std::stoi(option.arg); + if (arg == nullptr) + PrintUsage(); + g_masterVolume = std::stoi(arg); break; case 'X': g_clocksPerBeat = 2; break; + default: + PrintUsage(); } } else { - switch (i) - { - case 1: + if (inputFilename.empty()) inputFilename = argv[i]; - break; - case 2: + else if (outputFilename.empty()) outputFilename = argv[i]; - break; - default: + else PrintUsage(); - } } } @@ -218,7 +204,7 @@ int main(int argc, char** argv) RaiseError("output filename extension is not \"s\""); if (g_asmLabel.empty()) - g_asmLabel = StripPathAndExtension(outputFilename); + g_asmLabel = BaseName(outputFilename); g_inputFile = std::fopen(inputFilename.c_str(), "rb"); diff --git a/tools/mid2agb/midi.cpp b/tools/mid2agb/midi.cpp index 7c05c52374..09480e139c 100644 --- a/tools/mid2agb/midi.cpp +++ b/tools/mid2agb/midi.cpp @@ -52,7 +52,7 @@ static std::int32_t s_absoluteTime; static int s_blockCount = 0; static int s_minNote; static int s_maxNote; -static int s_runningStatus = 0; +static int s_runningStatus; void Seek(long offset) { @@ -171,6 +171,7 @@ void StartTrack() { Seek(s_trackDataStart); s_absoluteTime = 0; + s_runningStatus = 0; } void SkipEventData() @@ -182,20 +183,18 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz { typeChan = ReadInt8(); - if (typeChan < 0x80 && s_runningStatus != 0) + if (typeChan < 0x80) { + // If data byte was found, use the running status. + ungetc(typeChan, g_inputFile); typeChan = s_runningStatus; - Skip(-1); } - + if (typeChan == 0xFF) { category = MidiEventCategory::Meta; size = 0; - } - else if (typeChan >= 0xF8) - { - category = MidiEventCategory::Invalid; + s_runningStatus = 0; } else if (typeChan >= 0xF0) { @@ -206,7 +205,6 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz else if (typeChan >= 0x80) { category = MidiEventCategory::Control; - s_runningStatus = typeChan; switch (typeChan >> 4) { @@ -218,6 +216,7 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz size = 2; break; } + s_runningStatus = typeChan; } else { @@ -434,7 +433,10 @@ bool CheckNoteEnd(Event& event) void FindNoteEnd(Event& event) { + // Save the current file position and running status + // which get modified by CheckNoteEnd. long startPos = ftell(g_inputFile); + int savedRunningStatus = s_runningStatus; event.param2 = 0; @@ -442,6 +444,7 @@ void FindNoteEnd(Event& event) ; Seek(startPos); + s_runningStatus = savedRunningStatus; } bool ReadTrackEvent(Event& event)