27 Commits

Author SHA1 Message Date
a0ec00652b reduce the level of logging for packet parse errors
patch bump
2026-03-01 17:22:06 +00:00
69263c22f2 add 2.7.0 to CHANGELOG 2026-03-01 17:04:37 +00:00
ad2cfeaae6 entry point now accepts a 'matrix' kind although it's main purpose is to disable the rt listener threads.
{VbanCmd}.sendtext():
- remove the @script decorator which I'm sure nobody has ever used anyway
- if rt listeners are disabled and it's a matrix query request, attempt to read a response.
2026-03-01 16:21:47 +00:00
1123fe6432 move header validation into class methods
add _parse_vban_service_header() helper function
2026-03-01 16:17:03 +00:00
3c3e415d7e add _send_request() helper method. 2026-03-01 11:09:45 +00:00
8cfeb45fcb update imports 2026-03-01 11:09:28 +00:00
10b38b3fcc move packet classes into internal packet module 2026-03-01 11:09:22 +00:00
ff5ac193c8 add ChannelState interface, use it in the meta functions.
reword busmodes bitwise logic.

comment out ratelimit, this will probably get permanently removed.
2026-03-01 03:37:57 +00:00
2f3cd0e07f use db levels throughout the package. This is cleaner than converting to db but comparing raw integer values. 2026-03-01 01:08:02 +00:00
d689b3a301 move voicemeetertype(), voicemeeterversion() and samplerate() properties into VbanPacket
add NamedTuples for Levels, Labels and States.

refactor the levels properties

update the math in util.comp()

StripLevel/BusLevel getters updated according to changes in VbanPacketNBS0

remove {VbanCmd}._get_levels(), it's no longer necessary.
2026-03-01 00:25:22 +00:00
a8ef82166c upd publish action 2026-02-27 20:59:25 +00:00
79f06ecc79 add ruff+publish workflows 2026-02-27 20:57:54 +00:00
b291c3a477 minor version bump 2026-02-27 20:36:54 +00:00
c335d35b9f fix config extends section 2026-02-27 20:16:04 +00:00
911d2f64a6 import abc namespace 2026-02-08 09:09:59 +00:00
e58d6c7242 remove comments 2026-01-18 19:57:12 +00:00
870a95b41e upd Strip Comp/Gate/EQ in README 2026-01-18 18:08:40 +00:00
59880bf582 remove comments 2026-01-18 17:22:20 +00:00
cc58d1f081 implement {strip}.gate 2026-01-18 17:06:10 +00:00
e37dea38b3 upd Run Tests in README 2026-01-18 15:25:05 +00:00
7f3b0ac7c9 upd examples to read conn from env 2026-01-18 15:17:00 +00:00
0512fac710 implement parametric eq 2026-01-18 15:16:48 +00:00
d439da725c implement parametric eq 2026-01-18 14:42:07 +00:00
45ffed9f63 implement audibility knobs (inc comp, gate, denoiser) 2026-01-18 13:13:05 +00:00
14f79d1388 move namedtuples 2026-01-18 12:22:53 +00:00
b45bd38706 use namedtuples to improve readability 2026-01-18 12:19:16 +00:00
312b5c5842 refactor header dataclasses 2026-01-18 11:43:43 +00:00
23 changed files with 1569 additions and 831 deletions

53
.github/workflows/publish.yml vendored Normal file
View File

@@ -0,0 +1,53 @@
name: Publish to PyPI
on:
release:
types: [published]
push:
tags:
- 'v*.*.*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install Poetry
run: |
pip install poetry==2.3.1
poetry --version
- name: Build package
run: |
poetry install --only-root
poetry build
- uses: actions/upload-artifact@v4
with:
name: dist
path: ./dist
pypi-publish:
needs: build
name: Upload release to PyPI
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/vban-cmd/
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: ./dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: ./dist

19
.github/workflows/ruff.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
name: Ruff
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: astral-sh/ruff-action@v3
with:
args: 'format --check --diff'

View File

