Created
September 23, 2022 22:37
-
-
Save llandsmeer/b22d56e05af5c1172d904bb233b4c8f0 to your computer and use it in GitHub Desktop.
Control LedFX using midi controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import json | |
import requests | |
import rtmidi | |
# pip install python-rtmidi | |
midi = rtmidi.MidiIn() | |
ports = midi.get_ports() | |
for idx, port in enumerate(ports): | |
print(f'[{idx: 4d}] {port}') | |
try: | |
selected = int(input('Select midi input: ')) | |
except EOFError: | |
selected = 1 | |
midi.open_port(selected) | |
url_base = 'http://localhost:8888' | |
key0 = 44 | |
scenes = requests.get(f'{url_base}/api/scenes').json() | |
assert scenes['status'] == 'success' | |
scene_ids = [] | |
active = None | |
for scene_id, scene in scenes['scenes'].items(): | |
scene_ids.append(scene_id) | |
def reset(): | |
os.system('clear') | |
print('Midi input:', ports[selected]) | |
for idx, (scene_id, scene) in enumerate(scenes['scenes'].items()): | |
print(f'[{44 + idx: 4d}] {scene["name"]} {"*" if active == scene_id else ""}') | |
def handle(m): | |
global active | |
if m[0] != 144: | |
return | |
key = m[1] | |
idx = key - 44 | |
if idx < 0 or idx >= len(scene_ids): | |
reset() | |
print(f'only key 44 until {44 + len(scene_ids)} is supported') | |
print(f'this is key {key}') | |
return | |
scene_id = scene_ids[idx] | |
data = json.dumps(dict(id=scene_id, action='activate')) | |
res = requests.put(f'{url_base}/api/scenes', data) | |
reset() | |
print(res.text) | |
if 'success' in res.text: | |
active = scene_id | |
reset() | |
with midi: | |
while True: | |
time.sleep(0.1) | |
while True: | |
m = midi.get_message() | |
if m is None: | |
break | |
handle(m[0]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment