From d428694fcf09f1934caebfb0a73c08ee7133d887 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 23 Jun 2023 18:13:45 +0100 Subject: [PATCH] add example events. --- examples/events/README.md | 33 +++++++++++++++++++++++ examples/events/__main__.py | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 examples/events/README.md create mode 100644 examples/events/__main__.py diff --git a/examples/events/README.md b/examples/events/README.md new file mode 100644 index 0000000..6340437 --- /dev/null +++ b/examples/events/README.md @@ -0,0 +1,33 @@ +## About + +This script demonstrates how to interact with the event thread/event object. It also demonstrates how to register event specific callbacks. + +By default the interface does not broadcast any events. So even though our callbacks are registered, and the event thread has been initiated, we won't receive updates. + +After five seconds the event object is used to subscribe to all events for a total of thirty seconds. + +Remember that events can also be unsubscribed to with `vm.event.remove()`. Callbacks can also be deregistered using vm.observer.remove(). + +The same can be done without a context manager: + +```python + vm = voicemeeterlib.api(KIND_ID) + vm.login() + vm.observer.add(on_midi) # register an `on_midi` callback function + vm.init_thread() + vm.event.add("midi") # in this case we only subscribe to midi events. + ... + vm.end_thread() + vm.logout() +``` + +Once initialized, the event thread will continously run until end_thread() is called. Even if all events are unsubscribed to. + +## Use + +Simply run the script and trigger events and you should see the output after 5 seconds. To trigger events do the following: + +- change GUI parameters to trigger pdirty +- press any macrobutton to trigger mdirty +- play audio through any bus to trigger ldirty +- any midi input to trigger midi diff --git a/examples/events/__main__.py b/examples/events/__main__.py new file mode 100644 index 0000000..5ababd7 --- /dev/null +++ b/examples/events/__main__.py @@ -0,0 +1,54 @@ +import json +import logging +import time +from logging import config + +import voicemeeterlib + +logging.basicConfig(level=logging.INFO) + + +class App: + def __init__(self, vm): + self.vm = vm + # register the callbacks for each event + self.vm.observer.add( + [self.on_pdirty, self.on_mdirty, self.on_ldirty, self.on_midi] + ) + + def __enter__(self): + self.vm.init_thread() + + def __exit__(self, exc_type, exc_value, traceback): + self.vm.end_thread() + + def on_pdirty(self): + print("pdirty!") + + def on_mdirty(self): + print("mdirty!") + + def on_ldirty(self): + for bus in self.vm.bus: + if bus.levels.isdirty: + print(bus, bus.levels.all) + + def on_midi(self): + current = self.vm.midi.current + print(f"Value of midi button {current} is {self.vm.midi.get(current)}") + + +def main(): + KIND_ID = "banana" + + with voicemeeterlib.api(KIND_ID) as vm: + with App(vm) as app: + for i in range(5, 0, -1): + print(f"events start in {i} seconds") + time.sleep(1) + vm.event.add(["pdirty", "ldirty", "midi", "mdirty"]) + time.sleep(30) + + +if __name__ == "__main__": + main()