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

massive clean ups (i wonder if it starts)

This commit is contained in:
Kuba
2025-10-31 18:04:37 +01:00
parent 2247e7996c
commit 1f4a184418
8 changed files with 130 additions and 84 deletions

View File

@@ -1,14 +1,22 @@
import log95 import log95
from dataclasses import dataclass
@dataclass
class Track:
path: str
fade_out: bool
fade_in: bool
official: bool
args: dict[str, str] | None
class PlayerModule: class PlayerModule:
""" """
Simple passive observer, this allows you to send the current track the your RDS encoder, or to your website Simple passive observer, this allows you to send the current track the your RDS encoder, or to your website
""" """
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def on_new_playlist(self, playlist: list[Track]):
"""Tuple consists of the track path, to fade out, fade in, official, and args """This is called every new playlist"""
This is called every new playlist"""
pass pass
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool): def on_new_track(self, index: int, track: Track):
""" """
Called on every track including the ones added by the active modifier, you can check for that comparing the playlists[index] and the track Called on every track including the ones added by the active modifier, you can check for that comparing the playlists[index] and the track
""" """
@@ -24,7 +32,7 @@ class PlaylistModifierModule:
""" """
Playlist modifier, this type of module allows you to shuffle, or put jingles into your playlist Playlist modifier, this type of module allows you to shuffle, or put jingles into your playlist
""" """
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def modify(self, global_args: dict, playlist: list[Track]):
""" """
global_args are playlist global args (see radioPlayer_playlist_file.txt) global_args are playlist global args (see radioPlayer_playlist_file.txt)
""" """
@@ -60,13 +68,13 @@ class ActiveModifier:
Called at start up with the program arguments Called at start up with the program arguments
""" """
pass pass
def play(self, index:int, track: tuple[str, bool, bool, bool, dict[str, str]]) -> tuple[tuple[str, bool, bool, bool, dict[str, str]], bool] | tuple[None, None]: def play(self, index:int, track: Track) -> tuple[Track, bool] | tuple[None, None]:
""" """
Returns a tuple, in the first case where a is the track and b is a bool, b corresponds to whether to extend the playlist, set to true when adding content instead of replacing it Returns a tuple, in the first case where a is the track and b is a bool, b corresponds to whether to extend the playlist, set to true when adding content instead of replacing it
When None, None is returned then that is treated as a skip, meaning the core will skip this song When None, None is returned then that is treated as a skip, meaning the core will skip this song
""" """
return track, False return track, False
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def on_new_playlist(self, playlist: list[Track]):
""" """
Same behaviour as the basic module function Same behaviour as the basic module function
""" """

View File

