add example events.

This commit is contained in:
onyx-and-iris 2023-06-23 18:13:45 +01:00
parent 0548d82295
commit d428694fcf
2 changed files with 87 additions and 0 deletions

33
examples/events/README.md Normal file
View File

@ -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

View File

@ -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()