Using CoreMIDI and showing available network sessions in Swift

46 views Asked by At

I cannot find a way to show all the available network sessions with CoreMidi. If anyone has a way or a tutorial, would be appreciated. From there on, I will manage, but for now i just cannot find a way to see all the network sessions on macOS. On iOS, i was able to get it.

Thanks

Edit:

here is the code that worked for me to retrieve the hosts. However, i am stil looking that it is continuously updating. My ultimate goal is to retrieve a midi clock message

import Foundation
import SwiftUI
import CoreMIDI

struct MidiHostList: View {
    
    // Array to store the available network hosts for MIDI
    @State private var midiHosts = [String]()
  @State private var connectedToHostNetwork = false
  
  
  
    var body: some View {
      
      Text("Total available MIDI networks: \(midiHosts.count)")
      
        List(midiHosts, id: \.self) { host in
          
          HStack {
            Toggle(isOn: $connectedToHostNetwork) {
              Text(host)
            }
            if connectedToHostNetwork {
              Text("(Connected)")
              
            }
          }
        
        }
      
        .onAppear {
            
          getNumOfSources()
        }
    }
  
  func getNumOfSources() {
    // Get the number of MIDI sources available
    let numOfSources = MIDIGetNumberOfSources()
    
    // Loop through all MIDI sources and get their names
    for i in 0 ..< numOfSources {
        let midiSource = MIDIGetSource(i)
        var midiEndpointName: Unmanaged<CFString>?
        
        // Get the name of the MIDI endpoint
        MIDIObjectGetStringProperty(midiSource, kMIDIPropertyName, &midiEndpointName)
        
        if let endpointName = midiEndpointName?.takeRetainedValue() as String? {
            // Add the name to the array
            midiHosts.append(endpointName)
        }
    }
//    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
//      getNumOfSources()
//    }
    
  }
}

0

There are 0 answers