Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Created December 2, 2025 04:21
Show Gist options
  • Select an option

  • Save kmcallister/45bb2cf37da4ed36c0c5a314b23f7d96 to your computer and use it in GitHub Desktop.

Select an option

Save kmcallister/45bb2cf37da4ed36c0c5a314b23f7d96 to your computer and use it in GitHub Desktop.
MIDI to WLED UDP gateway
import mido
import socket
UDP_ADDRESS = "xmastree.local"
UDP_PORT = 21324
NUM_LEDS = 150
COLORS = [
[255, 0, 0],
[ 0, 255, 0],
[ 0, 0, 255]
]
def midi_to_wled(msg):
if msg.type == 'note_on':
color = COLORS[msg.note % len(COLORS)]
elif msg.type == 'note_off':
color = [0, 0, 0]
else:
return None
# https://github.com/wled/WLED/wiki/UDP-Realtime-Control#udp-realtime
#
# Byte 0: Protocol 2 (DRGB)
# Byte 1: Delay (in seconds) before returning to normal mode if no new message
v = [2, 2]
# For each LED, send [r, g, b]
for i in range(NUM_LEDS):
v.extend(color)
return bytearray(v)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
with mido.open_input() as inport:
for midi_msg in inport:
print(midi_msg)
wled_msg = midi_to_wled(midi_msg)
if wled_msg is not None:
sock.sendto(wled_msg, (UDP_ADDRESS, UDP_PORT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment