The following is a minimal working example of my code. Some of the print statement output the status of a CoreMIDI function. Every one of these CoreMIDI methods works properly, except for MIDIInputConnectSource(), which outputs -50. Please scroll to the end to see the output.
// Set up client
var client = MIDIClientRef()
var status: OSStatus = MIDIClientCreateWithBlock("MIDI Client" as CFString, &client) { _ in }
print("MIDIClientCreateWithBlock >>", String(describing: status))
// Set up port
var port = MIDIPortRef()
// List sources
let numSources = MIDIGetNumberOfSources()
print("\(numSources) sources found")
for i in 0..<numSources {
let source = MIDIGetSource(i)
var sourceName: Unmanaged<CFString>?
status = MIDIObjectGetStringProperty(source, kMIDIPropertyName, &sourceName)
print("MIDIObjectGetStringProperty >>", String(describing: status))
print("Name of source \(i): \(sourceName?.takeRetainedValue() as String?)")
}
// Choose source
var source: MIDIEndpointRef = MIDIGetSource(0)
// Connect port to source
status = MIDIPortConnectSource(port, source, &source)
print("port:", port)
print("source:", source)
print("client:", client)
print("MIDIPortConnectSource >>", String(describing: status))
var count = 0
status = MIDIInputPortCreateWithProtocol(
client,
"Destination Name" as CFString,
MIDIProtocolID._1_0,
&port) { _, _ in
count += 1
print("MIDI event(s) received.")
}
print("MIDIInputPortCreateWithProtocol >>", String(describing: status))
Output of code above:
MIDIClientCreateWithBlock >> 0
1 sources found
MIDIObjectGetStringProperty >> 0
Name of source 0: Optional("iRig Keys")
port: 0
source: 102203411
client: 102203464
MIDIPortConnectSource >> -50
MIDIInputPortCreateWithProtocol >> 0