|
| 1 | +/// ESP32 Serial MIDI example using libremidi's raw I/O backend. |
| 2 | +/// |
| 3 | +/// Wiring: MIDI IN on Serial2 RX (GPIO 16), MIDI OUT on Serial2 TX (GPIO 17) |
| 4 | +/// Standard MIDI baud rate is 31250. |
| 5 | +/// |
| 6 | +/// This sketch receives MIDI on Serial2, parses it through libremidi |
| 7 | +/// (giving you structured messages with sysex filtering, etc.), |
| 8 | +/// sends MIDI back out on the same serial port, |
| 9 | +/// and broadcasts every received message as an OSC /midi UDP packet |
| 10 | +/// to the local network (192.168.1.255:5678). |
| 11 | + |
| 12 | +#include <WiFi.h> |
| 13 | +#include <WiFiUdp.h> |
| 14 | + |
| 15 | +// Note: you need to have the "include/libremidi" folder copied or |
| 16 | +// symlinked into the sketch folder for this to work, or change the |
| 17 | +// Arduino platform configuration files to add the proper include path. |
| 18 | +#define LIBREMIDI_HEADER_ONLY 1 |
| 19 | +#include <libremidi/configurations.hpp> |
| 20 | +#include <libremidi/libremidi.hpp> |
| 21 | + |
| 22 | +// --- Configuration --- |
| 23 | +static constexpr int MIDI_BAUD = 31250; |
| 24 | +static constexpr int MIDI_RX_PIN = 16; |
| 25 | +static constexpr int MIDI_TX_PIN = 17; |
| 26 | + |
| 27 | +static const char* WIFI_SSID = "your-ssid"; |
| 28 | +static const char* WIFI_PASS = "your-password"; |
| 29 | + |
| 30 | +static const IPAddress UDP_BROADCAST{192, 168, 1, 255}; |
| 31 | +static constexpr uint16_t UDP_PORT = 49444; |
| 32 | +static const char* OSC_PATH = "/midi"; // must be 5 chars (+ padding) for the packet layout below |
| 33 | + |
| 34 | +// --- Globals --- |
| 35 | + |
| 36 | +// The callback that libremidi gives us to feed incoming bytes |
| 37 | +static libremidi::rawio_input_configuration::receive_callback g_feed_input; |
| 38 | + |
| 39 | +// The libremidi objects need to survive across loop() calls |
| 40 | +static std::optional<libremidi::midi_in> g_midi_in; |
| 41 | +static std::optional<libremidi::midi_out> g_midi_out; |
| 42 | + |
| 43 | +static WiFiUDP g_udp; |
| 44 | + |
| 45 | +/// Build and send an OSC message containing a single 3-byte MIDI event. |
| 46 | +/// |
| 47 | +/// OSC /midi packet layout (all 4-byte aligned): |
| 48 | +/// "/midi\0\0\0" (8 bytes: pattern + null + padding) |
| 49 | +/// ",m\0\0" (4 bytes: typetag string) |
| 50 | +/// [port, b0, b1, b2] (4 bytes: OSC MIDI atom) |
| 51 | +void osc_broadcast_midi(uint8_t status, uint8_t d1, uint8_t d2) |
| 52 | +{ |
| 53 | + // Pre-built packet: only the last 3 bytes change |
| 54 | + uint8_t pkt[16] = { |
| 55 | + // OSC address pattern "/midi" + null + 2 padding zeros |
| 56 | + '/', 'm', 'i', 'd', 'i', 0, 0, 0, |
| 57 | + // OSC type tag ",m" + null + 1 padding zero |
| 58 | + ',', 'm', 0, 0, |
| 59 | + // OSC MIDI data: port (0), status, data1, data2 |
| 60 | + 0, status, d1, d2}; |
| 61 | + |
| 62 | + g_udp.beginPacket(UDP_BROADCAST, UDP_PORT); |
| 63 | + g_udp.write(pkt, sizeof(pkt)); |
| 64 | + g_udp.endPacket(); |
| 65 | +} |
| 66 | + |
| 67 | +// Called by libremidi whenever a complete MIDI message is received and parsed |
| 68 | +void on_midi_message(const libremidi::message& msg) |
| 69 | +{ |
| 70 | + // Print to USB serial for debugging |
| 71 | + Serial.printf("MIDI [%02X", msg.bytes[0]); |
| 72 | + for (size_t i = 1; i < msg.bytes.size(); i++) |
| 73 | + Serial.printf(" %02X", msg.bytes[i]); |
| 74 | + Serial.println("]"); |
| 75 | + |
| 76 | + // Broadcast as OSC over UDP (channel messages are always 3 bytes) |
| 77 | + if (msg.bytes.size() == 3) |
| 78 | + osc_broadcast_midi(msg.bytes[0], msg.bytes[1], msg.bytes[2]); |
| 79 | + |
| 80 | + // Echo note-on messages back on serial with velocity halved |
| 81 | + if (msg.get_message_type() == libremidi::message_type::NOTE_ON && g_midi_out) |
| 82 | + { |
| 83 | + uint8_t velocity = msg.bytes[2] / 2; |
| 84 | + g_midi_out->send_message(msg.bytes[0], msg.bytes[1], velocity); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void setup() |
| 89 | +{ |
| 90 | + // USB serial for debug output |
| 91 | + Serial.begin(115200); |
| 92 | + |
| 93 | + // Connect to WiFi |
| 94 | + WiFi.begin(WIFI_SSID, WIFI_PASS); |
| 95 | + Serial.print("Connecting to WiFi"); |
| 96 | + while (WiFi.status() != WL_CONNECTED) |
| 97 | + { |
| 98 | + delay(500); |
| 99 | + Serial.print("."); |
| 100 | + } |
| 101 | + Serial.printf("\nConnected, IP: %s\n", WiFi.localIP().toString().c_str()); |
| 102 | + |
| 103 | + // Start UDP for OSC broadcast |
| 104 | + g_udp.begin(UDP_PORT); |
| 105 | + |
| 106 | + // MIDI serial port |
| 107 | + Serial2.begin(MIDI_BAUD, SERIAL_8N1, MIDI_RX_PIN, MIDI_TX_PIN); |
| 108 | + |
| 109 | + // Create MIDI input |
| 110 | + g_midi_in.emplace( |
| 111 | + libremidi::input_configuration{ |
| 112 | + .on_message = on_midi_message, |
| 113 | + .ignore_sysex = true, |
| 114 | + .ignore_timing = true, |
| 115 | + .ignore_sensing = true, |
| 116 | + }, |
| 117 | + libremidi::rawio_input_configuration{ |
| 118 | + .set_receive_callback = [](auto cb) { g_feed_input = std::move(cb); }, |
| 119 | + .stop_receive = [] { g_feed_input = nullptr; }, |
| 120 | + }); |
| 121 | + |
| 122 | + // Create MIDI output (serial) |
| 123 | + g_midi_out.emplace( |
| 124 | + libremidi::output_configuration{}, |
| 125 | + libremidi::rawio_output_configuration{ |
| 126 | + .write_bytes = [](std::span<const uint8_t> bytes) -> stdx::error { |
| 127 | + Serial2.write(bytes.data(), bytes.size()); |
| 128 | + return {}; |
| 129 | + }}); |
| 130 | + |
| 131 | + // Open ports to activate the callbacks |
| 132 | + g_midi_in->open_virtual_port("esp32_in"); |
| 133 | + g_midi_out->open_virtual_port("esp32_out"); |
| 134 | + |
| 135 | + Serial.println("MIDI ready, broadcasting OSC to " + UDP_BROADCAST.toString() + ":" + UDP_PORT); |
| 136 | +} |
| 137 | + |
| 138 | +void loop() |
| 139 | +{ |
| 140 | + // Read all available bytes from the MIDI serial port |
| 141 | + // and feed them into libremidi for parsing |
| 142 | + int avail = Serial2.available(); |
| 143 | + if (avail > 0 && g_feed_input) |
| 144 | + { |
| 145 | + uint8_t buf[64]; |
| 146 | + int n = Serial2.readBytes(buf, min(avail, (int)sizeof(buf))); |
| 147 | + g_feed_input({buf, static_cast<size_t>(n)}, 0); |
| 148 | + } |
| 149 | +} |
0 commit comments