Last active
September 8, 2017 14:21
-
-
Save lxfly2000/8170461d179ccd8b25452e30b3804374 to your computer and use it in GitHub Desktop.
RMI与MIDI文件相互转换程序。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//参考:http://www.yueshou.net/jiao/shuma/yingjian/200608/14735.html | |
#include<stdio.h> | |
#include<stdlib.h> | |
#include<string.h> | |
#define stricmp _stricmp | |
struct RMIDHeader | |
{ | |
char strRIFF[4]; | |
int chunkSize; | |
char strFormat[4]; | |
char strdata[4]; | |
int midiFileChunkSize; | |
}; | |
size_t WriteToFile(const char *fn, const char *pdata, int length) | |
{ | |
FILE *pfileOut; | |
fopen_s(&pfileOut, fn, "wb"); | |
size_t wrotebytes = 0; | |
if (pfileOut) | |
{ | |
const int tbuffersize = 64; | |
size_t writebytes; | |
for (int i = 0; i < length; i += tbuffersize) | |
{ | |
writebytes = min(tbuffersize, length - i); | |
wrotebytes += fwrite(pdata + i, writebytes, 1, pfileOut)*writebytes; | |
} | |
fclose(pfileOut); | |
printf("写入了%zd字节至 %s。\n", wrotebytes, fn); | |
} | |
else printf("无法写入文件至 %s。\n", fn); | |
return wrotebytes; | |
} | |
int ConvertToMIDI(FILE *pfileIn, const char *fn) | |
{ | |
struct RMIDHeader rh; | |
fread(&rh, sizeof rh, 1, pfileIn); | |
if (strncmp(rh.strRIFF, "RIFF", 4))printf("文件头不正确:%s\n", rh.strRIFF); | |
if (strncmp(rh.strFormat, "RMID", 4))printf("格式标签不正确:%s\n", rh.strFormat); | |
if (strncmp(rh.strdata, "data", 4))printf("data标签不正确:%s\n", rh.strdata); | |
printf("RIFF文件内容区段大小:%d字节\nMIDI文件大小:%d字节\n", rh.chunkSize, rh.midiFileChunkSize); | |
strcpy_s(strrchr(fn, '.'), 5, ".mid"); | |
char *pbuffer = (char*)malloc(rh.midiFileChunkSize); | |
fseek(pfileIn, sizeof rh, SEEK_SET); | |
fread(pbuffer, rh.midiFileChunkSize, 1, pfileIn); | |
if (!WriteToFile(fn, pbuffer, rh.midiFileChunkSize))return -1; | |
free(pbuffer); | |
return 0; | |
} | |
int ConvertToRMI(FILE *pfileIn, const char *fn) | |
{ | |
struct RMIDHeader rh = { "RIFF",sizeof rh - 8,"RMID","data",0 }; | |
fseek(pfileIn, 0, SEEK_END); | |
rh.midiFileChunkSize = ftell(pfileIn); | |
fseek(pfileIn, 0, SEEK_SET); | |
rh.chunkSize += rh.midiFileChunkSize; | |
strcpy_s(strrchr(fn, '.'), 5, ".rmi"); | |
int filesize = rh.midiFileChunkSize + sizeof rh; | |
char *pbuffer = (char*)malloc(filesize); | |
memcpy(pbuffer, &rh, sizeof rh); | |
fread(pbuffer + sizeof rh, rh.midiFileChunkSize, 1, pfileIn); | |
if (!WriteToFile(fn, pbuffer, filesize))return -1; | |
free(pbuffer); | |
return 0; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
FILE *pfileIn; | |
char rstrfile[_MAX_PATH], *strfile = rstrfile; | |
if (argc != 2) | |
{ | |
puts("请指定要转换的文件:"); | |
gets_s(strfile, _MAX_PATH); | |
if (strfile[0] == '\0') | |
{ | |
puts("没有选择文件。"); | |
return 1; | |
} | |
} | |
else | |
strcpy_s(strfile, _MAX_PATH, argv[1]); | |
if (strfile[0] == '\"') | |
{ | |
strfile++; | |
strfile[strlen(strfile) - 1] = '\0'; | |
} | |
fopen_s(&pfileIn, strfile, "rb"); | |
if (!pfileIn) | |
{ | |
puts("文件打开失败……"); | |
return -1; | |
} | |
if (stricmp(strrchr(strfile, '.'), ".rmi") == 0) | |
ConvertToMIDI(pfileIn, strfile); | |
else if (stricmp(strrchr(strfile, '.'), ".mid") == 0) | |
ConvertToRMI(pfileIn, strfile); | |
else puts("不支持的格式。"); | |
fclose(pfileIn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment