diff -r aa7dc7faf1bb -r 358a601bfe81 MessageCodec.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MessageCodec.cpp Sun May 11 00:30:03 2025 +0200 @@ -0,0 +1,118 @@ +/** + * djm-fix + * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "MessageCodec.h" + +Message MessageCodec::decode(std::vector data) { + using ERR = std::invalid_argument; + + if (data.empty() || data.front() != 0xf0 || data.back() != 0xf7) + throw ERR("not a MIDI SysEx message"); + + // Manufacturer MIDI SysEx ID Numbers: + // 00H 40H 05H = AlphaTheta Corporation + // 00H 40H 06H = Pioneer Corporation + if (data.size() < 4 + || data[1] != 0x00 + || data[2] != 0x40 + || data[3] != 0x05) + throw ERR("wrong message Manufacturer MIDI SysEx ID"); + + if (data.size() < 8) throw ERR("missing message version"); + + uint8_t msgVersion = data[7]; + + if (data.size() < 10) throw ERR("missing message type"); + + MessageType msgType; + if /**/ (data[9] == 0x11) msgType = MessageType::D11_GREETING; + else if (data[9] == 0x12) msgType = MessageType::H12_SEED1; + else if (data[9] == 0x13) msgType = MessageType::D13_HASH1_SEED2; + else if (data[9] == 0x14) msgType = MessageType::H14_HASH2; + else if (data[9] == 0x15) msgType = MessageType::D15_CONFIRMATION; + else throw ERR("unsupported message type"); + + if (data.size() < 11) throw ERR("missing message length"); + if (data[10] != data.size() - 10) throw ERR("wrong message length"); + + std::vector fields; + + for (size_t start = 11; data.size() > start + 1;) { + FieldType fieldType; + if /**/ (data[start] == 0x01) fieldType = FieldType::F01; + else if (data[start] == 0x02) fieldType = FieldType::F02; + else if (data[start] == 0x03) fieldType = FieldType::F03; + else if (data[start] == 0x04) fieldType = FieldType::F04; + else if (data[start] == 0x05) fieldType = FieldType::F05; + else throw ERR("unsupported field type"); + + size_t fieldSize = data[start + 1]; + + if (data.size() < start + fieldSize + 1) throw ERR("field overflow"); + // ^ 0xf7 messsage end + + std::vector fieldData( + data.begin() + start + 2, + data.begin() + start + fieldSize); + + fields.push_back({fieldType, fieldData}); + + start += fieldSize; + } + + return Message(msgType, msgVersion, fields); +} + +std::vector MessageCodec::encode(Message msg) { + using ERR = std::invalid_argument; + + std::vector data; + + data.push_back(0xf0); + + data.push_back(0x00); + data.push_back(0x40); + data.push_back(0x05); + + data.push_back(0x00); + data.push_back(0x00); + data.push_back(0x00); + + data.push_back(msg.version); + data.push_back(0x00); + data.push_back((uint8_t) msg.type); + + uint8_t msgLength = 2; // 2 = type + lenght + for (const auto& field : msg.fields) msgLength += field.data.size() + 2; + + data.push_back(msgLength); + + for (const auto& field : msg.fields) { + size_t fieldSize = field.data.size() + 2; + if (fieldSize > 256) throw ERR("message field too long"); + data.push_back((uint8_t) field.type); + data.push_back((uint8_t) (fieldSize)); + data.insert(data.end(), field.data.begin(), field.data.end()); + } + + data.push_back(0xf7); + + return data; +}