I'm working on a Swift app, where I utilise MIDI signal from a specific and work with it. An example is: I want to move the value of an NSSlider around based on the input signal. So, what I did was create a MIDI input stream (which works) and what I need to do now, is take the value and set it to self.wave_mode_slider?.integerValue
.
My method from where I make the input connection is as follows:
func makeInputSource() {
var midiClient : MIDIClientRef = 0
var inPort : MIDIPortRef = 0
var observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
(pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) -> Void in
let packetList : MIDIPacketList = pktList.pointee
var packet : MIDIPacket = packetList.packet
let mySelf = Unmanaged<Wob>.fromOpaque(readProcRefCon!).takeUnretainedValue()
for _ in 1...packetList.numPackets {
let bytes = Mirror(reflecting: packet.data).children
var params : [UInt64] = []
var i = packet.length
for (_, attr) in bytes.enumerated() {
let string = String(format: "%02X ", attr.value as! UInt8)
params.append(UInt64(strtoul(string, nil, 16)))
i -= 1
if (i <= 0) {
break
}
}
mySelf.wave_mode_slider?.integerValue = ("\(params[2])" as NSString).integerValue
packet = MIDIPacketNext(&packet).pointee
}
}, nil, &inPort)
MIDIPortConnectSource(inPort, self.source, &observer)
}
As you can see, I have the observer variable made like this:
var observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
Which I then pass to the closure and I try to use like this:
let mySelf = Unmanaged<Wob>.fromOpaque(readProcRefCon!).takeUnretainedValue()
mySelf.wave_mode_slider?.integerValue = ("\(params[2])" as NSString).integerValue
However, this leads to EXC_BAD_INSTRUCTION
on the line where mySelf
is declared.
I also tried to use the retained
variants, but this led to the same EXC_BAD_INSTRUCTION
.
Now, I'm not a seasoned swift dev, and especially not when it comes to this stuff, so it might be that I missed something obvious, so does anyone know why this happens?
Thanks
Edit---- As suggested, I changed the code slightly,
MIDIPortConnectSource(inPort, self.source, &observer)
became
MIDIPortConnectSource(inPort, self.source, observer)
Which also made me change the observer
variable to an UnsafeMutableRawPointer