diff options
author | MaZderMind <git@mazdermind.de> | 2016-08-21 21:02:34 +0200 |
---|---|---|
committer | MaZderMind <git@mazdermind.de> | 2016-08-21 21:02:34 +0200 |
commit | d4506ffc6e2b91b2aa18f4cca25b319a7f551ad3 (patch) | |
tree | f9fa1e0f169d8d8623e37e26872c722a06dd2ae9 /example-scripts/voctomidi/voctomidi.py | |
parent | 7ce3b12e4a76d1926077d7efa57e5e9d3538de33 (diff) | |
parent | 0319be21855f9c9f4d618552ddde2a852d56869a (diff) |
Merge branch 'Florob-voctomidi'
Diffstat (limited to 'example-scripts/voctomidi/voctomidi.py')
-rwxr-xr-x | example-scripts/voctomidi/voctomidi.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/example-scripts/voctomidi/voctomidi.py b/example-scripts/voctomidi/voctomidi.py new file mode 100755 index 0000000..b21f529 --- /dev/null +++ b/example-scripts/voctomidi/voctomidi.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +import atexit +import socket +import sys +import time +from rtmidi.midiutil import open_midiport + +from lib.config import Config + +NOTE_ON = 144 + +host = Config.get("server", "host") +port = 9999 + +device = Config.get("midi", "device") + +event_map = dict(map(lambda x: (int(x[0]), x[1]), Config.items("eventmap"))) + + +class MidiInputHandler(object): + def __init__(self, port): + self.port = port + + def __call__(self, event, data=None): + global conn + message, _deltatime = event + if message[0] != NOTE_ON: + return + if message[1] in event_map: + note = message[1] + msg = "set_videos_and_composite " + event_map[note] + print("Sending: '{}'".format(msg)) + try: + conn.sendall(msg.encode('ascii') + b"\n") + except BrokenPipeError: + print("voctocore disconnected, trying to reconnect") + try: + conn = socket.create_connection((host, port)) + print("Reconnected to voctocore") + except: + pass + else: + print("[{}]: Unhandled NOTE ON event {}".format(self.port, + message[1])) + + +@atexit.register +def kthxbye(): + print("Exit") + +conn, midiin = None, None + +try: + conn = socket.create_connection((host, port)) +except (ConnectionRefusedError, KeyboardInterrupt): + print("Could not connect to voctocore") + sys.exit() + + +@atexit.register +def close_conn(): + global conn + conn and conn.close() + +try: + midiin, port_name = open_midiport(device) +except (EOFError, KeyboardInterrupt): + print("Opening midi port failed") + sys.exit() + + +@atexit.register +def close_midi(): + global midiin + midiin and midiin.close_port() + del midiin + +midiin.set_callback(MidiInputHandler(port_name)) + +print("Entering main loop. Press Control-C to exit.") +try: + # just wait for keyboard interrupt in main thread + while True: + time.sleep(1) +except KeyboardInterrupt: + print("") |