Skip to content

Commit

Permalink
Update MIKMIDICommand.m
Browse files Browse the repository at this point in the history
different messages within one packet parsed correctly
  • Loading branch information
mlostekk authored Feb 27, 2017
1 parent 95c723b commit e4c6e34
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions Source/MIKMIDICommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit e4c6e34

Please sign in to comment.