From ef0c94a6f13e7f6fcdf50e9d4204da7870c63743 Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sat, 24 Sep 2022 12:04:07 +0100 Subject: [PATCH] move updater thread logic out of base class. patch bump --- pyproject.toml | 2 +- voicemeeterlib/base.py | 36 ++++---------------------------- voicemeeterlib/updater.py | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 33 deletions(-) create mode 100644 voicemeeterlib/updater.py diff --git a/pyproject.toml b/pyproject.toml index 319f86f..2457c5e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "voicemeeter-api" -version = "0.7.0" +version = "0.7.1" description = "A Python wrapper for the Voiceemeter API" authors = ["onyx-and-iris "] license = "MIT" diff --git a/voicemeeterlib/base.py b/voicemeeterlib/base.py index 8b9f6c6..3858836 100644 --- a/voicemeeterlib/base.py +++ b/voicemeeterlib/base.py @@ -2,7 +2,6 @@ import ctypes as ct import time from abc import abstractmethod from functools import partial -from threading import Thread from typing import Iterable, NoReturn, Optional, Union from .cbindings import CBindings @@ -11,7 +10,8 @@ from .inst import bits from .kinds import KindId from .misc import Event, Midi from .subject import Subject -from .util import comp, grouper, polling, script +from .util import grouper, polling, script +from .worker import Updater class Remote(CBindings): @@ -47,36 +47,8 @@ class Remote(CBindings): """Starts updates thread.""" self.running = True print(f"Listening for {', '.join(self.event.get())} events") - t = Thread(target=self._updates, daemon=True) - t.start() - - def _updates(self): - """ - Continously update observers of dirty states. - - Generate _strip_comp, _bus_comp and update level cache if ldirty. - - Runs updates at a rate of self.ratelimit. - """ - while self.running: - if self.event.pdirty and self.pdirty: - self.subject.notify("pdirty") - if self.event.mdirty and self.mdirty: - self.subject.notify("mdirty") - if self.event.midi and self.get_midi_message(): - self.subject.notify("midi") - if self.event.ldirty and self.ldirty: - self._strip_comp, self._bus_comp = ( - tuple( - not x for x in comp(self.cache["strip_level"], self._strip_buf) - ), - tuple(not x for x in comp(self.cache["bus_level"], self._bus_buf)), - ) - self.cache["strip_level"] = self._strip_buf - self.cache["bus_level"] = self._bus_buf - self.subject.notify("ldirty") - - time.sleep(self.ratelimit if self.event.any() else 0.5) + self.updater = Updater(self) + self.updater.start() def login(self) -> NoReturn: """Login to the API, initialize dirty parameters""" diff --git a/voicemeeterlib/updater.py b/voicemeeterlib/updater.py new file mode 100644 index 0000000..f8bb119 --- /dev/null +++ b/voicemeeterlib/updater.py @@ -0,0 +1,44 @@ +import threading +import time + +from .util import comp + + +class Updater(threading.Thread): + def __init__(self, remote): + super().__init__(name="updater", target=self.update, daemon=True) + self._remote = remote + + def update(self): + """ + Continously update observers of dirty states. + + Generate _strip_comp, _bus_comp and update level cache if ldirty. + + Runs updates at a rate of self.ratelimit. + """ + while self._remote.running: + if self._remote.event.pdirty and self._remote.pdirty: + self._remote.subject.notify("pdirty") + if self._remote.event.mdirty and self._remote.mdirty: + self._remote.subject.notify("mdirty") + if self._remote.event.midi and self._remote.get_midi_message(): + self._remote.subject.notify("midi") + if self._remote.event.ldirty and self._remote.ldirty: + self._remote._strip_comp, self._remote._bus_comp = ( + tuple( + not x + for x in comp( + self.cache["strip_level"], self._remote._strip_buf + ) + ), + tuple( + not x + for x in comp(self.cache["bus_level"], self._remote._bus_buf) + ), + ) + self._remote.cache["strip_level"] = self._remote._strip_buf + self._remote.cache["bus_level"] = self._remote._bus_buf + self._remote.subject.notify("ldirty") + + time.sleep(self._remote.ratelimit if self._remote.event.any() else 0.5)