Fixes re: zognia's testing #2

Merged
skeh merged 11 commits from feat/zogpog into main 2025-01-24 08:28:14 +00:00
9 changed files with 61 additions and 36 deletions
Showing only changes of commit 82691789f7 - Show all commits

View file

@ -219,9 +219,9 @@ class MainProcess:
plugin_module = import_or_reload_mod(module_name, plugin_module = import_or_reload_mod(module_name,
default_package='ovtk_audiencekit.plugins', default_package='ovtk_audiencekit.plugins',
external=False) external=False)
plugin = plugin_module.Plugin(self.chat_processes, self.event_queue, plugin_name, global_ctx, plugin = plugin_module.Plugin(self.chat_processes, self.event_queue, plugin_name, global_ctx)
**node.props, **secrets_for_mod, _children=node.nodes)
self.plugins[plugin_name] = plugin self.plugins[plugin_name] = plugin
await plugin._setup(*node.args[1:], **node.props, **secrets_for_mod)
# Register UI with webserver # Register UI with webserver
self.webserver.register_blueprint(plugin.blueprint) self.webserver.register_blueprint(plugin.blueprint)
except Exception as e: except Exception as e:

View file

@ -86,6 +86,18 @@ class PluginBase(ABC):
raise e raise e
raise PluginError(self._name, str(e)) from e raise PluginError(self._name, str(e)) from e
async def _setup(self, *args, **kwargs):
try:
res = self.setup(*args, **kwargs)
if asyncio.iscoroutinefunction(self.setup):
return await res
else:
return res
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
raise PluginError(self._name, str(e)) from e
# Base class helpers # Base class helpers
def broadcast(self, event): def broadcast(self, event):
"""Send event to every active chat""" """Send event to every active chat"""
@ -123,6 +135,10 @@ class PluginBase(ABC):
self._event_queue.put_nowait(event) self._event_queue.put_nowait(event)
# User-defined # User-defined
async def setup(self, *args, **kwargs):
"""Called when plugin is being loaded."""
pass
def close(self): def close(self):
"""Called when plugin is about to be unloaded. Use this to safely close any resouces if needed""" """Called when plugin is about to be unloaded. Use this to safely close any resouces if needed"""
pass pass
@ -143,7 +159,7 @@ class PluginBase(ABC):
pass pass
@abstractmethod @abstractmethod
async def run(self, _children=None, _ctx={}, **kwargs): async def run(self, *args, _children=None, _ctx={}, **kwargs):
""" """
Run plugin action, either due to a definition in the config, or due to another plugin Run plugin action, either due to a definition in the config, or due to another plugin
""" """

View file

@ -1,11 +1,11 @@
import asyncio import asyncio
from collections import deque
from ovtk_audiencekit.plugins import PluginBase from ovtk_audiencekit.plugins import PluginBase
from ovtk_audiencekit.core import Clip from ovtk_audiencekit.core import Clip
class AudioAlert(PluginBase): class AudioAlert(PluginBase):
def __init__(self, *args, output=None, buffer_length=2048, cutoff_prevention_buffers=None, **kwargs): def setup(self, output=None, buffer_length=2048, cutoff_prevention_buffers=None):
super().__init__(*args, **kwargs)
if cutoff_prevention_buffers: if cutoff_prevention_buffers:
self.logger.info('`cutoff_prevention_buffers` are depricated') self.logger.info('`cutoff_prevention_buffers` are depricated')
@ -13,13 +13,33 @@ class AudioAlert(PluginBase):
self._buffer_length = int(buffer_length) self._buffer_length = int(buffer_length)
self._output_index = Clip.find_output_index(output) self._output_index = Clip.find_output_index(output)
def run(self, path, speed=1, immediate=True, **kwargs): def run(self, path, speed=1, immediate=True, poly=1, **kwargs):
if self.sounds.get(path) is None: sound = None
self.sounds[path] = Clip(path,
self._output_index, if poly != 1:
buffer_length=self._buffer_length, poly = int(poly)
speed=speed) sound_dq = self.sounds.get(path)
sound = self.sounds.get(path) if sound_dq is None or type(sound_dq) != deque or sound_dq.maxlen != poly:
sound_dq = deque(maxlen=poly)
self.sounds[path] = sound_dq
if len(sound_dq) != poly:
self.logger.debug("filling", len(sound_dq), poly, sound_dq)
sound = Clip(path,
self._output_index,
buffer_length=self._buffer_length,
speed=speed)
sound_dq.append(sound)
else:
self.logger.debug("rotate", len(sound_dq), poly, sound_dq)
sound_dq.rotate(1)
sound = sound_dq[0]
else:
if self.sounds.get(path) is None:
self.sounds[path] = Clip(path,
self._output_index,
buffer_length=self._buffer_length,
speed=speed)
sound = self.sounds.get(path)
if immediate: if immediate:
asyncio.create_task(sound.aplay()) asyncio.create_task(sound.aplay())

