C++midi编程
在c语言的win32api中,windows.h里有专门播放midi的API。这些api的调用需要链接winmm.lib。
#include <Windows.h>
#pragma comment(lib,"winmm.lib")
我们来看一下MIDI相关的API
//打开mid设备
MMRESULT midiOutOpen(
_Out_ LPHMIDIOUT phmo,
_In_ UINT uDeviceID,
_In_opt_ DWORD_PTR dwCallback,
_In_opt_ DWORD_PTR dwInstance,
_In_ DWORD fdwOpen
);
//输出midi信号
MMRESULT midiOutMessage(
_In_opt_ HMIDIOUT hmo,
_In_ UINT uMsg,
_In_opt_ DWORD_PTR dw1,
_In_opt_ DWORD_PTR dw2
);
//输出midi短消息
MMRESULT midiOutShortMsg(
_In_ HMIDIOUT hmo,
_In_ DWORD dwMsg
);
//输出midi长信号
MMRESULT midiOutLongMsg(
_In_ HMIDIOUT hmo,
_In_reads_bytes_(cbmh) LPMIDIHDR pmh,
_In_ UINT cbmh
);
//关闭midi设备
MMRESULT midiOutClose(
_In_ HMIDIOUT hmo
);
要播放midi,首先要打开midi设备,所以就要用到我们的第一个函数:
midiOutOpen(LPHMIDIOUT phmo,UINT uDeviceID,DWORD_PTR dwCallback,DWORD_PTR dwInstance,DWORD fdwOpen);
首先来分析一下这个函数的参数,第一个:LPHMIDIOUT phmo.根据我的调查,发现LPHMIDIOUT这个类型是HMIDIOUT的指针类型,而MIDIOUT是则是MIDI设备函数的句柄类型。因此midiOutOpen()的第一个参数也可以用MIDIOUT类型的变量的地址。
然后是第二个参数UINT uDeviceID。UINT顾名思义就是unsigned int,这个参数是设置MIDI设备的编号,因为可能会打开不止一个midi设备,所以要设置编号区分。
第3,4,5个参数为uDeviceID,DWORD_PTR dwCallback,DWORD_PTR dwInstance,DWORD fdwOpen。其中dwCallback是回调函数地址或窗口句柄; 若不使用回调机制, 设为 NULL,即0,dwInstance是给回调函数的实例数据; 不用于窗口,一般也设置为0。fdwOpen是打开选项,一般也设置为0。
这个函数的返回值为MMRESULT,即一个unsigned int数值
若成功打开MIDI设备则返回0,否则会返回错误码,其他MIDI函数同理。
于是我们可以这样打开MIDI设备
MIDIOUT handle;
midiOutOpen(handle,0,0,0,0)
之后就是输出MIDI消息了
本次只讲解midiOutShortMsg()函数,其他函数有兴趣的可以自己去研究。
MMRESULT midiOutShortMsg(HMIDIOUT hmo,DWORD dwMsg);
第一个参数就是midi的句柄了,第二个参数则是一个unsigned long数值。
但是,经过我搜集到的资料,根据关于Windows下的MIDI基础编程这篇文章的描述,这个参数应该是要放一个8位的16进制数。
midiOutShortMsg(handle, 0x00403C90);
根据我的研究,这个参数只有前6位有效,应该是6位16进制数作为参数即(当然,前面加n个0也无影响),并且,我发现这个参数可以分成三部分。以上面的参数为例,0x403C90,可以分为0x40,0x3c,0x90这三个字节。而0x40代表音量,范围是0x0-0x7f。0x3c代表音阶,范围是0x0-7f,0x90代表乐器,根据我的测试范围应该是0x90-0x9f ,其中0x99为鼓类,其余皆为钢琴。设这三个字节为a,b,c,将这三个字节通过位移组合起来可得到midiOutShortMsg()的合法参数
int a=0x40,b=0x3c,c=0x90;
msg=(a<<16)+(b<<8)+c;
然后即可调用midiOutShortMsg()函数输出声音信息,midiOutShortMsg()要和Sleep()配合使用才行,否则声音会没有长度。
midiOutShortMsg(handle, 0x00403C90);
Sleep(2000);
最后,别忘了关闭MIDI设备哦
midiOutClose(handle)
然后,奉上源码
#include <iostream>
#include <Windows.h>
#include<map>
#pragma comment(lib,"winmm.lib")
using namespace std;
/*
midi参数 音量<<16+声调<<8+乐器类型
音量范围 0x0-0x7f
乐器范围 0x90-0x9f 其中0x99为鼓类,其余皆为钢琴
音阶范围 0x0-7f 以下为中音的音调16进制参数对照表
(注意:以下数据仅为本人通过自己乐感来测试得出的,不一定正确,仅供参考)
Do:0x3c Re:0x3e Mi:0x40 Fa:0x41 So:0x43 La:0x45 Xi:0x47 Do+1:0x48
*/
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
};
void Summer() {
HMIDIOUT handle;
midiOutOpen(&handle, 0, 0, 0, CALLBACK_NULL);
int volume = 0x7f;
int voice = 0x0;
int sleep = 300;
int summer[] = {
H1,_,M5,M1,H1,H2,M3,_,H5,_,H1,H2,_,H3,_,H1,H2,_,H3,_,
H1,_,M5,M1,H1,H2,M3,_,H5,_,H2,H2,_,H3,_,H2,H3,_,H3,_,
H3,H2,H1,_,H1,M5,M6,M3,M5,_,H3,H2,H1,_,H1,M5,M6,M3,M5,_,
H3,H2,H2,_,H2,H1,M7,_,M6,M7,M5,M6,_,_,
M6,M5,M6,_,
M5,H1,H2,H5,M5,H1,H2,M5,H5,_,
M5,H1,H2,M5,H5,M5,H1,M5,H2,M5,H5,_,
M5,H1,H2,H5,M5,H1,H2,M5,H5,_,
M5,H1,H2,H5,M5,H1,M5,H2,M5,H5,_,
H1,_,M5,M1,H1,H2,M5,H3,_,H5,M5,H2,H2,_,H3,_,H1,H2,_,H3,_,
H1,_,M5,M1,H1,H2,M5,H3,_,H5,_,H2,H2,_,H3,_,H2,M3,_,H3,_,
H3,H2,M3,H1,_,H1,M5,M6,M3,M5,_,H3,H2,M3,H1,_,H1,M5,M6,M3,M5,_,
H3,H2,H2,_,H2,H1,M7,_,M6,M7,M5,M6,_,_,_,
};
for (auto i : summer) {
if (i == LOW_SPEED || i == HIGH_SPEED || i == MIDDLE_SPEED) {
sleep = i;
continue;
}
if (i == _) {
Sleep(300);
continue;
}
voice = (volume << 16) + (i << 8) + 0x94;
printf("%p\n", voice);
midiOutShortMsg(handle, voice);
Sleep(sleep);
}
midiOutClose(handle);
}
void Piano() {
HMIDIOUT handle;
midiOutOpen(&handle, 0, 0, 0, CALLBACK_NULL);
std::map<char, int>v = {
{'Z',C3},{'X',D3},{'C',E3},{'V',F3},{'B',G3},{'N',A3},{'M',B3},
{'A',C4},{'S',D4},{'D',E4},{'F',F4},{'G',G4},{'H',A4},{'J',B4},
{'Q',C5},{'W',D5},{'E',E5},{'R',F5},{'T',G5},{'Y',A5},{'U',B5},
};
printf("钢琴已开启,敲击键盘Q-U,A-J,Z-M\n");
while (1) {
for (char i = 'A'; i <= 'Z'; i++) {
if (GetKeyState(i) < 0) {
if (i == 'L') {
midiOutClose(handle);
return;
}
midiOutShortMsg(handle, (0x007f << 16) + (v[i] << 8) + 0x90);
system("cls");
printf("请按L键退出\n");
printf("已按下%c键\n", i);
while (GetKeyState(i) < 0)Sleep(100);
}
}
}
}
int main()
{
Summer();
Piano();
return 0;
}
附上作品视频链接:你愿意听我用c++演奏一曲吗–c/c++Midi编程
笔者qq:1017006174
c语言学习群:255320966