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

103 lines
2.9 KiB
Python
Raw Normal View History

2022-06-18 22:16:45 +01:00
import time
import voicemeeterlib
from pyparsing import (
Combine,
Group,
OneOrMore,
Optional,
Suppress,
Word,
alphanums,
alphas,
nums,
)
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 main(cmds=None):
2022-06-18 22:34:40 +01:00
kind_id = "banana"
with voicemeeterlib.api(kind_id) as vm:
2022-06-18 22:16:45 +01:00
parser = Parser(vm)
if cmds:
2022-06-18 23:49:15 +01:00
res = parser.parse(cmds)
if res:
print(res)
2022-06-18 22:16:45 +01:00
else:
try:
while True:
cmd = input("please enter command (Return to exit)\n")
if not cmd:
break
2022-06-18 23:49:15 +01:00
res = parser.parse((cmd,))
if res:
print(res)
2022-06-18 22:16:45 +01:00
except KeyboardInterrupt as e:
SystemExit(e)
if __name__ == "__main__":
cmds = (
2022-06-18 23:49:15 +01:00
"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",
2022-06-18 22:16:45 +01:00
)
# pass cmds to parse cmds, otherwise simply run main() to test stdin parsing
main(cmds)