MIDI connect source

Hello everyone,


first time here…


I'm making an app that uses MIDI in & out on network. When I test it with my Mac, the app sends MIDI without any problem, but I can't manage to make my app respond to incoming MIDI. Obviously the problem is with MIDIPortConnectSource.


Here is the method inside a MIDIClient struct where the problem is:


func initializeMIDIClient() throws -> (inPort: MIDIPortRef, outPort: MIDIPortRef) {
     var clientRef = MIDIClientRef()
     var ports = (inPort: MIDIPortRef(), outPort: MIDIPortRef())
     
     func midiInitializationCallback(message: UnsafePointer<MIDINotification>) -> Void {
      
      }

     func incomingMIDIHandler(_ packetList: UnsafePointer<MIDIPacketList>, _: UnsafeMutableRawPointer?) -> Void {
       delegate.midiClientDidReceiveMIDIIn()
      

     func connectSourcesToInputPort() {
       let sourceCount = MIDIGetNumberOfSources()
       print("source count \(sourceCount)")
      
       for _ in 0 ..< sourceCount {
            let status = MIDIPortConnectSource(ports.inPort, source, nil) // Here is the problem!
           
            if status == OSStatus(noErr) {
                 print("MIDIPortConnectSource OK")
            } else {
                 print("MIDIPortConnectSource not OK")
            }
       }
  }

       var status = OSStatus(noErr)
       status = MIDIClientCreateWithBlock("com.PhoProd.MIDIClient" as CFString, &clientRef, midiInitializationCallback)
       guard status == noErr else { throw MIDIError.midiClientCreationFailed }
      
       status = MIDIInputPortCreateWithBlock(clientRef, "In" as CFString, &ports.inPort, incomingMIDIHandler)
       guard status == noErr else { throw MIDIError.inputPortCreationFailed }
      
       connectSourcesToInputPort()
      
       status = MIDIOutputPortCreate(clientRef, "Out" as CFString, &ports.outPort)
       guard status == noErr else { throw MIDIError.outputPortCreationFailed }

       return ports
  }


Thanks in advance for your help!


Best regards,

Philippe

Replies

Did you figure this out? I entered it and found that source was not defined. I changed your connectSourcesToInputPort function a bit


func connectSourcesToInputPort()
    {
        let sourceCount = MIDIGetNumberOfSources()
        print("source count \(sourceCount)")
     
        for sourceIndex in 0 ..< sourceCount {
            let source = MIDIGetSource(sourceIndex)
         
            let status = MIDIPortConnectSource(ports.inPort, source, nil) /
         
            if status == OSStatus(noErr) {
                print("MIDIPortConnectSource OK")
            } else {
                print("MIDIPortConnectSource not OK")
            }
        }


I ran it and got back


source count 2
MIDIPortConnectSource OK
MIDIPortConnectSource OK


on my system