@@ -1,5 +1,5 @@
from modules import InterModuleCommunication from modules import InterModuleCommunication
from . import ActiveModifier, log95 from . import ActiveModifier, log95, Track
import os import os
import subprocess import subprocess
import datetime import datetime
@@ -20,31 +20,30 @@ class Module(ActiveModifier):
self.last_track = None self.last_track = None
self.limit_tracks = True self.limit_tracks = True
self.imc_class = None self.imc_class = None
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def on_new_playlist(self, playlist: list[Track]):
self.playlist = playlist self.playlist = playlist
if not self.imc_class: return if not self.imc_class: return
self.limit_tracks = bool(self.imc_class.send(self, "advisor", None)) self.limit_tracks = bool(self.imc_class.send(self, "advisor", None))
def play(self, index: int, track: tuple[str, bool, bool, bool, dict[str, str]]): def play(self, index: int, track: Track):
if not self.playlist: return track if not self.playlist: return track
if not os.path.exists("/tmp/radioPlayer_toplay"): open("/tmp/radioPlayer_toplay", "a").close() if not os.path.exists("/tmp/radioPlayer_toplay"): open("/tmp/radioPlayer_toplay", "a").close()
with open("/tmp/radioPlayer_toplay", "r") as f: with open("/tmp/radioPlayer_toplay", "r") as f: songs = [s.strip() for s in f.readlines() if s.strip()]
songs = [s.strip() for s in f.readlines() if s.strip()]
if len(songs): if len(songs):
song = songs.pop(0) song = songs.pop(0)
if self.last_track: if self.last_track:
_, last_track_to_fade_out, _, _, _ = self.last_track last_track_to_fade_out = self.last_track.fade_out
else: else:
if (index - 1) >= 0: if (index - 1) >= 0:
_, last_track_to_fade_out, _, _, _ = self.playlist[index - 1] last_track_to_fade_out = self.playlist[index - 1].fade_out
else: last_track_to_fade_out = False else: last_track_to_fade_out = False
if len(songs) != 0: if len(songs) != 0:
next_track_to_fade_in = True next_track_to_fade_in = True
else: else:
if index + 1 < len(self.playlist): if index + 1 < len(self.playlist):
_, _, next_track_to_fade_in, _, _ = self.playlist[index + 1] next_track_to_fade_in = self.playlist[index + 1].fade_in
else: else:
next_track_to_fade_in = True next_track_to_fade_in = True
@@ -56,13 +55,13 @@ class Module(ActiveModifier):
logger.info(f"Playing {song} instead, as instructed by toplay") logger.info(f"Playing {song} instead, as instructed by toplay")
self.last_track = (song, next_track_to_fade_in, last_track_to_fade_out, True, {}) self.last_track = Track(song, next_track_to_fade_in, last_track_to_fade_out, True, {})
return self.last_track, True return self.last_track, True
elif len(self.originals): self.last_track = self.originals.pop(0) elif len(self.originals): self.last_track = self.originals.pop(0)
else: self.last_track = track else: self.last_track = track
if self.limit_tracks: if self.limit_tracks:
last_track_duration = get_audio_duration(self.last_track[0]) last_track_duration = get_audio_duration(self.last_track.path)
if last_track_duration and last_track_duration > 5*60: if last_track_duration and last_track_duration > 5*60:
now = datetime.datetime.now() now = datetime.datetime.now()
timestamp = now.timestamp() + last_track_duration timestamp = now.timestamp() + last_track_duration

View File

@@ -8,22 +8,22 @@ Reacts to the 'no_jingle' argument, for global usage it does not add jingles to
import random import random
from . import PlaylistModifierModule from . import PlaylistModifierModule, Track
class Module(PlaylistModifierModule): class Module(PlaylistModifierModule):
def __init__(self, file: str) -> None: def __init__(self, file: str) -> None:
self.file = file self.file = file
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict]]): def modify(self, global_args: dict, playlist: list[Track]):
if int(global_args.get("no_jingle", 0)): return playlist if int(global_args.get("no_jingle", 0)): return playlist
out: list[tuple[str, bool, bool, bool, dict]] = [] out: list[Track] = []
last_jingiel = True last_jingiel = True
for (track, _, _, _, args) in playlist: for track in playlist:
if not last_jingiel and random.choice([False, True, False, False]) and self.file and int(args.get("no_jingle", 0)) == 0: if not last_jingiel and random.choice([False, True, False, False]) and self.file and (track.args and int(track.args.get("no_jingle", 0)) == 0):
out.append((track, True, False, True, args)) out.append(Track(track, True, False, True, track.args))
out.append((self.file, False, False, False, {})) out.append(Track(self.file, False, False, False, {}))
last_jingiel = True last_jingiel = True
else: else:
out.append((track, True, True, True, args)) out.append(Track(track, True, True, True, track.args))
last_jingiel = False last_jingiel = False
del last_jingiel del last_jingiel
return out return out

View File

