use tracklist from config. fixes #1, #2

This commit is contained in:
Patrick Moessler 2023-05-21 22:30:07 +02:00
parent d089e11345
commit 5b0695ec3e

View file

@ -43,6 +43,8 @@ class TitleConfig(BaseModel):
mode: Optional[PlayMode] = None mode: Optional[PlayMode] = None
timeout: Optional[int] = None timeout: Optional[int] = None
resume_track: Optional[bool] = True resume_track: Optional[bool] = True
tracks: Optional[List[str]] = None
start_times: Optional[Dict[str, int]] = None
class TitleStatus(BaseModel): class TitleStatus(BaseModel):
@ -55,14 +57,9 @@ class Title:
tag_id: int tag_id: int
config: TitleConfig config: TitleConfig
status: TitleStatus status: TitleStatus
tracks: list[str]
class Renderer(ABC): class Renderer(ABC):
@abstractmethod
def get_tracks(self, path: str) -> list[str]:
pass
@abstractmethod @abstractmethod
def play(self, path: str, from_time: int) -> None: def play(self, path: str, from_time: int) -> None:
pass pass
@ -153,18 +150,29 @@ class Player:
def advance(self) -> None: def advance(self) -> None:
assert self.current_title is not None assert self.current_title is not None
assert self.current_title.config.tracks is not None
track_id = ( 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 if self.current_title.status.last_track
else -1 else -1
) )
log.debug(f"advance from #{track_id}: {self.current_title.status.last_track}") log.debug(f"advance from #{track_id}: {self.current_title.status.last_track}")
if self.current_title.config.mode == "sequence": 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": elif self.current_title.config.mode == "random":
track_id = randrange(0, len(self.current_title.tracks), 1) track_id = randrange(0, len(self.current_title.config.tracks), 1)
self.current_title.status.last_track = self.current_title.tracks[track_id] self.current_title.status.last_track = self.current_title.config.tracks[
track_id
]
self.current_title.status.last_time = 0 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}") log.debug(f"advance to #{track_id}: {self.current_title.status.last_track}")
def start_playing(self) -> None: def start_playing(self) -> None:
@ -233,7 +241,6 @@ class Player:
tag_id=tag_id, tag_id=tag_id,
config=cfg, config=cfg,
status=self.get_title_status(tag_id), status=self.get_title_status(tag_id),
tracks=self.renderer.get_tracks(self.get_title_path(tag_id)),
) )
if ( if (
self.current_title.status.last_track == "" self.current_title.status.last_track == ""
@ -300,10 +307,6 @@ class MpdRenderer(Renderer):
# self.start: int = 0 # self.start: int = 0
# self.offset: 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: def play(self, path: str, from_time: int) -> None:
log.info(f"MpdRenderer: play({path}, {from_time})") log.info(f"MpdRenderer: play({path}, {from_time})")
self.mpd.clear() self.mpd.clear()