Question

I need to understand how to return the source of a MIDI packet when using multiple MIDI devices.

I have all my sources connected by using the following loop:

ItemCount sourceCount = MIDIGetNumberOfSources();
for (ItemCount i = 0 ; i < sourceCount ; ++i) {

MIDIEndpointRef source = MIDIGetSource(i);
MIDISourceConnectPort( inPort, source, &i);

}

I understand that the last parameter in MIDISourceConnectPort() is a context to identify the source that is sent to the MIDIReadProc callback. So I'm trying to send the index of the source to MIDIReadProc.

void MIDIReadProc (const MIDIPacketList   *pktlist,
                 void                   *readProcRefCon,
                 void                   *srcConnRefCon)
{

\\ How do I access the source index passed in the conRef by using *srcConnRefSource?

}

The reason I need to know this is that I'm trying to send LED feedback to devices and I need to know which device sent the packets so I can send the feedback to the correct device.

Was it helpful?

Solution

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...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top