我需要了解如何在使用多个 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 反馈,并且我需要知道哪个设备发送了数据包,以便我可以将反馈发送到正确的设备。
最佳答案
下面的代码假设您已经为输入和输出设置了 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...
}
关于CoreMIDI refCon 传递给 MIDIReadProc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15586755/