diff --git a/modules/jingle.py b/modules/jingle.py index f47e17e..7b8c5e2 100644 --- a/modules/jingle.py +++ b/modules/jingle.py @@ -8,7 +8,9 @@ Reacts to the 'no_jingle' argument, for global usage it does not add jingles to import random -from . import PlaylistModifierModule, Track, Path +from modules import BaseIMCModule, InterModuleCommunication + +from . import PlaylistModifierModule, Track, Path, PlayerModule class Module(PlaylistModifierModule): def __init__(self, primary: Path, secondary: list[Path] | None = None) -> None: @@ -32,5 +34,22 @@ class Module(PlaylistModifierModule): out.append(Track(track.path, crossfade, crossfade, True, track.args,focus_time_offset=-crossfade)) last_jingiel = False return out + +class Module2(PlayerModule): + def __init__(self, primary: Path, secondary: list[Path] | None = None) -> None: + if secondary is None: secondary = [] + self.primary = primary.absolute() + assert primary.exists() + self.secondary = [f.absolute() for f in secondary if f.exists()] + def imc(self, imc: InterModuleCommunication) -> None: + super().imc(imc) + self._imc.register(self, "jingle") + def imc_data(self, source: BaseIMCModule, source_name: str | None, data: object, broadcast: bool) -> object: + if broadcast: return + jingle = self.primary + if self.secondary and (random.randint(1,3) == 1): jingle = random.choice(self.secondary) + return self._imc.send(self, "activemod", {"action": "add_to_toplay", "songs": [jingle]}) -playlistmod = (Module(Path("/home/user/Jingiel.mp3"), [Path("/home/user/jing2.opus"), Path("Jing3.opus")]), 1) \ No newline at end of file +options = Path("/home/user/Jingiel.mp3"), [Path("/home/user/jing2.opus"), Path("Jing3.opus")] +module = Module2(*options) +playlistmod = (Module(*options), 1) \ No newline at end of file diff --git a/modules/web.html b/modules/web.html index 17b920e..7557fd7 100644 --- a/modules/web.html +++ b/modules/web.html @@ -76,6 +76,7 @@ + @@ -378,6 +379,10 @@ if(ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({action:"skip_next"})); else console.error("WebSocket not connected."); }); + document.getElementById("jingle-btn").addEventListener("click", () => { + if(ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({action:"jingle"})); + else console.error("WebSocket not connected."); + }); document.getElementById("readd-btn").addEventListener("click", () => { if(selectedPlaylistIndex == null){ diff --git a/modules/web.py b/modules/web.py index 64df0ce..a4dba8e 100644 --- a/modules/web.py +++ b/modules/web.py @@ -51,7 +51,7 @@ async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: mult await websocket.send(json.dumps({"status": "ok", "message": f"{len(songs)} song(s) queued"})) result = await get_imc("activemod", {"action": "get_toplay"}) - if result is not None: + if result is not None: await websocket.loop.run_in_executor(None, ws_q.put, {"data": result, "event": "toplay"}) elif action == "get_toplay": result = await get_imc("activemod", {"action": "get_toplay"}) @@ -60,11 +60,21 @@ async def ws_handler(websocket: ServerConnection, shared_data: dict, imc_q: mult elif action == "clear_toplay": result = await get_imc("activemod", {"action": "clear_toplay"}) if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) - else: await websocket.send(json.dumps({"data": result, "event": "toplay"})) # Yes, this is not an accident + else: + await websocket.send(json.dumps({"data": result, "event": "toplay"})) # Yes, this is not an accident + await websocket.loop.run_in_executor(None, ws_q.put, {"data": result, "event": "toplay"}) elif action == "skip_next": result = await get_imc("activemod", {"action": "skip_next", "set": msg.get("set",True)}) if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) else: await websocket.send(json.dumps({"data": result, "event": "skip_next"})) + elif action == "jingle": + result = await get_imc("jingle", None) + if result is None: await websocket.send(json.dumps({"error": "timeout", "code": 504})) + else: + await websocket.send(json.dumps(result)) + result = await get_imc("activemod", {"action": "get_toplay"}) + if result is not None: + await websocket.loop.run_in_executor(None, ws_q.put, {"data": result, "event": "toplay"}) elif action == "request_state": # supports requesting specific parts if provided what = msg.get("what", "") @@ -224,7 +234,7 @@ class Module(PlayerModule): self.data["progress"] = json.dumps(payload) try: self.ws_q.put({"event": "progress", "data": payload}) except Exception: pass - + def imc_data(self, source: BaseIMCModule, source_name: str | None, data: object, broadcast: bool) -> object: try: self.ws_q.put({"event": "imc", "data": {"name": source_name, "data": data, "broadcast": broadcast}}) except Exception: pass