voicemeeter-api-python/examples/dsl/__main__.py

99 lines
2.8 KiB
Python
Raw Permalink Normal View History

import argparse
2022-09-29 10:01:18 +01:00
import logging
2022-06-18 22:16:45 +01:00
import time
from pyparsing import (
Combine,
Group,
OneOrMore,
Optional,
Suppress,
Word,
alphanums,
alphas,
nums,
)
2023-06-23 17:50:48 +01:00
import voicemeeterlib
logging.basicConfig(level=logging.INFO)
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
class Parser:
def __init__(self, vm):
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("->")
2022-06-18 23:49:15 +01:00
self.param = Word(alphanums)
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
def parse(self, cmds):
2022-06-18 23:49:15 +01:00
res = list()
2022-06-18 22:16:45 +01:00
for cmd in cmds:
2022-06-26 02:07:37 +01:00
if len(self.event.parseString(cmd)) == 2:
kls, param = self.event.parseString(cmd)
2022-06-18 23:49:15 +01:00
target = getattr(self.vm, kls[0])[int(kls[-1])]
res.append(getattr(target, param))
2022-06-26 02:07:37 +01:00
elif len(self.event.parseString(cmd)) == 3:
kls, param, val = self.event.parseString(cmd)
2022-06-18 23:49:15 +01:00
target = getattr(self.vm, kls[0])[int(kls[-1])]
if "".join(val) in ["off", "on"]:
setattr(target, param, bool(["off", "on"].index("".join(val))))
elif param in ["gain", "comp", "gate", "limit", "audibility"]:
setattr(target, param, float("".join(val)))
elif param in ["label"]:
setattr(target, param, " ".join(val))
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
def interactive_mode(parser):
while cmd := input("Please enter command (Press <Enter> to exit)\n"):
if not cmd:
break
2022-10-28 02:18:39 +01:00
if res := parser.parse((cmd,)):
print(res)
def main():
# fmt: off
cmds = (
"strip 0 -> mute -> on", "strip 0 -> mute", "bus 0 -> mute -> on",
"strip 0 -> mute -> off", "bus 0 -> mute -> on", "strip 3 -> solo -> on",
"strip 3 -> solo -> off", "strip 1 -> A1 -> on", "strip 1 -> A1",
"strip 1 -> A1 -> off", "strip 1 -> A1", "bus 3 -> eq -> on",
"bus 3 -> eq -> off", "strip 4 -> gain -> 1.2", "strip 0 -> gain -> -8.2",
"strip 0 -> gain", "strip 1 -> label -> rode podmic", "strip 2 -> limit -> -28",
"strip 2 -> limit",
)
# fmt: on
2023-06-23 17:50:48 +01:00
KIND_ID = "banana"
2022-06-18 22:34:40 +01:00
2023-06-23 17:50:48 +01:00
with voicemeeterlib.api(KIND_ID) as vm:
2022-06-18 22:16:45 +01:00
parser = Parser(vm)
if args.i:
interactive_mode(parser)
return
2022-06-18 22:16:45 +01:00
2022-10-28 02:18:39 +01:00
if res := parser.parse(cmds):
print(res)
2022-06-18 22:16:45 +01:00
if __name__ == "__main__":
main()