@@ -7,28 +7,43 @@ First of all, ther are in total only 4 modules:
- Active modifier (ActiveModifier), this module is optional, but there can still be only one. This module can replace the track while playing, allowing you to skip tracks or play tracks on demand, it can also extend the playlist - Active modifier (ActiveModifier), this module is optional, but there can still be only one. This module can replace the track while playing, allowing you to skip tracks or play tracks on demand, it can also extend the playlist
```python ```python
@dataclass
class Track:
path: str
fade_out: bool
fade_in: bool
official: bool
args: dict[str, str] | None
class PlayerModule: class PlayerModule:
""" """
Simple passive observer, this allows you to send the current track the your RDS encoder, or to your website Simple passive observer, this allows you to send the current track the your RDS encoder, or to your website
""" """
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def on_new_playlist(self, playlist: list[Track]):
"""Tuple consists of the track path, to fade out, fade in, official, and args """This is called every new playlist"""
This is called every new playlist"""
pass pass
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool): def on_new_track(self, index: int, track: Track):
""" """
Called on every track including the ones added by the active modifier, you can check for that comparing the playlists[index] and the track Called on every track including the ones added by the active modifier, you can check for that comparing the playlists[index] and the track
""" """
pass pass
def imc(self, imc: 'InterModuleCommunication'):
"""
Receive an IMC object
"""
pass
def imc_data(self, source: 'PlayerModule | ActiveModifier | PlaylistAdvisor', data: object) -> object:
pass
class PlaylistModifierModule: class PlaylistModifierModule:
""" """
Playlist modifier, this type of module allows you to shuffle, or put jingles into your playlist Playlist modifier, this type of module allows you to shuffle, or put jingles into your playlist
""" """
def modify(self, global_args: dict, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def modify(self, global_args: dict, playlist: list[Track]):
""" """
global_args are playlist global args (see radioPlayer_playlist_file.txt) global_args are playlist global args (see radioPlayer_playlist_file.txt)
""" """
return playlist return playlist
# No IMC, as we only run on new playlists
class PlaylistAdvisor: class PlaylistAdvisor:
""" """
Only one of a playlist advisor can be loaded. This module picks the playlist file to play, this can be a scheduler or just a static file Only one of a playlist advisor can be loaded. This module picks the playlist file to play, this can be a scheduler or just a static file
@@ -38,11 +53,18 @@ class PlaylistAdvisor:
Arguments are the arguments passed to the program on startup Arguments are the arguments passed to the program on startup
""" """
return "/path/to/playlist.txt" return "/path/to/playlist.txt"
def new_playlist(self) -> int: def new_playlist(self) -> bool:
""" """
Whether to play a new playlist, if this is 1, then the player will refresh, if this is two then the player will refresh quietly Whether to play a new playlist, if this is True, then the player will refresh and fetch a new playlist, calling advise
""" """
return 0 return False
def imc(self, imc: 'InterModuleCommunication'):
"""
Receive an IMC object
"""
pass
def imc_data(self, source: 'PlayerModule | ActiveModifier | PlaylistAdvisor', data: object) -> object:
pass
class ActiveModifier: class ActiveModifier:
""" """
This changes the next song to be played live, which means that this picks the next song, not the playlist, but this is affected by the playlist This changes the next song to be played live, which means that this picks the next song, not the playlist, but this is affected by the playlist
@@ -52,17 +74,44 @@ class ActiveModifier:
Called at start up with the program arguments Called at start up with the program arguments
""" """
pass pass
def play(self, index:int, track: tuple[str, bool, bool, bool, dict[str, str]]) -> tuple[tuple[str, bool, bool, bool, dict[str, str]], bool] | tuple[None, None]: def play(self, index:int, track: Track) -> tuple[Track, bool] | tuple[None, None]:
""" """
Returns a tuple, in the first case where a is the track and b is a bool, b corresponds to whether to extend the playlist, set to true when adding content instead of replacing it Returns a tuple, in the first case where a is the track and b is a bool, b corresponds to whether to extend the playlist, set to true when adding content instead of replacing it
When None, None is returned then that is treated as a skip, meaning the core will skip this song When None, None is returned then that is treated as a skip, meaning the core will skip this song
""" """
return track, False return track, False
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict[str, str]]]): def on_new_playlist(self, playlist: list[Track]):
""" """
Same behaviour as the basic module function Same behaviour as the basic module function
""" """
pass pass
def imc(self, imc: 'InterModuleCommunication'):
"""
Receive an IMC object
"""
pass
def imc_data(self, source: 'PlayerModule | ActiveModifier | PlaylistAdvisor', data: object) -> object:
pass
class InterModuleCommunication:
def __init__(self, advisor: PlaylistAdvisor, active_modifier: ActiveModifier | None, simple_modules: list[PlayerModule]) -> None:
self.advisor = advisor
self.active_modifier = active_modifier
self.simple_modules = simple_modules
self.names_modules: dict[str, PlaylistAdvisor | ActiveModifier | PlayerModule] = {}
def broadcast(self, source: PlaylistAdvisor | ActiveModifier | PlayerModule, data: object) -> None:
"""
Send data to all modules
"""
self.advisor.imc_data(source, data)
if self.active_modifier: self.active_modifier.imc_data(source, data)
for module in self.simple_modules: module.imc_data(source, data)
def register(self, module: PlaylistAdvisor | ActiveModifier | PlayerModule, name: str):
if name in self.names_modules.keys(): return False
self.names_modules[name] = module
return True
def send(self, source: PlaylistAdvisor | ActiveModifier | PlayerModule, name: str, data: object) -> object:
if not name in self.names_modules.keys(): raise Exception
return self.names_modules[name].imc_data(source, data)
``` ```
Each module shall have a python script in the modules directory. Each of the modules need to define one or more global variables in order to be seen by the core: Each module shall have a python script in the modules directory. Each of the modules need to define one or more global variables in order to be seen by the core:

