first commit

This commit is contained in:
2023-09-23 22:55:58 +01:00
commit c8d9628964
20 changed files with 1350 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
import json
import time
from pathlib import Path
import globalPluginHandler
from logHandler import log
from .commands import CommandsMixin
from .controller import Controller
from .kinds import KindId, request_kind_map
def _make_gestures():
defaults = {
"kb:NVDA+alt+s": "strip_mode",
"kb:NVDA+alt+b": "bus_mode",
"kb:NVDA+shift+q": "announce_controller",
"kb:NVDA+shift+a": "announce_voicemeeter_version",
"kb:NVDA+shift+o": "toggle_mono",
"kb:NVDA+shift+s": "toggle_solo",
"kb:NVDA+shift+m": "toggle_mute",
"kb:NVDA+shift+upArrow": "increase_gain",
"kb:NVDA+shift+downArrow": "decrease_gain",
"kb:NVDA+shift+alt+upArrow": "increase_gain",
"kb:NVDA+shift+alt+downArrow": "decrease_gain",
"kb:NVDA+shift+control+upArrow": "increase_gain",
"kb:NVDA+shift+control+downArrow": "decrease_gain",
}
overrides = None
pn = Path.home() / "Documents" / "Voicemeeter" / "keybinds.json"
if pn.exists():
with open(pn, "r") as f:
data = json.load(f)
overrides = {f"kb:{v}": k for k, v in data.items()}
log.info("INFO - loading settings from keybinds.json")
if overrides:
return {**defaults, **overrides}
return defaults
def _get_kind_id():
pn = Path.home() / "Documents" / "Voicemeeter" / "settings.json"
if pn.exists():
with open(pn, "r") as f:
data = json.load(f)
return data["voicemeeter"]
return "potato"
class GlobalPlugin(globalPluginHandler.GlobalPlugin, CommandsMixin):
__gestures = _make_gestures()
__kind_id = _get_kind_id()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.controller = Controller()
if self.controller.login() == 1:
self.controller.run_voicemeeter(KindId[self.__kind_id.upper()])
time.sleep(1)
self.kind = request_kind_map(self.controller.kind_id)
for i in range(1, self.kind.num_strip + 1):
self.bindGesture(f"kb:NVDA+alt+{i}", "index")
for i in range(1, self.kind.phys_out + self.kind.virt_out + 1):
self.bindGesture(f"kb:NVDA+shift+{i}", "bus_assignment")
def terminate(self, *args, **kwargs):
super().terminate(*args, **kwargs)
self.controller.logout()

View File

@@ -0,0 +1,51 @@
import ctypes as ct
import winreg
from pathlib import Path
from .error import VMError
class Binds:
VM_KEY = "VB:Voicemeeter {17359A74-1236-5467}"
BITS = 64 if ct.sizeof(ct.c_voidp) == 8 else 32
def __init__(self):
dll_path = Path(self.__vmpath()).parent.joinpath(
f'VoicemeeterRemote{"64" if self.BITS == 64 else ""}.dll'
)
if self.BITS == 64:
self.libc = ct.CDLL(str(dll_path))
else:
self.libc = ct.WinDLL(str(dll_path))
def __vmpath(self):
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"{}".format(
"\\".join(
(
"\\".join(
filter(
None,
(
"SOFTWARE",
"WOW6432Node" if self.BITS == 64 else "",
"Microsoft",
"Windows",
"CurrentVersion",
"Uninstall",
),
)
),
self.VM_KEY,
)
)
),
) as vm_key:
return winreg.QueryValueEx(vm_key, r"UninstallString")[0]
def call(self, fn, *args, ok=(0,)):
retval = getattr(self.libc, fn)(*args)
if retval not in ok:
raise VMError(f"{fn} returned {retval}")
return retval

View File

@@ -0,0 +1,65 @@
import ui
from logHandler import log
from . import context
class CommandsMixin:
### ANNOUNCEMENTS ###
def script_announce_voicemeeter_version(self, _):
ui.message(f"Running Voicemeeter {self.kind}")
def script_announce_controller(self, _):
ui.message(f"Controller for {self.controller.ctx.strategy} {self.controller.ctx.index + 1}")
### ALTER THE CONTEXT ###
def script_strip_mode(self, _):
if self.controller.ctx.index >= self.kind.num_strip:
ui.message(f"Controller strip {self.controller.ctx.index + 1} does not exist for Voicemeeter {self.kind}")
return
self.controller.ctx.strategy = context.StripStrategy(self.controller, self.controller.ctx.index)
ui.message(f"Controller for strip {self.controller.ctx.index + 1}")
log.info(f"INFO - strip {self.controller.ctx.index} mode")
def script_bus_mode(self, _):
if self.controller.ctx.index >= self.kind.num_bus:
ui.message(f"Controller bus {self.controller.ctx.index + 1} does not exist for Voicemeeter {self.kind}")
return
self.controller.ctx.strategy = context.BusStrategy(self.controller, self.controller.ctx.index)
ui.message(f"Controller for {self.controller.ctx.strategy} {self.controller.ctx.index + 1}")
log.info(f"INFO - {self.controller.ctx.strategy} {self.controller.ctx.index} mode")
def script_index(self, gesture):
proposed = int(gesture.displayName[-1])
self.controller.ctx.index = proposed - 1
ui.message(f"Controller for {self.controller.ctx.strategy} {self.controller.ctx.index + 1}")
log.info(f"INFO - {self.controller.ctx.strategy} {self.controller.ctx.index} mode")
### BOOLEAN PARMETERS ###
def script_toggle_mono(self, _):
val = not self.controller.ctx.get_bool("mono")
self.controller.ctx.set_bool("mono", val)
ui.message("on" if val else "off")
def script_toggle_solo(self, _):
val = not self.controller.ctx.get_bool("solo")
self.controller.ctx.set_bool("solo", val)
ui.message("on" if val else "off")
def script_toggle_mute(self, _):
val = not self.controller.ctx.get_bool("mute")
self.controller.ctx.set_bool("mute", val)
ui.message("on" if val else "off")
def script_bus_assignment(self, gesture):
proposed = int(gesture.displayName[-1])
if proposed - 1 < self.kind.phys_out:
output = f"A{proposed}"
else:
output = f"B{proposed - self.kind.phys_out}"
val = not self.controller.ctx.get_bool(output)
self.controller.ctx.set_bool(output, val)
ui.message("on" if val else "off")

