Merge pull request #327 from Diegoisawesome/master

Update mid2agb source
This commit is contained in:
Diegoisawesome 2018-09-15 16:12:25 -05:00 committed by GitHub
commit b183b99694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 61 deletions

View File

@ -1,12 +1,15 @@
CXX := g++ 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 SRCS := agb.cpp error.cpp main.cpp midi.cpp tables.cpp
HEADERS := agb.h error.h main.h midi.h tables.h HEADERS := agb.h error.h main.h midi.h tables.h
.PHONY: clean .PHONY: all clean
all: mid2agb
@:
mid2agb: $(SRCS) $(HEADERS) mid2agb: $(SRCS) $(HEADERS)
$(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS) $(CXX) $(CXXFLAGS) $(SRCS) -o $@ $(LDFLAGS)

View File

@ -50,7 +50,8 @@ bool g_compressionEnabled = true;
" input_file filename(.mid) of MIDI file\n" " input_file filename(.mid) of MIDI file\n"
" output_file filename(.s) for AGB file (default:input_file)\n" " output_file filename(.s) for AGB file (default:input_file)\n"
"\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" " -G??? voice group number (default:0)\n"
" -P??? priority (default:0)\n" " -P??? priority (default:0)\n"
" -R??? reverb (default:off)\n" " -R??? reverb (default:off)\n"
@ -85,57 +86,45 @@ static std::string GetExtension(std::string s)
return ""; return "";
} }
struct Option static std::string BaseName(std::string s)
{ {
char letter = 0; std::size_t posAfterSlash = s.find_last_of("/\\");
const char *arg = NULL;
};
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<char> optionsWithArg = { 'L', 'V', 'G', 'P', 'R' };
static std::set<char> optionsWithoutArg = { 'X', 'E', 'N' };
assert(index >= 0 && index < argc); assert(index >= 0 && index < argc);
const char *opt = argv[index]; const char *option = argv[index];
assert(opt[0] == '-'); assert(option != nullptr);
assert(std::strlen(opt) == 2); 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; // Otherwise, try to get the next arg.
bool hasArg = false; if (index + 1 < argc)
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)
{ {
index++; index++;
return argv[index];
if (index >= argc) }
RaiseError("missing argument for \"-%c\"", letter); else
{
retVal.arg = argv[index]; return nullptr;
} }
return retVal;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -145,51 +134,60 @@ int main(int argc, char** argv)
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
if (argv[i][0] == '-' && std::strlen(argv[i]) == 2) const char *option = argv[i];
{
Option option = ParseOption(i, argc, argv);
switch (option.letter) if (option[0] == '-' && option[1] != '\0')
{
const char *arg = GetArgument(argc, argv, i);
switch (std::toupper(option[1]))
{ {
case 'E': case 'E':
g_exactGateTime = true; g_exactGateTime = true;
break; break;
case 'G': case 'G':
g_voiceGroup = std::stoi(option.arg); if (arg == nullptr)
PrintUsage();
g_voiceGroup = std::stoi(arg);
break; break;
case 'L': case 'L':
g_asmLabel = option.arg; if (arg == nullptr)
PrintUsage();
g_asmLabel = std::stoi(arg);
break; break;
case 'N': case 'N':
g_compressionEnabled = false; g_compressionEnabled = false;
break; break;
case 'P': case 'P':
g_priority = std::stoi(option.arg); if (arg == nullptr)
PrintUsage();
g_priority = std::stoi(arg);
break; break;
case 'R': case 'R':
g_reverb = std::stoi(option.arg); if (arg == nullptr)
PrintUsage();
g_reverb = std::stoi(arg);
break; break;
case 'V': case 'V':
g_masterVolume = std::stoi(option.arg); if (arg == nullptr)
PrintUsage();
g_masterVolume = std::stoi(arg);
break; break;
case 'X': case 'X':
g_clocksPerBeat = 2; g_clocksPerBeat = 2;
break; break;
default:
PrintUsage();
} }
} }
else else
{ {
switch (i) if (inputFilename.empty())
{
case 1:
inputFilename = argv[i]; inputFilename = argv[i];
break; else if (outputFilename.empty())
case 2:
outputFilename = argv[i]; outputFilename = argv[i];
break; else
default:
PrintUsage(); PrintUsage();
}
} }
} }
@ -206,7 +204,7 @@ int main(int argc, char** argv)
RaiseError("output filename extension is not \"s\""); RaiseError("output filename extension is not \"s\"");
if (g_asmLabel.empty()) if (g_asmLabel.empty())
g_asmLabel = StripExtension(outputFilename); g_asmLabel = BaseName(outputFilename);
g_inputFile = std::fopen(inputFilename.c_str(), "rb"); g_inputFile = std::fopen(inputFilename.c_str(), "rb");

View File

@ -52,6 +52,7 @@ static std::int32_t s_absoluteTime;
static int s_blockCount = 0; static int s_blockCount = 0;
static int s_minNote; static int s_minNote;
static int s_maxNote; static int s_maxNote;
static int s_runningStatus;
void Seek(long offset) void Seek(long offset)
{ {
@ -170,6 +171,7 @@ void StartTrack()
{ {
Seek(s_trackDataStart); Seek(s_trackDataStart);
s_absoluteTime = 0; s_absoluteTime = 0;
s_runningStatus = 0;
} }
void SkipEventData() void SkipEventData()
@ -181,15 +183,24 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz
{ {
typeChan = ReadInt8(); typeChan = ReadInt8();
if (typeChan < 0x80)
{
// If data byte was found, use the running status.
ungetc(typeChan, g_inputFile);
typeChan = s_runningStatus;
}
if (typeChan == 0xFF) if (typeChan == 0xFF)
{ {
category = MidiEventCategory::Meta; category = MidiEventCategory::Meta;
size = 0; size = 0;
s_runningStatus = 0;
} }
else if (typeChan >= 0xF0) else if (typeChan >= 0xF0)
{ {
category = MidiEventCategory::SysEx; category = MidiEventCategory::SysEx;
size = 0; size = 0;
s_runningStatus = 0;
} }
else if (typeChan >= 0x80) else if (typeChan >= 0x80)
{ {
@ -205,6 +216,7 @@ void DetermineEventCategory(MidiEventCategory& category, int& typeChan, int& siz
size = 2; size = 2;
break; break;
} }
s_runningStatus = typeChan;
} }
else else
{ {
@ -421,7 +433,10 @@ bool CheckNoteEnd(Event& event)
void FindNoteEnd(Event& event) void FindNoteEnd(Event& event)
{ {
// Save the current file position and running status
// which get modified by CheckNoteEnd.
long startPos = ftell(g_inputFile); long startPos = ftell(g_inputFile);
int savedRunningStatus = s_runningStatus;
event.param2 = 0; event.param2 = 0;
@ -429,6 +444,7 @@ void FindNoteEnd(Event& event)
; ;
Seek(startPos); Seek(startPos);
s_runningStatus = savedRunningStatus;
} }
bool ReadTrackEvent(Event& event) bool ReadTrackEvent(Event& event)