add raw MIDI data in HEX v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 18 May 2020 23:37:38 +0200
branchv_0
changeset 4 30da4232cdbc
parent 3 0222c20f590f
child 5 40dd6deafaca
add raw MIDI data in HEX
src/JackCommand.h
--- a/src/JackCommand.h	Mon May 18 22:03:59 2020 +0200
+++ b/src/JackCommand.h	Mon May 18 23:37:38 2020 +0200
@@ -24,6 +24,8 @@
 #include <sys/mman.h>
 #include <atomic>
 #include <functional>
+#include <sstream>
+#include <iomanip>
 
 #include <jack/jack.h>
 #include <jack/midiport.h>
@@ -104,7 +106,8 @@
 	static void writeRecord(std::shared_ptr<RelationalWriter> writer,
 			string_t eventType, integer_t channel,
 			boolean_t noteOn, integer_t pitch, integer_t velocity,
-			integer_t controllerId, integer_t value) {
+			integer_t controllerId, integer_t value,
+			string_t raw) {
 		writer->writeAttribute(eventType);
 		writer->writeAttribute(&channel, typeid (channel));
 		writer->writeAttribute(&noteOn, typeid (noteOn));
@@ -112,6 +115,7 @@
 		writer->writeAttribute(&velocity, typeid (velocity));
 		writer->writeAttribute(&controllerId, typeid (controllerId));
 		writer->writeAttribute(&value, typeid (value));
+		writer->writeAttribute(&raw, typeid (raw));
 	}
 
 	void processMessage(std::shared_ptr<RelationalWriter> writer, MidiMessage* event) {
@@ -122,18 +126,31 @@
 			uint8_t channel = event->buffer[0] & 0x0F;
 
 			// TODO: write timestamp, message number
-			// TODO: write raw buffer in hex
 
 			if ((type == 0x90 || type == 0x80) && event->size == 3) {
-				writeRecord(writer, L"note", channel, type == 0x90, event->buffer[1], event->buffer[2], 0, 0);
+				writeRecord(writer, L"note", channel, type == 0x90, event->buffer[1], event->buffer[2], 0, 0, toHex(event));
 			} else if (type == 0xB0 && event->size == 3) {
-				writeRecord(writer, L"control", channel, false, 0, 0, event->buffer[1], event->buffer[2]);
+				writeRecord(writer, L"control", channel, false, 0, 0, event->buffer[1], event->buffer[2], toHex(event));
 			} else {
-				writeRecord(writer, L"unknown", channel, false, 0, 0, 0, 0);
+				writeRecord(writer, L"unknown", channel, false, 0, 0, 0, 0, toHex(event));
 			}
 		}
 	}
 
+	string_t toHex(MidiMessage* event) {
+		std::wstringstream result;
+		result << std::hex << std::setfill(L'0');
+
+		for (size_t i = 0; i < event->size && i < sizeof (event->buffer); i++) {
+			if (i > 0) result << L' ';
+			result << std::setw(2) << event->buffer[i];
+			// result << ("0123456789abcdef"[event->buffer[i] >> 4]);
+			// result << ("0123456789abcdef"[event->buffer[i] & 0xf]);
+		}
+
+		return result.str();
+	}
+
 public:
 
 	void finish(int sig) {
@@ -150,6 +167,7 @@
 		metadata.push_back({L"note_velocity", TypeId::INTEGER});
 		metadata.push_back({L"controller_id", TypeId::INTEGER});
 		metadata.push_back({L"controller_value", TypeId::INTEGER});
+		metadata.push_back({L"raw", TypeId::STRING});
 		writer->startRelation(L"midi", metadata, true);
 		relationalWriterFlush();