Skip to content

Commit

Permalink
Merge pull request #5 from Ruudjhuu/incomming_sysex
Browse files Browse the repository at this point in the history
handle incomming tcp midi sysex messages
  • Loading branch information
Ruudjhuu authored Nov 6, 2024
2 parents c5f9fbd + e8b1ab8 commit a0eda51
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
8 changes: 7 additions & 1 deletion src/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,15 @@ void Bridge::handle_tcp_read(tcp::Package &package) {
break;
}
case tcp::Body::Type::OutgoingMidi:
case tcp::Body::Type::SysEx:
throw std::invalid_argument("Received unexpected package of type: " +
std::to_string(package.get_body()->get_type()));
case tcp::Body::Type::SysEx: {
auto midi_body =
std::dynamic_pointer_cast<tcp::SysExMidiBody>(package.get_body());
midi_devices.at(midi_body->get_device_index())
->send_message(midi_body->get_message());
break;
}
case tcp::Body::Type::Unkown:
default: {
spdlog::warn("Ignored unkown package");
Expand Down
42 changes: 27 additions & 15 deletions src/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const uint16_t SIZE_OF_BYTE = 8;

const uint8_t INCOMMING_MIDI_DEVICE_BASE = 0x6c;
const uint8_t OUTGOING_MIDI_DEVICE_BASE = 0x67;
const int NR_OF_SUPPORTED_INCOMMINGMIDI_DEVICES = 5;
const int NR_OF_SUPPORTED_DEVICES = 5;

std::map<uint16_t, Body::Type> int16_to_type_map() {
try {
Expand Down Expand Up @@ -176,12 +176,13 @@ uint16_t Body::find_type_in_map(Type type) {

IncommingMidiBody::IncommingMidiBody(BufferView<std::byte *> buffer_view)
: Body(Body::Type::IncommingMidi,
BODY_HEADER_SIZE + buffer_view.distance()) {
BODY_HEADER_SIZE + buffer_view.distance()),
m_device(std::byte(0x0)) {
int step = 0;
for (auto iter : buffer_view) {
switch (step) {
case 0:
m_device = iter;
m_device = MidiDeviceIndicator(iter);
step++;
break;
case 1:
Expand All @@ -204,7 +205,7 @@ std::vector<std::byte> IncommingMidiBody::serialize() {
auto tmp = std::vector<std::byte>();
auto body_header = Body::serialize();
tmp.insert(tmp.end(), body_header.begin(), body_header.end());
tmp.push_back(m_device);
tmp.push_back(m_device.get_byte());
tmp.push_back(DELIMITER);
for (auto &iter : m_message) {
tmp.push_back(std::byte(iter));
Expand All @@ -213,10 +214,20 @@ std::vector<std::byte> IncommingMidiBody::serialize() {
return tmp;
}

int IncommingMidiBody::get_device_index() {
for (int i = 0; i < NR_OF_SUPPORTED_INCOMMINGMIDI_DEVICES; i++) {
if (m_device == std::byte(INCOMMING_MIDI_DEVICE_BASE + i)) {
return i;
int MidiDeviceIndicator::get_index() {
if (m_device_byte >= std::byte(INCOMMING_MIDI_DEVICE_BASE)) {
// incomming device type
for (int i = 0; i < NR_OF_SUPPORTED_DEVICES; i++) {
if (m_device_byte == std::byte(INCOMMING_MIDI_DEVICE_BASE + i)) {
return i;
}
}
} else {
// outgoing device type
for (int i = 0; i < NR_OF_SUPPORTED_DEVICES; i++) {
if (m_device_byte == std::byte(OUTGOING_MIDI_DEVICE_BASE + i)) {
return i;
}
}
}

Expand All @@ -237,15 +248,15 @@ OutgoingMidiBody::OutgoingMidiBody(
}

OutgoingMidiBody::OutgoingMidiBody(BufferView<std::byte *> buffer_view)
: Body(Body::Type::OutgoingMidi,
BODY_HEADER_SIZE + buffer_view.distance()) {
: Body(Body::Type::OutgoingMidi, BODY_HEADER_SIZE + buffer_view.distance()),
m_device(std::byte(0x0)) {
int step = 0;
libremidi::midi_bytes tmp_midi_bytes;

for (const auto &iter : buffer_view) {
switch (step) {
case 0:
m_device = iter;
m_device = MidiDeviceIndicator(iter);
step++;
break;
case 1:
Expand Down Expand Up @@ -274,7 +285,7 @@ std::vector<std::byte> OutgoingMidiBody::serialize() {
auto tmp = std::vector<std::byte>();
auto body_header = Body::serialize();
tmp.insert(tmp.end(), body_header.begin(), body_header.end());
tmp.push_back(m_device);
tmp.push_back(m_device.get_byte());
tmp.push_back(DELIMITER);
tmp.push_back(std::byte(m_messages.size()));
for (const auto &iter : m_messages) {
Expand All @@ -286,13 +297,14 @@ std::vector<std::byte> OutgoingMidiBody::serialize() {
}

SysExMidiBody::SysExMidiBody(BufferView<std::byte *> buffer_view)
: Body(Body::Type::SysEx, BODY_HEADER_SIZE + buffer_view.distance()) {
: Body(Body::Type::SysEx, BODY_HEADER_SIZE + buffer_view.distance()),
m_device(std::byte(0x0)) {
int step = 0;
size_t sysex_length = 0;
for (const auto &iter : buffer_view) {
switch (step) {
case 0:
m_device = iter;
m_device = MidiDeviceIndicator(iter);
step++;
break;
case 1:
Expand Down Expand Up @@ -324,7 +336,7 @@ std::vector<std::byte> SysExMidiBody::serialize() {
auto tmp = std::vector<std::byte>();
auto body_header = Body::serialize();
tmp.insert(tmp.end(), body_header.begin(), body_header.end());
tmp.push_back(m_device);
tmp.push_back(m_device.get_byte());
tmp.push_back(DELIMITER);
tmp.push_back(std::byte(m_message.size()));
tmp.push_back(DELIMITER);
Expand Down
23 changes: 17 additions & 6 deletions src/package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class ISerialize {
ISerialize() = default;
};

class MidiDeviceIndicator {
public:
explicit MidiDeviceIndicator(const std::byte device_byte)
: m_device_byte(device_byte) {}
int get_index();
std::byte get_byte() { return m_device_byte; }

private:
std::byte m_device_byte;
};

class Header : public ISerialize {
public:
explicit Header(BufferView<std::byte *> buffer_view);
Expand Down Expand Up @@ -88,11 +99,11 @@ class IncommingMidiBody : public Body {
m_device(device), m_message(message) {}
explicit IncommingMidiBody(BufferView<std::byte *> buffer_view);
std::vector<std::byte> serialize() override;
int get_device_index();
int get_device_index() { return m_device.get_index(); }
libremidi::message &get_message() { return m_message; }

private:
std::byte m_device;
MidiDeviceIndicator m_device;
libremidi::message m_message;
};

Expand All @@ -102,11 +113,11 @@ class OutgoingMidiBody : public Body {
const std::vector<libremidi::message> &message);
explicit OutgoingMidiBody(BufferView<std::byte *> buffer_view);
std::vector<std::byte> serialize() override;
int get_device_index();
int get_device_index() { return m_device.get_index(); }
const std::vector<libremidi::message> &get_messages() { return m_messages; }

private:
std::byte m_device;
MidiDeviceIndicator m_device;
std::vector<libremidi::message> m_messages;
};

Expand All @@ -117,11 +128,11 @@ class SysExMidiBody : public Body {
m_device(device), m_message(message) {}
explicit SysExMidiBody(BufferView<std::byte *> buffer_view);
std::vector<std::byte> serialize() override;
int get_device_index();
int get_device_index() { return m_device.get_index(); }
const libremidi::message &get_message() { return m_message; }

private:
std::byte m_device;
MidiDeviceIndicator m_device;
libremidi::message m_message;
};

Expand Down

0 comments on commit a0eda51

Please sign in to comment.