From 2d5d899c41d2969279760a96885b78d642b6cc43 Mon Sep 17 00:00:00 2001 From: KubaPro010 <132459354+KubaPro010@users.noreply.github.com> Date: Thu, 6 Nov 2025 18:54:13 +0100 Subject: [PATCH] caching --- modules/cache.py | 26 ++++++++++++++++++++++++++ modules/write_playlists.py | 4 ++-- radioPlayer.py | 9 +++------ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 modules/cache.py diff --git a/modules/cache.py b/modules/cache.py new file mode 100644 index 0000000..af141d6 --- /dev/null +++ b/modules/cache.py @@ -0,0 +1,26 @@ +import os +from . import PlayerModule, Track + +class Module(PlayerModule): + def __init__(self) -> None: + self.playlist = [] + def on_new_playlist(self, playlist: list[Track]): + self.playlist = [t.path.absolute() for t in playlist] + def on_new_track(self, index: int, track: Track): + if track.path.absolute().as_posix() != self.playlist[index].as_posix(): + # 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 + next = self.playlist[index] + else: + next = self.playlist[index+1] + + with open(track.path.absolute(), "rb") as f: + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_SEQUENTIAL) + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_NOREUSE) + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_WILLNEED) + with open(next, "rb") as f: + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_SEQUENTIAL) + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_NOREUSE) + os.posix_fadvise(f.fileno(), 0, 0, os.POSIX_FADV_WILLNEED) + +module = Module() \ No newline at end of file diff --git a/modules/write_playlists.py b/modules/write_playlists.py index 496ea9f..6b2b994 100644 --- a/modules/write_playlists.py +++ b/modules/write_playlists.py @@ -1,4 +1,4 @@ -from . import PlayerModule, log95, Track +from . import PlayerModule, log95, Track, Popen logger = log95.log95("PlayView") @@ -6,7 +6,7 @@ class Module(PlayerModule): def __init__(self) -> None: self.playlist = [] def on_new_playlist(self, playlist: list[Track]): - self.playlist = [str(t.path) for t in playlist] + self.playlist = [str(t.path.absolute()) for t in playlist] def on_new_track(self, index: int, track: Track): if str(track.path) != self.playlist[index]: # discrepancy, which means that the playing file was modified by the active modifier diff --git a/radioPlayer.py b/radioPlayer.py index d0ce618..17a79f2 100644 --- a/radioPlayer.py +++ b/radioPlayer.py @@ -34,23 +34,20 @@ class ProcessManager(Skeleton_ProcessManager): return result return None def play(self, track: Track, fade_time: int=5) -> Process: - cmd = ['ffmpeg', '-hide_banner', '-loglevel', 'quiet'] + cmd = ['ffplay', '-nodisp', '-hide_banner', '-autoexit', '-loglevel', 'quiet'] assert track.path.exists() duration = self._get_audio_duration(track.path.absolute()) if not duration: raise Exception("Failed to get file duration for", track.path) if track.offset >= duration: track.offset = max(duration - 0.1, 0) - - if track.offset > 0: - cmd.extend(['-ss', str(track.offset)]) - cmd.extend(['-i', str(track.path.absolute())]) + if track.offset > 0: cmd.extend(['-ss', str(track.offset)]) filters = [] if track.fade_in: filters.append(f"afade=t=in:st=0:d={fade_time}") if track.fade_out: filters.append(f"afade=t=out:st={duration - fade_time - track.offset}:d={fade_time}") if filters: cmd.extend(['-af', ",".join(filters)]) - cmd.extend(['-f', 'pulse', 'default']) + cmd.append(str(track.path.absolute())) pr = Process(Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True), track.path.name, time.monotonic(), duration - track.offset) with self.lock: self.processes.append(pr)