1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MessageCodec.cpp Sun May 11 00:30:03 2025 +0200
1.3 @@ -0,0 +1,118 @@
1.4 +/**
1.5 + * djm-fix
1.6 + * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 3 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.19 + */
1.20 +
1.21 +#include <iostream>
1.22 +#include <iomanip>
1.23 +
1.24 +#include "MessageCodec.h"
1.25 +
1.26 +Message MessageCodec::decode(std::vector<uint8_t> data) {
1.27 + using ERR = std::invalid_argument;
1.28 +
1.29 + if (data.empty() || data.front() != 0xf0 || data.back() != 0xf7)
1.30 + throw ERR("not a MIDI SysEx message");
1.31 +
1.32 + // Manufacturer MIDI SysEx ID Numbers:
1.33 + // 00H 40H 05H = AlphaTheta Corporation
1.34 + // 00H 40H 06H = Pioneer Corporation
1.35 + if (data.size() < 4
1.36 + || data[1] != 0x00
1.37 + || data[2] != 0x40
1.38 + || data[3] != 0x05)
1.39 + throw ERR("wrong message Manufacturer MIDI SysEx ID");
1.40 +
1.41 + if (data.size() < 8) throw ERR("missing message version");
1.42 +
1.43 + uint8_t msgVersion = data[7];
1.44 +
1.45 + if (data.size() < 10) throw ERR("missing message type");
1.46 +
1.47 + MessageType msgType;
1.48 + if /**/ (data[9] == 0x11) msgType = MessageType::D11_GREETING;
1.49 + else if (data[9] == 0x12) msgType = MessageType::H12_SEED1;
1.50 + else if (data[9] == 0x13) msgType = MessageType::D13_HASH1_SEED2;
1.51 + else if (data[9] == 0x14) msgType = MessageType::H14_HASH2;
1.52 + else if (data[9] == 0x15) msgType = MessageType::D15_CONFIRMATION;
1.53 + else throw ERR("unsupported message type");
1.54 +
1.55 + if (data.size() < 11) throw ERR("missing message length");
1.56 + if (data[10] != data.size() - 10) throw ERR("wrong message length");
1.57 +
1.58 + std::vector<Field> fields;
1.59 +
1.60 + for (size_t start = 11; data.size() > start + 1;) {
1.61 + FieldType fieldType;
1.62 + if /**/ (data[start] == 0x01) fieldType = FieldType::F01;
1.63 + else if (data[start] == 0x02) fieldType = FieldType::F02;
1.64 + else if (data[start] == 0x03) fieldType = FieldType::F03;
1.65 + else if (data[start] == 0x04) fieldType = FieldType::F04;
1.66 + else if (data[start] == 0x05) fieldType = FieldType::F05;
1.67 + else throw ERR("unsupported field type");
1.68 +
1.69 + size_t fieldSize = data[start + 1];
1.70 +
1.71 + if (data.size() < start + fieldSize + 1) throw ERR("field overflow");
1.72 + // ^ 0xf7 messsage end
1.73 +
1.74 + std::vector<uint8_t> fieldData(
1.75 + data.begin() + start + 2,
1.76 + data.begin() + start + fieldSize);
1.77 +
1.78 + fields.push_back({fieldType, fieldData});
1.79 +
1.80 + start += fieldSize;
1.81 + }
1.82 +
1.83 + return Message(msgType, msgVersion, fields);
1.84 +}
1.85 +
1.86 +std::vector<uint8_t> MessageCodec::encode(Message msg) {
1.87 + using ERR = std::invalid_argument;
1.88 +
1.89 + std::vector<uint8_t> data;
1.90 +
1.91 + data.push_back(0xf0);
1.92 +
1.93 + data.push_back(0x00);
1.94 + data.push_back(0x40);
1.95 + data.push_back(0x05);
1.96 +
1.97 + data.push_back(0x00);
1.98 + data.push_back(0x00);
1.99 + data.push_back(0x00);
1.100 +
1.101 + data.push_back(msg.version);
1.102 + data.push_back(0x00);
1.103 + data.push_back((uint8_t) msg.type);
1.104 +
1.105 + uint8_t msgLength = 2; // 2 = type + lenght
1.106 + for (const auto& field : msg.fields) msgLength += field.data.size() + 2;
1.107 +
1.108 + data.push_back(msgLength);
1.109 +
1.110 + for (const auto& field : msg.fields) {
1.111 + size_t fieldSize = field.data.size() + 2;
1.112 + if (fieldSize > 256) throw ERR("message field too long");
1.113 + data.push_back((uint8_t) field.type);
1.114 + data.push_back((uint8_t) (fieldSize));
1.115 + data.insert(data.end(), field.data.begin(), field.data.end());
1.116 + }
1.117 +
1.118 + data.push_back(0xf7);
1.119 +
1.120 + return data;
1.121 +}