Skip to content

Instantly share code, notes, and snippets.

@raybellis
Last active May 6, 2023 16:40
Show Gist options
  • Save raybellis/8136a76366dc0bffdff9d045fe8582dd to your computer and use it in GitHub Desktop.
Save raybellis/8136a76366dc0bffdff9d045fe8582dd to your computer and use it in GitHub Desktop.
import Foundation
import CoreMIDI
let source = Int(CommandLine.arguments[1])!
let dest = Int(CommandLine.arguments[2])!
var owner = "uk.me.rb.thru" as CFString?
// dispose of any existing connections with the same owner
var obj = Data()
var ref = Unmanaged<CFData>.passUnretained(obj as CFData)
print(MIDIThruConnectionFind(owner!, &ref))
var data = ref.takeUnretainedValue() as Data
var refs = data.withUnsafeBytes {
Array(UnsafeBufferPointer<MIDIThruConnectionRef>(start: $0, count: data.count / MemoryLayout<MIDIThruConnectionRef>.stride))
}
refs.forEach {
MIDIThruConnectionDispose($0)
}
// set up the new connection
var params = MIDIThruConnectionParams()
var connRef = MIDIThruConnectionRef()
MIDIThruConnectionParamsInitialize(&params)
params.numSources = 2
params.numDestinations = 2
params.sources.0.endpointRef = MIDIGetSource(source)
params.sources.1.endpointRef = MIDIGetSource(dest)
params.destinations.0.endpointRef = MIDIGetDestination(dest)
params.destinations.1.endpointRef = MIDIGetDestination(source)
var paramsRef = NSData(bytes: &params, length: MIDIThruConnectionParamsSize(&params))
MIDIThruConnectionCreate(owner, paramsRef, &connRef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment