對不起,我不會說英語(我正在使用Google翻譯)。CoreMidi:將收到的midi消息記錄到NSTextField
我是Xcode的新手。我試圖編寫一個應用程序,可以收聽收到的MIDI信息並在NSTextField
中顯示它們(就像MIDI顯示器一樣)。
我使用CoreMidi
,我可以將應用程序連接到所需的輸入並接收Midi消息(我可以使用NSLog
打印它們)。如何在NSTextField
中輸出這些信息(我可以在NSLog
中讀到的信息相同)?
我設置了一個屬性,@synthesize
d it並連接了Interface Builder中的NSTextField,但是從midi回調函數我無法訪問它(它說「Undeclared」)。
下面的代碼MyDocument.h
@property (retain,nonatomic) IBOutlet NSTextField *test_messages;
下面的代碼MyDocument.m
@synthesize test_messages;
void midiInputCallback (const MIDIPacketList *list, void *procRef, void *srcRef) {
id POOL = [[NSAutoreleasePool alloc] init];
UInt16 nBytes;
NSString *ric;
const MIDIPacket *packet = &list->packet[0];
for (unsigned int i = 0; i < list->numPackets; i++) {
nBytes = packet->length;
UInt16 iByte, size;
iByte = 0;
while (iByte < nBytes) {
size = 0;
unsigned char status = packet->data[iByte];
if (status < 0xC0) {
size = 3;
} else if (status < 0xE0) {
size = 2;
} else if (status < 0xF0) {
size = 3;
} else if (status < 0xF3) {
size = 3;
} else if (status == 0xF3) {
size = 2;
} else {
size = 1;
}
switch (status & 0xF0) {
case 0x80:
ric = @"Note Off";
break;
case 0x90:
ric = @"Note On";
break;
case 0xA0:
ric = @"Aftertouch";
break;
case 0xB0:
ric = @"Control change";
break;
case 0xC0:
ric = @"Program Change";
break;
case 0xD0:
ric = @"Channel Pressure";
break;
case 0xE0:
ric = @"Pitch Wheel";
break;
default:
ric = @"Unk";
break;
}
//TEST HERE
[test_messages setStringValue:@"TEST TEST"]; //THIS GET "test_messages undeclared (first use in this function)"
iByte += size;
}
packet = MIDIPacketNext(packet);
}
[POOL release];
}
int main(int argc, char *argv[]) {
MIDIClientRef midiClient;
MIDIEndpointRef src;
OSStatus result;
result = MIDIClientCreate(CFSTR("MIDI client"), NULL, NULL, &midiClient);
if (result != noErr) {
NSLog(@"Errore : %s - %s",
GetMacOSStatusErrorString(result),
GetMacOSStatusCommentString(result));
return 0;
}
result = MIDIDestinationCreate(midiClient, CFSTR("Porta virtuale"), midiInputCallback, NULL, &src);
if (result != noErr) {
NSLog(@"Errore : %s - %s",
GetMacOSStatusErrorString(result),
GetMacOSStatusCommentString(result));
return 0;
}
MIDIPortRef inputPort;
result = MIDIInputPortCreate(midiClient, CFSTR("Input"), midiInputCallback, NULL, &inputPort);
ItemCount numOfDevices = MIDIGetNumberOfDevices();
for (int i = 0; i < numOfDevices; i++) {
MIDIDeviceRef midiDevice = MIDIGetDevice(i);
NSDictionary *midiProperties;
MIDIObjectGetProperties(midiDevice, (CFPropertyListRef *)&midiProperties, YES);
MIDIEndpointRef src = MIDIGetSource(i);
MIDIPortConnectSource(inputPort, src, NULL);
}
return NSApplicationMain(argc, (const char **) argv);
}
預先感謝您的任何信息,可以幫助我。
你能後您使用註冊回調和回調函數本身的代碼? – 2012-01-10 02:11:40
非常感謝您閱讀我:) 添加代碼(希望以正確的方式) – 8003130124464 2012-01-10 08:38:31