question

Keitel-0291 avatar image
0 Votes"
Keitel-0291 asked RitaHan-MSFT commented

A midi controller in a desktop program freezes the graphic controls, though they still function

I have made a music program in Visual Studio for Windows with C and Fmod that contains a graphical keyboard, a selection of sound files etc., and which also contains a midi controller. I use the midi controller by connecting a midi keyboard to the computer. All the functionality of the midi controller is placed in a callback function, which is called when I press a specific button that is supposed to turn the midi functionality on and off. I am, however, not sure if this is necessary. Maybe I rather should turn it on automatically when starting the program, and turn it off only when quitting, though it’s maybe not so important. The button contains among others the following code:

 if (!Midion) {
 Midion = 1;
     nMidiDeviceNum = midiInGetNumDevs();
     if (nMidiDeviceNum == 0) return -1;
            rv = midiInOpen(&hMidiDevice, nMidiPort, (DWORD)(void*)MidiInProc, 0, CALLBACK_FUNCTION);
                         
     if (rv != MMSYSERR_NOERROR) return -1;
                         
     midiInStart(hMidiDevice);
                     
 if (Midion) {
 Midion = 0;
     rv = midiInStop(hMidiDevice);
     rv = midiInClose(hMidiDevice);
     hMidiDevice = NULL;
 } 

The midi program works fine, and for a while I can at any time stop playing the midi keyboard and set graphic controls. But after about 5 minutes, nothing happen to the graphic controls when clicking them or dragging them, though they still change their function in a correct way. It is however not satisfying when the changes is not shown on the screen.

I have also found that the “rv” variable for midiInStop(hMidiDevice) and midiInClose(hMidiDevice) gives the value 5, which means MMSYSERR_INVALHANDLE, and I don't understand why. This also means that I can not turn off the midi functionality, as I am supposed to. The main problem is however the freezing of the graphic controls.

Thanks in advance.

Sincerely

windows-apic++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You can check if there is any other places in your application call midiInClose cause the device handle is invalid. And based on your presented code piece I can't see relationship between midi controller and graphics controls, you may share a mini, complete and reproducible sample so I can do a further investigation.

1 Vote 1 ·

Sorry for late reply. I overlooked this comment, as I everytime were looking at the bottom of the page.

When it comes to the invalid handle, I finally realised that it was because the variable hMidiDevice was not set to static. Maybe kind of stupid, but that's how it is.

When I read that you couldn't see any relationship between the midi controller and the graphics controls, I took a new look at the backup function and removed everything that had to do with graphics display. I had among others some swprintf functions used for testing. After that it seems that the problem has dissapeared. I have now played almost for half an hour with the keyboard, without the graphics getting frozen. I don't understand why this is so, but as long as it works I am very happy. So thanks a lot for your comment.

Sincerely

0 Votes 0 ·

@Keitel-0291 Glad to hear this issue fixed. To make sure you can't miss any reply you can enable e-mail notifications . Refer to my answer for more detail info.

0 Votes 0 ·
RitaHan-MSFT avatar image
1 Vote"
RitaHan-MSFT answered

Hello @Keitel-0291,

I have also found that the “rv” variable for midiInStop(hMidiDevice) and midiInClose(hMidiDevice) gives the value 5, which means MMSYSERR_INVALHANDLE, and I don't understand why.

A handle is simply a number that Windows uses for internal reference to an object. In general, a handle becomes invalid after it has been closed.

For suggestions to troubleshoot error "handle is invalid":

  • Store the handle value when you get it at the first time. Compare it with the one causes this error. In this question, hMidiDevice is released because out of scope. It is never the initial one you got it.

  • When you found the handle value is incorrect, check if there is any other place close the handle, or it has been changed unexpectedly.


    The main problem is however the freezing of the graphic controls.

At first, you can narrow down this issue by excluding unrelated part, this often helps.

Thank you!



If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Keitel-0291 commented

If possible, test it without callback, i.e. ‘midiInOpen(&hMidiDevice, nMidiPort, NULL, 0, CALLBACK_NULL)’.

Then try ‘midiInOpen(&hMidiDevice, nMidiPort, (DWORD_PTR)&MidiInProc, 0, CALLBACK_FUNCTION)’. Make sure that the definition (parameters) of MidiInProc is good.






· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

With this I can play with the midi keyboard for a few seconds, then the program crashes.

0 Votes 0 ·