2022-04-05 20:05:55 +01:00
|
|
|
import abc
|
2022-11-07 11:08:56 +00:00
|
|
|
import logging
|
2022-04-05 20:05:55 +01:00
|
|
|
import threading
|
2022-08-07 23:55:51 +01:00
|
|
|
import time
|
2022-04-05 20:05:55 +01:00
|
|
|
from pathlib import Path
|
2022-11-07 15:22:59 +00:00
|
|
|
from typing import Optional, Union
|
2022-09-05 17:06:11 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import tomli as tomllib
|
2022-08-07 23:55:51 +01:00
|
|
|
|
|
|
|
from pythonosc.dispatcher import Dispatcher
|
|
|
|
from pythonosc.osc_message_builder import OscMessageBuilder
|
|
|
|
from pythonosc.osc_server import BlockingOSCUDPServer
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-11-07 11:08:56 +00:00
|
|
|
from . import adapter, kinds
|
2022-04-05 20:05:55 +01:00
|
|
|
from .bus import Bus
|
|
|
|
from .config import Config
|
2022-08-07 23:55:51 +01:00
|
|
|
from .dca import DCA
|
|
|
|
from .errors import XAirRemoteError
|
2022-11-07 11:08:56 +00:00
|
|
|
from .fx import FX, FXSend
|
2022-08-07 23:55:51 +01:00
|
|
|
from .kinds import KindMap
|
|
|
|
from .lr import LR
|
2022-11-07 11:08:56 +00:00
|
|
|
from .rtn import AuxRtn, FxRtn
|
2022-08-07 23:55:51 +01:00
|
|
|
from .strip import Strip
|
2022-04-05 20:05:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OSCClientServer(BlockingOSCUDPServer):
|
2022-08-07 23:55:51 +01:00
|
|
|
def __init__(self, address: str, dispatcher: Dispatcher):
|
2022-04-05 20:05:55 +01:00
|
|
|
super().__init__(("", 0), dispatcher)
|
|
|
|
self.xr_address = address
|
|
|
|
|
2022-11-07 15:22:59 +00:00
|
|
|
def send_message(self, address: str, vals: Optional[Union[str, list]]):
|
2022-04-05 20:05:55 +01:00
|
|
|
builder = OscMessageBuilder(address=address)
|
2022-11-07 15:22:59 +00:00
|
|
|
vals = vals if vals is not None else []
|
|
|
|
if not isinstance(vals, list):
|
|
|
|
vals = [vals]
|
|
|
|
for val in vals:
|
2022-04-05 20:05:55 +01:00
|
|
|
builder.add_arg(val)
|
|
|
|
msg = builder.build()
|
|
|
|
self.socket.sendto(msg.dgram, self.xr_address)
|
|
|
|
|
|
|
|
|
2022-08-07 23:55:51 +01:00
|
|
|
class XAirRemote(abc.ABC):
|
2022-08-08 00:13:14 +01:00
|
|
|
"""Handles the communication with the mixer via the OSC protocol"""
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-11-07 11:08:56 +00:00
|
|
|
logger = logging.getLogger("xair.xairremote")
|
|
|
|
|
2022-04-05 20:05:55 +01:00
|
|
|
_CONNECT_TIMEOUT = 0.5
|
|
|
|
|
2022-11-07 17:27:41 +00:00
|
|
|
_info_response = []
|
2022-04-05 20:05:55 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
dispatcher = Dispatcher()
|
|
|
|
dispatcher.set_default_handler(self.msg_handler)
|
2022-08-07 23:55:51 +01:00
|
|
|
self.xair_ip = kwargs["ip"] or self._ip_from_toml()
|
2022-11-07 11:08:56 +00:00
|
|
|
self.xair_port = kwargs["port"]
|
2022-11-08 15:09:20 +00:00
|
|
|
self._delay = kwargs["delay"]
|
|
|
|
if not self.xair_ip:
|
|
|
|
raise XAirRemoteError("No valid ip detected")
|
2022-05-01 03:46:44 +01:00
|
|
|
self.server = OSCClientServer((self.xair_ip, self.xair_port), dispatcher)
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-09-05 17:06:11 +01:00
|
|
|
def __enter__(self):
|
2022-08-07 23:55:51 +01:00
|
|
|
self.worker = threading.Thread(target=self.run_server, daemon=True)
|
2022-04-05 20:05:55 +01:00
|
|
|
self.worker.start()
|
|
|
|
self.validate_connection()
|
|
|
|
return self
|
|
|
|
|
2022-08-07 23:55:51 +01:00
|
|
|
def _ip_from_toml(self) -> str:
|
|
|
|
filepath = Path.cwd() / "config.toml"
|
|
|
|
with open(filepath, "rb") as f:
|
|
|
|
conn = tomllib.load(f)
|
|
|
|
return conn["connection"].get("ip")
|
2022-04-05 20:05:55 +01:00
|
|
|
|
|
|
|
def validate_connection(self):
|
|
|
|
self.send("/xinfo")
|
|
|
|
time.sleep(self._CONNECT_TIMEOUT)
|
2022-11-07 15:22:59 +00:00
|
|
|
if not self.info_response:
|
|
|
|
raise XAirRemoteError(
|
2022-11-08 15:09:20 +00:00
|
|
|
"Failed to setup OSC connection to mixer. Please check for correct ip address."
|
2022-04-05 20:05:55 +01:00
|
|
|
)
|
2022-11-07 15:22:59 +00:00
|
|
|
print(
|
|
|
|
f"Successfully connected to {self.info_response[2]} at {self.info_response[0]}."
|
|
|
|
)
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-11-07 17:27:41 +00:00
|
|
|
@property
|
|
|
|
def info_response(self):
|
|
|
|
return self._info_response
|
|
|
|
|
2022-04-05 20:05:55 +01:00
|
|
|
def run_server(self):
|
|
|
|
self.server.serve_forever()
|
|
|
|
|
|
|
|
def msg_handler(self, addr, *data):
|
2022-11-07 11:08:56 +00:00
|
|
|
self.logger.debug(f"received: {addr} {data if data else ''}")
|
2022-11-07 17:27:41 +00:00
|
|
|
self._info_response = data[:]
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-11-07 11:08:56 +00:00
|
|
|
def send(self, addr: str, param: Optional[str] = None):
|
2022-11-07 21:05:27 +00:00
|
|
|
self.logger.debug(f"sending: {addr} {param if param is not None else ''}")
|
2022-11-07 11:08:56 +00:00
|
|
|
self.server.send_message(addr, param)
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-11-07 17:27:41 +00:00
|
|
|
def query(self, address):
|
2022-04-05 20:05:55 +01:00
|
|
|
self.send(address)
|
2024-02-03 13:03:49 +00:00
|
|
|
time.sleep(self._delay)
|
2022-04-05 20:05:55 +01:00
|
|
|
return self.info_response
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_tr):
|
|
|
|
self.server.shutdown()
|
|
|
|
|
|
|
|
|
2022-08-07 23:55:51 +01:00
|
|
|
def _make_remote(kind: KindMap) -> XAirRemote:
|
2022-04-05 20:05:55 +01:00
|
|
|
"""
|
2022-08-08 00:13:14 +01:00
|
|
|
Creates a new XAIR remote class.
|
2022-04-05 20:05:55 +01:00
|
|
|
|
2022-10-28 21:07:14 +01:00
|
|
|
The returned class will subclass XAirRemote.
|
2022-04-05 20:05:55 +01:00
|
|
|
"""
|
|
|
|
|
2022-11-07 11:08:56 +00:00
|
|
|
def init_x32(self, *args, **kwargs):
|
2022-11-08 15:09:20 +00:00
|
|
|
defaultkwargs = {"ip": None, "port": 10023, "delay": 0.02}
|
2022-11-07 11:08:56 +00:00
|
|
|
kwargs = defaultkwargs | kwargs
|
|
|
|
XAirRemote.__init__(self, *args, **kwargs)
|
|
|
|
self.kind = kind
|
|
|
|
self.mainst = adapter.MainStereo.make(self)
|
|
|
|
self.mainmono = adapter.MainMono.make(self)
|
|
|
|
self.matrix = tuple(
|
|
|
|
adapter.Matrix.make(self, i) for i in range(kind.num_matrix)
|
|
|
|
)
|
|
|
|
self.strip = tuple(Strip.make(self, i) for i in range(kind.num_strip))
|
|
|
|
self.bus = tuple(adapter.Bus.make(self, i) for i in range(kind.num_bus))
|
|
|
|
self.dca = tuple(DCA(self, i) for i in range(kind.num_dca))
|
|
|
|
self.fx = tuple(FX(self, i) for i in range(kind.num_fx))
|
|
|
|
self.fxreturn = tuple(adapter.FxRtn.make(self, i) for i in range(kind.num_fx))
|
|
|
|
self.auxin = tuple(adapter.AuxRtn.make(self, i) for i in range(kind.num_auxrtn))
|
2022-11-07 17:27:41 +00:00
|
|
|
self.config = Config.make(self)
|
2022-11-07 11:08:56 +00:00
|
|
|
|
|
|
|
def init_xair(self, *args, **kwargs):
|
2022-11-08 15:09:20 +00:00
|
|
|
defaultkwargs = {"ip": None, "port": 10024, "delay": 0.02}
|
2022-04-05 20:05:55 +01:00
|
|
|
kwargs = defaultkwargs | kwargs
|
2022-08-07 23:55:51 +01:00
|
|
|
XAirRemote.__init__(self, *args, **kwargs)
|
2022-04-05 20:05:55 +01:00
|
|
|
self.kind = kind
|
|
|
|
self.lr = LR.make(self)
|
|
|
|
self.strip = tuple(Strip.make(self, i) for i in range(kind.num_strip))
|
|
|
|
self.bus = tuple(Bus.make(self, i) for i in range(kind.num_bus))
|
|
|
|
self.dca = tuple(DCA(self, i) for i in range(kind.num_dca))
|
2022-11-07 11:08:56 +00:00
|
|
|
self.fx = tuple(FX(self, i) for i in range(kind.num_fx))
|
2022-04-05 20:05:55 +01:00
|
|
|
self.fxsend = tuple(FXSend.make(self, i) for i in range(kind.num_fx))
|
2022-11-07 11:08:56 +00:00
|
|
|
self.fxreturn = tuple(FxRtn.make(self, i) for i in range(kind.num_fx))
|
|
|
|
self.auxreturn = AuxRtn.make(self)
|
2022-11-07 17:27:41 +00:00
|
|
|
self.config = Config.make(self)
|
2022-11-07 11:08:56 +00:00
|
|
|
|
|
|
|
if kind.id_ == "X32":
|
|
|
|
return type(
|
|
|
|
f"XAirRemote{kind}",
|
|
|
|
(XAirRemote,),
|
|
|
|
{
|
|
|
|
"__init__": init_x32,
|
|
|
|
},
|
|
|
|
)
|
2022-04-05 20:05:55 +01:00
|
|
|
return type(
|
2022-08-08 00:13:14 +01:00
|
|
|
f"XAirRemote{kind}",
|
2022-08-07 23:55:51 +01:00
|
|
|
(XAirRemote,),
|
2022-04-05 20:05:55 +01:00
|
|
|
{
|
2022-11-07 11:08:56 +00:00
|
|
|
"__init__": init_xair,
|
2022-04-05 20:05:55 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
_remotes = {kind.id_: _make_remote(kind) for kind in kinds.all}
|
|
|
|
|
|
|
|
|
2022-08-07 23:55:51 +01:00
|
|
|
def request_remote_obj(kind_id: str, *args, **kwargs) -> XAirRemote:
|
|
|
|
"""
|
|
|
|
Interface entry point. Wraps factory expression and handles errors
|
|
|
|
|
|
|
|
Returns a reference to an XAirRemote class of a kind
|
|
|
|
"""
|
2022-08-08 00:13:14 +01:00
|
|
|
XAIRREMOTE_cls = None
|
2022-08-07 23:55:51 +01:00
|
|
|
try:
|
2022-08-08 00:13:14 +01:00
|
|
|
XAIRREMOTE_cls = _remotes[kind_id]
|
2022-08-07 23:55:51 +01:00
|
|
|
except ValueError as e:
|
|
|
|
raise SystemExit(e)
|
2022-08-08 00:13:14 +01:00
|
|
|
return XAIRREMOTE_cls(*args, **kwargs)
|