我用C ++编写了一个简单的MIDI控制台应用程序。这就是整个事情:
#include <windows.h>
#include <iostream>
#include <math.h>
using namespace std;
void CALLBACK midiInputCallback(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
switch (wMsg) {
case MIM_MOREDATA:
case MIM_DATA:
cout << dwParam1 << " ";
PlaySound("jingle.wav", NULL, SND_ASYNC | SND_FILENAME);
break;
}
}
int main() {
unsigned int numDevs = midiInGetNumDevs();
cout << numDevs << " MIDI devices connected:" << endl;
MIDIINCAPS inputCapabilities;
for (unsigned int i = 0; i < numDevs; i++) {
midiInGetDevCaps(i, &inputCapabilities, sizeof(inputCapabilities));
cout << "[" << i << "] " << inputCapabilities.szPname << endl;
}
int portID;
cout << "Enter the port which you want to connect to: ";
cin >> portID;
cout << "Trying to connect with the device on port " << portID << "..." << endl;
LPHMIDIIN device = new HMIDIIN[numDevs];
int flag = midiInOpen(&device[portID], portID, (DWORD)&midiInputCallback, 0, CALLBACK_FUNCTION);
if (flag != MMSYSERR_NOERROR) {
cout << "Error opening MIDI port." << endl;
return 1;
} else {
cout << "You are now connected to port " << portID << "!" << endl;
midiInStart(device[portID]);
}
while (1) {}
}
您可以看到有一个回调函数用于处理来自设备的传入MIDI消息。 Here is the description of this function on MSDN。在该页面上,他们表示dwParam1
和dwParam2
的含义已指定给消息类型(wMsg
),如MIM_DATA
。
如果我查看MIM_DATA
的文档,我可以看到它是双字(DWORD
?)并且它有一个'高字'和'低字'。我现在如何获取数据,如MIDI设备上发送数据的控件名称以及它发送的值?
如果有人可以更好地修改我的代码,我将不胜感激。
谢谢:)
答案 0 :(得分:5)
要访问您需要使用dwParam1
和dwParam2
的数据,并调用宏HIWORD
和LOWORD
以获取其中的高位和低位字。分别使用HIBYTE
和LOBYTE
从这些单词中获取数据。在MIM_DATA
的情况下,不幸的是,这是字节编码的MIDI数据,因此您必须找到这些的具体含义 - 这些在此处记录 - MIDI Messages。
但您的代码存在潜在问题 - 正如我们在MSDN页面中所读到的那样:
“应用程序不应该调用任何内容 内部的多媒体功能 回调函数,这样做可以 造成僵局。其他系统 可以安全地调用函数 回调“。
你在Callback中调用PlaySound ......