2022-09-28 18:13:07 +01:00
|
|
|
import logging
|
2022-06-16 16:10:06 +01:00
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
from abc import ABCMeta, abstractmethod
|
2022-10-07 20:00:56 +01:00
|
|
|
from pathlib import Path
|
2022-08-10 17:49:21 +01:00
|
|
|
from typing import Iterable, Optional, Union
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-10-07 20:00:56 +01:00
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
import tomli as tomllib
|
|
|
|
|
2022-09-28 18:03:22 +01:00
|
|
|
from .event import Event
|
2022-08-10 17:49:21 +01:00
|
|
|
from .packet import RequestHeader
|
2022-06-16 16:10:06 +01:00
|
|
|
from .subject import Subject
|
2022-10-04 15:42:36 +01:00
|
|
|
from .util import Socket, script
|
2022-08-08 13:43:19 +01:00
|
|
|
from .worker import Subscriber, Updater
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VbanCmd(metaclass=ABCMeta):
|
2022-08-10 17:49:21 +01:00
|
|
|
"""Base class responsible for communicating with the VBAN RT Packet Service"""
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
DELAY = 0.001
|
|
|
|
# fmt: off
|
|
|
|
BPS_OPTS = [
|
|
|
|
0, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 31250,
|
|
|
|
38400, 57600, 115200, 128000, 230400, 250000, 256000, 460800, 921600,
|
|
|
|
1000000, 1500000, 2000000, 3000000,
|
|
|
|
]
|
|
|
|
# fmt: on
|
2022-09-28 18:13:07 +01:00
|
|
|
logger = logging.getLogger("vbancmd.vbancmd")
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for attr, val in kwargs.items():
|
|
|
|
setattr(self, attr, val)
|
2022-10-07 20:00:56 +01:00
|
|
|
if self.ip is None:
|
|
|
|
conn = self._conn_from_toml()
|
|
|
|
for attr, val in conn.items():
|
|
|
|
setattr(self, attr, val)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-08-10 17:49:21 +01:00
|
|
|
self.packet_request = RequestHeader(
|
2022-06-16 16:10:06 +01:00
|
|
|
name=self.streamname,
|
|
|
|
bps_index=self.BPS_OPTS.index(self.bps),
|
|
|
|
channel=self.channel,
|
|
|
|
)
|
|
|
|
self.socks = tuple(
|
2022-06-18 11:12:09 +01:00
|
|
|
socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for _ in Socket
|
2022-06-16 16:10:06 +01:00
|
|
|
)
|
|
|
|
self.subject = Subject()
|
2022-10-04 15:42:36 +01:00
|
|
|
self.cache = {}
|
2022-08-02 09:28:32 +01:00
|
|
|
self.event = Event(self.subs)
|
2022-10-19 14:20:23 +01:00
|
|
|
self._pdirty = False
|
2022-10-19 14:32:54 +01:00
|
|
|
self._ldirty = False
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __str__(self):
|
|
|
|
"""Ensure subclasses override str magic method"""
|
|
|
|
pass
|
|
|
|
|
2022-10-07 20:00:56 +01:00
|
|
|
def _conn_from_toml(self) -> str:
|
|
|
|
filepath = Path.cwd() / "vban.toml"
|
|
|
|
with open(filepath, "rb") as f:
|
|
|
|
conn = tomllib.load(f)
|
|
|
|
return conn["connection"]
|
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
def __enter__(self):
|
|
|
|
self.login()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def login(self):
|
2022-08-08 13:43:19 +01:00
|
|
|
"""Starts the subscriber and updater threads"""
|
|
|
|
self.running = True
|
2022-09-29 11:48:30 +01:00
|
|
|
self.event.info()
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-08-08 13:43:19 +01:00
|
|
|
self.subscriber = Subscriber(self)
|
|
|
|
self.subscriber.start()
|
|
|
|
|
|
|
|
self.updater = Updater(self)
|
|
|
|
self.updater.start()
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-09-28 18:13:07 +01:00
|
|
|
self.logger.info(f"{type(self).__name__}: Successfully logged into {self}")
|
|
|
|
|
2022-06-16 16:10:06 +01:00
|
|
|
def _set_rt(
|
|
|
|
self,
|
|
|
|
id_: str,
|
|
|
|
param: Optional[str] = None,
|
|
|
|
val: Optional[Union[int, float]] = None,
|
|
|
|
):
|
|
|
|
"""Sends a string request command over a network."""
|
2022-10-19 14:20:23 +01:00
|
|
|
cmd = id_ if not param else f"{id_}.{param}={val};"
|
2022-08-08 13:43:19 +01:00
|
|
|
self.socks[Socket.request].sendto(
|
2022-08-10 17:49:21 +01:00
|
|
|
self.packet_request.header + cmd.encode(),
|
2022-06-16 16:10:06 +01:00
|
|
|
(socket.gethostbyname(self.ip), self.port),
|
|
|
|
)
|
2022-08-10 17:49:21 +01:00
|
|
|
count = int.from_bytes(self.packet_request.framecounter, "little") + 1
|
|
|
|
self.packet_request.framecounter = count.to_bytes(4, "little")
|
2022-06-16 16:10:06 +01:00
|
|
|
if param:
|
|
|
|
self.cache[f"{id_}.{param}"] = val
|
|
|
|
|
|
|
|
@script
|
|
|
|
def sendtext(self, cmd):
|
|
|
|
"""Sends a multiple parameter string over a network."""
|
|
|
|
self._set_rt(cmd)
|
|
|
|
time.sleep(self.DELAY)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
"""Returns the type of Voicemeeter installation."""
|
|
|
|
return self.public_packet.voicemeetertype
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self) -> str:
|
2022-06-17 17:52:09 +01:00
|
|
|
"""Returns Voicemeeter's version as a string"""
|
2022-10-04 15:42:36 +01:00
|
|
|
return "{0}.{1}.{2}.{3}".format(*self.public_packet.voicemeeterversion)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pdirty(self):
|
|
|
|
"""True iff a parameter has changed"""
|
2022-07-07 00:48:15 +01:00
|
|
|
return self._pdirty
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def ldirty(self):
|
|
|
|
"""True iff a level value has changed."""
|
2022-10-04 15:42:36 +01:00
|
|
|
return self._ldirty
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def public_packet(self):
|
|
|
|
return self._public_packet
|
|
|
|
|
|
|
|
def clear_dirty(self):
|
|
|
|
while self.pdirty:
|
2022-10-19 14:20:23 +01:00
|
|
|
time.sleep(self.DELAY)
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-07-06 13:40:46 +01:00
|
|
|
def _get_levels(self, packet) -> Iterable:
|
|
|
|
"""
|
|
|
|
returns both level arrays (strip_levels, bus_levels) BEFORE math conversion
|
2022-06-16 16:10:06 +01:00
|
|
|
|
2022-07-06 13:40:46 +01:00
|
|
|
strip levels in PREFADER mode.
|
|
|
|
"""
|
|
|
|
return (
|
2022-10-04 15:42:36 +01:00
|
|
|
packet.inputlevels,
|
|
|
|
packet.outputlevels,
|
2022-06-16 16:10:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def apply(self, data: dict):
|
|
|
|
"""
|
|
|
|
Sets all parameters of a dict
|
|
|
|
|
|
|
|
minor delay between each recursion
|
|
|
|
"""
|
|
|
|
|
|
|
|
def param(key):
|
|
|
|
obj, m2, *rem = key.split("-")
|
|
|
|
index = int(m2) if m2.isnumeric() else int(*rem)
|
|
|
|
if obj in ("strip", "bus"):
|
|
|
|
return getattr(self, obj)[index]
|
|
|
|
else:
|
|
|
|
raise ValueError(obj)
|
|
|
|
|
|
|
|
[param(key).apply(datum).then_wait() for key, datum in data.items()]
|
|
|
|
|
|
|
|
def apply_config(self, name):
|
|
|
|
"""applies a config from memory"""
|
|
|
|
error_msg = (
|
|
|
|
f"No config with name '{name}' is loaded into memory",
|
|
|
|
f"Known configs: {list(self.configs.keys())}",
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
self.apply(self.configs[name])
|
2022-09-29 12:34:02 +01:00
|
|
|
self.logger.info(f"Profile '{name}' applied!")
|
2022-06-16 16:10:06 +01:00
|
|
|
except KeyError as e:
|
2022-09-29 12:34:02 +01:00
|
|
|
self.logger.error(("\n").join(error_msg))
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
def logout(self):
|
|
|
|
self.running = False
|
|
|
|
time.sleep(0.2)
|
|
|
|
[sock.close() for sock in self.socks]
|
2022-09-29 11:48:30 +01:00
|
|
|
self.logger.info(f"{type(self).__name__}: Successfully logged out of {self}")
|
2022-06-16 16:10:06 +01:00
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
self.logout()
|