【问题标题】:iOS CoreMIDI Skipping MidiPacketsiOS CoreMIDI 跳过 MidiPackets
【发布时间】:2016-09-14 16:55:39
【问题描述】:

在我的 iOS 应用程序中实现 MIDI 时遇到问题,因为接收器回调似乎正在跳过 MIDI 消息和数据包。我正在使用Midi Monitor 来检查我遗漏了哪些 MIDI 消息、跳过了等等。

那么百万美元的问题是为什么 iOS 会跳过某些 MIDI 消息? 有时它不会跳过 MIDI 消息,但有时会。我不确定如何进行调试,因为此时我已经筋疲力尽了。

我的接收者代码:

void MidiReceiver(const MIDIPacketList *packets, 
                  void *context, void *sourceContext) {
    dispatch_async(dispatch_get_main_queue(), ^{
    if (packets->numPackets > 0) {
        MIDIPacket *packet = (MIDIPacket *)packets->packet;

        // Loop through total number of packets
        for (int i = 0; i < packets->numPackets; i++) {
            // Go through each packet, iOS sometimes clumps all data into one packet
            // if the MIDI messages are triggered at the same time
            for (int j = 0; j < packet->length; j += 3) {
                NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithUnsignedInt:packet->data[j]],
                                  [NSNumber numberWithUnsignedInt:packet->data[j+1]],
                                  [NSNumber numberWithUnsignedInt:packet->data[j+2]], nil];

                // Use the data to create do meaningful in the app
                [myViewController processMidiData:array];
            }

            // Next packet
            packet = MIDIPacketNext(packet);
        }
    }
});

监听代码格式为:(TIME) - (MIDI Command Type) - (CC Val or Velocity)

Midi 监视器调试:

12:45:32.697    Control 0
12:45:32.720    Control 1
12:45:32.737    Control 1
12:45:32.740    Control 2
12:45:32.750    Control 3
12:45:32.763    Note Off    A♯1 0
12:45:32.763    Note Off    F2  0
12:45:32.763    Note Off    D3  0
12:45:32.763    Control 4
12:45:32.770    Control 5
12:45:32.780    Control 6
12:45:32.790    Control 8
12:45:32.800    Control 9
12:45:32.810    Control 11
12:45:32.820    Control 13
12:45:32.832    Control 14
12:45:32.845    Control 16
12:45:32.850    Control 18
12:45:32.873    Control 21
12:45:32.883    Control 22
12:45:32.898    Control 24
12:45:32.913    Control 26
12:45:32.933    Control 27
12:45:32.948    Control 28
12:45:33.020    Control 27
12:45:33.030    Control 26
12:45:33.040    Control 25
12:45:33.050    Control 24
12:45:33.060    Control 22

我的应用程序的调试监视器:

12:45:33.050    Control 0
12:45:33.051    Control 1
12:45:33.051    Control 1
12:45:33.051    Control 2
12:45:33.051    Control 3
12:45:33.083    Note Off    D3 0       <----- Where's A#1 and F2!!! :(
12:45:33.087    Control 4
12:45:33.087    Control 4
12:45:33.097    Control 5
12:45:33.100    Control 6
12:45:33.110    Control 8
12:45:33.120    Control 9
12:45:33.130    Control 11
12:45:33.140    Control 13
12:45:33.153    Control 14
12:45:33.165    Control 16
12:45:33.170    Control 18
12:45:33.193    Control 21
12:45:33.203    Control 22
12:45:33.218    Control 24
12:45:33.233    Control 26
12:45:33.256    Control 27
12:45:33.268    Control 28
12:45:33.341    Control 27
12:45:33.351    Control 26
12:45:33.361    Control 25
12:45:33.374    Control 24
12:45:33.381    Control 22

【问题讨论】:

    标签: ios objective-c audio midi coremidi


    【解决方案1】:

    Kurt Revis 获得了一些帮助,由于我使用了dispatch_async,我似乎发送数据包太晚了。

    我修改后的代码(我先解析了数据包):

    void MidiReceiver(const MIDIPacketList *packets, 
                  void *context, void *sourceContext) {
    
        NSMutableArray *packetData = [[NSMutableArray alloc] init];
    
        if (packets->numPackets > 0 && object != nil) {
            MIDIPacket *packet = &packets->packet[0];
            // Loop through total number of packets
            for (int i = 0; i < packets->numPackets; ++i) {
                 int idx = 0;
                 while (idx < packet->length) {
                     NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithUnsignedInt:packet->data[idx]],
                                      [NSNumber numberWithUnsignedInt:packet->data[idx+1]],
                                      [NSNumber numberWithUnsignedInt:packet->data[idx+2]], nil];
    
                    [packetData addObject:array];
                    idx += 3;
                 }
                 packet = MIDIPacketNext(packet); 
             }
        }
    
        dispatch_async(dispatch_get_main_queue(), ^{
            for (NSArray *packet in packetData) {
                [object receiveMIDIInput:packet];
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      相关资源
      最近更新 更多