【问题标题】:Debugging MIDI in iOS app在 iOS 应用程序中调试 MIDI
【发布时间】:2016-02-25 16:30:10
【问题描述】:

我正在尝试在我的 iOS 应用程序中实现 CoreMIDI,但现在尝试调试它时遇到了麻烦。目前,我有一个 UILabel,只要调用我的 MIDI 回调,它就会更新。但是,我的 UILabel 永远不会更新!我不确定为什么会发生这种情况,我猜这要么是我在 MIDI 初始化中定义某些东西的方式,要么是我如何调用 UILabel。我仍在尝试解决这个问题,但有没有更好的方法在 iOS 应用程序上调试 MIDI(因为 iOS 设备只有一个端口,并且只能在给定时间使用该端口连接到计算机或 MIDI 控制器) .

我如何创建 MIDI 客户端:

Check(MIDIClientCreate(CFSTR("Yun Client"), NULL, NULL , &client));
Check(MIDIOutputPortCreate(client, CFSTR("Yun Output Port"), &outputPort));
Check(MIDIInputPortCreate(client, CFSTR("Yun Input Port"), MIDIInputCallback, 
                         (__bridge void *)self, &inputPort));
unsigned long sourceCount = MIDIGetNumberOfSources();

CFStringRef endpointName;
for (int i = 0; i < sourceCount; ++i) {
    MIDIEndpointRef endPoint = MIDIGetSource(i);
    endpointName = NULL;
    Check(MIDIObjectGetStringProperty(endPoint, kMIDIPropertyName, &endpointName));
    Check(MIDIPortConnectSource(inputPort, endPoint, NULL));
    [param addToMIDIInputsArray:[NSString stringWithFormat:@"%@", endpointName]];
}

我的 MIDI 回调:

// MIDI receiver callback
static void MIDIInputCallback(const MIDIPacketList *pktlist, 
                              void *refCon, void *connRefCon) {
    SynthViewController *vc = (__bridge SynthViewController*)refCon;

    MIDIPacket *packet = (MIDIPacket *)pktlist->packet;

    Byte midiCommand = packet->data[0] >> 4;
    NSInteger command = midiCommand;

    Byte noteByte = packet->data[1] & 0x7F;
    NSInteger note = noteByte;

    Byte velocityByte = packet->data[2] & 0x7F;
    float velocity = [[NSNumber numberWithInt:velocityByte] floatValue];

    // Note On event
    if (command == 9 && velocity > 0) {
        [vc midiKeyDown:(note+4) withVelocity:velocity];
    } 
    // Note off event
    else if ((command == 9 || command == 8) && velocity == 0) {
        [vc midiKeyUp:(note+4)];
    }

    [vc.logLabel addLogLine:[NSString stringWithFormat:@"%lu - %lu - %lu", 
                            (long)command, (long)note, (long)velocityByte]];
}

addLogLine方法:

- (void)addLogLine:(NSString *)line {
    NSString *str = [NSString stringWithFormat:@"%d - %@", _cnt++, line];
    _logLabel.text = str;
}

任何帮助都很棒!谢谢

【问题讨论】:

    标签: ios objective-c midi coremidi


    【解决方案1】:

    MIDIInputPortCreate 的标头文档说:

    readProc 将在一个单独的高优先级线程上调用 CoreMIDI。

    您必须仅在主线程上更新 UIKit。

    在解析传入的 MIDI 数据后,使用 dispatch_async 将控制权转移到主线程。

    static void MIDIInputCallback(const MIDIPacketList *pktlist, 
                              void *refCon, void *connRefCon) {
        SynthViewController *vc = (__bridge SynthViewController*)refCon;
        // ...
    
        dispatch_async(dispatch_get_main_queue(), ^{
            // Note On event
            if (command == 9 && velocity > 0) {
               [vc midiKeyDown:(note+4) withVelocity:velocity];
            } 
            // Note off event
            else if ((command == 9 || command == 8) && velocity == 0) {
               [vc midiKeyUp:(note+4)];
            }
    
            [vc.logLabel addLogLine:[NSString stringWithFormat:@"%lu - %lu - %lu", (long)command, (long)note, (long)velocityByte]];
        });
    }
    

    【讨论】:

    • 啊!谢谢,我应该更仔细地阅读文档。非常感谢,这样做了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2017-01-22
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多