forked from fferri/AbletonOSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
98 lines (81 loc) · 3.07 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from ableton.v2.control_surface import ControlSurface
from . import abletonosc
import importlib
import traceback
import logging
import sys
import os
from typing import Tuple
logger = logging.getLogger("abletonosc")
if sys.platform == "darwin":
# On macOS, put logs in /tmp
tmp_dir = "/tmp"
else:
# On Windows, put logs in c:\temp
tmp_dir = r"c:\temp"
log_path = os.path.join(tmp_dir, "abletonosc.log")
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('(%(asctime)s) [%(levelname)s] %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
class Manager(ControlSurface):
def __init__(self, c_instance):
ControlSurface.__init__(self, c_instance)
self.handler = None
self.show_message("AbletonOSC: Listening for OSC on port %d" % abletonosc.OSC_LISTEN_PORT)
self.osc_server = abletonosc.OSCServer()
self.schedule_message(0, self.tick)
self.init_api()
def init_api(self):
def test_callback(addr_args, params):
self.show_message("Received OSC OK")
self.osc_server.send("/test", ("ok",))
def reload_callback(addr_args, params):
self.reload_imports()
def pyeval_callback(addr_args, params) -> Tuple:
flags, code = params
logger.info(f'pyeval: {code}')
try:
ret = [True, eval(code)]
except Exception as e:
ret = [False, str(e)]
if not isinstance(ret[1], (int, float, bool, str)):
ret[1] = str(ret[1])
self.osc_server.send('/pyeval', ret)
self.osc_server.add_handler("/test", test_callback)
self.osc_server.add_handler("/reload", reload_callback)
self.osc_server.add_handler("/pyeval", pyeval_callback)
with self.component_guard():
self.handler = abletonosc.AbletonOSCHandler(self)
def clear_api(self):
self.osc_server.clear_handlers()
self.handler.clear_api()
def tick(self):
"""
Called once per 100ms "tick".
Live's embedded Python implementation does not appear to support threading,
and beachballs when a thread is started. Instead, this approach allows long-running
processes such as the OSC server to perform operations.
"""
logger.debug("Tick...")
self.osc_server.process()
self.schedule_message(1, self.tick)
def reload_imports(self):
try:
importlib.reload(abletonosc.handler)
importlib.reload(abletonosc.osc_server)
importlib.reload(abletonosc.constants)
importlib.reload(abletonosc)
except Exception as e:
exc = traceback.format_exc()
logging.warning(exc)
if self.handler:
self.clear_api()
self.init_api()
logger.info("Reloaded code")
def disconnect(self):
self.show_message("Disconnecting...")
logger.info("Disconneting...")
self.osc_server.shutdown()
super().disconnect()