0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-26 13:52:00 +01:00

modularize playlist change

This commit is contained in:
Kuba
2025-10-11 17:29:56 +02:00
parent e7219c5770
commit cf4f3137bd
5 changed files with 46 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
class PlayerModule:
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool]]):
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict]]):
pass
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool):
pass

13
modules/shuffler.py Normal file
View File

@@ -0,0 +1,13 @@
import random
class PlaylistModifierModule:
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict]]):
return playlist
class Module(PlaylistModifierModule):
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict]]):
if int(global_args.get("no_shuffle", 0)) == 0:
random.shuffle(playlist)
return playlist
playlistmod = (Module(), 0)

View File

@@ -1,5 +1,5 @@
class PlayerModule:
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool]]):
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict]]):
pass
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool):
pass
@@ -7,7 +7,7 @@ class PlayerModule:
class Module(PlayerModule):
def __init__(self) -> None:
self.playlist = []
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool]]):
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict]]):
self.playlist = [t[0] for t in playlist]
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool):
lines = self.playlist[:index] + [f"> {self.playlist[index]}"] + self.playlist[index+1:]

View File

@@ -10,12 +10,16 @@ import log95, copy
from pathlib import Path
class PlayerModule:
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool]]):
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict]]):
pass
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool):
pass
class PlaylistModifierModule:
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict]]):
return playlist
simple_modules: list[PlayerModule] = []
playlist_modifier_modules: list[PlaylistModifierModule] = []
SCRIPT_DIR = Path(__file__).resolve().parent
MODULES_DIR = SCRIPT_DIR / "modules"
@@ -222,34 +226,33 @@ def play_playlist(playlist_path, custom_playlist: bool=False):
logger.info(f"Exception while parsing playlist, retrying in 15 seconds...")
time.sleep(15)
return
lines_args = copy.deepcopy(parsed)
lines = []
for (lns, args) in lines_args:
playlist: list[tuple[str, bool, bool, bool, dict]] = [] # name, fade in, fade out, official, args
for (lns, args) in parsed:
lns: list[str]
args: dict[str, str]
for line in lns: playlist.append((line, True, True, True, args))
for i in range(int(args.get("multiplier", 1))): lines.extend(lns)
for module in playlist_modifier_modules: playlist = module.modify(global_args, playlist)
cross_fade = int(global_args.get("crossfade", 5))
if int(global_args.get("no_shuffle", 0)) == 0: random.shuffle(lines)
playlist: list[tuple[str, bool, bool, bool]] = [] # name, fade in, fade out, official
last_jingiel = True
for line in lines:
if not last_jingiel and random.choice([False, True, False, False]) and JINGIEL_FILE:
playlist.append((line, True, False, True))
playlist.append((JINGIEL_FILE, False, False, False))
last_jingiel = True
else:
playlist.append((line, True, True, True))
last_jingiel = False
del last_jingiel
# last_jingiel = True
# for line in lines:
# if not last_jingiel and random.choice([False, True, False, False]) and JINGIEL_FILE:
# playlist.append((line, True, False, True))
# playlist.append((JINGIEL_FILE, False, False, False))
# last_jingiel = True
# else:
# playlist.append((line, True, True, True))
# last_jingiel = False
# del last_jingiel
for module in simple_modules: module.on_new_playlist(playlist)
return_pending = False
for i, (track, to_fade_in, to_fade_out, official) in enumerate(playlist):
cross_fade = int(global_args.get("crossfade", 5))
for i, (track, to_fade_in, to_fade_out, official, args) in enumerate(playlist):
if return_pending:
logger.info("Return reached, next song will reload the playlist.")
procman.wait_all()
@@ -332,6 +335,11 @@ def main():
if md := getattr(module, "module", None):
simple_modules.append(md)
elif md := getattr(module, "playlistmod", None):
if isinstance(md, tuple):
md, index = md
playlist_modifier_modules.insert(index, md)
else: playlist_modifier_modules.append(md)
try:
while True:

View File

@@ -14,7 +14,3 @@ Each lines starting with `@` is taken as a import, meaning after the `@` a file
Lines which start with `;` are considered comments and will not be processed
Lines containing `|` will be split into two parts, the first part will be treated as the file itself, and the argument part will be treated as arguments for that file, if the file name is empty then those arguments will be treated as global
The arguments shall have a `a=b` format, multiple arguments are seperated with a `;`, example: `a=b;c=d`
As of now these arguments are defined:
`no_shuffle` - Global argument, does not shuffle the playlist if it is 0
`multiplier` - File argument, integer which duplicates the file(s)