I have a midi read proc callback setup in a Swift 3 project. I would like to keep the project in Swift entirely without having to resort to Objective C just to make this work. There are a lot of articles on Core Midi and Swift but as the framework is changing often, the syntax in those articles don't apply any longer.
//Midi Message Callback
func MIDIReadCallback (pktList :UnsafePointer<MIDIPacketList>, refCon :UnsafeMutableRawPointer?, srcConRef :UnsafeMutableRawPointer?) -> Void{
let packet = pktList.pointee.packet
for _ in 0..<Int(pktList.pointee.numPackets) {
let mirrorData = Mirror(reflecting: packet.data)
var counter: UInt16 = 0
for(_, value)in mirrorData.children{
let packetCount = packet.length
let n = value as! UInt8
let st = String(format: "%2X", n)
messageData.append(st)
counter += 1
if(value as! UInt8 == 247){
processMidiMessage()
break}
if(packetCount == counter){break}
}
}
}
I put this callback in when creating the input port like this:
CheckError(error: MIDIInputPortCreate(client, "Input port" as CFString, MIDIReadCallback, &player, &inPort),
This gives me the following exception:
A C function pointer can only be formed from a reference to a 'func' or a literal closure
It is unclear to me what this exception means. The signature of the function matches the expected callback signature and it just looks like a Swift function.
What do I need to change to make the compiler accept my Swift function as a proper c pointer callback?