diff --git a/Source/MIKMIDICommand.m b/Source/MIKMIDICommand.m index b1e32b59..4ebb2350 100644 --- a/Source/MIKMIDICommand.m +++ b/Source/MIKMIDICommand.m @@ -51,28 +51,18 @@ + (instancetype)commandWithMIDIPacket:(MIDIPacket *)packet; + (NSArray *)commandsWithMIDIPacket:(MIDIPacket *)inputPacket { - NSInteger firstCommandType = inputPacket->data[0]; - NSInteger standardLength = MIKMIDIStandardLengthOfMessageForCommandType(firstCommandType); - if (standardLength <= 0 || inputPacket->length == standardLength) { - // Can't parse multiple message because we don't know the length of each one, - // or there's only one message there - MIKMIDICommand *command = [MIKMIDICommand commandWithMIDIPacket:inputPacket]; - return command ? @[command] : @[]; - } - NSMutableArray *result = [NSMutableArray array]; - NSInteger packetCount = 0; + NSInteger packetIndex = 0; + NSInteger packetType = 0; + NSInteger packetLength = 0; while (1) { - - NSInteger dataOffset = packetCount * standardLength; - if (dataOffset > (inputPacket->length - standardLength)) break; + + packetType = inputPacket->data[packetIndex]; + packetLength = MIKMIDIStandardLengthOfMessageForCommandType(packetType); + NSInteger dataOffset = packetIndex; + if (dataOffset > (inputPacket->length - packetLength)) break; const Byte *packetData = inputPacket->data + dataOffset; - if (packetData[0] != firstCommandType && ((packetData[0] | 0x0F) != (firstCommandType | 0x0F))) { - // Doesn't look like multiple messages because they're not all the same type - MIKMIDICommand *command = [MIKMIDICommand commandWithMIDIPacket:inputPacket]; - return command ? @[command] : @[]; - } - + // This is gross, but it's the only way I can find to reliably create a // single-message MIDIPacket. MIDIPacketList packetList; @@ -81,13 +71,13 @@ + (NSArray *)commandsWithMIDIPacket:(MIDIPacket *)inputPacket sizeof(MIDIPacketList), midiPacket, inputPacket->timeStamp, - standardLength, + packetLength, packetData); MIKMIDICommand *command = [MIKMIDICommand commandWithMIDIPacket:midiPacket]; if (command) [result addObject:command]; - packetCount++; + packetIndex += packetLength; } - + return result; }