MIDIUtils.cpp (5445B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/MIDIUtils.h" 8 9 #include "mozilla/UniquePtr.h" 10 #include "mozilla/dom/MIDITypes.h" 11 12 // Taken from MIDI IMPLEMENTATION CHART INSTRUCTIONS, MIDI Spec v1.0, Pg. 97 13 static const uint8_t kCommandByte = 0x80; 14 static const uint8_t kSysexMessageStart = 0xF0; 15 static const uint8_t kSystemMessage = 0xF0; 16 static const uint8_t kSysexMessageEnd = 0xF7; 17 static const uint8_t kSystemRealtimeMessage = 0xF8; 18 // Represents the length of all possible command messages. 19 // Taken from MIDI Spec, Pg. 101v 1.0, Table 2 20 static const uint8_t kCommandLengths[] = {3, 3, 3, 3, 2, 2, 3}; 21 // Represents the length of all possible system messages. The length of sysex 22 // messages is variable, so we just put zero since it won't be checked anyways. 23 // Taken from MIDI Spec v1.0, Pg. 105, Table 5 24 static const uint8_t kSystemLengths[] = {0, 2, 3, 2, 1, 1, 1, 1}; 25 static const uint8_t kReservedStatuses[] = {0xf4, 0xf5, 0xf9, 0xfd}; 26 27 namespace mozilla::dom::MIDIUtils { 28 29 static bool IsSystemRealtimeMessage(uint8_t aByte) { 30 return (aByte & kSystemRealtimeMessage) == kSystemRealtimeMessage; 31 } 32 33 static bool IsCommandByte(uint8_t aByte) { 34 return (aByte & kCommandByte) == kCommandByte; 35 } 36 37 static bool IsReservedStatus(uint8_t aByte) { 38 for (const auto& msg : kReservedStatuses) { 39 if (aByte == msg) { 40 return true; 41 } 42 } 43 44 return false; 45 } 46 47 // Checks validity of MIDIMessage passed to it. Throws debug warnings and 48 // returns false if message is not valid. 49 bool IsValidMessage(const MIDIMessage* aMsg) { 50 if (aMsg->data().Length() == 0) { 51 return false; 52 } 53 54 uint8_t cmd = aMsg->data()[0]; 55 // If first byte isn't a command, something is definitely wrong. 56 if (!IsCommandByte(cmd)) { 57 NS_WARNING("Constructed a MIDI packet where first byte is not command!"); 58 return false; 59 } 60 61 if (IsReservedStatus(cmd)) { 62 NS_WARNING("Using a reserved message"); 63 return false; 64 } 65 66 if (cmd == kSysexMessageStart) { 67 // All we can do with sysex is make sure it starts and ends with the 68 // correct command bytes and that it does not contain other command bytes. 69 if (aMsg->data()[aMsg->data().Length() - 1] != kSysexMessageEnd) { 70 NS_WARNING("Last byte of Sysex Message not 0xF7!"); 71 return false; 72 } 73 74 for (size_t i = 1; i < aMsg->data().Length() - 2; i++) { 75 if (IsCommandByte(aMsg->data()[i])) { 76 return false; 77 } 78 } 79 80 return true; 81 } 82 // For system realtime messages, the length should always be 1. 83 if (IsSystemRealtimeMessage(cmd)) { 84 return aMsg->data().Length() == 1; 85 } 86 // Otherwise, just use the correct array for testing lengths. We can't tell 87 // much about message validity other than that. 88 if ((cmd & kSystemMessage) == kSystemMessage) { 89 if (cmd - kSystemMessage >= 90 static_cast<uint8_t>(std::size(kSystemLengths))) { 91 NS_WARNING("System Message Command byte not valid!"); 92 return false; 93 } 94 return aMsg->data().Length() == kSystemLengths[cmd - kSystemMessage]; 95 } 96 // For non system commands, we only care about differences in the high nibble 97 // of the first byte. Shift this down to give the index of the expected packet 98 // length. 99 uint8_t cmdIndex = (cmd - kCommandByte) >> 4; 100 if (cmdIndex >= std::size(kCommandLengths)) { 101 // If our index is bigger than our array length, command byte is unknown; 102 NS_WARNING("Unknown MIDI command!"); 103 return false; 104 } 105 return aMsg->data().Length() == kCommandLengths[cmdIndex]; 106 } 107 108 bool ParseMessages(const nsTArray<uint8_t>& aByteBuffer, 109 const TimeStamp& aTimestamp, 110 nsTArray<MIDIMessage>& aMsgArray) { 111 bool inSysexMessage = false; 112 UniquePtr<MIDIMessage> currentMsg = nullptr; 113 for (const auto& byte : aByteBuffer) { 114 if (IsSystemRealtimeMessage(byte)) { 115 MIDIMessage rt_msg; 116 rt_msg.data().AppendElement(byte); 117 rt_msg.timestamp() = aTimestamp; 118 if (!IsValidMessage(&rt_msg)) { 119 return false; 120 } 121 aMsgArray.AppendElement(rt_msg); 122 continue; 123 } 124 125 if (byte == kSysexMessageEnd) { 126 if (!inSysexMessage) { 127 NS_WARNING( 128 "Got sysex message end with no sysex message being processed!"); 129 return false; 130 } 131 inSysexMessage = false; 132 } else if (IsCommandByte(byte)) { 133 if (currentMsg) { 134 if (!IsValidMessage(currentMsg.get())) { 135 return false; 136 } 137 138 aMsgArray.AppendElement(*currentMsg); 139 } 140 141 currentMsg = MakeUnique<MIDIMessage>(); 142 currentMsg->timestamp() = aTimestamp; 143 } 144 145 if (!currentMsg) { 146 NS_WARNING("No command byte has been encountered yet!"); 147 return false; 148 } 149 150 currentMsg->data().AppendElement(byte); 151 152 if (byte == kSysexMessageStart) { 153 inSysexMessage = true; 154 } 155 } 156 157 if (currentMsg) { 158 if (!IsValidMessage(currentMsg.get())) { 159 return false; 160 } 161 aMsgArray.AppendElement(*currentMsg); 162 } 163 164 return true; 165 } 166 167 bool IsSysexMessage(const MIDIMessage& aMsg) { 168 if (aMsg.data().Length() == 0) { 169 return false; 170 } 171 return aMsg.data()[0] == kSysexMessageStart; 172 } 173 } // namespace mozilla::dom::MIDIUtils