Suruthika Balaji
Suruthika Balaji

Reputation: 11

Core MIDI - How and what's the use of srcConnRefCon in MIDIInputPortCreateWithProtocol for Multiple MIDI device connection in Swift

I am working on CoreMIDI and in this, I've done the BLE MIDI device connection using the Apple default page (CABTMIDICentralViewController).

And I am trying to differentiate multiple MIDI devices while receiving MIDI data (MIDIEventList) and scrConnRefCon from MIDIReceiveBlock in MIDIInputPortCreateWithProtocol

The scrConnRefCon in MIDIInputPortCreateWithProtocol is the UnsafeMutableRawPointer. And I don't know how to use the MIDI Device Object (Input Device One, Input Device Two or etc.,) in this pointer.

I have already referred to the below links. Even though I didn't get it clearly.

StackOverflow

MIDIInputPortCreateWithProtocol

MIDIReceiveBlock

MIDIInputPortCreateWithProtocol(client, "MidiMonitor Input Port" as CFString, MIDIProtocolID._1_0, &inputPort, { midiEventList, srcConnRefCon in
    let midiEventPacket = midiEventList.pointee.packet
    let timeStamp = midiEventPacket.timeStamp
    let midiData = midiEventPacket.words.0

    if var sourceConnectionRef = srcConnRefCon {
        let decimalVal = Int64(Int(bitPattern: sourceConnectionRef))
        print("TRY 1 => HEXA => \(sourceConnectionRef) AND DECIMAL => \(decimalVal)")
        
        let endpointRef = MIDIEndpointRef(bitPattern: Int32.init(truncatingIfNeeded: decimalVal))
        let midiStatus = MIDIPortConnectSource(self.inputPort, endpointRef, &sourceConnectionRef)
        print("TRY 2 => MIDIPortConnectSource midiStatus => \(midiStatus)")
        
        let intVal = sourceConnectionRef.assumingMemoryBound(to: UInt64.self).pointee
        print("TRY 3 => AssumingMemoryBound intVal \(intVal)")

        for index in 0..<MIDIGetNumberOfDestinations() {
            let outputEndPoint: MIDIEndpointRef = MIDIGetDestination(index)
            if (outputEndPoint != 0) {
                let midiStatus = MIDIPortConnectSource(self.inputPort, outputEndPoint, &sourceConnectionRef)
                print("TRY 4 => MIDIPortConnectSource midiStatus => \(midiStatus)")
                
                let srcRef: MIDIEndpointRef = sourceConnectionRef.load(as: MIDIEndpointRef.self)
                print("TRY 5 => MIDI Received From Source: \(self.getDisplayName(srcRef))")
                
                self.delegate?.didReceiveMidiData(midiEventPacket: midiEventPacket, timeStamp: timeStamp, midiData: midiData, srcConnRefCon: srcConnRefCon)
            }
        }
    }
})

And my console response is,

TRY 1 => HEXA => 0x000000016b867084 AND DECIMAL => 6098940036
TRY 2 => MIDIPortConnectSource midiStatus => -50
TRY 3 => AssumingMemoryBound intVal 0
TRY 4 => MIDIPortConnectSource midiStatus => -10832
TRY 5 => MIDI Received From Source: 

I want to know how to differentiate the multiple MIDI device connection in MIDIInputPortCreateWithProtocol.

Thanks in Advance.

Upvotes: 1

Views: 177

Answers (0)

Related Questions