首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS CoreMIDI跳过MidiPackets

iOS CoreMIDI跳过MidiPackets
EN

Stack Overflow用户
提问于 2016-09-14 16:55:39
回答 1查看 134关注 0票数 0

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

因此,百万美元的问题是为什么iOS要跳过某些MIDI消息?有时不会跳过MIDI消息,但有时会跳过MIDI消息。我不知道如何接近调试,因为我已经耗尽了我的大脑在这一点上。

我的接收器代码:

代码语言:javascript
复制
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);
        }
    }
});

监视器代码格式是:(时间)- (MIDI命令类型)- (CC、Val或流速)

Midi监视器调试:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 16:11:14

库尔特·雷维斯那里得到了一些帮助,我似乎因为使用dispatch_async而发送数据包太晚了。

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

代码语言:javascript
复制
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];
        }
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39495896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档