mirror of
https://github.com/onyx-and-iris/vban-cmd-python.git
synced 2026-03-18 07:49:10 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba85373f94 | |||
| 22cc980fc2 | |||
|
|
2986d451ad | ||
| 96f0008654 | |||
|
|
6ddead68d0 | ||
| 2794b14cf1 | |||
| c46ca8a8c8 | |||
| f8b56b4a30 | |||
| 09259269d7 | |||
| 242401e294 | |||
| 98ec9b715f |
@ -548,7 +548,7 @@ You may pass the following optional keyword arguments:
|
|||||||
- `channel`: int=0, channel on which to send the UDP requests.
|
- `channel`: int=0, channel on which to send the UDP requests.
|
||||||
- `pdirty`: boolean=False, parameter updates
|
- `pdirty`: boolean=False, parameter updates
|
||||||
- `ldirty`: boolean=False, level updates
|
- `ldirty`: boolean=False, level updates
|
||||||
- `script_ratelimit`: float=0.05, default to 20 script requests per second. This affects vban.sendtext() specifically.
|
- `script_ratelimit`: float | None=None, ratelimit for vban.sendtext() specifically.
|
||||||
- `timeout`: int=5, timeout for socket operations.
|
- `timeout`: int=5, timeout for socket operations.
|
||||||
- `disable_rt_listeners`: boolean=False, set `True` if you don't wish to receive RT packets.
|
- `disable_rt_listeners`: boolean=False, set `True` if you don't wish to receive RT packets.
|
||||||
- You can still send Matrix string requests ending with `?` and receive a response.
|
- You can still send Matrix string requests ending with `?` and receive a response.
|
||||||
|
|||||||
42
poetry.lock
generated
42
poetry.lock
generated
@ -1,4 +1,4 @@
|
|||||||
# This file is automatically @generated by Poetry 2.0.1 and should not be changed by hand.
|
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cachetools"
|
name = "cachetools"
|
||||||
@ -55,7 +55,7 @@ description = "Backport of PEP 654 (exception groups)"
|
|||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.7"
|
python-versions = ">=3.7"
|
||||||
groups = ["dev"]
|
groups = ["dev"]
|
||||||
markers = "python_version < \"3.11\""
|
markers = "python_version == \"3.10\""
|
||||||
files = [
|
files = [
|
||||||
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
|
{file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
|
||||||
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
|
{file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
|
||||||
@ -66,21 +66,16 @@ test = ["pytest (>=6)"]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "filelock"
|
name = "filelock"
|
||||||
version = "3.16.1"
|
version = "3.20.3"
|
||||||
description = "A platform independent file lock."
|
description = "A platform independent file lock."
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.8"
|
python-versions = ">=3.10"
|
||||||
groups = ["dev"]
|
groups = ["dev"]
|
||||||
files = [
|
files = [
|
||||||
{file = "filelock-3.16.1-py3-none-any.whl", hash = "sha256:2082e5703d51fbf98ea75855d9d5527e33d8ff23099bec374a134febee6946b0"},
|
{file = "filelock-3.20.3-py3-none-any.whl", hash = "sha256:4b0dda527ee31078689fc205ec4f1c1bf7d56cf88b6dc9426c4f230e46c2dce1"},
|
||||||
{file = "filelock-3.16.1.tar.gz", hash = "sha256:c249fbfcd5db47e5e2d6d62198e565475ee65e4831e2561c8e313fa7eb961435"},
|
{file = "filelock-3.20.3.tar.gz", hash = "sha256:18c57ee915c7ec61cff0ecf7f0f869936c7c30191bb0cf406f1341778d0834e1"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.extras]
|
|
||||||
docs = ["furo (>=2024.8.6)", "sphinx (>=8.0.2)", "sphinx-autodoc-typehints (>=2.4.1)"]
|
|
||||||
testing = ["covdefaults (>=2.3)", "coverage (>=7.6.1)", "diff-cover (>=9.2)", "pytest (>=8.3.3)", "pytest-asyncio (>=0.24)", "pytest-cov (>=5)", "pytest-mock (>=3.14)", "pytest-timeout (>=2.3.1)", "virtualenv (>=20.26.4)"]
|
|
||||||
typing = ["typing-extensions (>=4.12.2)"]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "iniconfig"
|
name = "iniconfig"
|
||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
@ -243,7 +238,7 @@ description = "A lil' TOML parser"
|
|||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.8"
|
python-versions = ">=3.8"
|
||||||
groups = ["main", "dev"]
|
groups = ["main", "dev"]
|
||||||
markers = "python_version < \"3.11\""
|
markers = "python_version == \"3.10\""
|
||||||
files = [
|
files = [
|
||||||
{file = "tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249"},
|
{file = "tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249"},
|
||||||
{file = "tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6"},
|
{file = "tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6"},
|
||||||
@ -309,37 +304,38 @@ test = ["devpi-process (>=1.0.2)", "pytest (>=8.3.3)", "pytest-mock (>=3.14)"]
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typing-extensions"
|
name = "typing-extensions"
|
||||||
version = "4.12.2"
|
version = "4.15.0"
|
||||||
description = "Backported and Experimental Type Hints for Python 3.8+"
|
description = "Backported and Experimental Type Hints for Python 3.9+"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.8"
|
python-versions = ">=3.9"
|
||||||
groups = ["dev"]
|
groups = ["dev"]
|
||||||
markers = "python_version < \"3.11\""
|
markers = "python_version == \"3.10\""
|
||||||
files = [
|
files = [
|
||||||
{file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"},
|
{file = "typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548"},
|
||||||
{file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"},
|
{file = "typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "virtualenv"
|
name = "virtualenv"
|
||||||
version = "20.29.0"
|
version = "20.36.1"
|
||||||
description = "Virtual Python Environment builder"
|
description = "Virtual Python Environment builder"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.8"
|
python-versions = ">=3.8"
|
||||||
groups = ["dev"]
|
groups = ["dev"]
|
||||||
files = [
|
files = [
|
||||||
{file = "virtualenv-20.29.0-py3-none-any.whl", hash = "sha256:c12311863497992dc4b8644f8ea82d3b35bb7ef8ee82e6630d76d0197c39baf9"},
|
{file = "virtualenv-20.36.1-py3-none-any.whl", hash = "sha256:575a8d6b124ef88f6f51d56d656132389f961062a9177016a50e4f507bbcc19f"},
|
||||||
{file = "virtualenv-20.29.0.tar.gz", hash = "sha256:6345e1ff19d4b1296954cee076baaf58ff2a12a84a338c62b02eda39f20aa982"},
|
{file = "virtualenv-20.36.1.tar.gz", hash = "sha256:8befb5c81842c641f8ee658481e42641c68b5eab3521d8e092d18320902466ba"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
distlib = ">=0.3.7,<1"
|
distlib = ">=0.3.7,<1"
|
||||||
filelock = ">=3.12.2,<4"
|
filelock = {version = ">=3.20.1,<4", markers = "python_version >= \"3.10\""}
|
||||||
platformdirs = ">=3.9.1,<5"
|
platformdirs = ">=3.9.1,<5"
|
||||||
|
typing-extensions = {version = ">=4.13.2", markers = "python_version < \"3.11\""}
|
||||||
|
|
||||||
[package.extras]
|
[package.extras]
|
||||||
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.2,!=7.3)", "sphinx-argparse (>=0.4)", "sphinxcontrib-towncrier (>=0.2.1a0)", "towncrier (>=23.6)"]
|
docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.2,!=7.3)", "sphinx-argparse (>=0.4)", "sphinxcontrib-towncrier (>=0.2.1a0)", "towncrier (>=23.6)"]
|
||||||
test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7)", "packaging (>=23.1)", "pytest (>=7.4)", "pytest-env (>=0.8.2)", "pytest-freezer (>=0.4.8)", "pytest-mock (>=3.11.1)", "pytest-randomly (>=3.12)", "pytest-timeout (>=2.1)", "setuptools (>=68)", "time-machine (>=2.10)"]
|
test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess (>=1)", "flaky (>=3.7)", "packaging (>=23.1)", "pytest (>=7.4)", "pytest-env (>=0.8.2)", "pytest-freezer (>=0.4.8) ; platform_python_implementation == \"PyPy\" or platform_python_implementation == \"GraalVM\" or platform_python_implementation == \"CPython\" and sys_platform == \"win32\" and python_version >= \"3.13\"", "pytest-mock (>=3.11.1)", "pytest-randomly (>=3.12)", "pytest-timeout (>=2.1)", "setuptools (>=68)", "time-machine (>=2.10) ; platform_python_implementation == \"CPython\""]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "virtualenv-pyenv"
|
name = "virtualenv-pyenv"
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "vban-cmd"
|
name = "vban-cmd"
|
||||||
version = "2.10.0"
|
version = "2.10.3"
|
||||||
description = "Python interface for the VBAN RT Packet Service (Sendtext)"
|
description = "Python interface for the VBAN RT Packet Service (Sendtext)"
|
||||||
authors = [{ name = "Onyx and Iris", email = "code@onyxandiris.online" }]
|
authors = [{ name = "Onyx and Iris", email = "code@onyxandiris.online" }]
|
||||||
license = { text = "MIT" }
|
license = { text = "MIT" }
|
||||||
|
|||||||
@ -1,6 +1,18 @@
|
|||||||
|
from .packet.enums import ServiceTypes, SubProtocols
|
||||||
|
|
||||||
|
|
||||||
class VBANCMDError(Exception):
|
class VBANCMDError(Exception):
|
||||||
"""Base VBANCMD Exception class."""
|
"""Base VBANCMD Exception class."""
|
||||||
|
|
||||||
|
|
||||||
class VBANCMDConnectionError(VBANCMDError):
|
class VBANCMDConnectionError(VBANCMDError):
|
||||||
"""Exception raised when connection/timeout errors occur"""
|
"""Exception raised when connection/timeout errors occur"""
|
||||||
|
|
||||||
|
|
||||||
|
class VBANCMDPacketError(VBANCMDError):
|
||||||
|
"""Exception raised when packet parsing errors occur"""
|
||||||
|
|
||||||
|
def __init__(self, message: str, protocol: SubProtocols, type_: ServiceTypes):
|
||||||
|
super().__init__(message)
|
||||||
|
self.protocol = protocol
|
||||||
|
self.type = type_
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import abc
|
|
||||||
import logging
|
import logging
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
@ -89,7 +88,7 @@ class FactoryBase(VbanCmd):
|
|||||||
'streamname': 'Command1',
|
'streamname': 'Command1',
|
||||||
'bps': 256000,
|
'bps': 256000,
|
||||||
'channel': 0,
|
'channel': 0,
|
||||||
'script_ratelimit': 0.05, # 20 commands per second, to avoid overloading Voicemeeter
|
'script_ratelimit': None, # if None or 0, no rate limit applied to script commands
|
||||||
'timeout': 5, # timeout on socket operations, in seconds
|
'timeout': 5, # timeout on socket operations, in seconds
|
||||||
'disable_rt_listeners': False,
|
'disable_rt_listeners': False,
|
||||||
'sync': False,
|
'sync': False,
|
||||||
@ -122,11 +121,6 @@ class FactoryBase(VbanCmd):
|
|||||||
+ f"({self.kind}, ip='{self.ip}', port={self.port}, streamname='{self.streamname}')"
|
+ f"({self.kind}, ip='{self.ip}', port={self.port}, streamname='{self.streamname}')"
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
@abc.abstractmethod
|
|
||||||
def steps(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def configs(self):
|
def configs(self):
|
||||||
self._configs = configs(self.kind.name)
|
self._configs = configs(self.kind.name)
|
||||||
|
|||||||
@ -48,26 +48,18 @@ class IRemote(abc.ABC):
|
|||||||
def apply(self, data):
|
def apply(self, data):
|
||||||
"""Sets all parameters of a dict for the channel."""
|
"""Sets all parameters of a dict for the channel."""
|
||||||
|
|
||||||
script = ''
|
|
||||||
|
|
||||||
def fget(attr, val):
|
def fget(attr, val):
|
||||||
if attr == 'mode':
|
if attr == 'mode':
|
||||||
return (f'mode.{val}', 1)
|
return (getattr(self, attr), val, 1)
|
||||||
elif attr == 'knob':
|
return (self, attr, val)
|
||||||
return ('', val)
|
|
||||||
return (attr, val)
|
|
||||||
|
|
||||||
for attr, val in data.items():
|
for attr, val in data.items():
|
||||||
if not isinstance(val, dict):
|
if not isinstance(val, dict):
|
||||||
if attr in dir(self): # avoid calling getattr (with hasattr)
|
if attr in dir(self): # avoid calling getattr (with hasattr)
|
||||||
attr, val = fget(attr, val)
|
target, attr, val = fget(attr, val)
|
||||||
if isinstance(val, bool):
|
setattr(target, attr, val)
|
||||||
val = 1 if val else 0
|
else:
|
||||||
|
self.logger.error(f'invalid attribute {attr} for {self}')
|
||||||
self._remote.cache[self._cmd(attr)] = val
|
|
||||||
script += f'{self._cmd(attr)}={val};'
|
|
||||||
else:
|
else:
|
||||||
target = getattr(self, attr)
|
target = getattr(self, attr)
|
||||||
target.apply(val)
|
target.apply(val)
|
||||||
|
|
||||||
self._remote.sendtext(script)
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import struct
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from vban_cmd.enums import NBS
|
from vban_cmd.enums import NBS
|
||||||
|
from vban_cmd.error import VBANCMDPacketError
|
||||||
from vban_cmd.kinds import KindMapClass
|
from vban_cmd.kinds import KindMapClass
|
||||||
|
|
||||||
from .enums import ServiceTypes, StreamTypes, SubProtocols
|
from .enums import ServiceTypes, StreamTypes, SubProtocols
|
||||||
@ -10,6 +11,15 @@ PINGPONG_PACKET_SIZE = 704 # Size of the PING/PONG header + payload in bytes
|
|||||||
MAX_PACKET_SIZE = 1436
|
MAX_PACKET_SIZE = 1436
|
||||||
HEADER_SIZE = 4 + 1 + 1 + 1 + 1 + 16
|
HEADER_SIZE = 4 + 1 + 1 + 1 + 1 + 16
|
||||||
|
|
||||||
|
STREAMNAME_MAX_LENGTH = 16
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class VbanPingHeader:
|
class VbanPingHeader:
|
||||||
@ -28,7 +38,9 @@ class VbanPingHeader:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode('ascii')[:16].ljust(16, b'\x00')
|
return self.name.encode('ascii')[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_bytes(cls, framecounter: int = 0) -> bytes:
|
def to_bytes(cls, framecounter: int = 0) -> bytes:
|
||||||
@ -64,7 +76,9 @@ class VbanPongHeader:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode('ascii')[:16].ljust(16, b'\x00')
|
return self.name.encode('ascii')[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bytes(cls, data: bytes):
|
def from_bytes(cls, data: bytes):
|
||||||
@ -74,7 +88,11 @@ class VbanPongHeader:
|
|||||||
# PONG responses use the same service type as PING (0x00)
|
# PONG responses use the same service type as PING (0x00)
|
||||||
# and are identified by having payload data
|
# and are identified by having payload data
|
||||||
if parsed['format_nbc'] != ServiceTypes.PONG.value:
|
if parsed['format_nbc'] != ServiceTypes.PONG.value:
|
||||||
raise ValueError(f'Not a PONG response packet: {parsed["format_nbc"]:02x}')
|
raise VBANCMDPacketError(
|
||||||
|
f'Not a PONG response packet: {parsed["format_nbc"]:02x}',
|
||||||
|
protocol=SubProtocols(parsed['format_sr'] & SubProtocols.MASK.value),
|
||||||
|
type_=ServiceTypes(parsed['format_nbc']),
|
||||||
|
)
|
||||||
|
|
||||||
return cls(**parsed)
|
return cls(**parsed)
|
||||||
|
|
||||||
@ -133,7 +151,7 @@ class VbanRTPacket:
|
|||||||
class VbanRTSubscribeHeader:
|
class VbanRTSubscribeHeader:
|
||||||
"""Represents the header of an RT subscription packet"""
|
"""Represents the header of an RT subscription packet"""
|
||||||
|
|
||||||
nbs: NBS = NBS.zero
|
_nbs: NBS = NBS.zero
|
||||||
name: str = 'Register-RTP'
|
name: str = 'Register-RTP'
|
||||||
timeout: int = 15
|
timeout: int = 15
|
||||||
|
|
||||||
@ -142,36 +160,38 @@ class VbanRTSubscribeHeader:
|
|||||||
return b'VBAN'
|
return b'VBAN'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def format_sr(self) -> bytes:
|
def sr(self) -> int:
|
||||||
return SubProtocols.SERVICE.value.to_bytes(1, 'little')
|
return SubProtocols.SERVICE.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def format_nbs(self) -> bytes:
|
def nbs(self) -> int:
|
||||||
return (self.nbs.value & 0xFF).to_bytes(1, 'little')
|
return self._nbs.value & 0xFF
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def format_nbc(self) -> bytes:
|
def nbc(self) -> int:
|
||||||
return ServiceTypes.RTPACKETREGISTER.value.to_bytes(1, 'little')
|
return ServiceTypes.RTPACKETREGISTER.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def format_bit(self) -> bytes:
|
def bit(self) -> int:
|
||||||
return (self.timeout & 0xFF).to_bytes(1, 'little')
|
return self.timeout & 0xFF
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode('ascii') + bytes(16 - len(self.name))
|
return self.name.encode()[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_bytes(cls, nbs: NBS, framecounter: int) -> bytes:
|
def to_bytes(cls, nbs: NBS, framecounter: int) -> bytes:
|
||||||
header = cls(nbs=nbs)
|
header = cls(_nbs=nbs)
|
||||||
|
|
||||||
return struct.pack(
|
return struct.pack(
|
||||||
'<4s4B16sI',
|
'<4s4B16sI',
|
||||||
header.vban,
|
header.vban,
|
||||||
header.format_sr[0],
|
header.sr,
|
||||||
header.format_nbs[0],
|
header.nbs,
|
||||||
header.format_nbc[0],
|
header.nbc,
|
||||||
header.format_bit[0],
|
header.bit,
|
||||||
header.streamname,
|
header.streamname,
|
||||||
framecounter,
|
framecounter,
|
||||||
)
|
)
|
||||||
@ -182,59 +202,64 @@ class VbanRTRequestHeader:
|
|||||||
"""Represents the header of an RT request packet"""
|
"""Represents the header of an RT request packet"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
bps_index: int
|
bps: int
|
||||||
channel: int
|
channel: int
|
||||||
framecounter: int = 0
|
framecounter: int = 0
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.bps not in BPS_OPTS:
|
||||||
|
raise ValueError(
|
||||||
|
f'Invalid bps value: {self.bps}. Must be one of {BPS_OPTS}'
|
||||||
|
)
|
||||||
|
self.bps_index = BPS_OPTS.index(self.bps)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def vban(self) -> bytes:
|
def vban(self) -> bytes:
|
||||||
return b'VBAN'
|
return b'VBAN'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sr(self) -> bytes:
|
def sr(self) -> int:
|
||||||
return (self.bps_index | SubProtocols.TEXT.value).to_bytes(1, 'little')
|
return self.bps_index | SubProtocols.TEXT.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def nbs(self) -> bytes:
|
def nbs(self) -> int:
|
||||||
return (0).to_bytes(1, 'little')
|
return 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def nbc(self) -> bytes:
|
def nbc(self) -> int:
|
||||||
return (self.channel).to_bytes(1, 'little')
|
return self.channel
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def bit(self) -> bytes:
|
def bit(self) -> int:
|
||||||
return (StreamTypes.UTF8.value).to_bytes(1, 'little')
|
return StreamTypes.UTF8.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode()[:16].ljust(16, b'\x00')
|
return self.name.encode()[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_bytes(
|
def to_bytes(cls, name: str, bps: int, channel: int, framecounter: int) -> bytes:
|
||||||
cls, name: str, bps_index: int, channel: int, framecounter: int
|
header = cls(name=name, bps=bps, channel=channel, framecounter=framecounter)
|
||||||
) -> bytes:
|
|
||||||
header = cls(
|
|
||||||
name=name, bps_index=bps_index, channel=channel, framecounter=framecounter
|
|
||||||
)
|
|
||||||
|
|
||||||
return struct.pack(
|
return struct.pack(
|
||||||
'<4s4B16sI',
|
'<4s4B16sI',
|
||||||
header.vban,
|
header.vban,
|
||||||
header.sr[0],
|
header.sr,
|
||||||
header.nbs[0],
|
header.nbs,
|
||||||
header.nbc[0],
|
header.nbc,
|
||||||
header.bit[0],
|
header.bit,
|
||||||
header.streamname,
|
header.streamname,
|
||||||
header.framecounter,
|
header.framecounter,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def encode_with_payload(
|
def encode_with_payload(
|
||||||
cls, name: str, bps_index: int, channel: int, framecounter: int, payload: str
|
cls, name: str, bps: int, channel: int, framecounter: int, payload: str
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
"""Creates the complete packet with header and payload."""
|
"""Creates the complete packet with header and payload."""
|
||||||
return cls.to_bytes(name, bps_index, channel, framecounter) + payload.encode()
|
return cls.to_bytes(name, bps, channel, framecounter) + payload.encode()
|
||||||
|
|
||||||
|
|
||||||
def _parse_vban_service_header(data: bytes) -> dict:
|
def _parse_vban_service_header(data: bytes) -> dict:
|
||||||
@ -253,7 +278,10 @@ def _parse_vban_service_header(data: bytes) -> dict:
|
|||||||
# Verify this is a service protocol packet
|
# Verify this is a service protocol packet
|
||||||
protocol = format_sr & SubProtocols.MASK.value
|
protocol = format_sr & SubProtocols.MASK.value
|
||||||
if protocol != SubProtocols.SERVICE.value:
|
if protocol != SubProtocols.SERVICE.value:
|
||||||
raise ValueError(f'Not a service protocol packet: {protocol:02x}')
|
raise VBANCMDPacketError(
|
||||||
|
f'Invalid protocol in service header: {protocol:02x}',
|
||||||
|
protocol=SubProtocols(protocol),
|
||||||
|
)
|
||||||
|
|
||||||
# Extract stream name and frame counter
|
# Extract stream name and frame counter
|
||||||
name = data[8:24].rstrip(b'\x00').decode('utf-8', errors='ignore')
|
name = data[8:24].rstrip(b'\x00').decode('utf-8', errors='ignore')
|
||||||
@ -286,7 +314,9 @@ class VbanRTResponseHeader:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode('ascii') + bytes(16 - len(self.name))
|
return self.name.encode()[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bytes(cls, data: bytes):
|
def from_bytes(cls, data: bytes):
|
||||||
@ -295,8 +325,10 @@ class VbanRTResponseHeader:
|
|||||||
|
|
||||||
# Validate this is an RTPacket response
|
# Validate this is an RTPacket response
|
||||||
if parsed['format_nbc'] != ServiceTypes.RTPACKET.value:
|
if parsed['format_nbc'] != ServiceTypes.RTPACKET.value:
|
||||||
raise ValueError(
|
raise VBANCMDPacketError(
|
||||||
f'Not an RTPacket response packet: {parsed["format_nbc"]:02x}'
|
f'Not an RTPacket response packet: {parsed["format_nbc"]:02x}',
|
||||||
|
protocol=SubProtocols(parsed['format_sr'] & SubProtocols.MASK.value),
|
||||||
|
type_=ServiceTypes(parsed['format_nbc']),
|
||||||
)
|
)
|
||||||
|
|
||||||
return cls(**parsed)
|
return cls(**parsed)
|
||||||
@ -319,16 +351,29 @@ class VbanMatrixResponseHeader:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode('ascii')[:16].ljust(16, b'\x00')
|
return self.name.encode()[:STREAMNAME_MAX_LENGTH].ljust(
|
||||||
|
STREAMNAME_MAX_LENGTH, b'\x00'
|
||||||
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bytes(cls, data: bytes):
|
def from_bytes(cls, data: bytes):
|
||||||
"""Parse a matrix response packet from bytes."""
|
"""Parse a matrix response packet from bytes."""
|
||||||
parsed = _parse_vban_service_header(data)
|
parsed = _parse_vban_service_header(data)
|
||||||
|
|
||||||
# Validate this is a service reply packet
|
# Validate this is a service reply packet (dual encoding scheme)
|
||||||
if parsed['format_nbs'] != ServiceTypes.FNCT_REPLY.value:
|
if parsed['format_nbs'] != ServiceTypes.FNCT_REPLY.value:
|
||||||
raise ValueError(f'Not a service reply packet: {parsed["format_nbs"]:02x}')
|
raise VBANCMDPacketError(
|
||||||
|
f'Not a service reply packet: {parsed["format_nbs"]:02x}',
|
||||||
|
protocol=SubProtocols(parsed['format_sr'] & SubProtocols.MASK.value),
|
||||||
|
type_=ServiceTypes(parsed['format_nbs']),
|
||||||
|
)
|
||||||
|
|
||||||
|
if parsed['format_nbc'] != ServiceTypes.REQUESTREPLY.value:
|
||||||
|
raise VBANCMDPacketError(
|
||||||
|
f'Not a request reply packet: {parsed["format_nbc"]:02x}',
|
||||||
|
protocol=SubProtocols(parsed['format_sr'] & SubProtocols.MASK.value),
|
||||||
|
type_=ServiceTypes(parsed['format_nbc']),
|
||||||
|
)
|
||||||
|
|
||||||
return cls(**parsed)
|
return cls(**parsed)
|
||||||
|
|
||||||
|
|||||||
@ -5,12 +5,12 @@ from typing import Iterator
|
|||||||
from .error import VBANCMDConnectionError
|
from .error import VBANCMDConnectionError
|
||||||
|
|
||||||
|
|
||||||
def ratelimit(func):
|
def script_ratelimit(func):
|
||||||
"""ratelimit decorator for {VbanCmd}.sendtext, to prevent flooding the network with script requests."""
|
"""script_ratelimit decorator for {VbanCmd}.sendtext, to prevent flooding the network with script requests."""
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
self, *rem = args
|
self, *rem = args
|
||||||
if self.script_ratelimit > 0:
|
if self.script_ratelimit:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
elapsed = now - self._last_script_request_time
|
elapsed = now - self._last_script_request_time
|
||||||
if elapsed < self.script_ratelimit:
|
if elapsed < self.script_ratelimit:
|
||||||
@ -124,16 +124,11 @@ def comp(t0: tuple, t1: tuple) -> Iterator[bool]:
|
|||||||
"""
|
"""
|
||||||
Generator function, accepts two tuples of dB values.
|
Generator function, accepts two tuples of dB values.
|
||||||
|
|
||||||
Evaluates equality of each member in both tuples.
|
Returns True when levels are equal (no change), False when different.
|
||||||
Only ignores changes when levels are very quiet (below -72 dB).
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for a, b in zip(t0, t1):
|
for a, b in zip(t0, t1):
|
||||||
# If both values are very quiet (below -72dB), ignore small changes
|
yield a == b
|
||||||
if a <= -72.0 and b <= -72.0:
|
|
||||||
yield a == b # Both quiet, check if they're equal
|
|
||||||
else:
|
|
||||||
yield a != b # At least one has significant level, detect changes
|
|
||||||
|
|
||||||
|
|
||||||
def deep_merge(dict1, dict2):
|
def deep_merge(dict1, dict2):
|
||||||
|
|||||||
@ -17,7 +17,7 @@ from .packet.headers import (
|
|||||||
)
|
)
|
||||||
from .packet.ping0 import VbanPing0Payload, VbanServerType
|
from .packet.ping0 import VbanPing0Payload, VbanServerType
|
||||||
from .subject import Subject
|
from .subject import Subject
|
||||||
from .util import bump_framecounter, deep_merge, pong_timeout, ratelimit
|
from .util import bump_framecounter, deep_merge, pong_timeout, script_ratelimit
|
||||||
from .worker import Producer, Subscriber, Updater
|
from .worker import Producer, Subscriber, Updater
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -27,13 +27,6 @@ class VbanCmd(abc.ABC):
|
|||||||
"""Abstract Base Class for Voicemeeter VBAN Command Interfaces"""
|
"""Abstract Base Class for Voicemeeter VBAN Command Interfaces"""
|
||||||
|
|
||||||
DELAY = 0.001
|
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
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.logger = logger.getChild(self.__class__.__name__)
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
@ -43,6 +36,13 @@ class VbanCmd(abc.ABC):
|
|||||||
for attr, val in kwargs.items():
|
for attr, val in kwargs.items():
|
||||||
setattr(self, attr, val)
|
setattr(self, attr, val)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._host_ip = socket.gethostbyname(self.host)
|
||||||
|
except socket.gaierror as e:
|
||||||
|
raise VBANCMDConnectionError(
|
||||||
|
f'Unable to resolve hostname {self.host}'
|
||||||
|
) from e
|
||||||
|
|
||||||
self._framecounter = 0
|
self._framecounter = 0
|
||||||
self._framecounter_lock = threading.Lock()
|
self._framecounter_lock = threading.Lock()
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
@ -56,9 +56,10 @@ class VbanCmd(abc.ABC):
|
|||||||
self.producer = None
|
self.producer = None
|
||||||
self._last_script_request_time = 0
|
self._last_script_request_time = 0
|
||||||
|
|
||||||
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def __str__(self):
|
def steps(self):
|
||||||
"""Ensure subclasses override str magic method"""
|
"""Steps required to build the interface for this Voicemeeter kind"""
|
||||||
|
|
||||||
def _conn_from_toml(self) -> dict:
|
def _conn_from_toml(self) -> dict:
|
||||||
try:
|
try:
|
||||||
@ -143,14 +144,10 @@ class VbanCmd(abc.ABC):
|
|||||||
try:
|
try:
|
||||||
self.sock.sendto(
|
self.sock.sendto(
|
||||||
VbanPing0Payload.create_packet(self._get_next_framecounter()),
|
VbanPing0Payload.create_packet(self._get_next_framecounter()),
|
||||||
(socket.gethostbyname(self.host), self.port),
|
(self._host_ip, self.port),
|
||||||
)
|
)
|
||||||
self.logger.debug(f'PING sent to {self.host}:{self.port}')
|
self.logger.debug(f'PING sent to {self.host}:{self.port}')
|
||||||
|
|
||||||
except socket.gaierror as e:
|
|
||||||
raise VBANCMDConnectionError(
|
|
||||||
f'Unable to resolve hostname {self.host}'
|
|
||||||
) from e
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise VBANCMDConnectionError(f'PING failed: {e}') from e
|
raise VBANCMDConnectionError(f'PING failed: {e}') from e
|
||||||
|
|
||||||
@ -203,12 +200,12 @@ class VbanCmd(abc.ABC):
|
|||||||
self.sock.sendto(
|
self.sock.sendto(
|
||||||
VbanRTRequestHeader.encode_with_payload(
|
VbanRTRequestHeader.encode_with_payload(
|
||||||
name=self.streamname,
|
name=self.streamname,
|
||||||
bps_index=self.BPS_OPTS.index(self.bps),
|
bps=self.bps,
|
||||||
channel=self.channel,
|
channel=self.channel,
|
||||||
framecounter=self._get_next_framecounter(),
|
framecounter=self._get_next_framecounter(),
|
||||||
payload=payload,
|
payload=payload,
|
||||||
),
|
),
|
||||||
(socket.gethostbyname(self.host), self.port),
|
(self._host_ip, self.port),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _set_rt(self, cmd: str, val: Union[str, float]):
|
def _set_rt(self, cmd: str, val: Union[str, float]):
|
||||||
@ -216,7 +213,7 @@ class VbanCmd(abc.ABC):
|
|||||||
self._send_request(f'{cmd}={val};')
|
self._send_request(f'{cmd}={val};')
|
||||||
self.cache[cmd] = val
|
self.cache[cmd] = val
|
||||||
|
|
||||||
@ratelimit
|
@script_ratelimit
|
||||||
def sendtext(self, script) -> str | None:
|
def sendtext(self, script) -> str | None:
|
||||||
"""Sends a multiple parameter string over a network."""
|
"""Sends a multiple parameter string over a network."""
|
||||||
self._send_request(script)
|
self._send_request(script)
|
||||||
|
|||||||
@ -3,7 +3,8 @@ import threading
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from .enums import NBS
|
from .enums import NBS
|
||||||
from .error import VBANCMDConnectionError
|
from .error import VBANCMDConnectionError, VBANCMDPacketError
|
||||||
|
from .packet.enums import SubProtocols
|
||||||
from .packet.headers import (
|
from .packet.headers import (
|
||||||
HEADER_SIZE,
|
HEADER_SIZE,
|
||||||
VbanRTPacket,
|
VbanRTPacket,
|
||||||
@ -32,7 +33,7 @@ class Subscriber(threading.Thread):
|
|||||||
nbs, self._remote._get_next_framecounter()
|
nbs, self._remote._get_next_framecounter()
|
||||||
)
|
)
|
||||||
self._remote.sock.sendto(
|
self._remote.sock.sendto(
|
||||||
sub_packet, (self._remote.host, self._remote.port)
|
sub_packet, (self._remote._host_ip, self._remote.port)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.wait_until_stopped(10)
|
self.wait_until_stopped(10)
|
||||||
@ -81,7 +82,12 @@ class Producer(threading.Thread):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
header = VbanRTResponseHeader.from_bytes(data[:HEADER_SIZE])
|
header = VbanRTResponseHeader.from_bytes(data[:HEADER_SIZE])
|
||||||
except ValueError as e:
|
except VBANCMDPacketError as e:
|
||||||
|
match e.protocol:
|
||||||
|
case SubProtocols.SERVICE:
|
||||||
|
# Silently ignore periodic SERVICE packets unrelated to vban-cmd
|
||||||
|
pass
|
||||||
|
case _:
|
||||||
self.logger.debug(f'Error parsing response packet: {e}')
|
self.logger.debug(f'Error parsing response packet: {e}')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user