View File

@@ -1,4 +1,4 @@
from . import PlayerModule, log95 from . import PlayerModule, log95, Track
import socket, re, os import socket, re, os
DEBUG = False DEBUG = False
@@ -72,9 +72,9 @@ def update_rds(track_name: str):
return prt, ','.join(list(map(str, rtp))) return prt, ','.join(list(map(str, rtp)))
class Module(PlayerModule): class Module(PlayerModule):
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool): def on_new_track(self, index: int, track: Track):
if official: if track.official:
rds_rt, rds_rtp = update_rds(os.path.basename(track)) rds_rt, rds_rtp = update_rds(os.path.basename(track.path))
logger.info(f"RT set to '{rds_rt}'") logger.info(f"RT set to '{rds_rt}'")
logger.debug(f"{rds_rtp=}") logger.debug(f"{rds_rtp=}")

View File

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

View File

@@ -1,17 +1,17 @@
from . import PlayerModule, log95 from . import PlayerModule, log95, Track
logger = log95.log95("PlayView") logger = log95.log95("PlayView")
class Module(PlayerModule): class Module(PlayerModule):
def __init__(self) -> None: def __init__(self) -> None:
self.playlist = [] self.playlist = []
def on_new_playlist(self, playlist: list[tuple[str, bool, bool, bool, dict]]): def on_new_playlist(self, playlist: list[Track]):
self.playlist = [t[0] for t in playlist] self.playlist = [t.path for t in playlist]
def on_new_track(self, index: int, track: str, to_fade_in: bool, to_fade_out: bool, official: bool): def on_new_track(self, index: int, track: Track):
if track != self.playlist[index]: if track.path != self.playlist[index]:
# discrepancy, which means that the playing file was modified by the active modifier # discrepancy, which means that the playing file was modified by the active modifier
# we are playing a file that was not determined in the playlist, that means it was chosen by the active modifier and made up on the fly # we are playing a file that was not determined in the playlist, that means it was chosen by the active modifier and made up on the fly
lines = self.playlist[:index] + [f"> ({track})"] + [self.playlist[index]] + self.playlist[index+1:] lines = self.playlist[:index] + [f"> ({track.path})"] + [self.playlist[index]] + self.playlist[index+1:]
logger.info("Next up:", self.playlist[index]) logger.info("Next up:", self.playlist[index])
else: else:
lines = self.playlist[:index] + [f"> {self.playlist[index]}"] + self.playlist[index+1:] lines = self.playlist[:index] + [f"> {self.playlist[index]}"] + self.playlist[index+1:]

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
DEBUG = False DEBUG = False
import time import time
import os, subprocess, importlib.util import os, subprocess, importlib.util, types
import sys, signal, threading, glob import sys, signal, threading, glob
import unidecode import unidecode
from dataclasses import dataclass from dataclasses import dataclass
@@ -14,10 +14,8 @@ playlist_modifier_modules: list[PlaylistModifierModule] = []
playlist_advisor: PlaylistAdvisor | None = None playlist_advisor: PlaylistAdvisor | None = None
active_modifier: ActiveModifier | None = None active_modifier: ActiveModifier | None = None
SCRIPT_DIR = Path(__file__).resolve().parent
MODULES_PACKAGE = "modules" MODULES_PACKAGE = "modules"
MODULES_DIR = SCRIPT_DIR / MODULES_PACKAGE MODULES_DIR = (Path(__file__).resolve().parent / MODULES_PACKAGE).resolve()
MODULES_DIR = MODULES_DIR.resolve()
def print_wait(ttw: float, frequency: float, duration: float=-1, prefix: str="", bias: float = 0): def print_wait(ttw: float, frequency: float, duration: float=-1, prefix: str="", bias: float = 0):
interval = 1.0 / frequency interval = 1.0 / frequency
@@ -101,11 +99,10 @@ procman = ProcessManager()
def handle_sigint(signum, frame): def handle_sigint(signum, frame):
global exit_pending, intr_time global exit_pending, intr_time
logger.info("Received SIGINT") logger.info("Received SIGINT")
if (time.time() - intr_time) > 10: if (time.time() - intr_time) > 5:
intr_time = time.time() intr_time = time.time()
logger.info("Will quit on song end.") logger.info("Will quit on song end.")
exit_pending = True exit_pending = True
return
else: else:
logger.warning("Force-Quit pending") logger.warning("Force-Quit pending")
procman.stop_all() procman.stop_all()
@@ -120,7 +117,7 @@ def load_filelines(path):
logger.error(f"Playlist not found: {path}") logger.error(f"Playlist not found: {path}")
return [] return []
def parse_playlistfile(playlist_path: str): def parse_playlistfile(playlist_path: str) -> tuple[dict[str, str], list[tuple[list[str], dict[str, str]]]]:
parser_log = log95.log95("PARSER", logger_level) parser_log = log95.log95("PARSER", logger_level)
parser_log.debug("Reading", playlist_path) parser_log.debug("Reading", playlist_path)
@@ -165,18 +162,15 @@ def parse_playlistfile(playlist_path: str):
def play_playlist(playlist_path): def play_playlist(playlist_path):
if not playlist_advisor: raise Exception("No playlist advisor") if not playlist_advisor: raise Exception("No playlist advisor")
try: try: global_args, parsed = parse_playlistfile(playlist_path)
global_args, parsed = parse_playlistfile(playlist_path)
except Exception: except Exception:
logger.info(f"Exception while parsing playlist, retrying in 15 seconds...") logger.info(f"Exception while parsing playlist, retrying in 15 seconds...")
time.sleep(15) time.sleep(15)
return return
playlist: list[tuple[str, bool, bool, bool, dict]] = [] # name, fade in, fade out, official, args playlist: list[Track] = []
for (lns, args) in parsed: for (lns, args) in parsed:
lns: list[str] for line in lns: playlist.append(Track(line, True, True, True, args)) # simple entry, just to convert to a format taken by the modules
args: dict[str, str]
for line in lns: playlist.append((line, True, True, True, args)) # simple entry, just to convert to a format taken by the modules
for module in playlist_modifier_modules: playlist = module.modify(global_args, playlist) for module in playlist_modifier_modules: playlist = module.modify(global_args, playlist)
for module in simple_modules: module.on_new_playlist(playlist) for module in simple_modules: module.on_new_playlist(playlist)
@@ -187,8 +181,7 @@ def play_playlist(playlist_path):
cross_fade = int(global_args.get("crossfade", 5)) cross_fade = int(global_args.get("crossfade", 5))
max_iterator = len(playlist) max_iterator = len(playlist)
i = 0 song_i = i = 0
song_i = 0
while i < max_iterator: while i < max_iterator:
if exit_pending: if exit_pending:
@@ -205,31 +198,30 @@ def play_playlist(playlist_path):
return_pending = True return_pending = True
continue continue
old_track_tuple = playlist[song_i % len(playlist)] old_track = playlist[song_i % len(playlist)]
if active_modifier: if active_modifier:
track_tuple, extend = active_modifier.play(song_i, old_track_tuple) track, extend = active_modifier.play(song_i, old_track)
if track_tuple is None: if track is None:
song_i += 1 song_i += 1
continue continue
if extend: max_iterator += 1 if extend: max_iterator += 1
else: else:
extend = False extend = False
track_tuple = old_track_tuple track = old_track
track, to_fade_in, to_fade_out, official, args = track_tuple
track_path = os.path.abspath(os.path.expanduser(track)) track_path = os.path.abspath(os.path.expanduser(track.path))
track_name = os.path.basename(track_path) track_name = os.path.basename(track_path)
for module in simple_modules: module.on_new_track(song_i, track_path, to_fade_in, to_fade_out, official)
logger.info(f"Now playing: {track_name}") logger.info(f"Now playing: {track_name}")
pr = procman.play(track_path, to_fade_in, to_fade_out, cross_fade) for module in simple_modules: module.on_new_track(song_i, track)
pr = procman.play(track_path, track.fade_in, track.fade_out, cross_fade)
ttw = pr.duration ttw = pr.duration
if to_fade_out: ttw -= cross_fade if track.fade_out: ttw -= cross_fade
if official: print_wait(ttw, 1, pr.duration, f"{track_name}: ") if track.official: print_wait(ttw, 1, pr.duration, f"{track_name}: ")
else: time.sleep(ttw) else: time.sleep(ttw)
i += 1 i += 1
@@ -250,7 +242,6 @@ def main():
sys.modules[full_module_name] = module sys.modules[full_module_name] = module
if MODULES_PACKAGE not in sys.modules: if MODULES_PACKAGE not in sys.modules:
import types
parent = types.ModuleType(MODULES_PACKAGE) parent = types.ModuleType(MODULES_PACKAGE)
parent.__path__ = [str(MODULES_DIR)] parent.__path__ = [str(MODULES_DIR)]
parent.__package__ = MODULES_PACKAGE parent.__package__ = MODULES_PACKAGE
@@ -283,7 +274,6 @@ def main():
exit(1) exit(1)
imc = InterModuleCommunication(playlist_advisor, active_modifier, simple_modules) imc = InterModuleCommunication(playlist_advisor, active_modifier, simple_modules)
playlist_advisor.imc(imc) playlist_advisor.imc(imc)
if active_modifier: active_modifier.imc(imc) if active_modifier: active_modifier.imc(imc)
for module in simple_modules: module.imc(imc) for module in simple_modules: module.imc(imc)
@@ -292,7 +282,7 @@ def main():
arg = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else None arg = " ".join(sys.argv[1:]) if len(sys.argv) > 1 else None
if active_modifier: active_modifier.arguments(arg) if active_modifier: active_modifier.arguments(arg)
while True: while True:
playlist = playlist_advisor.advise(arg) if playlist := playlist_advisor.advise(arg):
logger.info(f"Advisor picked '{playlist}' to play") logger.info(f"Advisor picked '{playlist}' to play")
play_playlist(playlist) play_playlist(playlist)
if exit_pending: exit() if exit_pending: exit()