I use the following code to receive MIDI events on a Swift Playground:
import Cocoa
import CoreMIDI
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
func notifyCallback(message:UnsafePointer<MIDINotification>,refCon:UnsafeMutablePointer<Void>)
{
println("MIDI Notify")
}
func eventCallback(pktlist:UnsafePointer<MIDIPacketList>, refCon:UnsafeMutablePointer<Void>, connRefCon:UnsafeMutablePointer<Void>)
{
println("MIDI Read")
}
var client = MIDIClientRef()
MIDIClientCreate("Core MIDI Callback Demo" as NSString, MIDINotifyProc(COpaquePointer([notifyCallback])), nil, &client)
var inPort = MIDIPortRef()
MIDIInputPortCreate(client, "Input port",MIDIReadProc(COpaquePointer([eventCallback])), nil, &inPort)
let sourceCount = MIDIGetNumberOfSources()
for var count:UInt = 0; count < sourceCount; ++count
{
let src:MIDIEndpointRef = MIDIGetSource(count)
MIDIPortConnectSource(inPort, src, nil)
}
I got this by translating the working Objective-C code into what I think will be the correct version of Swift.
It compiles and works fine until one of the callbacks fires, for example. when I disconnect a MIDI device or press one of its keys. I always get BAD_EXEC.
Any ideas on how to make this work, or Swift is simply not ready, like some blog posts are in a web state. Anything official from Apple that I overlooked that itβs clear that Swift is not ready for CoreMIDI callbacks yet?
Update 2015-03-10: The corresponding Objective-C code can be found at http://www.digital-aud.io/blog/2015/03/10/on-coremidi-callbacks/