乐器数字接口(MIDI)简介
乐器数字接口(Musical Instrument Digital Interface,MIDI),是20世纪80年代初为解决电声乐器之间的通信问题而提出的,是电子乐器制造商们建立的通信标准,是电子乐器之间以及电子乐器与电脑之间的统一交流协议。从一个MIDI设备转送到另一个MIDI设备上去的数据就是MIDI消息(MIDI Message)。
例如,在电子钢琴上按下某个音符对应的键(称为开音),则产生一个MIDI消息,该消息记录了所按的键、力度;当松开所按的键时(称为关音),便产生另一个MIDI消息用以表示所松开的键及松开的速度。当然这些信息都是易编辑的。
显然,MIDI消息不是音频波形的数字化,而是一种电子乐谱。MIDI已成为编曲界最广泛的音乐标准格式,可称其为“计算机能理解的乐谱”。
MIDI音符编码
是不是看晕了,没关系,我在下面给出了简易版本,还容易编曲哦~(求赞赞)
enum枚举音符参数
enum Scale //音阶参数
{Rest = 0, C8 = 108, B7 = 107, A7s = 106, A7 = 105, G7s = 104, G7 = 103, F7s = 102, F7 = 101, E7 = 100,D7s = 99, D7 = 98, C7s = 97, C7 = 96, B6 = 95, A6s = 94, A6 = 93, G6s = 92, G6 = 91, F6s = 90, F6 = 89,E6 = 88, D6s = 87, D6 = 86, C6s = 85, C6 = 84, B5 = 83, A5s = 82, A5 = 81, G5s = 80, G5 = 79, F5s = 78,F5 = 77, E5 = 76, D5s = 75, D5 = 74, C5s = 73, C5 = 72, B4 = 71, A4s = 70, A4 = 69, G4s = 68, G4 = 67,F4s = 66, F4 = 65, E4 = 64, D4s = 63, D4 = 62, C4s = 61, C4 = 60, B3 = 59, A3s = 58, A3 = 57, G3s = 56,G3 = 55, F3s = 54, F3 = 53, E3 = 52, D3s = 51, D3 = 50, C3s = 49, C3 = 48, B2 = 47, A2s = 46, A2 = 45,G2s = 44, G2 = 43, F2s = 42, F2 = 41, E2 = 40, D2s = 39, D2 = 38, C2s = 37, C2 = 36, B1 = 35, A1s = 34,A1 = 33, G1s = 32, G1 = 31, F1s = 30, F1 = 29, E1 = 28, D1s = 27, D1 = 26, C1s = 25, C1 = 24, B0 = 23,A0s = 22, A0 = 21
};
enum Voice //声调
{L1 = C3, L2 = D3, L3 = E3, L4 = F3, L5 = G3, L6 = A3, L7 = B3,M1 = C4, M2 = D4, M3 = E4, M4 = F4, M5 = G4, M6 = A4, M7 = B4,H1 = C5, H2 = D5, H3 = E5, H4 = F5, H5 = G5, H6 = A5, H7 = B5,LOW_SPEED = 500, MIDDLE_SPEED = 400, HIGH_SPEED = 300,_ = 0XFF
};
凭借上面的代码,你就可以调用来谱曲啦,还可以自己修改speed,以及_的数量得到节奏不同的音乐。
我们一起学猫叫
void Cats()
{HMIDIOUT handle;cout << "我们一起学猫叫~~~" << endl;cout << "\n\n我们一起学猫叫\n\n一起喵喵喵喵喵\n\n在你面前撒个娇\n\n哎呦喵喵喵喵喵\n\n我的心脏砰砰跳\n\n迷恋上你的坏笑\n\n你不说爱我我就喵喵喵\n\n";midiOutOpen(&handle, 0, 0, 0, 0);int volume = 0x7f;int voice = 0x0;int sleep = 150;int helloworld[] = { // 声调M1,_,M2,_,M3,_,L5,_,M1,_,M3,_,M3,_,_,_,M2,_,M1,_,M2,_,M5,_,M5,_,M5,_,M5,_,_,_,M1,_,L7,_,M1,_,M1,_,M1,_,M1,_,M1,_,_,_,L7,_,M1,_,L7,_,M1,_,L7,_,L6,_,L5,_,_,_,L5,_,L5,_,L6,_,L1,_,L4,_,L6,_,L6,_,_,_,L5,_,L3,_,L5,_,L3,_,L5,_,M2,_,M1,_,_,_,L5,_,M3,_,M3,_,M3,_,M4,_,M5,_,M1,_,M1,_,M2,M3,M2,_,_,_,_,_,_,_,};for (auto i : helloworld){if (i == LOW_SPEED || i == HIGH_SPEED || i == MIDDLE_SPEED){sleep = i;continue;}if (i == _){Sleep(150);continue;}voice = (volume << 16) + (i << 8) + 0x90;midiOutShortMsg(handle, voice);Sleep(sleep);}midiOutClose(handle);}
具体的音乐程序已经封装好放这里啦
MIDI音乐程序设计
可以看看效果