diff options
author | Wohlstand <admin@wohlnet.ru> | 2020-01-28 17:36:42 +0300 |
---|---|---|
committer | Wohlstand <admin@wohlnet.ru> | 2020-01-28 17:36:42 +0300 |
commit | bdb7f81da3e43db01f4ab5781287d22ecb56e025 (patch) | |
tree | 28b412c284e7126f94bfa1eb4ababe5eb0bacf2e /utils | |
parent | 1ef47f7db773a76a5652094b5ac8a9ec6408d064 (diff) | |
download | libADLMIDI-bdb7f81da3e43db01f4ab5781287d22ecb56e025.tar.gz libADLMIDI-bdb7f81da3e43db01f4ab5781287d22ecb56e025.tar.bz2 libADLMIDI-bdb7f81da3e43db01f4ab5781287d22ecb56e025.zip |
Added MUS2MID conversion tool
Diffstat (limited to 'utils')
-rw-r--r-- | utils/mus2mid/CMakeLists.txt | 7 | ||||
-rw-r--r-- | utils/mus2mid/mus2mid.cpp | 74 |
2 files changed, 81 insertions, 0 deletions
diff --git a/utils/mus2mid/CMakeLists.txt b/utils/mus2mid/CMakeLists.txt new file mode 100644 index 0000000..6430dd4 --- /dev/null +++ b/utils/mus2mid/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(mus2mid mus2mid.cpp) +# TODO: Use own library +target_include_directories(mus2mid PRIVATE ${PROJECT_SOURCE_DIR}/src) + +install(TARGETS mus2mid + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") + diff --git a/utils/mus2mid/mus2mid.cpp b/utils/mus2mid/mus2mid.cpp new file mode 100644 index 0000000..f737f8c --- /dev/null +++ b/utils/mus2mid/mus2mid.cpp @@ -0,0 +1,74 @@ + +#include "cvt_mus2mid.hpp" +#include <stdio.h> +#include <sys/stat.h> +#if !defined(_WIN32) +#include <unistd.h> +#else +#include <io.h> +#define fileno(fd) _fileno(fd) +#define isatty(fd) _isatty(fd) +#endif + +int main(int argc, char *argv[]) +{ + if(argc != 2) + { + fprintf(stderr, "Usage: mus2mid <midi-file>\n"); + return 1; + } + + const char *filename = argv[1]; + + FILE *fh = fopen(filename, "rb"); + if(!fh) + { + fprintf(stderr, "Error opening file.\n"); + return 1; + } + + struct stat st; + if(fstat(fileno(fh), &st) != 0) + { + fprintf(stderr, "Error reading file status.\n"); + return 1; + } + + size_t insize = (size_t)st.st_size; + if(insize > 8 * 1024 * 1024) + { + fprintf(stderr, "File too large.\n"); + return 1; + } + + uint8_t *filedata = new uint8_t[insize]; + if(fread(filedata, 1, insize, fh) != insize) + { + fprintf(stderr, "Error reading file data.\n"); + return 1; + } + + uint8_t *xmidata = NULL; + uint32_t xmisize = 0; + if(Convert_mus2midi(filedata, static_cast<uint32_t>(insize), &xmidata, &xmisize, 0) < 0) + { + fprintf(stderr, "Error converting MUS to SMF.\n"); + return 1; + } + + FILE *out = stdout; + if(isatty(fileno(out))) + { + fprintf(stderr, "Not writing SMF data on the text terminal.\n"); + } + else + { + if (fwrite(xmidata, 1, xmisize, out) != xmisize || fflush(out) != 0) + { + fprintf(stderr, "Error writing SMF data.\n"); + return 1; + } + } + + return 0; +} |