The code below assumes you have already set up the MIDIClient and the MIDIPortRef for both input and output:
-(void)connectMidiSources{
MIDIEndpointRef src;
ItemCount sourceCount = MIDIGetNumberOfSources();
for (int i = 0; i < sourceCount; ++i) {
src = MIDIGetSource(i);
CFStringRef endpointName = NULL;
MIDIUniqueID sourceUniqueID = NULL;
CheckError(MIDIObjectGetStringProperty(src, kMIDIPropertyName, &endpointName), "Unable to get property Name");
CheckError(MIDIObjectGetIntegerProperty(src, kMIDIPropertyUniqueID, &sourceUniqueID), "Unable to get property UniqueID");
NSLog(@"Source: %u; Name: %@; UniqueID: %u", i, endpointName, sourceUniqueID);
// *** The last paramenter in this function is a pointer to srcConnRefCon ***
CheckError(MIDIPortConnectSource(clientInputPort, src, (void*)sourceUniqueID), "Couldn't connect MIDI port");
}
}
And to access the source refCon context in MIDIReadProc:
void midiReadProc (const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon){
//make a reference to the class you have the MIDIReadProc implemented within
MidiManager *midiListener = (MidiManager*)readProcRefCon;
//print the UniqueID for the source MIDIEndpointRef
int sourceUniqueID = (int*)srcConnRefCon;
NSLog(@"Note On sourceIdx: %u", sourceUniqueID);
// the rest of your code here...
}