@@ -11,6 +11,31 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- [x]
## [2.7.0] - 2026-03-01
### Added
- new kind `matrix` has been added, it does two things:
- scales the interface according to `potato` kind, in practice this has no affect but it's required by the builder classes.
- disables the rt listener threads since we aren't expecting to receive any from a Matrix VBAN server.
- however, matrix responses may still be received with the {VbanCmd}.sendtext() method.
### Changed
- `outbound` kwarg has been renamed to `disable_rt_listeners`. Since it's job is to disable the listener threads for incoming RT packets this new name is more descriptive.
- dataclasses representing packet headers and packets with ident:0 and ident:1 have been moved into an internal packet module.
### Removed
- {VbanCmd}.sendtext() @script decorator removed. It's purpose was to attempt to convert a dictionary to a script but it was poorly implemented and there exists the {VbanCmd}.apply() method already.
## [2.6.0] - 2026-02-26
### Added
- support for packet with [ident:1](https://github.com/onyx-and-iris/Voicemeeter-SDK/blob/3be2c1c36563afbd6df3da8436406c77d2cc1f10/VoicemeeterRemote.h#L982) in VBAN TEXT subprotocol.
- This includes Strip 3D, PEQ, comp, gate, denoiser and pitch parameters.
## [2.5.2] - 2025-01-25
### Changed

View File

@@ -8,19 +8,19 @@
# VBAN CMD
This python interface allows you to transmit Voicemeeter parameters over a network.
This python interface allows you to send Voicemeeter/Matrix commands over a network.
It may be used standalone or to extend the [Voicemeeter Remote Python API](https://github.com/onyx-and-iris/voicemeeter-api-python)
It offers the same public API as [Voicemeeter Remote Python API](https://github.com/onyx-and-iris/voicemeeter-api-python).
There is no support for audio transfer in this package, only parameters.
Only the VBAN SERVICE/TEXT subprotocols are supported, there is no support for AUDIO or MIDI in this package.
For an outline of past/future changes refer to: [CHANGELOG](CHANGELOG.md)
## Tested against
- Basic 1.0.8.8
- Banana 2.0.6.8
- Potato 3.0.2.8
- Basic 1.1.2.2
- Banana 2.1.2.2
- Potato 3.1.2.2
## Requirements
@@ -29,7 +29,9 @@ For an outline of past/future changes refer to: [CHANGELOG](CHANGELOG.md)
## Installation
`pip install vban-cmd`
```console
pip install vban-cmd
```
## `Use`
@@ -113,6 +115,8 @@ Pass the kind of Voicemeeter as an argument. KIND_ID may be:
- `banana`
- `potato`
A fourth kind `matrix` has been added, if you pass it as a KIND_ID you are expected to use the [{VbanCmd}.sendtext()](https://github.com/onyx-and-iris/vban-cmd-python?tab=readme-ov-file#vbansendtextscript) method for sending text requests.
## `Available commands`
### Strip
@@ -171,9 +175,7 @@ example:
print(vban.strip[4].comp.knob)
```
Strip Comp properties are defined as write only.
`knob` defined for all versions, all other parameters potato only.
Strip Comp `knob` is defined for all versions, all other parameters potato only.
##### Strip.Gate
@@ -193,9 +195,7 @@ example:
vban.strip[2].gate.attack = 300.8
```
Strip Gate properties are defined as write only, potato version only.
`knob` defined for all versions, all other parameters potato only.
Strip Gate `knob` is defined for all versions, all other parameters potato only.
##### Strip.Denoiser
@@ -212,7 +212,32 @@ The following properties are available.
- `on`: boolean
- `ab`: boolean
Strip EQ properties are defined as write only, potato version only.
example:
```python
vban.strip[0].eq.ab = True
```
##### Strip.EQ.Channel.Cell
The following properties are available.
- `on`: boolean
- `type`: int, from 0 up to 6
- `f`: float, from 20.0 up to 20_000.0
- `gain`: float, from -36.0 up to 18.0
- `q`: float, from 0.3 up to 100
example:
```python
vban.strip[0].eq.channel[0].cell[2].on = True
vban.strip[1].eq.channel[0].cell[2].f = 5000
```
Strip EQ parameters are defined for PhysicalStrips, potato version only.
Only channel[0] properties are readable over VBAN.
##### Gainlayers
@@ -400,8 +425,8 @@ You just need to define a key `extends` in the config TOML, that names the confi
Three example 'extender' configs are included with the repo. You may load them with:
```python
import voicemeeterlib
with voicemeeterlib.api('banana') as vm:
import vban_cmd
with vban_cmd.api('banana') as vm:
vm.apply_config('extender')
```
@@ -490,7 +515,8 @@ You may pass the following optional keyword arguments:
- `pdirty`: boolean=False, parameter updates
- `ldirty`: boolean=False, level updates
- `timeout`: int=5, amount of time (seconds) to wait for an incoming RT data packet (parameter states).
- `outbound`: boolean=False, set `True` if you are only interested in sending commands. (no rt packets will be received)
- `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.
#### `vban.pdirty`
@@ -508,6 +534,14 @@ Sends a script block as a string request, for example:
vban.sendtext('Strip[0].Mute=1;Bus[0].Mono=1')
```
You can even use it to send matrix commands:
```python
vban.sendtext('Point(ASIO128.IN[1..4],ASIO128.OUT[1]).dBGain = -3.0')
vban.sendtext('Command.Version = ?')
```
## Errors
- `errors.VBANCMDError`: Base VBANCMD Exception class.
@@ -528,13 +562,15 @@ with vban_cmd.api('banana', **opts) as vban:
...
```
## Tests
### Run tests
First make sure you installed the [development dependencies](https://github.com/onyx-and-iris/vban-cmd-python#installation)
Install [poetry](https://python-poetry.org/docs/#installation) and then:
Then from tests directory:
`pytest -v`
```powershell
poetry poe test-basic
poetry poe test-banana
poetry poe test-potato
```
## Resources

View File

@@ -1,4 +1,5 @@
import logging
import os
import tkinter as tk
from tkinter import ttk
@@ -100,7 +101,14 @@ class App(tk.Tk):
def main():
with vban_cmd.api('banana', ldirty=True) as vban:
KIND_ID = 'banana'
conn = {
'ip': os.environ.get('VBANCMD_IP', 'localhost'),
'port': int(os.environ.get('VBANCMD_PORT', 6980)),
'streamname': os.environ.get('VBANCMD_STREAMNAME', 'Command1'),
}
with vban_cmd.api(KIND_ID, ldirty=True, **conn) as vban:
app = App(vban)
app.mainloop()

View File

@@ -1,3 +1,4 @@
import os
import threading
from logging import config
@@ -92,8 +93,13 @@ class Observer:
def main():
KIND_ID = 'potato'
conn = {
'ip': os.environ.get('VBANCMD_IP', 'localhost'),
'port': int(os.environ.get('VBANCMD_PORT', 6980)),
'streamname': os.environ.get('VBANCMD_STREAMNAME', 'Command1'),
}
with vban_cmd.api(KIND_ID) as vban:
with vban_cmd.api(KIND_ID, **conn) as vban:
stop_event = threading.Event()
with Observer(vban, stop_event):

View File

@@ -1,4 +1,5 @@
import logging
import os
import vban_cmd
@@ -23,8 +24,13 @@ class App:
def main():
KIND_ID = 'banana'
conn = {
'ip': os.environ.get('VBANCMD_IP', 'localhost'),
'port': int(os.environ.get('VBANCMD_PORT', 6980)),
'streamname': os.environ.get('VBANCMD_STREAMNAME', 'Command1'),
}
with vban_cmd.api(KIND_ID, pdirty=True, ldirty=True) as vban:
with vban_cmd.api(KIND_ID, pdirty=True, ldirty=True, **conn) as vban:
App(vban)
while _ := input('Press <Enter> to exit\n'):

View File

@@ -1,6 +1,6 @@
[project]
name = "vban-cmd"
version = "2.5.2"
version = "2.7.1"
description = "Python interface for the VBAN RT Packet Service (Sendtext)"
authors = [{ name = "Onyx and Iris", email = "code@onyxandiris.online" }]
license = { text = "MIT" }

View File

@@ -1,10 +1,10 @@
import abc
import time
from abc import abstractmethod
from typing import Union
from .enums import NBS, BusModes
from .iremote import IRemote
from .meta import bus_mode_prop, channel_bool_prop, channel_label_prop
from .meta import bus_mode_prop, channel_bool_prop, channel_int_prop, channel_label_prop
class Bus(IRemote):
@@ -14,7 +14,7 @@ class Bus(IRemote):
Defines concrete implementation for bus
"""
@abstractmethod
@abc.abstractmethod
def __str__(self):
pass
@@ -90,20 +90,9 @@ class BusLevel(IRemote):
def getter(self):
"""Returns a tuple of level values for the channel."""
def fget(i):
return round((((1 << 16) - 1) - i) * -0.01, 1)
if not self._remote.stopped() and self._remote.event.ldirty:
return tuple(
fget(i)
for i in self._remote.cache['bus_level'][self.range[0] : self.range[-1]]
)
return tuple(
fget(i)
for i in self._remote._get_levels(self.public_packets[NBS.zero])[1][
self.range[0] : self.range[-1]
]
)
return self._remote.cache['bus_level'][self.range[0] : self.range[-1]]
return self.public_packets[NBS.zero].levels.bus[self.range[0] : self.range[-1]]
@property
def identifier(self) -> str:
@@ -128,45 +117,49 @@ class BusLevel(IRemote):
def _make_bus_mode_mixin():
"""Creates a mixin of Bus Modes."""
modestates = {
'normal': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'amix': [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
'repeat': [0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2],
'bmix': [1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],
'composite': [0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0],
'tvmix': [1, 0, 1, 4, 5, 4, 5, 0, 1, 0, 1],
'upmix21': [0, 2, 2, 4, 4, 6, 6, 0, 0, 2, 2],
'upmix41': [1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3],
'upmix61': [0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8],
'centeronly': [1, 0, 1, 0, 1, 0, 1, 8, 9, 8, 9],
'lfeonly': [0, 2, 2, 0, 0, 2, 2, 8, 8, 10, 10],
'rearonly': [1, 2, 3, 0, 1, 2, 3, 8, 9, 10, 11],
}
mode_names = [
'normal',
'amix',
'repeat',
'bmix',
'composite',
'tvmix',
'upmix21',
'upmix41',
'upmix61',
'centeronly',
'lfeonly',
'rearonly',
]
def identifier(self) -> str:
return f'bus[{self.index}].mode'
def get(self):
states = [
(
int.from_bytes(
self.public_packets[NBS.zero].busstate[self.index], 'little'
)
& val
)
>> 4
for val in self._modes.modevals
"""Get current bus mode using ChannelState for clean bit extraction."""
mode_cache_items = [
(k, v)
for k, v in self._remote.cache.items()
if k.startswith(f'{self.identifier}.') and v == 1
]
for k, v in modestates.items():
if states == v:
return k
if mode_cache_items:
latest_cached = mode_cache_items[-1][0]
mode_name = latest_cached.split('.')[-1]
return mode_name
bus_state = self.public_packets[NBS.zero].states.bus[self.index]
# Extract bus mode from bits 4-7 (mask 0xF0, shift right by 4)
mode_value = (bus_state._state & 0x000000F0) >> 4
return mode_names[mode_value] if mode_value < len(mode_names) else 'normal'
return type(
'BusModeMixin',
(IRemote,),
{
'identifier': property(identifier),
'modestates': modestates,
**{mode.name: bus_mode_prop(mode.name) for mode in BusModes},
'get': get,
},
@@ -188,7 +181,8 @@ def bus_factory(phys_bus, remote, i) -> Union[PhysicalBus, VirtualBus]:
'eq': BusEQ.make(remote, i),
'levels': BusLevel(remote, i),
'mode': BUSMODEMIXIN_cls(remote, i),
**{param: channel_bool_prop(param) for param in ['mute', 'mono']},
**{param: channel_bool_prop(param) for param in ('mute',)},
**{param: channel_int_prop(param) for param in ('mono',)},
'label': channel_label_prop(),
},
)(remote, i)

View File

@@ -1,4 +1,11 @@
from enum import IntEnum
from enum import Enum, IntEnum, unique
@unique
class KindId(Enum):
BASIC = 1
BANANA = 2
POTATO = 3
class NBS(IntEnum):
@@ -11,5 +18,3 @@ BusModes = IntEnum(
'normal amix bmix repeat composite tvmix upmix21 upmix41 upmix61 centeronly lfeonly rearonly',
start=0,
)
EQGains = IntEnum('EQGains', 'bass mid treble', start=0)

View File

@@ -1,5 +1,5 @@
import abc
import logging
from abc import abstractmethod
from enum import IntEnum
from functools import cached_property
from typing import Iterable
@@ -85,7 +85,7 @@ class FactoryBase(VbanCmd):
'channel': 0,
'ratelimit': 0.01,
'timeout': 5,
'outbound': False,
'disable_rt_listeners': False,
'sync': False,
'pdirty': False,
'ldirty': False,
@@ -115,7 +115,7 @@ class FactoryBase(VbanCmd):
)
@property
@abstractmethod
@abc.abstractmethod
def steps(self):
pass
@@ -202,7 +202,13 @@ def vbancmd_factory(kind_id: str, **kwargs) -> VbanCmd:
_factory = BasicFactory
case 'banana':
_factory = BananaFactory
case 'potato':
case 'potato' | 'matrix':
# matrix is a special kind where:
# - we don't need to scale the interface with the builder (in other words kind is arbitrary).
# - we don't ever need to use real-time listeners, so we disable them to avoid confusion
if kind_id == 'matrix':
kwargs['disable_rt_listeners'] = True
kind_id = 'potato'
_factory = PotatoFactory
case _:
raise ValueError(f"Unknown Voicemeeter kind '{kind_id}'")

View File

@@ -1,6 +1,6 @@
import abc
import logging
import time
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
logger = logging.getLogger(__name__)
@@ -78,7 +78,7 @@ class Modes:
)
class IRemote(metaclass=ABCMeta):
class IRemote(abc.ABC):
"""
Common interface between base class and extended (higher) classes
@@ -111,7 +111,7 @@ class IRemote(metaclass=ABCMeta):
return ''.join(cmd)
@property
@abstractmethod
@abc.abstractmethod
def identifier(self):
pass

View File

@@ -1,16 +1,9 @@
from dataclasses import dataclass
from enum import Enum, unique
from .enums import KindId
from .error import VBANCMDError
@unique
class KindId(Enum):
BASIC = 1
BANANA = 2
POTATO = 3
class SingletonType(type):
"""ensure only a single instance of a kind map object"""
@@ -22,12 +15,15 @@ class SingletonType(type):
return cls._instances[cls]
@dataclass
@dataclass(frozen=True)
class KindMapClass(metaclass=SingletonType):
name: str
ins: tuple
outs: tuple
vban: tuple
strip_channels: int
bus_channels: int
cells: int
@property
def phys_in(self):
@@ -65,28 +61,37 @@ class KindMapClass(metaclass=SingletonType):
return self.name.capitalize()
@dataclass
@dataclass(frozen=True)
class BasicMap(KindMapClass):
name: str
ins: tuple = (2, 1)
outs: tuple = (1, 1)
vban: tuple = (4, 4, 1, 1)
strip_channels: int = 0
bus_channels: int = 0
cells: int = 0
@dataclass
@dataclass(frozen=True)
class BananaMap(KindMapClass):
name: str
ins: tuple = (3, 2)
outs: tuple = (3, 2)
vban: tuple = (8, 8, 1, 1)
strip_channels: int = 0
bus_channels: int = 8
cells: int = 6
@dataclass
@dataclass(frozen=True)
class PotatoMap(KindMapClass):
name: str
ins: tuple = (5, 3)
outs: tuple = (5, 3)
vban: tuple = (8, 8, 1, 1)
strip_channels: int = 2
bus_channels: int = 8
cells: int = 6
def kind_factory(kind_id):

View File

@@ -1,7 +1,7 @@
from functools import partial
from .enums import NBS
from .util import cache_bool, cache_float, cache_string
from .enums import NBS, BusModes
from .util import cache_bool, cache_float, cache_int, cache_string
def channel_bool_prop(param):
@@ -11,17 +11,23 @@ def channel_bool_prop(param):
def fget(self):
cmd = self._cmd(param)
self.logger.debug(f'getter: {cmd}')
return (
not int.from_bytes(
getattr(
self.public_packets[NBS.zero],
f'{"strip" if "strip" in type(self).__name__.lower() else "bus"}state',
)[self.index],
'little',
)
& getattr(self._modes, f'_{param.lower()}')
== 0
states = self.public_packets[NBS.zero].states
channel_states = (
states.strip if 'strip' in type(self).__name__.lower() else states.bus
)
channel_state = channel_states[self.index]
if param.lower() == 'mute':
return channel_state.mute
elif param.lower() == 'solo':
return channel_state.solo
elif param.lower() == 'mono':
return channel_state.mono
elif param.lower() == 'mc':
return channel_state.mc
else:
return channel_state.get_mode(getattr(self._modes, f'_{param.lower()}'))
def fset(self, val):
self.setter(param, 1 if val else 0)
@@ -29,18 +35,46 @@ def channel_bool_prop(param):
return property(fget, fset)
def channel_int_prop(param):
"""meta function for channel integer parameters"""
@partial(cache_int, param=param)
def fget(self):
cmd = self._cmd(param)
self.logger.debug(f'getter: {cmd}')
states = self.public_packets[NBS.zero].states
channel_states = (
states.strip if 'strip' in type(self).__name__.lower() else states.bus
)
channel_state = channel_states[self.index]
# Special case: bus mono is an integer (0-2) encoded using bits 2 and 9
if param.lower() == 'mono' and 'bus' in type(self).__name__.lower():
bit_2 = (channel_state._state >> 2) & 1
bit_9 = (channel_state._state >> 9) & 1
return (bit_9 << 1) | bit_2
else:
return channel_state.get_mode_int(getattr(self._modes, f'_{param.lower()}'))
def fset(self, val):
self.setter(param, val)
return property(fget, fset)
def channel_label_prop():
"""meta function for channel label parameters"""
@partial(cache_string, param='label')
def fget(self) -> str:
return getattr(
self.public_packets[NBS.zero],
f'{"strip" if "strip" in type(self).__name__.lower() else "bus"}labels',
)[self.index]
if 'strip' in type(self).__name__.lower():
return self.public_packets[NBS.zero].labels.strip[self.index]
else:
return self.public_packets[NBS.zero].labels.bus[self.index]
def fset(self, val: str):
self.setter('label', str(val))
self.setter('label', f'"{val}"')
return property(fget, fset)
@@ -52,13 +86,10 @@ def strip_output_prop(param):
def fget(self):
cmd = self._cmd(param)
self.logger.debug(f'getter: {cmd}')
return (
not int.from_bytes(
self.public_packets[NBS.zero].stripstate[self.index], 'little'
)
& getattr(self._modes, f'_bus{param.lower()}')
== 0
)
strip_state = self.public_packets[NBS.zero].states.strip[self.index]
return strip_state.get_mode(getattr(self._modes, f'_bus{param.lower()}'))
def fset(self, val):
self.setter(param, 1 if val else 0)
@@ -73,16 +104,15 @@ def bus_mode_prop(param):
def fget(self):
cmd = self._cmd(param)
self.logger.debug(f'getter: {cmd}')
return [
(
int.from_bytes(
self.public_packets[NBS.zero].busstate[self.index], 'little'
)
& val
)
>> 4
for val in self._modes.modevals
] == self.modestates[param]
bus_state = self.public_packets[NBS.zero].states.bus[self.index]
# Extract current bus mode from bits 4-7
current_mode = (bus_state._state & 0x000000F0) >> 4
expected_mode = getattr(BusModes, param.lower())
return current_mode == expected_mode
def fset(self, val):
self.setter(param, 1 if val else 0)
@@ -106,13 +136,23 @@ def xy_prop(param):
def fget(self):
cmd = self._cmd(param)
self.logger.debug(f'getter: {cmd}')
_type, axis = param.split('_')
if self.public_packets[NBS.one] is None:
return 0.0
x, y = getattr(
self.public_packets[NBS.one].strips[self.index], f'position_{_type.lower()}'
)
return x if axis == 'x' else y
positions = self.public_packets[NBS.one].strips[self.index].positions
match param:
case 'pan_x':
return positions.pan_x
case 'pan_y':
return positions.pan_y
case 'color_x':
return positions.color_x
case 'color_y':
return positions.color_y
case 'fx1':
return positions.fx1
case 'fx2':
return positions.fx2
def fset(self, val):
self.setter(param, val)
@@ -129,12 +169,17 @@ def send_prop(param):
self.logger.debug(f'getter: {cmd}')
if self.public_packets[NBS.one] is None:
return 0.0
val = getattr(self.public_packets[NBS.one].strips[self.index], f'send_{param}')
sends = self.public_packets[NBS.one].strips[self.index].sends
match param:
case 'reverb' | 'fx1':
return val[0]
case 'delay' | 'fx2':
return val[1]
case 'reverb':
return sends.reverb
case 'delay':
return sends.delay
case 'fx1':
return sends.fx1
case 'fx2':
return sends.fx2
def fset(self, val):
self.setter(param, val)

View File

@@ -1,520 +0,0 @@
from dataclasses import dataclass
from .enums import NBS
from .kinds import KindMapClass
from .util import comp
VBAN_PROTOCOL_TXT = 0x40
VBAN_PROTOCOL_SERVICE = 0x60
VBAN_SERVICE_RTPACKETREGISTER = 32
VBAN_SERVICE_RTPACKET = 33
VBAN_SERVICE_MASK = 0xE0
MAX_PACKET_SIZE = 1436
HEADER_SIZE = 4 + 1 + 1 + 1 + 1 + 16
VMPARAMSTRIP_SIZE = 174
@dataclass
class VbanRtPacket:
"""Represents the body of a VBAN RT data packet"""
nbs: NBS
_kind: KindMapClass
_voicemeeterType: bytes # data[28:29]
_reserved: bytes # data[29:30]
_buffersize: bytes # data[30:32]
_voicemeeterVersion: bytes # data[32:36]
_optionBits: bytes # data[36:40]
_samplerate: bytes # data[40:44]
@dataclass
class VbanRtPacketNBS0(VbanRtPacket):
"""Represents the body of a VBAN RT data packet with NBS 0"""
_inputLeveldB100: bytes # data[44:112]
_outputLeveldB100: bytes # data[112:240]
_TransportBit: bytes # data[240:244]
_stripState: bytes # data[244:276]
_busState: bytes # data[276:308]
_stripGaindB100Layer1: bytes # data[308:324]
_stripGaindB100Layer2: bytes # data[324:340]
_stripGaindB100Layer3: bytes # data[340:356]
_stripGaindB100Layer4: bytes # data[356:372]
_stripGaindB100Layer5: bytes # data[372:388]
_stripGaindB100Layer6: bytes # data[388:404]
_stripGaindB100Layer7: bytes # data[404:420]
_stripGaindB100Layer8: bytes # data[420:436]
_busGaindB100: bytes # data[436:452]
_stripLabelUTF8c60: bytes # data[452:932]
_busLabelUTF8c60: bytes # data[932:1412]
@classmethod
def from_bytes(cls, nbs: NBS, kind: KindMapClass, data: bytes):
return cls(
nbs=nbs,
_kind=kind,
_voicemeeterType=data[28:29],
_reserved=data[29:30],
_buffersize=data[30:32],
_voicemeeterVersion=data[32:36],
_optionBits=data[36:40],
_samplerate=data[40:44],
_inputLeveldB100=data[44:112],
_outputLeveldB100=data[112:240],
_TransportBit=data[240:244],
_stripState=data[244:276],
_busState=data[276:308],
_stripGaindB100Layer1=data[308:324],
_stripGaindB100Layer2=data[324:340],
_stripGaindB100Layer3=data[340:356],
_stripGaindB100Layer4=data[356:372],
_stripGaindB100Layer5=data[372:388],
_stripGaindB100Layer6=data[388:404],
_stripGaindB100Layer7=data[404:420],
_stripGaindB100Layer8=data[420:436],
_busGaindB100=data[436:452],
_stripLabelUTF8c60=data[452:932],
_busLabelUTF8c60=data[932:1412],
)
def _generate_levels(self, levelarray) -> tuple:
return tuple(
int.from_bytes(levelarray[i : i + 2], 'little')
for i in range(0, len(levelarray), 2)
)
@property
def strip_levels(self):
return self._generate_levels(self._inputLeveldB100)
@property
def bus_levels(self):
return self._generate_levels(self._outputLeveldB100)
def pdirty(self, other) -> bool:
"""True iff any defined parameter has changed"""
return not (
self._stripState == other._stripState
and self._busState == other._busState
and self._stripGaindB100Layer1 == other._stripGaindB100Layer1
and self._stripGaindB100Layer2 == other._stripGaindB100Layer2
and self._stripGaindB100Layer3 == other._stripGaindB100Layer3
and self._stripGaindB100Layer4 == other._stripGaindB100Layer4
and self._stripGaindB100Layer5 == other._stripGaindB100Layer5
and self._stripGaindB100Layer6 == other._stripGaindB100Layer6
and self._stripGaindB100Layer7 == other._stripGaindB100Layer7
and self._stripGaindB100Layer8 == other._stripGaindB100Layer8
and self._busGaindB100 == other._busGaindB100
and self._stripLabelUTF8c60 == other._stripLabelUTF8c60
and self._busLabelUTF8c60 == other._busLabelUTF8c60
)
def ldirty(self, strip_cache, bus_cache) -> bool:
self._strip_comp, self._bus_comp = (
tuple(not val for val in comp(strip_cache, self.strip_levels)),
tuple(not val for val in comp(bus_cache, self.bus_levels)),
)
return any(any(li) for li in (self._strip_comp, self._bus_comp))
@property
def voicemeetertype(self) -> str:
"""returns voicemeeter type as a string"""
type_ = ('basic', 'banana', 'potato')
return type_[int.from_bytes(self._voicemeeterType, 'little') - 1]
@property
def voicemeeterversion(self) -> tuple:
"""returns voicemeeter version as a tuple"""
return tuple(
reversed(
tuple(
int.from_bytes(self._voicemeeterVersion[i : i + 1], 'little')
for i in range(4)
)
)
)
@property
def samplerate(self) -> int:
"""returns samplerate as an int"""
return int.from_bytes(self._samplerate, 'little')
@property
def inputlevels(self) -> tuple:
"""returns the entire level array across all inputs for a kind"""
return self.strip_levels[0 : self._kind.num_strip_levels]
@property
def outputlevels(self) -> tuple:
"""returns the entire level array across all outputs for a kind"""
return self.bus_levels[0 : self._kind.num_bus_levels]
@property
def stripstate(self) -> tuple:
"""returns tuple of strip states accessable through bit modes"""
return tuple(self._stripState[i : i + 4] for i in range(0, 32, 4))
@property
def busstate(self) -> tuple:
"""returns tuple of bus states accessable through bit modes"""
return tuple(self._busState[i : i + 4] for i in range(0, 32, 4))
"""
these functions return an array of gainlayers[i] across all strips
ie stripgainlayer1 = [strip[0].gainlayer[0], strip[1].gainlayer[0], strip[2].gainlayer[0]...]
"""
@property
def gainlayers(self) -> tuple:
"""returns tuple of all strip gain layers as tuples"""
return tuple(
tuple(
round(
int.from_bytes(
getattr(self, f'_stripGaindB100Layer{layer}')[i : i + 2],
'little',
signed=True,
)
* 0.01,
2,
)
for i in range(0, 16, 2)
)
for layer in range(1, 9)
)
@property
def busgain(self) -> tuple:
"""returns tuple of bus gains"""
return tuple(
round(
int.from_bytes(self._busGaindB100[i : i + 2], 'little', signed=True)
* 0.01,
2,
)
for i in range(0, 16, 2)
)
@property
def striplabels(self) -> tuple:
"""returns tuple of strip labels"""
return tuple(
self._stripLabelUTF8c60[i : i + 60].decode().split('\x00')[0]
for i in range(0, 480, 60)
)
@property
def buslabels(self) -> tuple:
"""returns tuple of bus labels"""
return tuple(
self._busLabelUTF8c60[i : i + 60].decode().split('\x00')[0]
for i in range(0, 480, 60)
)
@dataclass
class VbanVMParamStrip:
"""Represents the VBAN_VMPARAMSTRIP_PACKET structure"""
_mode: bytes # long = 4 bytes data[0:4]
_dblevel: bytes # float = 4 bytes data[4:8]
_audibility: bytes # short = 2 bytes data[8:10]
_pos3D_x: bytes # short = 2 bytes data[10:12]
_pos3D_y: bytes # short = 2 bytes data[12:14]
_posColor_x: bytes # short = 2 bytes data[14:16]
_posColor_y: bytes # short = 2 bytes data[16:18]
_EQgain1: bytes # short = 2 bytes data[18:20]
_EQgain2: bytes # short = 2 bytes data[20:22]
_EQgain3: bytes # short = 2 bytes data[22:24]
# First channel parametric EQ
_PEQ_eqOn: bytes # 6 * char = 6 bytes data[24:30]
_PEQ_eqtype: bytes # 6 * char = 6 bytes data[30:36]
_PEQ_eqgain: bytes # 6 * float = 24 bytes data[36:60]
_PEQ_eqfreq: bytes # 6 * float = 24 bytes data[60:84]
_PEQ_eqq: bytes # 6 * float = 24 bytes data[84:108]
_audibility_c: bytes # short = 2 bytes data[108:110]
_audibility_g: bytes # short = 2 bytes data[110:112]
_audibility_d: bytes # short = 2 bytes data[112:114]
_posMod_x: bytes # short = 2 bytes data[114:116]
_posMod_y: bytes # short = 2 bytes data[116:118]
_send_reverb: bytes # short = 2 bytes data[118:120]
_send_delay: bytes # short = 2 bytes data[120:122]
_send_fx1: bytes # short = 2 bytes data[122:124]
_send_fx2: bytes # short = 2 bytes data[124:126]
_dblimit: bytes # short = 2 bytes data[126:128]
_nKaraoke: bytes # short = 2 bytes data[128:130]
_COMP_gain_in: bytes # short = 2 bytes data[130:132]
_COMP_attack_ms: bytes # short = 2 bytes data[132:134]
_COMP_release_ms: bytes # short = 2 bytes data[134:136]
_COMP_n_knee: bytes # short = 2 bytes data[136:138]
_COMP_comprate: bytes # short = 2 bytes data[138:140]
_COMP_threshold: bytes # short = 2 bytes data[140:142]
_COMP_c_enabled: bytes # short = 2 bytes data[142:144]
_COMP_c_auto: bytes # short = 2 bytes data[144:146]
_COMP_gain_out: bytes # short = 2 bytes data[146:148]
_GATE_dBThreshold_in: bytes # short = 2 bytes data[148:150]
_GATE_dBDamping_max: bytes # short = 2 bytes data[150:152]
_GATE_BP_Sidechain: bytes # short = 2 bytes data[152:154]
_GATE_attack_ms: bytes # short = 2 bytes data[154:156]
_GATE_hold_ms: bytes # short = 2 bytes data[156:158]
_GATE_release_ms: bytes # short = 2 bytes data[158:160]
_DenoiserThreshold: bytes # short = 2 bytes data[160:162]
_PitchEnabled: bytes # short = 2 bytes data[162:164]
_Pitch_DryWet: bytes # short = 2 bytes data[164:166]
_Pitch_Value: bytes # short = 2 bytes data[166:168]
_Pitch_formant_lo: bytes # short = 2 bytes data[168:170]
_Pitch_formant_med: bytes # short = 2 bytes data[170:172]
_Pitch_formant_high: bytes # short = 2 bytes data[172:174]
@classmethod
def from_bytes(cls, data: bytes):
return cls(
_mode=data[0:4],
_dblevel=data[4:8],
_audibility=data[8:10],
_pos3D_x=data[10:12],
_pos3D_y=data[12:14],
_posColor_x=data[14:16],
_posColor_y=data[16:18],
_EQgain1=data[18:20],
_EQgain2=data[20:22],
_EQgain3=data[22:24],
_PEQ_eqOn=data[24:30],
_PEQ_eqtype=data[30:36],
_PEQ_eqgain=data[36:60],
_PEQ_eqfreq=data[60:84],
_PEQ_eqq=data[84:108],
_audibility_c=data[108:110],
_audibility_g=data[110:112],
_audibility_d=data[112:114],
_posMod_x=data[114:116],
_posMod_y=data[116:118],
_send_reverb=data[118:120],
_send_delay=data[120:122],
_send_fx1=data[122:124],
_send_fx2=data[124:126],
_dblimit=data[126:128],
_nKaraoke=data[128:130],
_COMP_gain_in=data[130:132],
_COMP_attack_ms=data[132:134],
_COMP_release_ms=data[134:136],
_COMP_n_knee=data[136:138],
_COMP_comprate=data[138:140],
_COMP_threshold=data[140:142],
_COMP_c_enabled=data[142:144],
_COMP_c_auto=data[144:146],
_COMP_gain_out=data[146:148],
_GATE_dBThreshold_in=data[148:150],
_GATE_dBDamping_max=data[150:152],
_GATE_BP_Sidechain=data[152:154],
_GATE_attack_ms=data[154:156],
_GATE_hold_ms=data[156:158],
_GATE_release_ms=data[158:160],
_DenoiserThreshold=data[160:162],
_PitchEnabled=data[162:164],
_Pitch_DryWet=data[164:166],
_Pitch_Value=data[166:168],
_Pitch_formant_lo=data[168:170],
_Pitch_formant_med=data[170:172],
_Pitch_formant_high=data[172:174],
)
@property
def mode(self) -> int:
return int.from_bytes(self._mode, 'little')
@property
def position_pan(self) -> tuple[int, int]:
return (
round(int.from_bytes(self._pos3D_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._pos3D_y, 'little', signed=True) * 0.01, 2),
)
@property
def position_color(self) -> tuple[int, int]:
return (
round(int.from_bytes(self._posColor_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posColor_y, 'little', signed=True) * 0.01, 2),
)
@property
def position_fx(self) -> tuple[int, int]:
return (
round(int.from_bytes(self._posMod_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posMod_y, 'little', signed=True) * 0.01, 2),
)
@property
def send_reverb(self) -> tuple[float, float]:
return (
round(int.from_bytes(self._send_reverb, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._send_delay, 'little', signed=True) * 0.01, 2),
)
send_delay = send_reverb
@property
def send_fx1(self) -> tuple[float, float]:
return (
round(int.from_bytes(self._send_fx1, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._send_fx2, 'little', signed=True) * 0.01, 2),
)
send_fx2 = send_fx1
@property
def eqgains(self) -> tuple[float, float, float]:
return tuple(
round(
int.from_bytes(getattr(self, f'_EQgain{i}'), 'little', signed=True)
* 0.01,
2,
)
for i in range(1, 4)
)
@property
def karaoke(self) -> int:
return int.from_bytes(self._nKaraoke, 'little')
@dataclass
class VbanRtPacketNBS1(VbanRtPacket):
"""Represents the body of a VBAN RT data packet with NBS 1"""
strips: tuple[VbanVMParamStrip, ...]
@classmethod
def from_bytes(
cls,
nbs: NBS,
kind: KindMapClass,
data: bytes,
):
return cls(
nbs=nbs,
_kind=kind,
_voicemeeterType=data[28:29],
_reserved=data[29:30],
_buffersize=data[30:32],
_voicemeeterVersion=data[32:36],
_optionBits=data[36:40],
_samplerate=data[40:44],
strips=tuple(
VbanVMParamStrip.from_bytes(
data[44 + i * VMPARAMSTRIP_SIZE : 44 + (i + 1) * VMPARAMSTRIP_SIZE]
)
for i in range(16)
),
)
@dataclass
class SubscribeHeader:
"""Represents the header of an RT subscription packet"""
ident: NBS = NBS.zero
name = 'Register-RTP'
timeout = 15
vban: bytes = 'VBAN'.encode()
format_sr: bytes = (VBAN_PROTOCOL_SERVICE).to_bytes(1, 'little')
format_nbs: bytes = (ident.value & 0xFF).to_bytes(1, 'little')
format_nbc: bytes = (VBAN_SERVICE_RTPACKETREGISTER).to_bytes(1, 'little')
format_bit: bytes = (timeout & 0xFF).to_bytes(1, 'little') # timeout
streamname: bytes = name.encode('ascii') + bytes(16 - len(name))
@classmethod
def to_bytes(cls, nbs: NBS, framecounter: int) -> bytes:
header = cls(ident=nbs)
data = bytearray()
data.extend(header.vban)
data.extend(header.format_sr)
data.extend(header.format_nbs)
data.extend(header.format_nbc)
data.extend(header.format_bit)
data.extend(header.streamname)
data.extend(framecounter.to_bytes(4, 'little'))
return bytes(data)
@dataclass
class VbanRtPacketHeader:
"""Represents the header of an RT response packet"""
name: str = 'Voicemeeter-RTP'
vban: bytes = 'VBAN'.encode()
format_sr: bytes = (VBAN_PROTOCOL_SERVICE).to_bytes(1, 'little')
format_nbs: bytes = (0).to_bytes(1, 'little')
format_nbc: bytes = (VBAN_SERVICE_RTPACKET).to_bytes(1, 'little')
format_bit: bytes = (0).to_bytes(1, 'little')
streamname: bytes = name.encode('ascii') + bytes(16 - len(name))
@classmethod
def from_bytes(cls, data: bytes):
if len(data) < HEADER_SIZE:
raise ValueError('Data is too short to be a valid VbanRTPPacketHeader')
vban = data[0:4]
format_sr = data[4]
format_nbs = data[5]
format_nbc = data[6]
format_bit = data[7]
name = data[8:24].rstrip(b'\x00').decode('utf-8')
return cls(
name=name,
vban=vban,
format_sr=format_sr & VBAN_SERVICE_MASK,
format_nbs=format_nbs,
format_nbc=format_nbc,
format_bit=format_bit,
)
@dataclass
class RequestHeader:
"""Represents the header of an RT request packet"""
name: str
bps_index: int
channel: int
vban: bytes = 'VBAN'.encode()
nbs: bytes = (0).to_bytes(1, 'little')
bit: bytes = (0x10).to_bytes(1, 'little')
framecounter: bytes = (0).to_bytes(4, 'little')
@property
def sr(self):
return (VBAN_PROTOCOL_TXT + self.bps_index).to_bytes(1, 'little')
@property
def nbc(self):
return (self.channel).to_bytes(1, 'little')
@property
def streamname(self):
return self.name.encode() + bytes(16 - len(self.name))
@classmethod
def to_bytes(
cls, name: str, bps_index: int, channel: int, framecounter: int
) -> bytes:
header = cls(
name=name, bps_index=bps_index, channel=channel, framecounter=framecounter
)
data = bytearray()
data.extend(header.vban)
data.extend(header.sr)
data.extend(header.nbs)
data.extend(header.nbc)
data.extend(header.bit)
data.extend(header.streamname)
data.extend(header.framecounter.to_bytes(4, 'little'))
return bytes(data)

266
vban_cmd/packet/headers.py Normal file
View File

@@ -0,0 +1,266 @@
from dataclasses import dataclass
from vban_cmd.enums import NBS
from vban_cmd.kinds import KindMapClass
VBAN_PROTOCOL_TXT = 0x40
VBAN_PROTOCOL_SERVICE = 0x60
VBAN_SERVICE_RTPACKETREGISTER = 32
VBAN_SERVICE_RTPACKET = 33
VBAN_SERVICE_MASK = 0xE0
VBAN_PROTOCOL_MASK = 0xE0
VBAN_SERVICE_REQUESTREPLY = 0x02
VBAN_SERVICE_FNCT_REPLY = 0x02
MAX_PACKET_SIZE = 1436
HEADER_SIZE = 4 + 1 + 1 + 1 + 1 + 16
@dataclass
class VbanPacket:
"""Represents the header of an incoming VBAN data packet"""
nbs: NBS
_kind: KindMapClass
_voicemeeterType: bytes
_reserved: bytes
_buffersize: bytes
_voicemeeterVersion: bytes
_optionBits: bytes
_samplerate: bytes
@property
def voicemeetertype(self) -> str:
"""returns voicemeeter type as a string"""
return ['', 'basic', 'banana', 'potato'][
int.from_bytes(self._voicemeeterType, 'little')
]
@property
def voicemeeterversion(self) -> tuple:
"""returns voicemeeter version as a tuple"""
return tuple(self._voicemeeterVersion[i] for i in range(3, -1, -1))
@property
def samplerate(self) -> int:
"""returns samplerate as an int"""
return int.from_bytes(self._samplerate, 'little')
@dataclass
class VbanSubscribeHeader:
"""Represents the header of a subscription packet"""
nbs: NBS = NBS.zero
name: str = 'Register-RTP'
timeout: int = 15
@property
def vban(self) -> bytes:
return b'VBAN'
@property
def format_sr(self) -> bytes:
return VBAN_PROTOCOL_SERVICE.to_bytes(1, 'little')
@property
def format_nbs(self) -> bytes:
return (self.nbs.value & 0xFF).to_bytes(1, 'little')
@property
def format_nbc(self) -> bytes:
return VBAN_SERVICE_RTPACKETREGISTER.to_bytes(1, 'little')
@property
def format_bit(self) -> bytes:
return (self.timeout & 0xFF).to_bytes(1, 'little')
@property
def streamname(self) -> bytes:
return self.name.encode('ascii') + bytes(16 - len(self.name))
@classmethod
def to_bytes(cls, nbs: NBS, framecounter: int) -> bytes:
header = cls(nbs=nbs)
data = bytearray()
data.extend(header.vban)
data.extend(header.format_sr)
data.extend(header.format_nbs)
data.extend(header.format_nbc)
data.extend(header.format_bit)
data.extend(header.streamname)
data.extend(framecounter.to_bytes(4, 'little'))
return bytes(data)
def _parse_vban_service_header(data: bytes) -> dict:
"""Common parsing and validation for VBAN service protocol headers."""
if len(data) < HEADER_SIZE:
raise ValueError('Data is too short to be a valid VBAN header')
if data[:4] != b'VBAN':
raise ValueError('Invalid VBAN magic bytes')
format_sr = data[4]
format_nbs = data[5]
format_nbc = data[6]
format_bit = data[7]
# Verify this is a service protocol packet
protocol = format_sr & VBAN_PROTOCOL_MASK
if protocol != VBAN_PROTOCOL_SERVICE:
raise ValueError(f'Not a service protocol packet: {protocol:02x}')
# Extract stream name and frame counter
name = data[8:24].rstrip(b'\x00').decode('utf-8', errors='ignore')
framecounter = int.from_bytes(data[24:28], 'little')
return {
'format_sr': format_sr,
'format_nbs': format_nbs,
'format_nbc': format_nbc,
'format_bit': format_bit,
'name': name,
'framecounter': framecounter,
}
@dataclass
class VbanResponseHeader:
"""Represents the header of a response packet"""
name: str = 'Voicemeeter-RTP'
format_sr: int = VBAN_PROTOCOL_SERVICE
format_nbs: int = 0
format_nbc: int = VBAN_SERVICE_RTPACKET
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') + bytes(16 - len(self.name))
@classmethod
def from_bytes(cls, data: bytes):
"""Parse a VbanResponseHeader from bytes."""
parsed = _parse_vban_service_header(data)
# Validate this is an RTPacket response
if parsed['format_nbc'] != VBAN_SERVICE_RTPACKET:
raise ValueError(
f'Not an RTPacket response packet: {parsed["format_nbc"]:02x}'
)
return cls(**parsed)
@dataclass
class VbanMatrixResponseHeader:
"""Represents the header of a matrix response packet"""
name: str = 'Request Reply'
format_sr: int = VBAN_PROTOCOL_SERVICE
format_nbs: int = VBAN_SERVICE_FNCT_REPLY
format_nbc: int = VBAN_SERVICE_REQUESTREPLY
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 matrix response packet from bytes."""
parsed = _parse_vban_service_header(data)
# Validate this is a service reply packet
if parsed['format_nbs'] != VBAN_SERVICE_FNCT_REPLY:
raise ValueError(f'Not a service reply packet: {parsed["format_nbs"]:02x}')
return cls(**parsed)
@classmethod
def extract_payload(cls, data: bytes) -> str:
"""Extract the text payload from a matrix response packet."""
if len(data) <= HEADER_SIZE:
return ''
payload_bytes = data[HEADER_SIZE:]
return payload_bytes.rstrip(b'\x00').decode('utf-8', errors='ignore')
@classmethod
def parse_response(cls, data: bytes) -> tuple['VbanMatrixResponseHeader', str]:
"""Parse a complete matrix response packet returning header and payload."""
header = cls.from_bytes(data)
payload = cls.extract_payload(data)
return header, payload
@dataclass
class VbanRequestHeader:
"""Represents the header of a request packet"""
name: str
bps_index: int
channel: int
framecounter: int = 0
@property
def vban(self) -> bytes:
return b'VBAN'
@property
def sr(self) -> bytes:
return (VBAN_PROTOCOL_TXT + self.bps_index).to_bytes(1, 'little')
@property
def nbs(self) -> bytes:
return (0).to_bytes(1, 'little')
@property
def nbc(self) -> bytes:
return (self.channel).to_bytes(1, 'little')
@property
def bit(self) -> bytes:
return (0x10).to_bytes(1, 'little')
@property
def streamname(self) -> bytes:
return self.name.encode() + bytes(16 - len(self.name))
@classmethod
def to_bytes(
cls, name: str, bps_index: int, channel: int, framecounter: int
) -> bytes:
header = cls(
name=name, bps_index=bps_index, channel=channel, framecounter=framecounter
)
data = bytearray()
data.extend(header.vban)
data.extend(header.sr)
data.extend(header.nbs)
data.extend(header.nbc)
data.extend(header.bit)
data.extend(header.streamname)
data.extend(header.framecounter.to_bytes(4, 'little'))
return bytes(data)
@classmethod
def encode_with_payload(
cls, name: str, bps_index: int, channel: int, framecounter: int, payload: str
) -> bytes:
"""Creates the complete packet with header and payload."""
return cls.to_bytes(name, bps_index, channel, framecounter) + payload.encode()

288
vban_cmd/packet/nbs0.py Normal file
View File

@@ -0,0 +1,288 @@
from dataclasses import dataclass
from typing import NamedTuple
from vban_cmd.enums import NBS
from vban_cmd.kinds import KindMapClass
from vban_cmd.util import comp
from .headers import VbanPacket
class Levels(NamedTuple):
strip: tuple[float, ...]
bus: tuple[float, ...]
class ChannelState:
"""Represents the processed state of a single strip or bus channel"""
def __init__(self, state_bytes: bytes):
# Convert 4-byte state to integer once for efficient lookups
self._state = int.from_bytes(state_bytes, 'little')
def get_mode(self, mode_value: int) -> bool:
"""Get boolean state for a specific mode"""
return (self._state & mode_value) != 0
def get_mode_int(self, mode_value: int) -> int:
"""Get integer state for a specific mode"""
return self._state & mode_value
# Common boolean modes
@property
def mute(self) -> bool:
return (self._state & 0x00000001) != 0
@property
def solo(self) -> bool:
return (self._state & 0x00000002) != 0
@property
def mono(self) -> bool:
return (self._state & 0x00000004) != 0
@property
def mc(self) -> bool:
return (self._state & 0x00000008) != 0
# EQ modes
@property
def eq_on(self) -> bool:
return (self._state & 0x00000100) != 0
@property
def eq_ab(self) -> bool:
return (self._state & 0x00000800) != 0
# Bus assignments (strip to bus routing)
@property
def busa1(self) -> bool:
return (self._state & 0x00001000) != 0
@property
def busa2(self) -> bool:
return (self._state & 0x00002000) != 0
@property
def busa3(self) -> bool:
return (self._state & 0x00004000) != 0
@property
def busa4(self) -> bool:
return (self._state & 0x00008000) != 0
@property
def busb1(self) -> bool:
return (self._state & 0x00010000) != 0
@property
def busb2(self) -> bool:
return (self._state & 0x00020000) != 0
@property
def busb3(self) -> bool:
return (self._state & 0x00040000) != 0
class States(NamedTuple):
strip: tuple[ChannelState, ...]
bus: tuple[ChannelState, ...]
class Labels(NamedTuple):
strip: tuple[str, ...]
bus: tuple[str, ...]
@dataclass
class VbanPacketNBS0(VbanPacket):
"""Represents the body of a VBAN data packet with ident:0"""
_inputLeveldB100: bytes
_outputLeveldB100: bytes
_TransportBit: bytes
_stripState: bytes
_busState: bytes
_stripGaindB100Layer1: bytes
_stripGaindB100Layer2: bytes
_stripGaindB100Layer3: bytes
_stripGaindB100Layer4: bytes
_stripGaindB100Layer5: bytes
_stripGaindB100Layer6: bytes
_stripGaindB100Layer7: bytes
_stripGaindB100Layer8: bytes
_busGaindB100: bytes
_stripLabelUTF8c60: bytes
_busLabelUTF8c60: bytes
@classmethod
def from_bytes(cls, nbs: NBS, kind: KindMapClass, data: bytes):
return cls(
nbs=nbs,
_kind=kind,
_voicemeeterType=data[28:29],
_reserved=data[29:30],
_buffersize=data[30:32],
_voicemeeterVersion=data[32:36],
_optionBits=data[36:40],
_samplerate=data[40:44],
_inputLeveldB100=data[44:112],
_outputLeveldB100=data[112:240],
_TransportBit=data[240:244],
_stripState=data[244:276],
_busState=data[276:308],
_stripGaindB100Layer1=data[308:324],
_stripGaindB100Layer2=data[324:340],
_stripGaindB100Layer3=data[340:356],
_stripGaindB100Layer4=data[356:372],
_stripGaindB100Layer5=data[372:388],
_stripGaindB100Layer6=data[388:404],
_stripGaindB100Layer7=data[404:420],
_stripGaindB100Layer8=data[420:436],
_busGaindB100=data[436:452],
_stripLabelUTF8c60=data[452:932],
_busLabelUTF8c60=data[932:1412],
)
def pdirty(self, other) -> bool:
"""True iff any defined parameter has changed"""
self_gains = (
self._stripGaindB100Layer1
+ self._stripGaindB100Layer2
+ self._stripGaindB100Layer3
+ self._stripGaindB100Layer4
+ self._stripGaindB100Layer5
+ self._stripGaindB100Layer6
+ self._stripGaindB100Layer7
+ self._stripGaindB100Layer8
)
other_gains = (
other._stripGaindB100Layer1
+ other._stripGaindB100Layer2
+ other._stripGaindB100Layer3
+ other._stripGaindB100Layer4
+ other._stripGaindB100Layer5
+ other._stripGaindB100Layer6
+ other._stripGaindB100Layer7
+ other._stripGaindB100Layer8
)
return (
self._stripState != other._stripState
or self._busState != other._busState
or self_gains != other_gains
or self._busGaindB100 != other._busGaindB100
or self._stripLabelUTF8c60 != other._stripLabelUTF8c60
or self._busLabelUTF8c60 != other._busLabelUTF8c60
)
def ldirty(self, strip_cache, bus_cache) -> bool:
"""True iff any level has changed, ignoring changes when levels are very quiet"""
self._strip_comp, self._bus_comp = (
tuple(not val for val in comp(strip_cache, self.strip_levels)),
tuple(not val for val in comp(bus_cache, self.bus_levels)),
)
return any(self._strip_comp) or any(self._bus_comp)
@property
def strip_levels(self) -> tuple[float, ...]:
"""Returns strip levels in dB"""
return tuple(
round(
int.from_bytes(self._inputLeveldB100[i : i + 2], 'little', signed=True)
* 0.01,
1,
)
for i in range(0, len(self._inputLeveldB100), 2)
)[: self._kind.num_strip_levels]
@property
def bus_levels(self) -> tuple[float, ...]:
"""Returns bus levels in dB"""
return tuple(
round(
int.from_bytes(self._outputLeveldB100[i : i + 2], 'little', signed=True)
* 0.01,
1,
)
for i in range(0, len(self._outputLeveldB100), 2)
)[: self._kind.num_bus_levels]
@property
def levels(self) -> Levels:
"""Returns strip and bus levels as a namedtuple"""
return Levels(strip=self.strip_levels, bus=self.bus_levels)
@property
def states(self) -> States:
"""returns States object with processed strip and bus channel states"""
return States(
strip=tuple(
ChannelState(self._stripState[i : i + 4]) for i in range(0, 32, 4)
),
bus=tuple(ChannelState(self._busState[i : i + 4]) for i in range(0, 32, 4)),
)
@property
def gainlayers(self) -> tuple:
"""returns tuple of all strip gain layers as tuples"""
return tuple(
tuple(
round(
int.from_bytes(
getattr(self, f'_stripGaindB100Layer{layer}')[i : i + 2],
'little',
signed=True,
)
* 0.01,
2,
)
for i in range(0, 16, 2)
)
for layer in range(1, 9)
)
@property
def busgain(self) -> tuple:
"""returns tuple of bus gains"""
return tuple(
round(
int.from_bytes(self._busGaindB100[i : i + 2], 'little', signed=True)
* 0.01,
2,
)
for i in range(0, 16, 2)
)
@property
def labels(self) -> Labels:
"""returns Labels namedtuple of strip and bus labels"""
def _extract_labels_from_bytes(label_bytes: bytes) -> tuple[str, ...]:
"""Extract null-terminated UTF-8 labels from 60-byte chunks"""
labels = []
for i in range(0, len(label_bytes), 60):
chunk = label_bytes[i : i + 60]
null_pos = chunk.find(b'\x00')
if null_pos == -1:
try:
label = chunk.decode('utf-8', errors='replace').rstrip('\x00')
except UnicodeDecodeError:
label = ''
else:
try:
label = (
chunk[:null_pos].decode('utf-8', errors='replace')
if null_pos > 0
else ''
)
except UnicodeDecodeError:
label = ''
labels.append(label)
return tuple(labels)
return Labels(
strip=_extract_labels_from_bytes(self._stripLabelUTF8c60),
bus=_extract_labels_from_bytes(self._busLabelUTF8c60),
)

357
vban_cmd/packet/nbs1.py Normal file
View File

@@ -0,0 +1,357 @@
import struct
from dataclasses import dataclass
from typing import NamedTuple
from vban_cmd.enums import NBS
from vban_cmd.kinds import KindMapClass
from .headers import VbanPacket
VMPARAMSTRIP_SIZE = 174
class Audibility(NamedTuple):
knob: float
comp: float
gate: float
denoiser: float
class Positions(NamedTuple):
pan_x: float
pan_y: float
color_x: float
color_y: float
fx1: float
fx2: float
class EqGains(NamedTuple):
bass: float
mid: float
treble: float
class ParametricEQSettings(NamedTuple):
on: bool
type: int
gain: float
freq: float
q: float
class Sends(NamedTuple):
reverb: float
delay: float
fx1: float
fx2: float
class CompressorSettings(NamedTuple):
gain_in: float
attack_ms: float
release_ms: float
n_knee: float
ratio: float
threshold: float
c_enabled: bool
makeup: bool
gain_out: float
class GateSettings(NamedTuple):
threshold_in: float
damping_max: float
bp_sidechain: bool
attack_ms: float
hold_ms: float
release_ms: float
class DenoiserSettings(NamedTuple):
threshold: float
class PitchSettings(NamedTuple):
enabled: bool
dry_wet: float
value: float
formant_lo: float
formant_med: float
formant_high: float
@dataclass
class VbanVMParamStrip:
"""Represents the VBAN_VMPARAMSTRIP_PACKET structure"""
_mode: bytes
_dblevel: bytes
_audibility: bytes
_pos3D_x: bytes
_pos3D_y: bytes
_posColor_x: bytes
_posColor_y: bytes
_EQgain1: bytes
_EQgain2: bytes
_EQgain3: bytes
# First channel parametric EQ
_PEQ_eqOn: bytes
_PEQ_eqtype: bytes
_PEQ_eqgain: bytes
_PEQ_eqfreq: bytes
_PEQ_eqq: bytes
_audibility_c: bytes
_audibility_g: bytes
_audibility_d: bytes
_posMod_x: bytes
_posMod_y: bytes
_send_reverb: bytes
_send_delay: bytes
_send_fx1: bytes
_send_fx2: bytes
_dblimit: bytes
_nKaraoke: bytes
_COMP_gain_in: bytes
_COMP_attack_ms: bytes
_COMP_release_ms: bytes
_COMP_n_knee: bytes
_COMP_comprate: bytes
_COMP_threshold: bytes
_COMP_c_enabled: bytes
_COMP_c_auto: bytes
_COMP_gain_out: bytes
_GATE_dBThreshold_in: bytes
_GATE_dBDamping_max: bytes
_GATE_BP_Sidechain: bytes
_GATE_attack_ms: bytes
_GATE_hold_ms: bytes
_GATE_release_ms: bytes
_DenoiserThreshold: bytes
_PitchEnabled: bytes
_Pitch_DryWet: bytes
_Pitch_Value: bytes
_Pitch_formant_lo: bytes
_Pitch_formant_med: bytes
_Pitch_formant_high: bytes
@classmethod
def from_bytes(cls, data: bytes):
return cls(
_mode=data[0:4],
_dblevel=data[4:8],
_audibility=data[8:10],
_pos3D_x=data[10:12],
_pos3D_y=data[12:14],
_posColor_x=data[14:16],
_posColor_y=data[16:18],
_EQgain1=data[18:20],
_EQgain2=data[20:22],
_EQgain3=data[22:24],
_PEQ_eqOn=data[24:30],
_PEQ_eqtype=data[30:36],
_PEQ_eqgain=data[36:60],
_PEQ_eqfreq=data[60:84],
_PEQ_eqq=data[84:108],
_audibility_c=data[108:110],
_audibility_g=data[110:112],
_audibility_d=data[112:114],
_posMod_x=data[114:116],
_posMod_y=data[116:118],
_send_reverb=data[118:120],
_send_delay=data[120:122],
_send_fx1=data[122:124],
_send_fx2=data[124:126],
_dblimit=data[126:128],
_nKaraoke=data[128:130],
_COMP_gain_in=data[130:132],
_COMP_attack_ms=data[132:134],
_COMP_release_ms=data[134:136],
_COMP_n_knee=data[136:138],
_COMP_comprate=data[138:140],
_COMP_threshold=data[140:142],
_COMP_c_enabled=data[142:144],
_COMP_c_auto=data[144:146],
_COMP_gain_out=data[146:148],
_GATE_dBThreshold_in=data[148:150],
_GATE_dBDamping_max=data[150:152],
_GATE_BP_Sidechain=data[152:154],
_GATE_attack_ms=data[154:156],
_GATE_hold_ms=data[156:158],
_GATE_release_ms=data[158:160],
_DenoiserThreshold=data[160:162],
_PitchEnabled=data[162:164],
_Pitch_DryWet=data[164:166],
_Pitch_Value=data[166:168],
_Pitch_formant_lo=data[168:170],
_Pitch_formant_med=data[170:172],
_Pitch_formant_high=data[172:174],
)
@property
def mode(self) -> int:
return int.from_bytes(self._mode, 'little')
@property
def audibility(self) -> Audibility:
return Audibility(
round(int.from_bytes(self._audibility, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._audibility_c, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._audibility_g, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._audibility_d, 'little', signed=True) * 0.01, 2),
)
@property
def positions(self) -> Positions:
return Positions(
round(int.from_bytes(self._pos3D_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._pos3D_y, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posColor_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posColor_y, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posMod_x, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._posMod_y, 'little', signed=True) * 0.01, 2),
)
@property
def eqgains(self) -> EqGains:
return EqGains(
*[
round(
int.from_bytes(getattr(self, f'_EQgain{i}'), 'little', signed=True)
* 0.01,
2,
)
for i in range(1, 4)
]
)
@property
def parametric_eq(self) -> tuple[ParametricEQSettings, ...]:
return tuple(
ParametricEQSettings(
on=bool(int.from_bytes(self._PEQ_eqOn[i : i + 1], 'little')),
type=int.from_bytes(self._PEQ_eqtype[i : i + 1], 'little'),
freq=struct.unpack('<f', self._PEQ_eqfreq[i * 4 : (i + 1) * 4])[0],
gain=struct.unpack('<f', self._PEQ_eqgain[i * 4 : (i + 1) * 4])[0],
q=struct.unpack('<f', self._PEQ_eqq[i * 4 : (i + 1) * 4])[0],
)
for i in range(6)
)
@property
def sends(self) -> Sends:
return Sends(
round(int.from_bytes(self._send_reverb, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._send_delay, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._send_fx1, 'little', signed=True) * 0.01, 2),
round(int.from_bytes(self._send_fx2, 'little', signed=True) * 0.01, 2),
)
@property
def karaoke(self) -> int:
return int.from_bytes(self._nKaraoke, 'little')
@property
def compressor(self) -> CompressorSettings:
return CompressorSettings(
gain_in=round(
int.from_bytes(self._COMP_gain_in, 'little', signed=True) * 0.01, 2
),
attack_ms=round(int.from_bytes(self._COMP_attack_ms, 'little') * 0.1, 2),
release_ms=round(int.from_bytes(self._COMP_release_ms, 'little') * 0.1, 2),
n_knee=round(int.from_bytes(self._COMP_n_knee, 'little') * 0.01, 2),
ratio=round(int.from_bytes(self._COMP_comprate, 'little') * 0.01, 2),
threshold=round(
int.from_bytes(self._COMP_threshold, 'little', signed=True) * 0.01, 2
),
c_enabled=bool(int.from_bytes(self._COMP_c_enabled, 'little')),
makeup=bool(int.from_bytes(self._COMP_c_auto, 'little')),
gain_out=round(
int.from_bytes(self._COMP_gain_out, 'little', signed=True) * 0.01, 2
),
)
@property
def gate(self) -> GateSettings:
return GateSettings(
threshold_in=round(
int.from_bytes(self._GATE_dBThreshold_in, 'little', signed=True) * 0.01,
2,
),
damping_max=round(
int.from_bytes(self._GATE_dBDamping_max, 'little', signed=True) * 0.01,
2,
),
bp_sidechain=round(
int.from_bytes(self._GATE_BP_Sidechain, 'little') * 0.1, 2
),
attack_ms=round(int.from_bytes(self._GATE_attack_ms, 'little') * 0.1, 2),
hold_ms=round(int.from_bytes(self._GATE_hold_ms, 'little') * 0.1, 2),
release_ms=round(int.from_bytes(self._GATE_release_ms, 'little') * 0.1, 2),
)
@property
def denoiser(self) -> DenoiserSettings:
return DenoiserSettings(
threshold=round(
int.from_bytes(self._DenoiserThreshold, 'little', signed=True) * 0.01, 2
)
)
@property
def pitch(self) -> PitchSettings:
return PitchSettings(
enabled=bool(int.from_bytes(self._PitchEnabled, 'little')),
dry_wet=round(
int.from_bytes(self._Pitch_DryWet, 'little', signed=True) * 0.01, 2
),
value=round(
int.from_bytes(self._Pitch_Value, 'little', signed=True) * 0.01, 2
),
formant_lo=round(
int.from_bytes(self._Pitch_formant_lo, 'little', signed=True) * 0.01, 2
),
formant_med=round(
int.from_bytes(self._Pitch_formant_med, 'little', signed=True) * 0.01, 2
),
formant_high=round(
int.from_bytes(self._Pitch_formant_high, 'little', signed=True) * 0.01,
2,
),
)
@dataclass
class VbanPacketNBS1(VbanPacket):
"""Represents the body of a VBAN data packet with ident:1"""
strips: tuple[VbanVMParamStrip, ...]
@classmethod
def from_bytes(
cls,
nbs: NBS,
kind: KindMapClass,
data: bytes,
):
return cls(
nbs=nbs,
_kind=kind,
_voicemeeterType=data[28:29],
_reserved=data[29:30],
_buffersize=data[30:32],
_voicemeeterVersion=data[32:36],
_optionBits=data[36:40],
_samplerate=data[40:44],
strips=tuple(
VbanVMParamStrip.from_bytes(
data[44 + i * VMPARAMSTRIP_SIZE : 44 + (i + 1) * VMPARAMSTRIP_SIZE]
)
for i in range(16)
),
)

View File

@@ -1,9 +1,9 @@
import abc
import time
from abc import abstractmethod
from typing import Union
from . import kinds
from .enums import NBS, EQGains
from .enums import NBS
from .iremote import IRemote
from .meta import (
channel_bool_prop,
@@ -21,7 +21,7 @@ class Strip(IRemote):
Defines concrete implementation for strip
"""
@abstractmethod
@abc.abstractmethod
def __str__(self):
pass
@@ -68,7 +68,7 @@ class PhysicalStrip(Strip):
'comp': StripComp(remote, index),
'gate': StripGate(remote, index),
'denoiser': StripDenoiser(remote, index),
'eq': StripEQ(remote, index),
'eq': StripEQ.make(remote, index),
},
)
@@ -76,12 +76,14 @@ class PhysicalStrip(Strip):
return f'{type(self).__name__}{self.index}'
@property
def device(self):
return
def audibility(self) -> float:
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].audibility.knob
@property
def sr(self):
return
@audibility.setter
def audibility(self, val: float):
self.setter('audibility', val)
class StripComp(IRemote):
@@ -91,7 +93,9 @@ class StripComp(IRemote):
@property
def knob(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].audibility.comp
@knob.setter
def knob(self, val: float):
@@ -99,7 +103,9 @@ class StripComp(IRemote):
@property
def gainin(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.gain_in
@gainin.setter
def gainin(self, val: float):
@@ -107,7 +113,9 @@ class StripComp(IRemote):
@property
def ratio(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.ratio
@ratio.setter
def ratio(self, val: float):
@@ -115,7 +123,9 @@ class StripComp(IRemote):
@property
def threshold(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.threshold
@threshold.setter
def threshold(self, val: float):
@@ -123,7 +133,9 @@ class StripComp(IRemote):
@property
def attack(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.attack_ms
@attack.setter
def attack(self, val: float):
@@ -131,7 +143,9 @@ class StripComp(IRemote):
@property
def release(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.release_ms
@release.setter
def release(self, val: float):
@@ -139,7 +153,9 @@ class StripComp(IRemote):
@property
def knee(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.n_knee
@knee.setter
def knee(self, val: float):
@@ -147,7 +163,9 @@ class StripComp(IRemote):
@property
def gainout(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].compressor.gain_out
@gainout.setter
def gainout(self, val: float):
@@ -155,7 +173,9 @@ class StripComp(IRemote):
@property
def makeup(self) -> bool:
return
if self.public_packets[NBS.one] is None:
return False
return bool(self.public_packets[NBS.one].strips[self.index].compressor.makeup)
@makeup.setter
def makeup(self, val: bool):
@@ -169,7 +189,9 @@ class StripGate(IRemote):
@property
def knob(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].audibility.gate
@knob.setter
def knob(self, val: float):
@@ -177,7 +199,9 @@ class StripGate(IRemote):
@property
def threshold(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].gate.threshold_in
@threshold.setter
def threshold(self, val: float):
@@ -185,7 +209,9 @@ class StripGate(IRemote):
@property
def damping(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].gate.damping_max
@damping.setter
def damping(self, val: float):
@@ -193,7 +219,9 @@ class StripGate(IRemote):
@property
def bpsidechain(self) -> int:
return
if self.public_packets[NBS.one] is None:
return 0
return self.public_packets[NBS.one].strips[self.index].gate.bp_sidechain
@bpsidechain.setter
def bpsidechain(self, val: int):
@@ -201,7 +229,9 @@ class StripGate(IRemote):
@property
def attack(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].gate.attack_ms
@attack.setter
def attack(self, val: float):
@@ -209,7 +239,9 @@ class StripGate(IRemote):
@property
def hold(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].gate.hold_ms
@hold.setter
def hold(self, val: float):
@@ -217,7 +249,9 @@ class StripGate(IRemote):
@property
def release(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].gate.release_ms
@release.setter
def release(self, val: float):
@@ -231,7 +265,9 @@ class StripDenoiser(IRemote):
@property
def knob(self) -> float:
return
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].audibility.denoiser
@knob.setter
def knob(self, val: float):
@@ -239,6 +275,25 @@ class StripDenoiser(IRemote):
class StripEQ(IRemote):
@classmethod
def make(cls, remote, i):
"""
Factory method for Strip EQ.
Returns a StripEQ class.
"""
STRIPEQ_cls = type(
'StripEQ',
(cls,),
{
'channel': tuple(
StripEQCh.make(remote, i, j)
for j in range(remote.kind.strip_channels)
)
},
)
return STRIPEQ_cls(remote, i)
@property
def identifier(self) -> str:
return f'strip[{self.index}].eq'
@@ -260,6 +315,140 @@ class StripEQ(IRemote):
self.setter('ab', 1 if val else 0)
class StripEQCh(IRemote):
@classmethod
def make(cls, remote, i, j):
"""
Factory method for Strip EQ channel.
Returns a StripEQCh class.
"""
StripEQCh_cls = type(
'StripEQCh',
(cls,),
{
'cell': tuple(
StripEQChCell(remote, i, j, k) for k in range(remote.kind.cells)
)
},
)
return StripEQCh_cls(remote, i, j)
def __init__(self, remote, i, j):
super().__init__(remote, i)
self.channel_index = j
@property
def identifier(self) -> str:
return f'Strip[{self.index}].eq.channel[{self.channel_index}]'
class StripEQChCell(IRemote):
def __init__(self, remote, i, j, k):
super().__init__(remote, i)
self.channel_index = j
self.cell_index = k
@property
def identifier(self) -> str:
return f'Strip[{self.index}].eq.channel[{self.channel_index}].cell[{self.cell_index}]'
@property
def on(self) -> bool:
if self.channel_index > 0:
self.logger.warning(
'Only channel 0 is supported over VBAN for Strip EQ cells'
)
if self.public_packets[NBS.one] is None:
return False
return (
self.public_packets[NBS.one]
.strips[self.index]
.parametric_eq[self.cell_index]
.on
)
@on.setter
def on(self, val: bool):
self.setter('on', 1 if val else 0)
@property
def type(self) -> int:
if self.channel_index > 0:
self.logger.warning(
'Only channel 0 is supported over VBAN for Strip EQ cells'
)
if self.public_packets[NBS.one] is None:
return 0
return (
self.public_packets[NBS.one]
.strips[self.index]
.parametric_eq[self.cell_index]
.type
)
@type.setter
def type(self, val: int):
self.setter('type', val)
@property
def f(self) -> float:
if self.channel_index > 0:
self.logger.warning(
'Only channel 0 is supported over VBAN for Strip EQ cells'
)
if self.public_packets[NBS.one] is None:
return 0.0
return (
self.public_packets[NBS.one]
.strips[self.index]
.parametric_eq[self.cell_index]
.freq
)
@f.setter
def f(self, val: float):
self.setter('f', val)
@property
def gain(self) -> float:
if self.channel_index > 0:
self.logger.warning(
'Only channel 0 is supported over VBAN for Strip EQ cells'
)
if self.public_packets[NBS.one] is None:
return 0.0
return (
self.public_packets[NBS.one]
.strips[self.index]
.parametric_eq[self.cell_index]
.gain
)
@gain.setter
def gain(self, val: float):
self.setter('gain', val)
@property
def q(self) -> float:
if self.channel_index > 0:
self.logger.warning(
'Only channel 0 is supported over VBAN for Strip EQ cells'
)
if self.public_packets[NBS.one] is None:
return 0.0
return (
self.public_packets[NBS.one]
.strips[self.index]
.parametric_eq[self.cell_index]
.q
)
@q.setter
def q(self, val: float):
self.setter('q', val)
class VirtualStrip(Strip):
@classmethod
def make(cls, remote, i, is_phys):
@@ -296,7 +485,7 @@ class VirtualStrip(Strip):
def bass(self) -> float:
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].eqgains[EQGains.bass]
return self.public_packets[NBS.one].strips[self.index].eqgains.bass
@bass.setter
def bass(self, val: float):
@@ -306,7 +495,7 @@ class VirtualStrip(Strip):
def mid(self) -> float:
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].eqgains[EQGains.mid]
return self.public_packets[NBS.one].strips[self.index].eqgains.mid
@mid.setter
def mid(self, val: float):
@@ -318,7 +507,7 @@ class VirtualStrip(Strip):
def treble(self) -> float:
if self.public_packets[NBS.one] is None:
return 0.0
return self.public_packets[NBS.one].strips[self.index].eqgains[EQGains.treble]
return self.public_packets[NBS.one].strips[self.index].eqgains.treble
@treble.setter
def treble(self, val: float):
@@ -351,22 +540,11 @@ class StripLevel(IRemote):
def getter(self):
"""Returns a tuple of level values for the channel."""
def fget(i):
return round((((1 << 16) - 1) - i) * -0.01, 1)
if not self._remote.stopped() and self._remote.event.ldirty:
return tuple(
fget(i)
for i in self._remote.cache['strip_level'][
self.range[0] : self.range[-1]
]
)
return tuple(
fget(i)
for i in self._remote._get_levels(self.public_packets[NBS.zero])[0][
self.range[0] : self.range[-1]
]
)
return self._remote.cache['strip_level'][self.range[0] : self.range[-1]]
return self.public_packets[NBS.zero].levels.strip[
self.range[0] : self.range[-1]
]
@property
def identifier(self) -> str:

View File

@@ -15,13 +15,27 @@ def cache_bool(func, param):
return wrapper
def cache_int(func, param):
"""Check cache for an int prop"""
def wrapper(*args, **kwargs):
self, *rem = args
if self._cmd(param) in self._remote.cache:
return self._remote.cache.pop(self._cmd(param))
if self._remote.sync:
self._remote.clear_dirty()
return func(*args, **kwargs)
return wrapper
def cache_string(func, param):
"""Check cache for a string prop"""
def wrapper(*args, **kwargs):
self, *rem = args
if self._cmd(param) in self._remote.cache:
return self._remote.cache.pop(self._cmd(param))
return self._remote.cache.pop(self._cmd(param)).strip('"')
if self._remote.sync:
self._remote.clear_dirty()
return func(*args, **kwargs)
@@ -49,39 +63,20 @@ def depth(d):
return 0
def script(func):
"""Convert dictionary to script"""
def wrapper(*args):
remote, script = args
if isinstance(script, dict):
params = ''
for key, val in script.items():
obj, m2, *rem = key.split('-')
index = int(m2) if m2.isnumeric() else int(*rem)
params += ';'.join(
f'{obj}{f".{m2}stream" if not m2.isnumeric() else ""}[{index}].{k}={int(v) if isinstance(v, bool) else v}'
for k, v in val.items()
)
params += ';'
script = params
return func(remote, script)
return wrapper
def comp(t0: tuple, t1: tuple) -> Iterator[bool]:
"""
Generator function, accepts two tuples.
Generator function, accepts two tuples of dB values.
Evaluates equality of each member in both tuples.
Only ignores changes when levels are very quiet (below -72 dB).
"""
for a, b in zip(t0, t1):
if ((1 << 16) - 1) - b <= 7200:
yield a == b
# If both values are very quiet (below -72dB), ignore small changes
if a <= -72.0 and b <= -72.0:
yield a == b # Both quiet, check if they're equal
else:
yield True
yield a != b # At least one has significant level, detect changes
def deep_merge(dict1, dict2):

View File

@@ -1,4 +1,4 @@
from abc import abstractmethod
import abc
from . import kinds
from .iremote import IRemote
@@ -11,7 +11,7 @@ class VbanStream(IRemote):
Defines concrete implementation for vban stream
"""
@abstractmethod
@abc.abstractmethod
def __str__(self):
pass

View File

@@ -1,25 +1,25 @@
import abc
import logging
import socket
import threading
import time
from abc import ABCMeta, abstractmethod
from pathlib import Path
from queue import Queue
from typing import Iterable, Union
from typing import Union
from .enums import NBS
from .error import VBANCMDError
from .event import Event
from .packet import RequestHeader
from .packet.headers import VbanMatrixResponseHeader, VbanRequestHeader
from .subject import Subject
from .util import bump_framecounter, deep_merge, script
from .util import bump_framecounter, deep_merge
from .worker import Producer, Subscriber, Updater
logger = logging.getLogger(__name__)
class VbanCmd(metaclass=ABCMeta):
"""Base class responsible for communicating with the VBAN RT Packet Service"""
class VbanCmd(abc.ABC):
"""Abstract Base Class for Voicemeeter VBAN Command Interfaces"""
DELAY = 0.001
# fmt: off
@@ -49,7 +49,7 @@ class VbanCmd(metaclass=ABCMeta):
self.stop_event = None
self.producer = None
@abstractmethod
@abc.abstractmethod
def __str__(self):
"""Ensure subclasses override str magic method"""
pass
@@ -58,7 +58,7 @@ class VbanCmd(metaclass=ABCMeta):
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import tomli as tomllib # type: ignore[import]
def get_filepath():
for pn in (
@@ -86,8 +86,8 @@ class VbanCmd(metaclass=ABCMeta):
self.logout()
def login(self) -> None:
"""Starts the subscriber and updater threads (unless in outbound mode)"""
if not self.outbound:
"""Starts the subscriber and updater threads (unless disable_rt_listeners is True) and logs into Voicemeeter."""
if not self.disable_rt_listeners:
self.event.info()
self.stop_event = threading.Event()
@@ -120,38 +120,39 @@ class VbanCmd(metaclass=ABCMeta):
def stopped(self):
return self.stop_event is None or self.stop_event.is_set()
def _send_request(self, payload: str) -> None:
"""Sends a request packet over the network and bumps the framecounter."""
self.sock.sendto(
VbanRequestHeader.encode_with_payload(
name=self.streamname,
bps_index=self.BPS_OPTS.index(self.bps),
channel=self.channel,
framecounter=self._framecounter,
payload=payload,
),
(socket.gethostbyname(self.ip), self.port),
)
self._framecounter = bump_framecounter(self._framecounter)
def _set_rt(self, cmd: str, val: Union[str, float]):
"""Sends a string request command over a network."""
req_packet = RequestHeader.to_bytes(
name=self.streamname,
bps_index=self.BPS_OPTS.index(self.bps),
channel=self.channel,
framecounter=self._framecounter,
)
self.sock.sendto(
req_packet + f'{cmd}={val};'.encode(),
(socket.gethostbyname(self.ip), self.port),
)
self._framecounter = bump_framecounter(self._framecounter)
self._send_request(f'{cmd}={val};')
self.cache[cmd] = val
@script
def sendtext(self, script):
def sendtext(self, script) -> str | None:
"""Sends a multiple parameter string over a network."""
req_packet = RequestHeader.to_bytes(
name=self.streamname,
bps_index=self.BPS_OPTS.index(self.bps),
channel=self.channel,
framecounter=self._framecounter,
)
self.sock.sendto(
req_packet + script.encode(),
(socket.gethostbyname(self.ip), self.port),
)
self._framecounter = bump_framecounter(self._framecounter)
self._send_request(script)
self.logger.debug(f'sendtext: {script}')
if self.disable_rt_listeners and script.endswith(('?', '?;')):
try:
response = VbanMatrixResponseHeader.extract_payload(
self.sock.recv(1024)
)
return response
except ValueError as e:
self.logger.warning(f'Error extracting matrix response: {e}')
time.sleep(self.DELAY)
@property
@@ -184,17 +185,6 @@ class VbanCmd(metaclass=ABCMeta):
while self.pdirty:
time.sleep(self.DELAY)
def _get_levels(self, packet) -> Iterable:
"""
returns both level arrays (strip_levels, bus_levels) BEFORE math conversion
strip levels in PREFADER mode.
"""
return (
packet.inputlevels,
packet.outputlevels,
)
def apply(self, data: dict):
"""
Sets all parameters of a dict

View File

@@ -5,16 +5,14 @@ import time
from .enums import NBS
from .error import VBANCMDConnectionError
from .packet import (
from .packet.headers import (
HEADER_SIZE,
VBAN_PROTOCOL_SERVICE,
VBAN_SERVICE_RTPACKET,
SubscribeHeader,
VbanRtPacket,
VbanRtPacketHeader,
VbanRtPacketNBS0,
VbanRtPacketNBS1,
VbanPacket,
VbanResponseHeader,
VbanSubscribeHeader,
)
from .packet.nbs0 import VbanPacketNBS0
from .packet.nbs1 import VbanPacketNBS1
from .util import bump_framecounter
logger = logging.getLogger(__name__)
@@ -34,14 +32,11 @@ class Subscriber(threading.Thread):
while not self.stopped():
try:
for nbs in NBS:
sub_packet = SubscribeHeader().to_bytes(nbs, self._framecounter)
sub_packet = VbanSubscribeHeader().to_bytes(nbs, self._framecounter)
self._remote.sock.sendto(
sub_packet, (self._remote.ip, self._remote.port)
)
self._framecounter = bump_framecounter(self._framecounter)
self.logger.debug(
f'sent subscription for NBS {nbs.name} to {self._remote.ip}:{self._remote.port}'
)
self.wait_until_stopped(10)
except socket.gaierror as e:
@@ -78,61 +73,45 @@ class Producer(threading.Thread):
(
self._remote.cache['strip_level'],
self._remote.cache['bus_level'],
) = self._remote._get_levels(self._remote.public_packets[NBS.zero])
) = self._remote.public_packets[NBS.zero].levels
def _get_rt(self) -> VbanRtPacket:
def _get_rt(self) -> VbanPacket:
"""Attempt to fetch data packet until a valid one found"""
while True:
if resp := self._fetch_rt_packet():
return resp
def _fetch_rt_packet(self) -> VbanRtPacket | None:
def _fetch_rt_packet(self) -> VbanPacket | None:
try:
data, _ = self._remote.sock.recvfrom(2048)
if len(data) < HEADER_SIZE:
return
response_header = VbanRtPacketHeader.from_bytes(data[:HEADER_SIZE])
if (
response_header.format_sr != VBAN_PROTOCOL_SERVICE
or response_header.format_nbc != VBAN_SERVICE_RTPACKET
):
return
match response_header.format_nbs:
case NBS.zero:
"""
self.logger.debug(
'Received NB0 RTP Packet from %s, Size: %d bytes',
addr,
len(data),
)
"""
return VbanRtPacketNBS0.from_bytes(
nbs=NBS.zero, kind=self._remote.kind, data=data
)
case NBS.one:
"""
self.logger.debug(
'Received NB1 RTP Packet from %s, Size: %d bytes',
addr,
len(data),
)
"""
return VbanRtPacketNBS1.from_bytes(
nbs=NBS.one, kind=self._remote.kind, data=data
)
return None
except TimeoutError as e:
self.logger.exception(f'{type(e).__name__}: {e}')
raise VBANCMDConnectionError(
f'timeout waiting for RtPacket from {self._remote.ip}'
f'timeout waiting for response from {self._remote.ip}:{self._remote.port}'
) from e
try:
header = VbanResponseHeader.from_bytes(data[:HEADER_SIZE])
except ValueError as e:
self.logger.debug(f'Error parsing response packet: {e}')
return None
match header.format_nbs:
case NBS.zero:
return VbanPacketNBS0.from_bytes(
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):
return self.stop_event.is_set()
@@ -159,7 +138,7 @@ class Producer(threading.Thread):
self.queue.put('pdirty')
if self._remote.event.ldirty:
self.queue.put('ldirty')
time.sleep(self._remote.ratelimit)
# time.sleep(self._remote.ratelimit)
self.logger.debug(f'terminating {self.name} thread')
self.queue.put(None)
@@ -196,9 +175,6 @@ class Updater(threading.Thread):
(
self._remote.cache['strip_level'],
self._remote.cache['bus_level'],
) = (
self._remote._public_packets[NBS.zero].inputlevels,
self._remote._public_packets[NBS.zero].outputlevels,
)
) = self._remote.public_packets[NBS.zero].levels
self._remote.subject.notify(event)
self.logger.debug(f'terminating {self.name} thread')