View file

@ -35,8 +35,7 @@ owomap = {
} }
class JailPlugin(PluginBase): class JailPlugin(PluginBase):
def __init__(self, *args, min_level='vip', persist=True, **kwargs): def setup(self, min_level='vip', persist=True):
super().__init__(*args, **kwargs)
self.persist = persist self.persist = persist
self._cache = os.path.join(CACHE_DIR, 'Jail', 'sentences') self._cache = os.path.join(CACHE_DIR, 'Jail', 'sentences')
os.makedirs(os.path.dirname(self._cache), exist_ok=True) os.makedirs(os.path.dirname(self._cache), exist_ok=True)

View file

@ -6,19 +6,15 @@ from ovtk_audiencekit.plugins import PluginBase
class OBSWSPlugin(PluginBase): class OBSWSPlugin(PluginBase):
def __init__(self, *args, password=None, uri='ws://localhost:4455', **kwargs): async def setup(self, password=None, uri='ws://localhost:4455'):
super().__init__(*args, **kwargs)
self.uri = uri self.uri = uri
self.obsws = simpleobsws.WebSocketClient(url=uri, password=password) self.obsws = simpleobsws.WebSocketClient(url=uri, password=password)
asyncio.get_event_loop().run_until_complete(self.setup()) await self.obsws.connect()
success = await self.obsws.wait_until_identified()
async def setup(self): if not success:
await self.obsws.connect() await self.obsws.disconnect()
success = await self.obsws.wait_until_identified() raise RuntimeError(f'Could not connect to OBS websocket at {self.uri}')
if not success:
await self.obsws.disconnect()
raise RuntimeError(f'Could not connect to OBS websocket at {self.uri}')
async def run(self, type, _children=None, _ctx={}, **kwargs): async def run(self, type, _children=None, _ctx={}, **kwargs):
req = simpleobsws.Request(type, requestData=kwargs) req = simpleobsws.Request(type, requestData=kwargs)

View file

@ -4,8 +4,7 @@ from ovtk_audiencekit.plugins import PluginBase
class OSCPlugin(PluginBase): class OSCPlugin(PluginBase):
def __init__(self, *args, ip='localhost', port=None, **kwargs): def setup(self, ip='localhost', port=None):
super().__init__(*args, **kwargs)
if port is None: if port is None:
raise RuntimeError('A unique port must be specified') raise RuntimeError('A unique port must be specified')
self.client = SimpleUDPClient(ip, int(port)) self.client = SimpleUDPClient(ip, int(port))

View file

@ -64,8 +64,7 @@ class PhraseCounter:
class PhraseCounterPlugin(PluginBase): class PhraseCounterPlugin(PluginBase):
def __init__(self, *args, debounce_time=1, persist=False, **kwargs): def setup(self, debounce_time=1, persist=False):
super().__init__(*args, **kwargs)
self.debounce_time = debounce_time self.debounce_time = debounce_time
self.persist = persist self.persist = persist

View file

@ -9,10 +9,8 @@ from ovtk_audiencekit.chats.Twitch import Process as Twitch
class ShoutoutPlugin(PluginBase): class ShoutoutPlugin(PluginBase):
def __init__(self, *args, command='so', min_level='vip', def setup(self, command='so', min_level='vip',
text='Check out {link}!~ They were last streaming {last_game}', text='Check out {link}!~ They were last streaming {last_game}'):
**kwargs):
super().__init__(*args, **kwargs)
self.text = text self.text = text
if command: if command:
self.command = Command(name=command, help='Shoutout another user', required_level=min_level) self.command = Command(name=command, help='Shoutout another user', required_level=min_level)

View file

@ -13,10 +13,8 @@ from ovtk_audiencekit.core.Data import CACHE_DIR
class TextToSpeechPlugin(PluginBase): class TextToSpeechPlugin(PluginBase):
def __init__(self, *args, output=None, cuda=None, def setup(self, output=None, cuda=None,
engine="tts_models/en/ljspeech/tacotron2-DDC", speaker_wav=None, engine="tts_models/en/ljspeech/tacotron2-DDC", speaker_wav=None, **kwargs):
_children=None, **kwargs):
super().__init__(*args, _children=_children)
self.speaker_wav = speaker_wav self.speaker_wav = speaker_wav