From bbd42b653db7a5519ddefb38db5a20777e39201e Mon Sep 17 00:00:00 2001 From: Mike-Goutokuji <83477269+mike-goutokuji@users.noreply.github.com> Date: Tue, 16 Jun 2026 15:26:23 -0400 Subject: [PATCH] [mid2agb] Add support for the rest of the missing commands Added the rest of the missing commands, including the loop and goto ops. --- sound/MPlayDef.s | 8 +++++++ tools/mid2agb/agb.cpp | 52 ++++++++++++++++++++++++++++++++++++++---- tools/mid2agb/midi.cpp | 6 ++--- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/sound/MPlayDef.s b/sound/MPlayDef.s index 95a788e85e..0dda12e5b6 100644 --- a/sound/MPlayDef.s +++ b/sound/MPlayDef.s @@ -69,8 +69,16 @@ .equ TUNE, 0xc8 @ micro tuning (c_v+??) .equ XCMD, 0xcd @ extend command ***lib + .equ xWAVE, 0x01 + .equ xTYPE, 0x02 + .equ xATTA, 0x04 + .equ xDECA, 0x05 + .equ xSUST, 0x06 + .equ xRELE, 0x07 .equ xIECV, 0x08 @ imi.echo vol ***lib .equ xIECL, 0x09 @ imi.echo len ***lib + .equ xLENG, 0x0A + .equ xSWEE, 0x0B .equ EOT, 0xce @ End of Tie .equ TIE, 0xcf @ diff --git a/tools/mid2agb/agb.cpp b/tools/mid2agb/agb.cpp index 1db758d1bd..41478a3460 100644 --- a/tools/mid2agb/agb.cpp +++ b/tools/mid2agb/agb.cpp @@ -330,16 +330,38 @@ void PrintMemAcc(const Event& event) void PrintExtendedOp(const Event& event) { - // TODO: support for other extended commands - switch (s_extendedCommand) { + case 0x01: + PrintOp(event.time, "XCMD ", "xWAVE , %u", event.param2); + break; + case 0x02: + PrintOp(event.time, "XCMD ", "xTYPE , %u", event.param2); + break; + case 0x04: + PrintOp(event.time, "XCMD ", "xATTA , %u", event.param2); + break; + case 0x05: + PrintOp(event.time, "XCMD ", "xDECA , %u", event.param2); + break; + case 0x06: + PrintOp(event.time, "XCMD ", "xSUST , %u", event.param2); + break; + case 0x07: + PrintOp(event.time, "XCMD ", "xRELE , %u", event.param2); + break; case 0x08: PrintOp(event.time, "XCMD ", "xIECV , %u", event.param2); break; case 0x09: PrintOp(event.time, "XCMD ", "xIECL , %u", event.param2); break; + case 0x0A: + PrintOp(event.time, "XCMD ", "xLENG , %u", event.param2); + break; + case 0x0B: + PrintOp(event.time, "XCMD ", "xSWEE , %u", event.param2); + break; default: PrintWait(event.time); break; @@ -348,7 +370,14 @@ void PrintExtendedOp(const Event& event) void PrintControllerOp(const Event& event) { - switch (event.param1) + int param1 = event.param1; + + // Map alternate controller ranges (42-63) down by 30 + // e.g. CC 63 -> 33 (PRIO) + if (param1 >= 42 && param1 < 64) + param1 -= 30; + + switch (param1) { case 0x01: PrintOp(event.time, "MOD ", "%u", event.param2); @@ -401,7 +430,22 @@ void PrintControllerOp(const Event& event) break; case 0x1E: s_extendedCommand = event.param2; - // TODO: loop op + if (event.param2 == 100) + { + std::fprintf(g_outputFile, "%s_%u_LOOP:\n", g_asmLabel.c_str(), g_agbTrack); + PrintWait(event.time); + ResetTrackVars(); + } + else if (event.param2 == 101) + { + PrintByte("GOTO"); + PrintWord("%s_%u_LOOP", g_asmLabel.c_str(), g_agbTrack); + PrintWait(event.time); + } + else + { + PrintWait(event.time); + } break; case 0x21: case 0x27: diff --git a/tools/mid2agb/midi.cpp b/tools/mid2agb/midi.cpp index fa7d9ce285..010849a82a 100644 --- a/tools/mid2agb/midi.cpp +++ b/tools/mid2agb/midi.cpp @@ -149,7 +149,7 @@ void ReadMidiFileHeader() g_midiTrackCount = ReadInt16(); g_midiTimeDiv = ReadInt16(); - if (g_midiTimeDiv < 0) + if (g_midiTimeDiv <= 0) RaiseError("unsupported MIDI time division (%d)", g_midiTimeDiv); } @@ -238,7 +238,7 @@ std::string ReadEventText() if (length <= 2) { - if (fread(buffer, length, 1, g_inputFile) != 1) + if (length > 0 && fread(buffer, length, 1, g_inputFile) != 1) RaiseError("failed to read event text"); } else @@ -636,7 +636,7 @@ void ConvertTimes(std::vector& events) if (event.type == EventType::Note) { - event.param1 = g_noteVelocityLUT[event.param1]; + event.param1 = g_noteVelocityLUT[event.param1 & 0x7F]; std::uint32_t duration = (24 * g_clocksPerBeat * event.param2) / g_midiTimeDiv;