voicemeeter-api-python/voicemeeterlib/updater.py

76 lines
2.8 KiB
Python
Raw Normal View History

2023-06-23 17:34:50 +01:00
import logging
import threading
import time
from .util import comp
2023-06-23 17:34:50 +01:00
logger = logging.getLogger(__name__)
class Producer(threading.Thread):
"""Continously send job queue to the Updater thread at a rate of self._remote.ratelimit."""
def __init__(self, remote, queue, stop_event):
2025-01-15 12:40:31 +00:00
super().__init__(name='producer', daemon=False)
self._remote = remote
self.queue = queue
self.stop_event = stop_event
2023-06-23 17:34:50 +01:00
self.logger = logger.getChild(self.__class__.__name__)
def stopped(self):
return self.stop_event.is_set()
def run(self):
while not self.stopped():
if self._remote.event.pdirty:
2025-01-15 12:40:31 +00:00
self.queue.put('pdirty')
if self._remote.event.mdirty:
2025-01-15 12:40:31 +00:00
self.queue.put('mdirty')
if self._remote.event.midi:
2025-01-15 12:40:31 +00:00
self.queue.put('midi')
if self._remote.event.ldirty:
2025-01-15 12:40:31 +00:00
self.queue.put('ldirty')
time.sleep(self._remote.ratelimit)
2025-01-15 12:40:31 +00:00
self.logger.debug(f'terminating {self.name} thread')
self.queue.put(None)
class Updater(threading.Thread):
def __init__(self, remote, queue):
2025-01-15 12:40:31 +00:00
super().__init__(name='updater', daemon=True)
self._remote = remote
self.queue = queue
self._remote._strip_comp = [False] * (self._remote.kind.num_strip_levels)
self._remote._bus_comp = [False] * (self._remote.kind.num_bus_levels)
2023-06-23 17:34:50 +01:00
(
2025-01-15 12:40:31 +00:00
self._remote.cache['strip_level'],
self._remote.cache['bus_level'],
2023-06-23 17:34:50 +01:00
) = self._remote._get_levels()
self.logger = logger.getChild(self.__class__.__name__)
2022-10-04 14:36:08 +01:00
def _update_comps(self, strip_level, bus_level):
self._remote._strip_comp, self._remote._bus_comp = (
2025-01-15 12:40:31 +00:00
tuple(not x for x in comp(self._remote.cache['strip_level'], strip_level)),
tuple(not x for x in comp(self._remote.cache['bus_level'], bus_level)),
2022-10-04 14:36:08 +01:00
)
def run(self):
"""
Continously update observers of dirty states.
Generate _strip_comp, _bus_comp and update level cache if ldirty.
"""
2023-08-04 16:21:07 +01:00
while event := self.queue.get():
2025-01-15 12:40:31 +00:00
if event == 'pdirty' and self._remote.pdirty:
self._remote.subject.notify(event)
2025-01-15 12:40:31 +00:00
elif event == 'mdirty' and self._remote.mdirty:
self._remote.subject.notify(event)
2025-01-15 12:40:31 +00:00
elif event == 'midi' and self._remote.get_midi_message():
self._remote.subject.notify(event)
2025-01-15 12:40:31 +00:00
elif event == 'ldirty' and self._remote.ldirty:
2022-10-04 14:36:08 +01:00
self._update_comps(self._remote._strip_buf, self._remote._bus_buf)
2025-01-15 12:40:31 +00:00
self._remote.cache['strip_level'] = self._remote._strip_buf
self._remote.cache['bus_level'] = self._remote._bus_buf
self._remote.subject.notify(event)
2025-01-15 12:40:31 +00:00
self.logger.debug(f'terminating {self.name} thread')