CoreMIDI refCon传递给MIDIReadProc

时间:2013-03-23 12:27:11

标签: coremidi

我需要了解在使用多个MIDI设备时如何返回MIDI数据包的来源。

我使用以下循环连接了所有源:

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

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

}

我知道MIDISourceConnectPort()中的最后一个参数是一个上下文,用于标识发送到MIDIReadProc回调的源。所以我试图将源的索引发送到MIDIReadProc。

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

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

}

我需要知道的原因是我正在尝试向设备发送LED反馈,我需要知道哪个设备发送了数据包,以便我可以将反馈发送到正确的设备。

1 个答案:

答案 0 :(得分:2)

下面的代码假设您已经为输入和输出设置了MIDIClient和MIDIPortRef:

-(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");


    }

}

并访问MIDIReadProc中的源refCon上下文:

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

}