mirror of
https://github.com/onyx-and-iris/vban-cmd-python.git
synced 2026-04-07 16:13:30 +00:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dd52a7258 | |||
| 28cbef5ef6 | |||
| 5b3b35fca3 | |||
| 7b3149a1e1 | |||
| 230d9f0eb3 | |||
| c9a505df0a | |||
| 3e3bec6d50 | |||
| 55b3125e10 | |||
| 7b3340042c | |||
| 6ea0859180 | |||
| 81ed963bea | |||
| 0b99b6a67f | |||
| 86d0aa91c3 | |||
| cf66ae252c | |||
| 42f6f29d1e | |||
| a210766b7b | |||
| 7d741d6e8b | |||
| 8be9d3cb7f | |||
| 23b99cb66b |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -159,3 +159,5 @@ config.toml
|
|||||||
vban.toml
|
vban.toml
|
||||||
|
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
PING_FEATURE.md
|
||||||
10
CHANGELOG.md
10
CHANGELOG.md
@@ -11,6 +11,16 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [x]
|
||||||
|
|
||||||
|
## [2.9.0] - 2026-03-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Recorder class, see [Recorder](https://github.com/onyx-and-iris/vban-cmd-python?tab=readme-ov-file#recorder) in README.
|
||||||
|
- Ping/pong implemented. If a pong is not received {VbanCmd}.login() will fail fast. This prevents the rt listener threads from starting up.
|
||||||
|
- It has the added benefit of automatically detecting the type of VBAN server (Voicemeeter or Matrix).
|
||||||
|
- A thread lock around the framecounter to improve thread safety since it can be accessed by both the main thread and the Producer thread.
|
||||||
|
|
||||||
|
|
||||||
## [2.7.0] - 2026-03-01
|
## [2.7.0] - 2026-03-01
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
15
README.md
15
README.md
@@ -41,14 +41,14 @@ Load VBAN connection info from toml config. A valid `vban.toml` might look like
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
[connection]
|
[connection]
|
||||||
ip = "gamepc.local"
|
host = "localhost"
|
||||||
port = 6980
|
port = 6980
|
||||||
streamname = "Command1"
|
streamname = "Command1"
|
||||||
```
|
```
|
||||||
|
|
||||||
It should be placed in \<user home directory\> / "Documents" / "Voicemeeter" / "configs"
|
It should be placed in \<user home directory\> / "Documents" / "Voicemeeter" / "configs"
|
||||||
|
|
||||||
Alternatively you may pass `ip`, `port`, `streamname` as keyword arguments.
|
Alternatively you may pass `host`, `port`, `streamname` as keyword arguments.
|
||||||
|
|
||||||
#### `__main__.py`
|
#### `__main__.py`
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ def main():
|
|||||||
KIND_ID = 'banana'
|
KIND_ID = 'banana'
|
||||||
|
|
||||||
with vban_cmd.api(
|
with vban_cmd.api(
|
||||||
KIND_ID, ip='gamepc.local', port=6980, streamname='Command1'
|
KIND_ID, host='localhost', port=6980, streamname='Command1'
|
||||||
) as vban:
|
) as vban:
|
||||||
do = ManyThings(vban)
|
do = ManyThings(vban)
|
||||||
do.things()
|
do.things()
|
||||||
@@ -474,7 +474,7 @@ example:
|
|||||||
import vban_cmd
|
import vban_cmd
|
||||||
|
|
||||||
opts = {
|
opts = {
|
||||||
'ip': '<ip address>',
|
'host': '<ip address>',
|
||||||
'streamname': 'Command1',
|
'streamname': 'Command1',
|
||||||
'port': 6980,
|
'port': 6980,
|
||||||
}
|
}
|
||||||
@@ -541,14 +541,15 @@ print(vban.event.get())
|
|||||||
|
|
||||||
You may pass the following optional keyword arguments:
|
You may pass the following optional keyword arguments:
|
||||||
|
|
||||||
- `ip`: str='localhost', ip or hostname of remote machine
|
- `host`: str='localhost', ip or hostname of remote machine
|
||||||
- `port`: int=6980, vban udp port of remote machine.
|
- `port`: int=6980, vban udp port of remote machine.
|
||||||
- `streamname`: str='Command1', name of the stream to connect to.
|
- `streamname`: str='Command1', name of the stream to connect to.
|
||||||
- `bps`: int=256000, bps rate of the stream.
|
- `bps`: int=256000, bps rate of the stream.
|
||||||
- `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
|
||||||
- `timeout`: int=5, amount of time (seconds) to wait for an incoming RT data packet (parameter states).
|
- `script_ratelimit`: float=0.05, default to 20 script requests per second. This affects vban.sendtext() specifically.
|
||||||
|
- `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.
|
||||||
|
|
||||||
@@ -591,7 +592,7 @@ import vban_cmd
|
|||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
opts = {'ip': 'ip.local', 'port': 6980, 'streamname': 'Command1'}
|
opts = {'host': 'localhost', 'port': 6980, 'streamname': 'Command1'}
|
||||||
with vban_cmd.api('banana', **opts) as vban:
|
with vban_cmd.api('banana', **opts) as vban:
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "vban-cmd"
|
name = "vban-cmd"
|
||||||
version = "2.8.0"
|
version = "2.9.5"
|
||||||
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" }
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from vban_cmd.kinds import request_kind_map as kindmap
|
|||||||
KIND_ID = os.environ.get('KIND', 'potato')
|
KIND_ID = os.environ.get('KIND', 'potato')
|
||||||
|
|
||||||
opts = {
|
opts = {
|
||||||
'ip': os.getenv('VBANCMD_IP', 'localhost'),
|
'host': os.getenv('VBANCMD_HOST', 'localhost'),
|
||||||
'streamname': os.getenv('VBANCMD_STREAMNAME', 'Command1'),
|
'streamname': os.getenv('VBANCMD_STREAMNAME', 'Command1'),
|
||||||
'port': int(os.getenv('VBANCMD_PORT', 6980)),
|
'port': int(os.getenv('VBANCMD_PORT', 6980)),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,18 +84,20 @@ class FactoryBase(VbanCmd):
|
|||||||
|
|
||||||
def __init__(self, kind_id: str, **kwargs):
|
def __init__(self, kind_id: str, **kwargs):
|
||||||
defaultkwargs = {
|
defaultkwargs = {
|
||||||
'ip': 'localhost',
|
'host': 'localhost',
|
||||||
'port': 6980,
|
'port': 6980,
|
||||||
'streamname': 'Command1',
|
'streamname': 'Command1',
|
||||||
'bps': 256000,
|
'bps': 256000,
|
||||||
'channel': 0,
|
'channel': 0,
|
||||||
'ratelimit': 0.01,
|
'script_ratelimit': 0.05, # 20 commands per second, to avoid overloading Voicemeeter
|
||||||
'timeout': 5,
|
'timeout': 5, # timeout on socket operations, in seconds
|
||||||
'disable_rt_listeners': False,
|
'disable_rt_listeners': False,
|
||||||
'sync': False,
|
'sync': False,
|
||||||
'pdirty': False,
|
'pdirty': False,
|
||||||
'ldirty': False,
|
'ldirty': False,
|
||||||
}
|
}
|
||||||
|
if 'ip' in kwargs:
|
||||||
|
defaultkwargs['host'] = kwargs.pop('ip') # for backwards compatibility
|
||||||
if 'subs' in kwargs:
|
if 'subs' in kwargs:
|
||||||
defaultkwargs |= kwargs.pop('subs') # for backwards compatibility
|
defaultkwargs |= kwargs.pop('subs') # for backwards compatibility
|
||||||
kwargs = defaultkwargs | kwargs
|
kwargs = defaultkwargs | kwargs
|
||||||
|
|||||||
@@ -1,83 +1,9 @@
|
|||||||
import abc
|
import abc
|
||||||
import logging
|
import logging
|
||||||
import time
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Modes:
|
|
||||||
"""Channel Modes"""
|
|
||||||
|
|
||||||
_mute: hex = 0x00000001
|
|
||||||
_solo: hex = 0x00000002
|
|
||||||
_mono: hex = 0x00000004
|
|
||||||
_mc: hex = 0x00000008
|
|
||||||
|
|
||||||
_amix: hex = 0x00000010
|
|
||||||
_repeat: hex = 0x00000020
|
|
||||||
_bmix: hex = 0x00000030
|
|
||||||
_composite: hex = 0x00000040
|
|
||||||
_tvmix: hex = 0x00000050
|
|
||||||
_upmix21: hex = 0x00000060
|
|
||||||
_upmix41: hex = 0x00000070
|
|
||||||
_upmix61: hex = 0x00000080
|
|
||||||
_centeronly: hex = 0x00000090
|
|
||||||
_lfeonly: hex = 0x000000A0
|
|
||||||
_rearonly: hex = 0x000000B0
|
|
||||||
|
|
||||||
_mask: hex = 0x000000F0
|
|
||||||
|
|
||||||
_on: hex = 0x00000100 # eq.on
|
|
||||||
_cross: hex = 0x00000200
|
|
||||||
_ab: hex = 0x00000800 # eq.ab
|
|
||||||
|
|
||||||
_busa: hex = 0x00001000
|
|
||||||
_busa1: hex = 0x00001000
|
|
||||||
_busa2: hex = 0x00002000
|
|
||||||
_busa3: hex = 0x00004000
|
|
||||||
_busa4: hex = 0x00008000
|
|
||||||
_busa5: hex = 0x00080000
|
|
||||||
|
|
||||||
_busb: hex = 0x00010000
|
|
||||||
_busb1: hex = 0x00010000
|
|
||||||
_busb2: hex = 0x00020000
|
|
||||||
_busb3: hex = 0x00040000
|
|
||||||
|
|
||||||
_pan0: hex = 0x00000000
|
|
||||||
_pancolor: hex = 0x00100000
|
|
||||||
_panmod: hex = 0x00200000
|
|
||||||
_panmask: hex = 0x00F00000
|
|
||||||
|
|
||||||
_postfx_r: hex = 0x01000000
|
|
||||||
_postfx_d: hex = 0x02000000
|
|
||||||
_postfx1: hex = 0x04000000
|
|
||||||
_postfx2: hex = 0x08000000
|
|
||||||
|
|
||||||
_sel: hex = 0x10000000
|
|
||||||
_monitor: hex = 0x20000000
|
|
||||||
|
|
||||||
@property
|
|
||||||
def modevals(self):
|
|
||||||
return (
|
|
||||||
val
|
|
||||||
for val in [
|
|
||||||
self._amix,
|
|
||||||
self._repeat,
|
|
||||||
self._bmix,
|
|
||||||
self._composite,
|
|
||||||
self._tvmix,
|
|
||||||
self._upmix21,
|
|
||||||
self._upmix41,
|
|
||||||
self._upmix61,
|
|
||||||
self._centeronly,
|
|
||||||
self._lfeonly,
|
|
||||||
self._rearonly,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class IRemote(abc.ABC):
|
class IRemote(abc.ABC):
|
||||||
"""
|
"""
|
||||||
Common interface between base class and extended (higher) classes
|
Common interface between base class and extended (higher) classes
|
||||||
@@ -89,7 +15,6 @@ class IRemote(abc.ABC):
|
|||||||
self._remote = remote
|
self._remote = remote
|
||||||
self.index = index
|
self.index = index
|
||||||
self.logger = logger.getChild(self.__class__.__name__)
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
self._modes = Modes()
|
|
||||||
|
|
||||||
def getter(self, param):
|
def getter(self, param):
|
||||||
cmd = self._cmd(param)
|
cmd = self._cmd(param)
|
||||||
@@ -123,6 +48,8 @@ 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 (f'mode.{val}', 1)
|
||||||
@@ -138,14 +65,9 @@ class IRemote(abc.ABC):
|
|||||||
val = 1 if val else 0
|
val = 1 if val else 0
|
||||||
|
|
||||||
self._remote.cache[self._cmd(attr)] = val
|
self._remote.cache[self._cmd(attr)] = val
|
||||||
self._remote._script += f'{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(self._remote._script)
|
self._remote.sendtext(script)
|
||||||
return self
|
|
||||||
|
|
||||||
def then_wait(self):
|
|
||||||
self._remote._script = str()
|
|
||||||
time.sleep(self._remote.DELAY)
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from .enums import NBS, BusModes
|
from .enums import NBS, BusModes
|
||||||
|
from .packet.enums import ChannelModes
|
||||||
from .util import cache_bool, cache_float, cache_int, cache_string
|
from .util import cache_bool, cache_float, cache_int, cache_string
|
||||||
|
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ def channel_bool_prop(param):
|
|||||||
elif param.lower() == 'mc':
|
elif param.lower() == 'mc':
|
||||||
return channel_state.mc
|
return channel_state.mc
|
||||||
else:
|
else:
|
||||||
return channel_state.get_mode(getattr(self._modes, f'_{param.lower()}'))
|
return channel_state.get_mode(getattr(ChannelModes, param.upper()).value)
|
||||||
|
|
||||||
def fset(self, val):
|
def fset(self, val):
|
||||||
self.setter(param, 1 if val else 0)
|
self.setter(param, 1 if val else 0)
|
||||||
@@ -55,7 +56,9 @@ def channel_int_prop(param):
|
|||||||
bit_9 = (channel_state._state >> 9) & 1
|
bit_9 = (channel_state._state >> 9) & 1
|
||||||
return (bit_9 << 1) | bit_2
|
return (bit_9 << 1) | bit_2
|
||||||
else:
|
else:
|
||||||
return channel_state.get_mode_int(getattr(self._modes, f'_{param.lower()}'))
|
return channel_state.get_mode_int(
|
||||||
|
getattr(ChannelModes, param.upper()).value
|
||||||
|
)
|
||||||
|
|
||||||
def fset(self, val):
|
def fset(self, val):
|
||||||
self.setter(param, val)
|
self.setter(param, val)
|
||||||
@@ -89,7 +92,7 @@ def strip_output_prop(param):
|
|||||||
|
|
||||||
strip_state = self.public_packets[NBS.zero].states.strip[self.index]
|
strip_state = self.public_packets[NBS.zero].states.strip[self.index]
|
||||||
|
|
||||||
return strip_state.get_mode(getattr(self._modes, f'_bus{param.lower()}'))
|
return strip_state.get_mode(getattr(ChannelModes, f'BUS{param.upper()}').value)
|
||||||
|
|
||||||
def fset(self, val):
|
def fset(self, val):
|
||||||
self.setter(param, 1 if val else 0)
|
self.setter(param, 1 if val else 0)
|
||||||
|
|||||||
53
vban_cmd/packet/enums.py
Normal file
53
vban_cmd/packet/enums.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
from enum import Flag
|
||||||
|
|
||||||
|
|
||||||
|
class ChannelModes(Flag):
|
||||||
|
"""Channel Modes - Bit flags that can be combined"""
|
||||||
|
|
||||||
|
MUTE = 0x00000001
|
||||||
|
SOLO = 0x00000002
|
||||||
|
MONO = 0x00000004
|
||||||
|
MC = 0x00000008
|
||||||
|
|
||||||
|
AMIX = 0x00000010
|
||||||
|
REPEAT = 0x00000020
|
||||||
|
BMIX = 0x00000030
|
||||||
|
COMPOSITE = 0x00000040
|
||||||
|
TVMIX = 0x00000050
|
||||||
|
UPMIX21 = 0x00000060
|
||||||
|
UPMIX41 = 0x00000070
|
||||||
|
UPMIX61 = 0x00000080
|
||||||
|
CENTERONLY = 0x00000090
|
||||||
|
LFEONLY = 0x000000A0
|
||||||
|
REARONLY = 0x000000B0
|
||||||
|
|
||||||
|
MASK = 0x000000F0
|
||||||
|
|
||||||
|
ON = 0x00000100 # eq.on
|
||||||
|
CROSS = 0x00000200
|
||||||
|
AB = 0x00000800 # eq.ab
|
||||||
|
|
||||||
|
BUSA = 0x00001000
|
||||||
|
BUSA1 = 0x00001000
|
||||||
|
BUSA2 = 0x00002000
|
||||||
|
BUSA3 = 0x00004000
|
||||||
|
BUSA4 = 0x00008000
|
||||||
|
BUSA5 = 0x00080000
|
||||||
|
|
||||||
|
BUSB = 0x00010000
|
||||||
|
BUSB1 = 0x00010000
|
||||||
|
BUSB2 = 0x00020000
|
||||||
|
BUSB3 = 0x00040000
|
||||||
|
|
||||||
|
PAN0 = 0x00000000
|
||||||
|
PANCOLOR = 0x00100000
|
||||||
|
PANMOD = 0x00200000
|
||||||
|
PANMASK = 0x00F00000
|
||||||
|
|
||||||
|
POSTFX_R = 0x01000000
|
||||||
|
POSTFX_D = 0x02000000
|
||||||
|
POSTFX1 = 0x04000000
|
||||||
|
POSTFX2 = 0x08000000
|
||||||
|
|
||||||
|
SEL = 0x10000000
|
||||||
|
MONITOR = 0x20000000
|
||||||
@@ -8,11 +8,15 @@ VBAN_PROTOCOL_SERVICE = 0x60
|
|||||||
|
|
||||||
VBAN_SERVICE_RTPACKETREGISTER = 32
|
VBAN_SERVICE_RTPACKETREGISTER = 32
|
||||||
VBAN_SERVICE_RTPACKET = 33
|
VBAN_SERVICE_RTPACKET = 33
|
||||||
|
VBAN_SERVICE_PING = 0
|
||||||
|
VBAN_SERVICE_PONG = 0 # PONG uses same service type as PING
|
||||||
VBAN_SERVICE_MASK = 0xE0
|
VBAN_SERVICE_MASK = 0xE0
|
||||||
VBAN_PROTOCOL_MASK = 0xE0
|
VBAN_PROTOCOL_MASK = 0xE0
|
||||||
VBAN_SERVICE_REQUESTREPLY = 0x02
|
VBAN_SERVICE_REQUESTREPLY = 0x02
|
||||||
VBAN_SERVICE_FNCT_REPLY = 0x02
|
VBAN_SERVICE_FNCT_REPLY = 0x02
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
@@ -207,6 +211,92 @@ class VbanMatrixResponseHeader:
|
|||||||
return header, payload
|
return header, payload
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class VbanPingHeader:
|
||||||
|
"""Represents the header of a PING packet"""
|
||||||
|
|
||||||
|
name: str = 'PING0'
|
||||||
|
format_sr: int = VBAN_PROTOCOL_SERVICE
|
||||||
|
format_nbs: int = 0
|
||||||
|
format_nbc: int = VBAN_SERVICE_PING
|
||||||
|
format_bit: int = 0
|
||||||
|
framecounter: int = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def vban(self) -> bytes:
|
||||||
|
return b'VBAN'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def streamname(self) -> bytes:
|
||||||
|
return self.name.encode('ascii')[:16].ljust(16, b'\x00')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_bytes(cls, framecounter: int = 0) -> bytes:
|
||||||
|
"""Creates the PING header bytes only."""
|
||||||
|
header = cls(framecounter=framecounter)
|
||||||
|
|
||||||
|
data = bytearray()
|
||||||
|
data.extend(header.vban)
|
||||||
|
data.extend(header.format_sr.to_bytes(1, 'little'))
|
||||||
|
data.extend(header.format_nbs.to_bytes(1, 'little'))
|
||||||
|
data.extend(header.format_nbc.to_bytes(1, 'little'))
|
||||||
|
data.extend(header.format_bit.to_bytes(1, 'little'))
|
||||||
|
data.extend(header.streamname)
|
||||||
|
data.extend(header.framecounter.to_bytes(4, 'little'))
|
||||||
|
return bytes(data)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class VbanPongHeader:
|
||||||
|
"""Represents the header of a PONG response packet"""
|
||||||
|
|
||||||
|
name: str = 'PING0'
|
||||||
|
format_sr: int = VBAN_PROTOCOL_SERVICE
|
||||||
|
format_nbs: int = 0
|
||||||
|
format_nbc: int = VBAN_SERVICE_PONG
|
||||||
|
format_bit: int = 0
|
||||||
|
framecounter: int = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def vban(self) -> bytes:
|
||||||
|
return b'VBAN'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def streamname(self) -> bytes:
|
||||||
|
return self.name.encode('ascii')[:16].ljust(16, b'\x00')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_bytes(cls, data: bytes):
|
||||||
|
"""Parse a PONG response packet from bytes."""
|
||||||
|
parsed = _parse_vban_service_header(data)
|
||||||
|
|
||||||
|
# PONG responses use the same service type as PING (0x00)
|
||||||
|
# and are identified by having payload data
|
||||||
|
if parsed['format_nbc'] != VBAN_SERVICE_PONG:
|
||||||
|
raise ValueError(f'Not a PONG response packet: {parsed["format_nbc"]:02x}')
|
||||||
|
|
||||||
|
return cls(**parsed)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_pong_response(cls, data: bytes) -> bool:
|
||||||
|
"""Check if packet is a PONG response by analyzing the actual response format."""
|
||||||
|
try:
|
||||||
|
parsed = _parse_vban_service_header(data)
|
||||||
|
|
||||||
|
# Validate this is a service protocol packet with PING/PONG service type
|
||||||
|
if parsed['format_nbc'] != VBAN_SERVICE_PONG:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if parsed['name'] not in ['PING0', 'VBAN Service']:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# PONG should have payload data (same size as PING)
|
||||||
|
return len(data) >= PINGPONG_PACKET_SIZE
|
||||||
|
|
||||||
|
except (ValueError, Exception):
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class VbanRequestHeader:
|
class VbanRequestHeader:
|
||||||
"""Represents the header of a request packet"""
|
"""Represents the header of a request packet"""
|
||||||
@@ -238,7 +328,7 @@ class VbanRequestHeader:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def streamname(self) -> bytes:
|
def streamname(self) -> bytes:
|
||||||
return self.name.encode() + bytes(16 - len(self.name))
|
return self.name.encode()[:16].ljust(16, b'\x00')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_bytes(
|
def to_bytes(
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from vban_cmd.enums import NBS
|
|||||||
from vban_cmd.kinds import KindMapClass
|
from vban_cmd.kinds import KindMapClass
|
||||||
from vban_cmd.util import comp
|
from vban_cmd.util import comp
|
||||||
|
|
||||||
|
from .enums import ChannelModes
|
||||||
from .headers import VbanPacket
|
from .headers import VbanPacket
|
||||||
|
|
||||||
|
|
||||||
@@ -31,57 +32,57 @@ class ChannelState:
|
|||||||
# Common boolean modes
|
# Common boolean modes
|
||||||
@property
|
@property
|
||||||
def mute(self) -> bool:
|
def mute(self) -> bool:
|
||||||
return (self._state & 0x00000001) != 0
|
return (self._state & ChannelModes.MUTE.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def solo(self) -> bool:
|
def solo(self) -> bool:
|
||||||
return (self._state & 0x00000002) != 0
|
return (self._state & ChannelModes.SOLO.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mono(self) -> bool:
|
def mono(self) -> bool:
|
||||||
return (self._state & 0x00000004) != 0
|
return (self._state & ChannelModes.MONO.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mc(self) -> bool:
|
def mc(self) -> bool:
|
||||||
return (self._state & 0x00000008) != 0
|
return (self._state & ChannelModes.MC.value) != 0
|
||||||
|
|
||||||
# EQ modes
|
# EQ modes
|
||||||
@property
|
@property
|
||||||
def eq_on(self) -> bool:
|
def eq_on(self) -> bool:
|
||||||
return (self._state & 0x00000100) != 0
|
return (self._state & ChannelModes.ON.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def eq_ab(self) -> bool:
|
def eq_ab(self) -> bool:
|
||||||
return (self._state & 0x00000800) != 0
|
return (self._state & ChannelModes.AB.value) != 0
|
||||||
|
|
||||||
# Bus assignments (strip to bus routing)
|
# Bus assignments (strip to bus routing)
|
||||||
@property
|
@property
|
||||||
def busa1(self) -> bool:
|
def busa1(self) -> bool:
|
||||||
return (self._state & 0x00001000) != 0
|
return (self._state & ChannelModes.BUSA1.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busa2(self) -> bool:
|
def busa2(self) -> bool:
|
||||||
return (self._state & 0x00002000) != 0
|
return (self._state & ChannelModes.BUSA2.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busa3(self) -> bool:
|
def busa3(self) -> bool:
|
||||||
return (self._state & 0x00004000) != 0
|
return (self._state & ChannelModes.BUSA3.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busa4(self) -> bool:
|
def busa4(self) -> bool:
|
||||||
return (self._state & 0x00008000) != 0
|
return (self._state & ChannelModes.BUSA4.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busb1(self) -> bool:
|
def busb1(self) -> bool:
|
||||||
return (self._state & 0x00010000) != 0
|
return (self._state & ChannelModes.BUSB1.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busb2(self) -> bool:
|
def busb2(self) -> bool:
|
||||||
return (self._state & 0x00020000) != 0
|
return (self._state & ChannelModes.BUSB2.value) != 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def busb3(self) -> bool:
|
def busb3(self) -> bool:
|
||||||
return (self._state & 0x00040000) != 0
|
return (self._state & ChannelModes.BUSB3.value) != 0
|
||||||
|
|
||||||
|
|
||||||
class States(NamedTuple):
|
class States(NamedTuple):
|
||||||
|
|||||||
124
vban_cmd/packet/ping0.py
Normal file
124
vban_cmd/packet/ping0.py
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from .headers import VbanPingHeader
|
||||||
|
|
||||||
|
# VBAN PING bitType constants
|
||||||
|
VBANPING_TYPE_RECEPTOR = 0x00000001 # Simple receptor
|
||||||
|
VBANPING_TYPE_TRANSMITTER = 0x00000002 # Simple Transmitter
|
||||||
|
VBANPING_TYPE_RECEPTORSPOT = 0x00000004 # SPOT receptor
|
||||||
|
VBANPING_TYPE_TRANSMITTERSPOT = 0x00000008 # SPOT transmitter
|
||||||
|
VBANPING_TYPE_VIRTUALDEVICE = 0x00000010 # Virtual Device
|
||||||
|
VBANPING_TYPE_VIRTUALMIXER = 0x00000020 # Virtual Mixer
|
||||||
|
VBANPING_TYPE_MATRIX = 0x00000040 # MATRIX
|
||||||
|
VBANPING_TYPE_DAW = 0x00000080 # Workstation
|
||||||
|
VBANPING_TYPE_SERVER = 0x01000000 # VBAN SERVER
|
||||||
|
|
||||||
|
# VBAN PING bitfeature constants
|
||||||
|
VBANPING_FEATURE_AUDIO = 0x00000001
|
||||||
|
VBANPING_FEATURE_AOIP = 0x00000002
|
||||||
|
VBANPING_FEATURE_VOIP = 0x00000004
|
||||||
|
VBANPING_FEATURE_SERIAL = 0x00000100
|
||||||
|
VBANPING_FEATURE_MIDI = 0x00000300
|
||||||
|
VBANPING_FEATURE_FRAME = 0x00001000
|
||||||
|
VBANPING_FEATURE_TXT = 0x00010000
|
||||||
|
|
||||||
|
|
||||||
|
class VbanServerType(Enum):
|
||||||
|
"""VBAN server types detected from PONG responses"""
|
||||||
|
|
||||||
|
UNKNOWN = 0
|
||||||
|
VOICEMEETER = VBANPING_TYPE_VIRTUALMIXER
|
||||||
|
MATRIX = VBANPING_TYPE_MATRIX
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class VbanPing0Payload:
|
||||||
|
"""Represents the VBAN PING0 payload structure as defined in the VBAN protocol documentation."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.bit_type = VBANPING_TYPE_RECEPTOR
|
||||||
|
self.bit_feature = VBANPING_FEATURE_TXT
|
||||||
|
self.bit_feature_ex = 0x00000000
|
||||||
|
self.preferred_rate = 48000
|
||||||
|
self.min_rate = 8000
|
||||||
|
self.max_rate = 192000
|
||||||
|
self.color_rgb = 0x00FF0000
|
||||||
|
self.version = b'\x01\x02\x03\x04'
|
||||||
|
self.gps_position = b'\x00' * 8
|
||||||
|
self.user_position = b'\x00' * 8
|
||||||
|
self.lang_code = b'EN\x00\x00\x00\x00\x00\x00'
|
||||||
|
self.reserved = b'\x00' * 8
|
||||||
|
self.reserved_ex = b'\x00' * 64
|
||||||
|
self.distant_ip = b'\x00' * 32
|
||||||
|
self.distant_port = 0
|
||||||
|
self.distant_reserved = 0
|
||||||
|
self.device_name = b'VBAN-CMD-Python\x00'.ljust(64, b'\x00')
|
||||||
|
self.manufacturer_name = b'Python-VBAN\x00'.ljust(64, b'\x00')
|
||||||
|
self.application_name = b'vban-cmd\x00'.ljust(64, b'\x00')
|
||||||
|
self.host_name = b'localhost\x00'.ljust(64, b'\x00')
|
||||||
|
self.user_name = b'Python User\x00'.ljust(128, b'\x00')
|
||||||
|
self.user_comment = b'VBAN CMD Python Client\x00'.ljust(128, b'\x00')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_bytes(cls) -> bytes:
|
||||||
|
"""Convert payload to bytes"""
|
||||||
|
payload = cls()
|
||||||
|
|
||||||
|
data = bytearray()
|
||||||
|
data.extend(payload.bit_type.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.bit_feature.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.bit_feature_ex.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.preferred_rate.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.min_rate.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.max_rate.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.color_rgb.to_bytes(4, 'little'))
|
||||||
|
data.extend(payload.version)
|
||||||
|
data.extend(payload.gps_position)
|
||||||
|
data.extend(payload.user_position)
|
||||||
|
data.extend(payload.lang_code)
|
||||||
|
data.extend(payload.reserved)
|
||||||
|
data.extend(payload.reserved_ex)
|
||||||
|
data.extend(payload.distant_ip)
|
||||||
|
data.extend(payload.distant_port.to_bytes(2, 'little'))
|
||||||
|
data.extend(payload.distant_reserved.to_bytes(2, 'little'))
|
||||||
|
data.extend(payload.device_name)
|
||||||
|
data.extend(payload.manufacturer_name)
|
||||||
|
data.extend(payload.application_name)
|
||||||
|
data.extend(payload.host_name)
|
||||||
|
data.extend(payload.user_name)
|
||||||
|
data.extend(payload.user_comment)
|
||||||
|
return bytes(data)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_packet(cls, framecounter: int) -> bytes:
|
||||||
|
"""Creates a complete PING packet with header and payload."""
|
||||||
|
data = bytearray()
|
||||||
|
data.extend(VbanPingHeader.to_bytes(framecounter))
|
||||||
|
data.extend(cls.to_bytes())
|
||||||
|
return bytes(data)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def detect_server_type(pong_data: bytes) -> VbanServerType:
|
||||||
|
"""Detect server type from PONG response packet.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pong_data: Raw bytes from PONG response packet
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
VbanServerType enum indicating the detected server type
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if len(pong_data) >= 32:
|
||||||
|
frame_counter_bytes = pong_data[28:32]
|
||||||
|
frame_counter = int.from_bytes(frame_counter_bytes, 'little')
|
||||||
|
|
||||||
|
if frame_counter == VbanServerType.MATRIX.value:
|
||||||
|
return VbanServerType.MATRIX
|
||||||
|
elif frame_counter == VbanServerType.VOICEMEETER.value:
|
||||||
|
return VbanServerType.VOICEMEETER
|
||||||
|
|
||||||
|
return VbanServerType.UNKNOWN
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
return VbanServerType.UNKNOWN
|
||||||
@@ -102,7 +102,9 @@ class Recorder(IRemote):
|
|||||||
|
|
||||||
def load(self, file: os.PathLike):
|
def load(self, file: os.PathLike):
|
||||||
try:
|
try:
|
||||||
self.setter('load', str(file))
|
# Convert to string, use forward slashes, and wrap in quotes for spaces
|
||||||
|
file_path = f'"{os.fspath(file).replace(chr(92), "/")}"'
|
||||||
|
self.setter('load', file_path)
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
raise VBANCMDError('File full directory must be a raw string')
|
raise VBANCMDError('File full directory must be a raw string')
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,73 @@
|
|||||||
|
import socket
|
||||||
|
import time
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
|
from .error import VBANCMDConnectionError
|
||||||
|
|
||||||
|
|
||||||
|
def ratelimit(func):
|
||||||
|
"""ratelimit decorator for {VbanCmd}.sendtext, to prevent flooding the network with script requests."""
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
self, *rem = args
|
||||||
|
if self.script_ratelimit > 0:
|
||||||
|
now = time.time()
|
||||||
|
elapsed = now - self._last_script_request_time
|
||||||
|
if elapsed < self.script_ratelimit:
|
||||||
|
time.sleep(self.script_ratelimit - elapsed)
|
||||||
|
self._last_script_request_time = time.time()
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def ping_timeout(func):
|
||||||
|
"""ping_timeout decorator for {VbanCmd}._ping, to handle timeout logic and socket management."""
|
||||||
|
|
||||||
|
def wrapper(self, timeout: float = None):
|
||||||
|
if timeout is None:
|
||||||
|
timeout = min(self.timeout, 3.0)
|
||||||
|
|
||||||
|
original_timeout = self.sock.gettimeout()
|
||||||
|
self.sock.settimeout(0.5)
|
||||||
|
|
||||||
|
try:
|
||||||
|
func(self)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
response_count = 0
|
||||||
|
|
||||||
|
while time.time() - start_time < timeout:
|
||||||
|
try:
|
||||||
|
data, addr = self.sock.recvfrom(2048)
|
||||||
|
response_count += 1
|
||||||
|
|
||||||
|
self.logger.debug(
|
||||||
|
f'Received packet #{response_count} from {addr}: {len(data)} bytes'
|
||||||
|
)
|
||||||
|
self.logger.debug(
|
||||||
|
f'Response header: {data[: min(32, len(data))].hex()}'
|
||||||
|
)
|
||||||
|
|
||||||
|
result = func(self, data, addr)
|
||||||
|
if result is True:
|
||||||
|
return
|
||||||
|
|
||||||
|
except socket.timeout:
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.logger.debug(
|
||||||
|
f'PING timeout after {timeout}s, received {response_count} non-PONG packets'
|
||||||
|
)
|
||||||
|
raise VBANCMDConnectionError(
|
||||||
|
f'PING timeout: No response from {self.host}:{self.port} after {timeout}s'
|
||||||
|
)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
self.sock.settimeout(original_timeout)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def cache_bool(func, param):
|
def cache_bool(func, param):
|
||||||
"""Check cache for a bool prop"""
|
"""Check cache for a bool prop"""
|
||||||
|
|||||||
@@ -5,14 +5,19 @@ import threading
|
|||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from typing import Union
|
from typing import Mapping, Union
|
||||||
|
|
||||||
from .enums import NBS
|
from .enums import NBS
|
||||||
from .error import VBANCMDError
|
from .error import VBANCMDConnectionError, VBANCMDError
|
||||||
from .event import Event
|
from .event import Event
|
||||||
from .packet.headers import VbanMatrixResponseHeader, VbanRequestHeader
|
from .packet.headers import (
|
||||||
|
VbanMatrixResponseHeader,
|
||||||
|
VbanPongHeader,
|
||||||
|
VbanRequestHeader,
|
||||||
|
)
|
||||||
|
from .packet.ping0 import VbanPing0Payload, VbanServerType
|
||||||
from .subject import Subject
|
from .subject import Subject
|
||||||
from .util import bump_framecounter, deep_merge
|
from .util import bump_framecounter, deep_merge, ping_timeout, ratelimit
|
||||||
from .worker import Producer, Subscriber, Updater
|
from .worker import Producer, Subscriber, Updater
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -33,21 +38,23 @@ class VbanCmd(abc.ABC):
|
|||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.logger = logger.getChild(self.__class__.__name__)
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
self.event = Event({k: kwargs.pop(k) for k in ('pdirty', 'ldirty')})
|
self.event = Event({k: kwargs.pop(k) for k in ('pdirty', 'ldirty')})
|
||||||
if not kwargs['ip']:
|
if not kwargs['host']:
|
||||||
kwargs |= self._conn_from_toml()
|
kwargs |= self._conn_from_toml()
|
||||||
for attr, val in kwargs.items():
|
for attr, val in kwargs.items():
|
||||||
setattr(self, attr, val)
|
setattr(self, attr, val)
|
||||||
|
|
||||||
self._framecounter = 0
|
self._framecounter = 0
|
||||||
|
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)
|
||||||
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
self.sock.settimeout(self.timeout)
|
||||||
self.subject = self.observer = Subject()
|
self.subject = self.observer = Subject()
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
self._pdirty = False
|
self._pdirty = False
|
||||||
self._ldirty = False
|
self._ldirty = False
|
||||||
self._script = str()
|
|
||||||
self.stop_event = None
|
self.stop_event = None
|
||||||
self.producer = None
|
self.producer = None
|
||||||
|
self._last_script_request_time = 0
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -86,7 +93,11 @@ class VbanCmd(abc.ABC):
|
|||||||
self.logout()
|
self.logout()
|
||||||
|
|
||||||
def login(self) -> None:
|
def login(self) -> None:
|
||||||
"""Starts the subscriber and updater threads (unless disable_rt_listeners is True) and logs into Voicemeeter."""
|
"""Sends a PING packet to the VBAN server to verify connectivity and detect server type.
|
||||||
|
If the server is detected as Matrix, RT listeners will be disabled for compatibility.
|
||||||
|
"""
|
||||||
|
self._ping()
|
||||||
|
|
||||||
if not self.disable_rt_listeners:
|
if not self.disable_rt_listeners:
|
||||||
self.event.info()
|
self.event.info()
|
||||||
|
|
||||||
@@ -102,7 +113,7 @@ class VbanCmd(abc.ABC):
|
|||||||
self.producer.start()
|
self.producer.start()
|
||||||
|
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
"Successfully logged into VBANCMD {kind} with ip='{ip}', port={port}, streamname='{streamname}'".format(
|
"Successfully logged into VBANCMD {kind} with host='{host}', port={port}, streamname='{streamname}'".format(
|
||||||
**self.__dict__
|
**self.__dict__
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -120,6 +131,75 @@ class VbanCmd(abc.ABC):
|
|||||||
def stopped(self):
|
def stopped(self):
|
||||||
return self.stop_event is None or self.stop_event.is_set()
|
return self.stop_event is None or self.stop_event.is_set()
|
||||||
|
|
||||||
|
def _get_next_framecounter(self) -> int:
|
||||||
|
"""Thread-safe method to get and increment framecounter."""
|
||||||
|
with self._framecounter_lock:
|
||||||
|
current = self._framecounter
|
||||||
|
self._framecounter = bump_framecounter(self._framecounter)
|
||||||
|
return current
|
||||||
|
|
||||||
|
@ping_timeout
|
||||||
|
def _ping(self, data=None, addr=None) -> bool:
|
||||||
|
"""Handles the PING/PONG handshake with the VBAN server, including timeout logic and server type detection.
|
||||||
|
|
||||||
|
If data and addr are None, it sends a PING packet. If a PONG response is received, it returns True.
|
||||||
|
|
||||||
|
If a non-PONG packet is received, it logs the packet details and continues waiting until timeout"""
|
||||||
|
if data is None and addr is None:
|
||||||
|
ping_packet = VbanPing0Payload.create_packet(self._get_next_framecounter())
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.sock.sendto(
|
||||||
|
ping_packet, (socket.gethostbyname(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:
|
||||||
|
raise VBANCMDConnectionError(f'PING failed: {e}') from e
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
if VbanPongHeader.is_pong_response(data):
|
||||||
|
self.logger.debug(f'PONG received from {addr}, connectivity confirmed')
|
||||||
|
|
||||||
|
server_type = VbanPing0Payload.detect_server_type(data)
|
||||||
|
self._handle_server_type(server_type)
|
||||||
|
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
if len(data) >= 8:
|
||||||
|
if data[:4] == b'VBAN':
|
||||||
|
protocol = data[4] & 0xE0
|
||||||
|
nbc = data[6]
|
||||||
|
self.logger.debug(
|
||||||
|
f'Non-PONG VBAN packet: protocol=0x{protocol:02x}, nbc=0x{nbc:02x}'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.logger.debug('Non-VBAN packet received')
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _handle_server_type(self, server_type: VbanServerType) -> None:
|
||||||
|
"""Handle the detected server type by adjusting settings accordingly."""
|
||||||
|
match server_type:
|
||||||
|
case VbanServerType.VOICEMEETER:
|
||||||
|
self.logger.debug(
|
||||||
|
'Detected Voicemeeter VBAN server - RT listeners supported'
|
||||||
|
)
|
||||||
|
case VbanServerType.MATRIX:
|
||||||
|
self.logger.info(
|
||||||
|
'Detected Matrix VBAN server - disabling RT listeners for compatibility'
|
||||||
|
)
|
||||||
|
self.disable_rt_listeners = True
|
||||||
|
case _:
|
||||||
|
self.logger.debug(
|
||||||
|
f'Unknown server type ({server_type}) - using default settings'
|
||||||
|
)
|
||||||
|
|
||||||
def _send_request(self, payload: str) -> None:
|
def _send_request(self, payload: str) -> None:
|
||||||
"""Sends a request packet over the network and bumps the framecounter."""
|
"""Sends a request packet over the network and bumps the framecounter."""
|
||||||
self.sock.sendto(
|
self.sock.sendto(
|
||||||
@@ -127,18 +207,18 @@ class VbanCmd(abc.ABC):
|
|||||||
name=self.streamname,
|
name=self.streamname,
|
||||||
bps_index=self.BPS_OPTS.index(self.bps),
|
bps_index=self.BPS_OPTS.index(self.bps),
|
||||||
channel=self.channel,
|
channel=self.channel,
|
||||||
framecounter=self._framecounter,
|
framecounter=self._get_next_framecounter(),
|
||||||
payload=payload,
|
payload=payload,
|
||||||
),
|
),
|
||||||
(socket.gethostbyname(self.ip), self.port),
|
(socket.gethostbyname(self.host), self.port),
|
||||||
)
|
)
|
||||||
self._framecounter = bump_framecounter(self._framecounter)
|
|
||||||
|
|
||||||
def _set_rt(self, cmd: str, val: Union[str, float]):
|
def _set_rt(self, cmd: str, val: Union[str, float]):
|
||||||
"""Sends a string request command over a network."""
|
"""Sends a string request command over a network."""
|
||||||
self._send_request(f'{cmd}={val};')
|
self._send_request(f'{cmd}={val};')
|
||||||
self.cache[cmd] = val
|
self.cache[cmd] = val
|
||||||
|
|
||||||
|
@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)
|
||||||
@@ -146,14 +226,15 @@ class VbanCmd(abc.ABC):
|
|||||||
|
|
||||||
if self.disable_rt_listeners and script.endswith(('?', '?;')):
|
if self.disable_rt_listeners and script.endswith(('?', '?;')):
|
||||||
try:
|
try:
|
||||||
response = VbanMatrixResponseHeader.extract_payload(
|
data, _ = self.sock.recvfrom(2048)
|
||||||
self.sock.recv(1024)
|
return VbanMatrixResponseHeader.extract_payload(data)
|
||||||
)
|
|
||||||
return response
|
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.logger.warning(f'Error extracting matrix response: {e}')
|
self.logger.warning(f'Error extracting matrix response: {e}')
|
||||||
|
except TimeoutError as e:
|
||||||
time.sleep(self.DELAY)
|
self.logger.exception(f'Timeout waiting for matrix response: {e}')
|
||||||
|
raise VBANCMDConnectionError(
|
||||||
|
f'Timeout waiting for response from {self.host}:{self.port}'
|
||||||
|
) from e
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self) -> str:
|
def type(self) -> str:
|
||||||
@@ -185,12 +266,8 @@ class VbanCmd(abc.ABC):
|
|||||||
while self.pdirty:
|
while self.pdirty:
|
||||||
time.sleep(self.DELAY)
|
time.sleep(self.DELAY)
|
||||||
|
|
||||||
def apply(self, data: dict):
|
def apply(self, data: Mapping):
|
||||||
"""
|
"""Set all parameters of a dict"""
|
||||||
Sets all parameters of a dict
|
|
||||||
|
|
||||||
minor delay between each recursion
|
|
||||||
"""
|
|
||||||
|
|
||||||
def target(key):
|
def target(key):
|
||||||
match key.split('-'):
|
match key.split('-'):
|
||||||
@@ -210,7 +287,8 @@ class VbanCmd(abc.ABC):
|
|||||||
raise ValueError(ERR_MSG)
|
raise ValueError(ERR_MSG)
|
||||||
return target[int(index)]
|
return target[int(index)]
|
||||||
|
|
||||||
[target(key).apply(di).then_wait() for key, di in data.items()]
|
for key, di in data.items():
|
||||||
|
target(key).apply(di)
|
||||||
|
|
||||||
def apply_config(self, name):
|
def apply_config(self, name):
|
||||||
"""applies a config from memory"""
|
"""applies a config from memory"""
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -13,7 +12,6 @@ from .packet.headers import (
|
|||||||
)
|
)
|
||||||
from .packet.nbs0 import VbanPacketNBS0
|
from .packet.nbs0 import VbanPacketNBS0
|
||||||
from .packet.nbs1 import VbanPacketNBS1
|
from .packet.nbs1 import VbanPacketNBS1
|
||||||
from .util import bump_framecounter
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -26,24 +24,18 @@ class Subscriber(threading.Thread):
|
|||||||
self._remote = remote
|
self._remote = remote
|
||||||
self.stop_event = stop_event
|
self.stop_event = stop_event
|
||||||
self.logger = logger.getChild(self.__class__.__name__)
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
self._framecounter = 0
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while not self.stopped():
|
while not self.stopped():
|
||||||
try:
|
for nbs in NBS:
|
||||||
for nbs in NBS:
|
sub_packet = VbanSubscribeHeader().to_bytes(
|
||||||
sub_packet = VbanSubscribeHeader().to_bytes(nbs, self._framecounter)
|
nbs, self._remote._get_next_framecounter()
|
||||||
self._remote.sock.sendto(
|
)
|
||||||
sub_packet, (self._remote.ip, self._remote.port)
|
self._remote.sock.sendto(
|
||||||
)
|
sub_packet, (self._remote.host, self._remote.port)
|
||||||
self._framecounter = bump_framecounter(self._framecounter)
|
)
|
||||||
|
|
||||||
self.wait_until_stopped(10)
|
self.wait_until_stopped(10)
|
||||||
except socket.gaierror as e:
|
|
||||||
self.logger.exception(f'{type(e).__name__}: {e}')
|
|
||||||
raise VBANCMDConnectionError(
|
|
||||||
f'unable to resolve hostname {self._remote.ip}'
|
|
||||||
) from e
|
|
||||||
self.logger.debug(f'terminating {self.name} thread')
|
self.logger.debug(f'terminating {self.name} thread')
|
||||||
|
|
||||||
def stopped(self):
|
def stopped(self):
|
||||||
@@ -66,7 +58,6 @@ class Producer(threading.Thread):
|
|||||||
self.queue = queue
|
self.queue = queue
|
||||||
self.stop_event = stop_event
|
self.stop_event = stop_event
|
||||||
self.logger = logger.getChild(self.__class__.__name__)
|
self.logger = logger.getChild(self.__class__.__name__)
|
||||||
self._remote.sock.settimeout(self._remote.timeout)
|
|
||||||
self._remote._public_packets = [None] * (max(NBS) + 1)
|
self._remote._public_packets = [None] * (max(NBS) + 1)
|
||||||
_pp = self._get_rt()
|
_pp = self._get_rt()
|
||||||
self._remote._public_packets[_pp.nbs] = _pp
|
self._remote._public_packets[_pp.nbs] = _pp
|
||||||
@@ -77,40 +68,33 @@ class Producer(threading.Thread):
|
|||||||
|
|
||||||
def _get_rt(self) -> VbanPacket:
|
def _get_rt(self) -> VbanPacket:
|
||||||
"""Attempt to fetch data packet until a valid one found"""
|
"""Attempt to fetch data packet until a valid one found"""
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
if resp := self._fetch_rt_packet():
|
try:
|
||||||
return resp
|
data, _ = self._remote.sock.recvfrom(2048)
|
||||||
|
if len(data) < HEADER_SIZE:
|
||||||
|
continue
|
||||||
|
except TimeoutError as e:
|
||||||
|
self.logger.exception(f'{type(e).__name__}: {e}')
|
||||||
|
raise VBANCMDConnectionError(
|
||||||
|
f'timeout waiting for response from {self._remote.host}:{self._remote.port}'
|
||||||
|
) from e
|
||||||
|
|
||||||
def _fetch_rt_packet(self) -> VbanPacket | None:
|
try:
|
||||||
try:
|
header = VbanResponseHeader.from_bytes(data[:HEADER_SIZE])
|
||||||
data, _ = self._remote.sock.recvfrom(2048)
|
except ValueError as e:
|
||||||
if len(data) < HEADER_SIZE:
|
self.logger.debug(f'Error parsing response packet: {e}')
|
||||||
return
|
continue
|
||||||
except TimeoutError as e:
|
|
||||||
self.logger.exception(f'{type(e).__name__}: {e}')
|
|
||||||
raise VBANCMDConnectionError(
|
|
||||||
f'timeout waiting for response from {self._remote.ip}:{self._remote.port}'
|
|
||||||
) from e
|
|
||||||
|
|
||||||
try:
|
match header.format_nbs:
|
||||||
header = VbanResponseHeader.from_bytes(data[:HEADER_SIZE])
|
case NBS.zero:
|
||||||
except ValueError as e:
|
return VbanPacketNBS0.from_bytes(
|
||||||
self.logger.debug(f'Error parsing response packet: {e}')
|
nbs=NBS.zero, kind=self._remote.kind, data=data
|
||||||
return None
|
)
|
||||||
|
|
||||||
match header.format_nbs:
|
case NBS.one:
|
||||||
case NBS.zero:
|
return VbanPacketNBS1.from_bytes(
|
||||||
return VbanPacketNBS0.from_bytes(
|
nbs=NBS.one, kind=self._remote.kind, data=data
|
||||||
nbs=NBS.zero, kind=self._remote.kind, data=data
|
)
|
||||||
)
|
|
||||||
|
|
||||||
case NBS.one:
|
|
||||||
return VbanPacketNBS1.from_bytes(
|
|
||||||
nbs=NBS.one, kind=self._remote.kind, data=data
|
|
||||||
)
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
def stopped(self):
|
def stopped(self):
|
||||||
return self.stop_event.is_set()
|
return self.stop_event.is_set()
|
||||||
@@ -138,7 +122,6 @@ class Producer(threading.Thread):
|
|||||||
self.queue.put('pdirty')
|
self.queue.put('pdirty')
|
||||||
if self._remote.event.ldirty:
|
if self._remote.event.ldirty:
|
||||||
self.queue.put('ldirty')
|
self.queue.put('ldirty')
|
||||||
# time.sleep(self._remote.ratelimit)
|
|
||||||
self.logger.debug(f'terminating {self.name} thread')
|
self.logger.debug(f'terminating {self.name} thread')
|
||||||
self.queue.put(None)
|
self.queue.put(None)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user