2

我需要了解在使用多个 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 反馈,并且我需要知道哪个设备发送了数据包,以便我可以将反馈发送到正确的设备。

4

1 回答 1

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

}
于 2013-03-24T15:21:15.530 回答