On Core MIDI Callbacks
It’s time for the next step in working with MIDI devices and Core MIDI. We already got to know how to query our system for any MIDI related information. But how can we use the information sent by a MIDI device to our system?
This is where Core MIDI callbacks come into play. A quick look into the documentation, we find three of them:
- MIDINotifyProc: “Called when the MIDI state changes.”
- MIDIReadProc: “Called when the system has one or more incoming MIDI messages to deliver to your app.”
- MIDICompletionProc: “Called when a MIDI system-exclusive event has been completely sent or has been aborted.”
For this post I have some good and some not so good news.
Good news is, that am showing an example on how to use the first two callbacks. The not so good news is, that I will not use Swift as promised, but good old Objective-C. As it seems like, Swift does not support Core MIDI callbacks, yet. I have opened up a question on StackExchange and hope this will be answered soon, but it depends on Apple of course.
So, here is the Objective-C code that uses two callbacks. One gets fired when a MIDI event comes in (eventCallback
) and the other when something in our MIDI setup changes (notifyCallback
).
{% gist 167ebaece067df022311 callbacks.m %}
Now run the code and then plug in your MIDI device. At this point the callback notifyCallback
should be triggered, actually several times and not just once. Then hit some controls on your controller and you will see eventCallback
in action.
So, let’s hope Apple comes up with a solution for the callbacks in Swift. Am sure they will, it is just a matter of time though.
Done for today!