View File

@@ -0,0 +1,66 @@
from abc import ABC, abstractmethod
class Strategy(ABC):
def __init__(self, controller, index):
self._controller = controller
self._index = index
@property
def index(self):
return self._index
@index.setter
def index(self, val):
self._index = val
def get_bool(self, param: str) -> bool:
return self._controller._get(f"{self.identifier}.{param}") == 1
def set_bool(self, param: str, val: bool):
self._controller._set(f"{self.identifier}.{param}", 1 if val else 0)
class StripStrategy(Strategy):
def __str__(self):
return "Strip"
@property
def identifier(self):
return f"strip[{self._index}]"
class BusStrategy(Strategy):
def __str__(self):
return "Bus"
@property
def identifier(self):
return f"bus[{self._index}]"
class Context:
def __init__(self, strategy: Strategy) -> None:
self._strategy = strategy
@property
def strategy(self) -> Strategy:
return self._strategy
@strategy.setter
def strategy(self, strategy: Strategy) -> None:
self._strategy = strategy
@property
def index(self):
return self._strategy._index
@index.setter
def index(self, val):
self._strategy._index = val
def get_bool(self, *args):
return self._strategy.get_bool(*args)
def set_bool(self, *args):
self._strategy.set_bool(*args)

View File

@@ -0,0 +1,47 @@
import ctypes as ct
from logHandler import log
from .binds import Binds
from .context import Context, StripStrategy
from .kinds import KindId
class Controller:
def __init__(self):
self.binds = Binds()
self.ctx = Context(StripStrategy(self, 0))
def login(self):
retval = self.binds.call("VBVMR_Login", ok=(0, 1))
log.info("INFO - logged into Voicemeeter Remote API")
return retval
def logout(self):
self.binds.call("VBVMR_Logout")
log.info("NFO - logged out of Voicemeeter Remote API")
@property
def kind_id(self):
c_type = ct.c_long()
self.binds.call("VBVMR_GetVoicemeeterType", ct.byref(c_type))
return KindId(c_type.value).name.lower()
def run_voicemeeter(self, kind_id):
val = kind_id.value
if val == 3 and Binds.BITS == 64:
val = 6
self.binds.call("VBVMR_RunVoicemeeter", val)
def __clear(self):
while self.binds.call("VBVMR_IsParametersDirty", ok=(0, 1)) == 1:
pass
def _get(self, param):
self.__clear()
buf = ct.c_float()
self.binds.call("VBVMR_GetParameterFloat", param.encode(), ct.byref(buf))
return buf.value
def _set(self, param, val):
self.binds.call("VBVMR_SetParameterFloat", param.encode(), ct.c_float(float(val)))

View File

@@ -0,0 +1,2 @@
class VMError(Exception):
"""Base VMError class"""

View File

@@ -0,0 +1,99 @@
from dataclasses import dataclass
from enum import Enum, unique
from .error import VMError
@unique
class KindId(Enum):
BASIC = 1
BANANA = 2
POTATO = 3
@dataclass
class KindMapClass:
name: str
ins: tuple
outs: tuple
vban: tuple
asio: tuple
insert: int
@property
def phys_in(self) -> int:
return self.ins[0]
@property
def virt_in(self) -> int:
return self.ins[-1]
@property
def phys_out(self) -> int:
return self.outs[0]
@property
def virt_out(self) -> int:
return self.outs[-1]
@property
def num_strip(self) -> int:
return sum(self.ins)
@property
def num_bus(self) -> int:
return sum(self.outs)
def __str__(self) -> str:
return self.name.capitalize()
@dataclass
class BasicMap(KindMapClass):
name: str
ins: tuple = (2, 1)
outs: tuple = (1, 1)
vban: tuple = (4, 4, 1, 1)
asio: tuple = (0, 0)
insert: int = 0
@dataclass
class BananaMap(KindMapClass):
name: str
ins: tuple = (3, 2)
outs: tuple = (3, 2)
vban: tuple = (8, 8, 1, 1)
asio: tuple = (6, 8)
insert: int = 22
@dataclass
class PotatoMap(KindMapClass):
name: str
ins: tuple = (5, 3)
outs: tuple = (5, 3)
vban: tuple = (8, 8, 1, 1)
asio: tuple = (10, 8)
insert: int = 34
def kind_factory(kind_id):
if kind_id == "basic":
_kind_map = BasicMap
elif kind_id == "banana":
_kind_map = BananaMap
elif kind_id == "potato":
_kind_map = PotatoMap
else:
raise ValueError(f"Unknown Voicemeeter kind {kind_id}")
return _kind_map(name=kind_id)
def request_kind_map(kind_id):
KIND_obj = None
try:
KIND_obj = kind_factory(kind_id)
except ValueError as e:
raise VMError(str(e)) from e
return KIND_obj