2022-10-28 02:14:08 +01:00
|
|
|
import argparse
|
2022-09-29 10:01:18 +01:00
|
|
|
import logging
|
2022-06-18 22:16:45 +01:00
|
|
|
import time
|
2023-08-18 20:02:46 +01:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from enum import IntEnum
|
2022-06-18 22:16:45 +01:00
|
|
|
|
|
|
|
from pyparsing import (
|
|
|
|
Combine,
|
|
|
|
Group,
|
|
|
|
OneOrMore,
|
|
|
|
Optional,
|
|
|
|
Suppress,
|
|
|
|
Word,
|
|
|
|
alphanums,
|
|
|
|
nums,
|
|
|
|
)
|
|
|
|
|
2023-06-23 17:50:48 +01:00
|
|
|
import voicemeeterlib
|
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-10-28 02:14:08 +01:00
|
|
|
argparser = argparse.ArgumentParser(description="creates a basic dsl")
|
|
|
|
argparser.add_argument("-i", action="store_true")
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
2022-06-18 22:16:45 +01:00
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
ParamKinds = IntEnum(
|
|
|
|
"ParamKinds",
|
|
|
|
"bool float string",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Strategy(ABC):
|
|
|
|
def __init__(self, target, param, val):
|
|
|
|
self.target = target
|
|
|
|
self.param = param
|
|
|
|
self.val = val
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BoolStrategy(Strategy):
|
|
|
|
def run(self):
|
|
|
|
setattr(self.target, self.param, self.strtobool(self.val))
|
|
|
|
|
|
|
|
def strtobool(self, val):
|
|
|
|
"""Convert a string representation of truth to it's numeric form."""
|
|
|
|
|
|
|
|
val = val.lower()
|
|
|
|
if val in ("y", "yes", "t", "true", "on", "1"):
|
|
|
|
return 1
|
|
|
|
elif val in ("n", "no", "f", "false", "off", "0"):
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
raise ValueError("invalid truth value %r" % (val,))
|
|
|
|
|
|
|
|
|
|
|
|
class FloatStrategy(Strategy):
|
|
|
|
def run(self):
|
|
|
|
setattr(self.target, self.param, float(self.val))
|
|
|
|
|
|
|
|
|
|
|
|
class StringStrategy(Strategy):
|
|
|
|
def run(self):
|
|
|
|
setattr(self.target, self.param, " ".join(self.val))
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.strategy.run()
|
|
|
|
|
|
|
|
|
2022-06-18 22:16:45 +01:00
|
|
|
class Parser:
|
2023-08-18 20:02:46 +01:00
|
|
|
IS_STRING = ("label",)
|
|
|
|
|
2022-06-18 22:16:45 +01:00
|
|
|
def __init__(self, vm):
|
2023-08-18 20:02:46 +01:00
|
|
|
self.logger = logger.getChild(self.__class__.__name__)
|
2022-06-18 22:16:45 +01:00
|
|
|
self.vm = vm
|
2022-06-18 23:49:15 +01:00
|
|
|
self.kls = Group(OneOrMore(Word(alphanums)))
|
2022-06-18 22:16:45 +01:00
|
|
|
self.token = Suppress("->")
|
2023-08-18 20:02:46 +01:00
|
|
|
self.param = Group(OneOrMore(Word(alphanums)))
|
2022-06-18 23:49:15 +01:00
|
|
|
self.value = Combine(
|
|
|
|
Optional("-") + Word(nums) + Optional(".") + Optional(Word(nums))
|
|
|
|
) | Group(OneOrMore(Word(alphanums)))
|
2022-06-26 02:07:37 +01:00
|
|
|
self.event = (
|
2022-06-18 22:16:45 +01:00
|
|
|
self.kls
|
|
|
|
+ self.token
|
2022-06-18 23:49:15 +01:00
|
|
|
+ self.param
|
2022-06-18 22:16:45 +01:00
|
|
|
+ Optional(self.token)
|
2022-06-18 23:49:15 +01:00
|
|
|
+ Optional(self.value)
|
2022-06-18 22:16:45 +01:00
|
|
|
)
|
2022-06-26 02:07:37 +01:00
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
def converter(self, cmds):
|
|
|
|
"""determines the kind of parameter from the parsed string"""
|
2022-06-18 22:16:45 +01:00
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
res = list()
|
2022-06-18 22:16:45 +01:00
|
|
|
for cmd in cmds:
|
2023-08-18 20:02:46 +01:00
|
|
|
self.logger.debug(f"running command: {cmd}")
|
|
|
|
match cmd_parsed := self.event.parseString(cmd):
|
|
|
|
case [[kls, index], [param]]:
|
|
|
|
target = getattr(self.vm, kls)[int(index)]
|
|
|
|
res.append(getattr(target, param))
|
|
|
|
case [[kls, index], [param], val] if param in self.IS_STRING:
|
|
|
|
target = getattr(self.vm, kls)[int(index)]
|
|
|
|
context = self._get_context(ParamKinds.string, target, param, val)
|
|
|
|
context.run()
|
|
|
|
case [[kls, index], [param], [val] | val]:
|
|
|
|
target = getattr(self.vm, kls)[int(index)]
|
|
|
|
try:
|
|
|
|
context = self._get_context(ParamKinds.bool, target, param, val)
|
|
|
|
context.run()
|
|
|
|
except ValueError as e:
|
|
|
|
self.logger.error(f"{e}... switching to float strategy")
|
|
|
|
context.strategy = FloatStrategy(target, param, val)
|
|
|
|
context.run()
|
|
|
|
case [
|
|
|
|
[kls, index],
|
|
|
|
[secondary, param],
|
|
|
|
[val] | val,
|
|
|
|
]:
|
|
|
|
primary = getattr(self.vm, kls)[int(index)]
|
|
|
|
target = getattr(primary, secondary)
|
|
|
|
try:
|
|
|
|
context = self._get_context(ParamKinds.bool, target, param, val)
|
|
|
|
context.run()
|
|
|
|
except ValueError as e:
|
|
|
|
self.logger.error(f"{e}... switching to float strategy")
|
|
|
|
context.strategy = FloatStrategy(target, param, val)
|
|
|
|
context.run()
|
|
|
|
case _:
|
|
|
|
self.logger.error(
|
|
|
|
f"unable to determine the kind of parameter from {cmd_parsed}"
|
|
|
|
)
|
2022-06-18 22:16:45 +01:00
|
|
|
time.sleep(0.05)
|
2022-06-18 23:49:15 +01:00
|
|
|
return res
|
2022-06-18 22:16:45 +01:00
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
def _get_context(self, kind, *args):
|
|
|
|
"""
|
|
|
|
determines a strategy for a kind of parameter and passes it to the context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
match kind:
|
|
|
|
case ParamKinds.bool:
|
|
|
|
context = Context(BoolStrategy(*args))
|
|
|
|
case ParamKinds.float:
|
|
|
|
context = Context(FloatStrategy(*args))
|
|
|
|
case ParamKinds.string:
|
|
|
|
context = Context(StringStrategy(*args))
|
|
|
|
return context
|
|
|
|
|
2022-06-18 22:16:45 +01:00
|
|
|
|
2022-10-28 02:14:08 +01:00
|
|
|
def interactive_mode(parser):
|
|
|
|
while cmd := input("Please enter command (Press <Enter> to exit)\n"):
|
2022-10-28 02:18:39 +01:00
|
|
|
if res := parser.parse((cmd,)):
|
2022-10-28 02:14:08 +01:00
|
|
|
print(res)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# fmt: off
|
|
|
|
cmds = (
|
2023-08-18 20:02:46 +01:00
|
|
|
"strip 0 -> mute -> true", "strip 0 -> mute", "bus 0 -> mute -> true",
|
|
|
|
"strip 0 -> mute -> false", "bus 0 -> mute -> true", "strip 3 -> solo -> true",
|
|
|
|
"strip 3 -> solo -> false", "strip 1 -> A1 -> true", "strip 1 -> A1",
|
|
|
|
"strip 1 -> A1 -> false", "strip 1 -> A1", "strip 3 -> eq on -> true",
|
|
|
|
"bus 3 -> eq on -> false", "strip 4 -> gain -> 1.2", "strip 0 -> gain -> -8.2",
|
2022-10-28 02:14:08 +01:00
|
|
|
"strip 0 -> gain", "strip 1 -> label -> rode podmic", "strip 2 -> limit -> -28",
|
2023-08-18 20:02:46 +01:00
|
|
|
"strip 2 -> limit", "strip 3 -> comp knob -> 3.8"
|
2022-10-28 02:14:08 +01:00
|
|
|
)
|
|
|
|
# fmt: on
|
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
with voicemeeterlib.api("potato") as vm:
|
2022-06-18 22:16:45 +01:00
|
|
|
parser = Parser(vm)
|
2022-10-28 02:14:08 +01:00
|
|
|
if args.i:
|
|
|
|
interactive_mode(parser)
|
|
|
|
return
|
2022-06-18 22:16:45 +01:00
|
|
|
|
2023-08-18 20:02:46 +01:00
|
|
|
if res := parser.converter(cmds):
|
2022-10-28 02:14:08 +01:00
|
|
|
print(res)
|
2022-06-18 22:16:45 +01:00
|
|
|
|
|
|
|
|
2022-10-28 02:14:08 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|