0

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

sushibrain
  • 2,712
  • 5
  • 33
  • 62

1 Answers1

1

On Swift 4 (receiving cc):

@objc func receiveMIDI(_ sender: UIButton) {

    let observer = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
    MIDIClientCreate(s, nil, nil, &midiClient);
    MIDIInputPortCreate(midiClient, p, {
    (pktList: UnsafePointer<MIDIPacketList>,
        readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) in
            // callback block
            let packetList:MIDIPacketList = pktList.pointee
            var packet:MIDIPacket = packetList.packet

            for _ in 1...packetList.numPackets{
                let bytes = Mirror(reflecting: packet.data).children
                var dumpStr = ""


                var i = packet.length
                for (_, attr) in bytes.enumerated(){
                        dumpStr += String(format:"%02d ", attr.value as! UInt8)
                        i -= 1
                        if (i <= 0)
                        {
                            break
                        }
                }
                let midi = dumpStr.split(separator: " ", maxSplits: 2, omittingEmptySubsequences: true)
                var midiIntValues = [Int(midi[0]),Int(midi[1]),Int(midi[2].trimmingCharacters(in: .whitespaces))]
                if let src = srcConnRefCon{
                    let mySelf = Unmanaged<DetailViewController>.fromOpaque(src).takeUnretainedValue()
                    if let v = midiIntValues[2]{
                       // print("control \(midiIntValues[1]) has \(v) value")
                    }

                }
                packet = MIDIPacketNext(&packet).pointee
            }
            // end callback block
        }, nil, &inputPort);

    let sourceCount = MIDIGetNumberOfSources()
    print("source count \(sourceCount)")

    for srcIndex in 0 ..< sourceCount {
        var source = MIDIGetSource(srcIndex)
        let status = MIDIPortConnectSource(inputPort,source,observer)
        if status == noErr {
            print("Connected to inputPort")
        } else {
            print("There was an error connecting the ports!")
        }
    }
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
MDJ
  • 13
  • 2