Skip to content

Commit

Permalink
Merge branch 'jagdeep-manik-Fix-Multi-Message-MIDIPacket'
Browse files Browse the repository at this point in the history
  • Loading branch information
armadsen committed Aug 22, 2017
2 parents 717699d + c64d658 commit b9c2449
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions Source/MIKMIDICommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,14 @@ + (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 dataOffset = 0;
while (1) {

NSInteger dataOffset = packetCount * standardLength;
if (dataOffset > (inputPacket->length - standardLength)) 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] : @[];
}

NSInteger commandType = (NSInteger) packetData[0];
NSInteger standardLength = MIKMIDIStandardLengthOfMessageForCommandType(commandType);
if (dataOffset > (inputPacket->length - standardLength)) break;

// This is gross, but it's the only way I can find to reliably create a
// single-message MIDIPacket.
MIDIPacketList packetList;
Expand All @@ -85,9 +71,9 @@ + (NSArray *)commandsWithMIDIPacket:(MIDIPacket *)inputPacket
packetData);
MIKMIDICommand *command = [MIKMIDICommand commandWithMIDIPacket:midiPacket];
if (command) [result addObject:command];
packetCount++;
dataOffset += standardLength;
}

return result;
}

Expand Down

0 comments on commit b9c2449

Please sign in to comment.