English 中文(简体)
C++: Get data from MIDI message (DWORD)
原标题:
  • 时间:2009-12-30 21:58:02
  •  标签:
  • c++
  • midi

I ve written a simple MIDI console application in C++. Here s the whole thing:

#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) {}
}

You can see that there s a callback function for handling the incoming MIDI messages from the device. Here is the description of this function on MSDN. On that page they say that the meaning of dwParam1 and dwParam2 are specified to the messagetype (wMsg), like MIM_DATA.

If I look up the documentation of MIM_DATA, I can see that it is a doubleword (DWORD?) and that it has a high word and a low word . How can I now get data like the name of the control on the MIDI device that sended the data and what value it sends?

I would appreciate it if somebody can correct my code if it can be done better.

Thanks :)

最佳回答

To access the data you need to use dwParam1 and dwParam2 and call the macros HIWORD and LOWORD to get the high and low word from them. Respectively use HIBYTE and LOBYTE to get the data out of those words. In case of MIM_DATA, unfortunately that s byte encoded MIDI data, so you ll have to find the specific meanings for those -- these are documented here -- MIDI Messages.

Your code however has a potential problem -- as we read in the MSDN pages:

"Applications should not call any multimedia functions from inside the callback function, as doing so can cause a deadlock. Other system functions can safely be called from the callback".

And you re calling PlaySound in the Callback...

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签