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 shows 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.
The good news is that I am showing an example of how to use the first two callbacks. The not-so-good news is that I will not use Swift as promised, but Objective-C. It seems that Swift does not yet support Core MIDI callbacks. I posted a question on StackExchange and hope this will be answered soon, but it ultimately depends on Apple.
So, here is the Objective-C code that uses two callbacks. One fires when a MIDI event comes in (eventCallback) and the other fires when something in our MIDI setup changes (notifyCallback).
{% gist 167ebaece067df022311 callbacks.m %}
Now run the code and then plug in your MIDI device. The notifyCallback should be triggered—actually several times, not just once. Then hit some controls on your controller and you will see eventCallback in action.
Let’s hope Apple provides a solution for callbacks in Swift. I’m sure they will; it ‘s just a matter of time.
Done for today!