Last active
May 6, 2023 16:40
-
-
Save raybellis/8136a76366dc0bffdff9d045fe8582dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶ms) | |
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: ¶ms, length: MIDIThruConnectionParamsSize(¶ms)) | |
MIDIThruConnectionCreate(owner, paramsRef, &connRef) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment