none
How to fetch and parse data from a midi keyboard RRS feed

  • Question

  • I have made a kind of music instrument using C with Winapi and Fmod, and I can control it both by using the mouse and the PC-keyboard. When pressing certain keys on the PC-keyboard, the program plays certain sounds. Now I want to replace the PC-keyboard with a midi keyboard, but don’t know how to get started. I have been looking around on the Internet, but have not found any understandable explanation. I have, however, always got the answers I need on this forum, so I therefore try to present the issue here.

    The midi keyboard is connected by USB, and the issue is the following: When I press a key on the midi keyboard, it sends certain data, or bytes, to the PC. What I need to know is how to fetch and parse the data in my program. With the PC keyboard I do the following:

    LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {     
    switch(msg) 
    {
        case WM_KEYDOWN:
           keypressed = 0x0FF & HIWORD (lParam);
           switch (keypressed)
           {
               case 44: …; break;           
               case 45: …; break;
               …
               etc.
           }
    …
    etc.
    }

    It's relatively simple and straightforward, and I wonder if there is a similar straightforward and simple procedure for the program to handle a midi keyboard.

    Thanks in advance.

    Sincerely


    • Edited by Gurunama Friday, August 2, 2019 4:06 PM
    Friday, August 2, 2019 4:03 PM

Answers

  • >I think it would be easier if I had a concrete example program.

    Something like this may help: https://gist.github.com/yoggy/1485181

    Dave

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 2:13 PM
  • So I therefore wonder which event is triggered when pressing a key on the midi keyboard. Could it be /WM_KEYDOWN?

    No. Look at MidiInProc. That processes messages from the keyboard. So next look at the reference to MidiInProc to see how that function is specified. Note that the MidiInProc (you can use a different name if you want) function will execute in a separate thread so you will need a way for that thread to communicate with the UI thread.

    Secondly, the program goes into a loop where the variable "c" receive numbers by the getch function, and which continues until one press "q" or Escape. I guess this then is the reading of the data from the midi device.

    No the only thing that loop does is keep the program running. You do not need the loop for a GUI program and nothing that is in it.

    Are you using MFC or are you using the Windows API directly?



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 4:22 PM
  • So I therefore wonder which event is triggered when pressing a key on the midi keyboard. Could it be WM_KEYDOWN?

    No.

    I've never done this either, but I think you'll have to open the midi device in the same way that the console project does, then
    midi events will be sent to the callback function you provide.

    I'd recommend keeping things simple first - see what the console example does and whether you can get the midi key note events.

    Secondly, the program goes into a loop where the variable "c" receive numbers by the getch function, and which continues until one press "q" or Escape. I guess this then is the reading of the data from the midi device.

    No, that's just a mechanism to wait for the user to quit the program, the callback function MidiInProc is where the midi data is
    received.

    Dave

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 6:50 PM

All replies

  • It depends on the device. What does the documentation say? There should be at least one sample program.


    Sam Hobbs
    SimpleSamples.Info

    Friday, August 2, 2019 4:11 PM
  • The midi keyboard is connected by USB, and the issue is the following: When I press a key on the midi keyboard, it sends certain data, or bytes, to the PC. What I need to know is how to fetch and parse the data in my program.

    Start here: https://docs.microsoft.com/en-us/windows/win32/multimedia/musical-instrument-digital-interface--midi

    It's relatively simple and straightforward, and I wonder if there is a similar straightforward and simple procedure for the program to handle a midi keyboard.

    It's not going to be as straightforward :)

    Dave

    Friday, August 2, 2019 4:35 PM
  • But isn't midi a device independent standard? I'm confused about this. In my manual there is not any descriptions how to program the computer. There follows some free programs with the keyboard, but they are all for x64, while I only have an x32. 

    Sincerely

    Saturday, August 3, 2019 11:07 AM
  • I have tried to read some on the site you suggest, but find it diffcult to put the bits and pieces together. I think it would be easier if I had a concrete example program. Anyway, it seems that this project will be much more demanding than what I thought, so I guess I will need a book that explains everything abouty midi thoroughly and step by step. 

    Sincerely

    Saturday, August 3, 2019 11:14 AM
  • >I think it would be easier if I had a concrete example program.

    Something like this may help: https://gist.github.com/yoggy/1485181

    Dave

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 2:13 PM
  • Yes, this is great. Might be just what I need. However, I have some holes in my knowledge of C and Winapi programming which might be kind of elemental, but I'll hope you don't mind me asking anyway.

    The program example is a console program, while I want to use a Windoes desktop program, which is event driven. So I therefore wonder which event is triggered when pressing a key on the midi keyboard. Could it be WM_KEYDOWN?

    Secondly, the program goes into a loop where the variable "c" receive numbers by the getch function, and which continues until one press "q" or Escape. I guess this then is the reading of the data from the midi device. But I don't understand what happens from there, as no function is called from inside the loop. Could you explain this a little?

    Sincerely

    Saturday, August 3, 2019 3:26 PM
  • So I therefore wonder which event is triggered when pressing a key on the midi keyboard. Could it be /WM_KEYDOWN?

    No. Look at MidiInProc. That processes messages from the keyboard. So next look at the reference to MidiInProc to see how that function is specified. Note that the MidiInProc (you can use a different name if you want) function will execute in a separate thread so you will need a way for that thread to communicate with the UI thread.

    Secondly, the program goes into a loop where the variable "c" receive numbers by the getch function, and which continues until one press "q" or Escape. I guess this then is the reading of the data from the midi device.

    No the only thing that loop does is keep the program running. You do not need the loop for a GUI program and nothing that is in it.

    Are you using MFC or are you using the Windows API directly?



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 4:22 PM
  • As far as I know I'm using Windows API directly, as I am programming in C and thus don't use classes.

    I haven't yet quite understood the concept of callback functions. So I believe that I first of all need to read about this in the litterature.

    Sincerely

    • Edited by Gurunama Saturday, August 3, 2019 5:57 PM
    Saturday, August 3, 2019 5:47 PM
  • So I therefore wonder which event is triggered when pressing a key on the midi keyboard. Could it be WM_KEYDOWN?

    No.

    I've never done this either, but I think you'll have to open the midi device in the same way that the console project does, then
    midi events will be sent to the callback function you provide.

    I'd recommend keeping things simple first - see what the console example does and whether you can get the midi key note events.

    Secondly, the program goes into a loop where the variable "c" receive numbers by the getch function, and which continues until one press "q" or Escape. I guess this then is the reading of the data from the midi device.

    No, that's just a mechanism to wait for the user to quit the program, the callback function MidiInProc is where the midi data is
    received.

    Dave

    • Marked as answer by Gurunama Sunday, August 4, 2019 10:27 AM
    Saturday, August 3, 2019 6:50 PM
  • Thank you very much for your help, both of you. Thanks to you I made it work. I just placed the code in the WM_COMMAND section of the WINAPI and connected it to a screen button. And I use another button to turn the midi off. I'm happy it wasn't such a big deal. I believe I'm now well on the track to make this work.

    Sincerely

    Saturday, August 3, 2019 9:23 PM
  • Perhaps I misunderstand what you are saying but probably a better place to do it is in the WM_CREATE and WM_CLOSE messages. Open the keyboard when the window is created and close it when the window is closed.

    If there is an error opening the MIDI then the error information from the create could then be painted in the WM_PAINT.



    Sam Hobbs
    SimpleSamples.Info


    Sunday, August 4, 2019 2:36 AM
  • Yes, I understand what you mean. Likely I will do it that way.

    Sincerely

    Sunday, August 4, 2019 10:14 AM