diff --git a/src/sleepywaves.py b/src/sleepywaves.py index 6baeb7b..550f5a8 100644 --- a/src/sleepywaves.py +++ b/src/sleepywaves.py @@ -43,6 +43,8 @@ class TitleConfig(BaseModel): mode: Optional[PlayMode] = None timeout: Optional[int] = None resume_track: Optional[bool] = True + tracks: Optional[List[str]] = None + start_times: Optional[Dict[str, int]] = None class TitleStatus(BaseModel): @@ -55,14 +57,9 @@ class Title: tag_id: int config: TitleConfig status: TitleStatus - tracks: list[str] class Renderer(ABC): - @abstractmethod - def get_tracks(self, path: str) -> list[str]: - pass - @abstractmethod def play(self, path: str, from_time: int) -> None: pass @@ -153,18 +150,29 @@ class Player: def advance(self) -> None: assert self.current_title is not None + assert self.current_title.config.tracks is not None track_id = ( - self.current_title.tracks.index(self.current_title.status.last_track) + self.current_title.config.tracks.index(self.current_title.status.last_track) if self.current_title.status.last_track else -1 ) log.debug(f"advance from #{track_id}: {self.current_title.status.last_track}") if self.current_title.config.mode == "sequence": - track_id = (track_id + 1) % len(self.current_title.tracks) + track_id = (track_id + 1) % len(self.current_title.config.tracks) elif self.current_title.config.mode == "random": - track_id = randrange(0, len(self.current_title.tracks), 1) - self.current_title.status.last_track = self.current_title.tracks[track_id] + track_id = randrange(0, len(self.current_title.config.tracks), 1) + self.current_title.status.last_track = self.current_title.config.tracks[ + track_id + ] self.current_title.status.last_time = 0 + if ( + self.current_title.config.start_times is not None + and self.current_title.status.last_track + in self.current_title.config.start_times + ): + self.current_title.status.last_time = self.current_title.config.start_times[ + self.current_title.status.last_track + ] log.debug(f"advance to #{track_id}: {self.current_title.status.last_track}") def start_playing(self) -> None: @@ -233,7 +241,6 @@ class Player: tag_id=tag_id, config=cfg, status=self.get_title_status(tag_id), - tracks=self.renderer.get_tracks(self.get_title_path(tag_id)), ) if ( self.current_title.status.last_track == "" @@ -300,10 +307,6 @@ class MpdRenderer(Renderer): # self.start: int = 0 # self.offset: int = 0 - def get_tracks(self, path: str) -> list[str]: - all_files = os.listdir(path) - return sorted([f for f in all_files if (f.endswith(".mp3") or f.endswith(".flac"))]) - def play(self, path: str, from_time: int) -> None: log.info(f"MpdRenderer: play({path}, {from_time})") self.mpd.clear()