Skip to content

Commit

Permalink
Add some workarounds to mid2agb from https://github.com/freshollie/sa2.
Browse files Browse the repository at this point in the history
  • Loading branch information
laqieer committed Apr 28, 2022
1 parent 0ec9639 commit 1105344
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
29 changes: 26 additions & 3 deletions tools/mid2agb/agb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,22 +431,33 @@ void PrintAgbTrack(std::vector<Event>& events)
if (event.type == EventType::Note)
break;

if (event.type == EventType::Controller && event.param1 == 0x07)
if (IsVolumeEvent(event))
{
foundVolBeforeNote = true;
break;
}
}

// If we find this signature we need to print
// the keyshift after the volume event
bool needsKeyshiftWorkaround =
events[0].type == EventType::WholeNoteMark &&
IsVolumeEvent(events[1]);

if (!foundVolBeforeNote)
PrintByte("\tVOL , 127*%s_mvl/mxv", g_asmLabel.c_str());

if (!needsKeyshiftWorkaround) {
PrintByte("KEYSH , %s_key%+d", g_asmLabel.c_str(), 0);
}
PrintWait(g_initialWait);
PrintByte("KEYSH , %s_key%+d", g_asmLabel.c_str(), 0);

for (unsigned i = 0; events[i].type != EventType::EndOfTrack; i++)
{
const Event& event = events[i];
// We are going to be hacky
// and modify the time value, so can't be const
Event& event = events[i];
std::int32_t eventTime = event.time;

if (IsPatternBoundary(event.type))
{
Expand Down Expand Up @@ -513,7 +524,19 @@ void PrintAgbTrack(std::vector<Event>& events)
PrintOp(event.time, "BEND ", "c_v%+d", event.param2 - 64);
break;
case EventType::Controller:
// Ensure that a wait value is not printed after the volume
// command
if (needsKeyshiftWorkaround && i == 1) {
event.time = 0;
}

PrintControllerOp(event);
// print the keyshift and any wait after we have written the volume value
if (needsKeyshiftWorkaround && i == 1) {
PrintByte("\tKEYSH , %s_key%+d", g_asmLabel.c_str(), 0);
PrintWait(eventTime);
event.time = eventTime;
}
break;
default:
PrintWait(event.time);
Expand Down
8 changes: 8 additions & 0 deletions tools/mid2agb/midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ bool EventCompare(const Event& event1, const Event& event2)
unsigned event1Type = (unsigned)event1.type;
unsigned event2Type = (unsigned)event2.type;

// For some reason if there is a volume
// event and an instrument change event
// at the start, we need to ensure the volume event
// happens first
if (event1.type == EventType::InstrumentChange && IsVolumeEvent(event2) &&
event1.time == 0 && event2.time == 0)
return false;

if (event1.type == EventType::Note)
event1Type += event1.note;

Expand Down
4 changes: 4 additions & 0 deletions tools/mid2agb/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ inline bool IsPatternBoundary(EventType type)
return type == EventType::EndOfTrack || (int)type <= 0x17;
}

inline bool IsVolumeEvent(const Event& event) {
return event.type == EventType::Controller && event.param1 == 0x07;
}

#endif // MIDI_H

0 comments on commit 1105344

Please sign in to comment.