Implement PlaylistTrack object

This commit is contained in:
Keith Edmunds 2022-12-30 21:43:47 +00:00
parent ce08790343
commit b476db188f
2 changed files with 168 additions and 132 deletions

View File

@ -111,24 +111,74 @@ class CartButton(QPushButton):
self.pgb.setGeometry(0, 0, self.width(), 10) self.pgb.setGeometry(0, 0, self.width(), 10)
class TrackData: class PlaylistTrack:
def __init__(self, track): """
self.id = track.id Used to provide a single reference point for specific playlist tracks,
self.title = track.title typicall the previous, current and next track.
self.artist = track.artist """
self.duration = track.duration
self.start_gap = track.start_gap def __init__(self) -> None:
self.fade_at = track.fade_at """
self.silence_at = track.silence_at Only initialises data structure. Call set_plr to populate.
self.path = track.path """
self.mtime = track.mtime
self.artist = None
self.duration = None
self.end_time = None
self.fade_at = None
self.fade_length = None
self.path = None
self.playlist_id = None
self.playlist_tab = None
self.plr_id = None
self.row_number = None
self.silence_at = None
self.start_gap = None
self.start_time = None
self.title = None
self.track_id = None
def __repr__(self) -> str: def __repr__(self) -> str:
return ( return (
f"<TrackData(id={self.id}, title={self.title}, " f"<PlaylistTrack(id={self.id}, title={self.title}, "
f"artist={self.artist}, path={self.path}>" f"artist={self.artist}, row_number={self.row_number} ",
f"playlist_id={self.playlist_id}>"
) )
def set_plr(self, session: Session, plr: PlaylistRows,
tab: PlaylistTab) -> None:
"""
Update with new plr information
"""
self.playlist_tab = tab
session.add(plr)
track = plr.track
self.artist = track.artist
self.duration = track.duration
self.end_time = None
self.fade_at = track.fade_at
self.fade_length = track.silence_at - track.fade_at
self.path = track.path
self.playlist_id = plr.playlist_id
self.plr_id = plr.id
self.row_number = plr.row_number
self.silence_at = track.silence_at
self.start_gap = track.start_gap
self.start_time = None
self.title = track.title
self.track_id = track.id
def start(self) -> None:
"""
Called when track starts playing
"""
self.start_time = datetime.now()
self.end_time = self.start_time + timedelta(milliseconds=self.duration)
class Window(QMainWindow, Ui_MainWindow): class Window(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None) -> None: def __init__(self, parent=None) -> None:
@ -137,13 +187,14 @@ class Window(QMainWindow, Ui_MainWindow):
self.timer: QTimer = QTimer() self.timer: QTimer = QTimer()
self.even_tick: bool = True self.even_tick: bool = True
self.music: music.Music = music.Music()
self.playing: bool = False self.playing: bool = False
self.current_plr: Optional[PlaylistRows] = None self.current_track = PlaylistTrack()
self.current_track_playlist_tab: Optional[PlaylistTab] = None self.next_track = PlaylistTrack()
self.next_plr: Optional[PlaylistRows] = None self.previous_track = PlaylistTrack()
self.next_track: Optional[TrackData] = None
self.previous_plr: Optional[PlaylistRows] = None
self.previous_track_position: Optional[int] = None self.previous_track_position: Optional[int] = None
self.selected_plrs = None self.selected_plrs = None
@ -309,7 +360,7 @@ class Window(QMainWindow, Ui_MainWindow):
"""Handle attempt to close main window""" """Handle attempt to close main window"""
# Don't allow window to close when a track is playing # Don't allow window to close when a track is playing
if self.music.player and self.music.player.is_playing(): if self.playing:
event.ignore() event.ignore()
helpers.show_warning( helpers.show_warning(
"Track playing", "Track playing",
@ -374,7 +425,7 @@ class Window(QMainWindow, Ui_MainWindow):
return return
# Don't close next track playlist # Don't close next track playlist
if self.tabPlaylist.widget(tab_index) == self.next_track_playlist_tab: if self.tabPlaylist.widget(tab_index) == self.next_track.playlist_tab:
self.statusbar.showMessage( self.statusbar.showMessage(
"Can't close next track playlist", 5000) "Can't close next track playlist", 5000)
return return
@ -560,17 +611,14 @@ class Window(QMainWindow, Ui_MainWindow):
# doesn't see player=None and kick off end-of-track actions # doesn't see player=None and kick off end-of-track actions
self.playing = False self.playing = False
# Reset current track # Tell playlist_tab track has finished
if self.current_plr: if self.current_track.playlist_tab:
self.previous_plr = self.current_plr self.current_track.playlist_tab.play_stopped()
self.current_plr = None
# Tell playlist_tab track has finished and # Reset PlaylistTrack objects
# reset current playlist_tab if self.current_track.track_id:
if self.current_track_playlist_tab: self.previous_track = self.current_track
self.previous_track_playlist_tab = self.current_track_playlist_tab self.current_track = PlaylistTrack()
self.current_track_playlist_tab.play_stopped()
self.current_track_playlist_tab = None
# Reset clocks # Reset clocks
self.frame_fade.setStyleSheet("") self.frame_fade.setStyleSheet("")
@ -582,7 +630,7 @@ class Window(QMainWindow, Ui_MainWindow):
self.label_start_time.setText("00:00:00") self.label_start_time.setText("00:00:00")
self.label_end_time.setText("00:00:00") self.label_end_time.setText("00:00:00")
if self.next_track: if self.next_track.track_id:
self.label_track_length.setText( self.label_track_length.setText(
helpers.ms_to_mmss(self.next_track.duration) helpers.ms_to_mmss(self.next_track.duration)
) )
@ -994,7 +1042,7 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
# If there is no next track set, return. # If there is no next track set, return.
if not self.next_track: if not self.next_track.track_id:
log.debug("musicmuster.play_next(): no next track selected") log.debug("musicmuster.play_next(): no next track selected")
return return
@ -1002,37 +1050,33 @@ class Window(QMainWindow, Ui_MainWindow):
# If there's currently a track playing, fade it. # If there's currently a track playing, fade it.
self.stop_playing(fade=True) self.stop_playing(fade=True)
# Move next track to current track.
self.current_plr = self.next_plr
self.next_plr = None
# Ensure playlist tabs are the correct colour # Ensure playlist tabs are the correct colour
# If current track on different playlist_tab to last, reset # If next track is on a different playlist_tab to the
# last track playlist_tab colour # current track, reset the current track playlist_tab colour
if self.current_track_playlist_tab != self.next_track_playlist_tab: if self.current_track.playlist_tab != self.next_track.playlist_tab:
self.set_tab_colour(self.current_track_playlist_tab, self.set_tab_colour(self.current_track.playlist_tab,
QColor(Config.COLOUR_NORMAL_TAB)) QColor(Config.COLOUR_NORMAL_TAB))
# # Update record of current track playlist_tab # Set current track playlist_tab colour
self.current_track_playlist_tab = self.next_track_playlist_tab self.set_tab_colour(self.current_track.playlist_tab,
self.next_track_playlist_tab = None QColor(Config.COLOUR_CURRENT_TAB))
# Set current track playlist_tab colour
self.set_tab_colour(self.current_track_playlist_tab, # Move next track to current track.
QColor(Config.COLOUR_CURRENT_TAB)) self.current_track = self.next_track
self.next_track = PlaylistTrack()
# Restore volume if -3dB active # Restore volume if -3dB active
if self.btnDrop3db.isChecked(): if self.btnDrop3db.isChecked():
self.btnDrop3db.setChecked(False) self.btnDrop3db.setChecked(False)
# Play (new) current track # Play (new) current track
start_at = datetime.now() self.current_track.start()
session.add(self.current_plr.track.path) self.music.play(self.current_track.path, position)
self.music.play(self.current_plr.track.path, position)
# Tell database to record it as played # Tell database to record it as played
Playdates(session, self.current_plr.track.id) Playdates(session, self.current_track.track_id)
# Tell playlist track is now playing # Tell playlist track is now playing
self.current_track_playlist_tab.play_started(session) self.current_track.playlist_tab.play_started(session)
# Note that track is now playing # Note that track is now playing
self.playing = True self.playing = True
@ -1045,18 +1089,15 @@ class Window(QMainWindow, Ui_MainWindow):
# Update clocks # Update clocks
self.label_track_length.setText( self.label_track_length.setText(
helpers.ms_to_mmss(self.current_plr.track.duration) helpers.ms_to_mmss(self.current_track.duration)
) )
fade_at = self.current_plr.track.fade_at
silence_at = self.current_plr.track.silence_at
self.label_fade_length.setText( self.label_fade_length.setText(
helpers.ms_to_mmss(silence_at - fade_at)) helpers.ms_to_mmss(self.current_track.fade_length))
self.label_start_time.setText( self.label_start_time.setText(
start_at.strftime(Config.TRACK_TIME_FORMAT)) self.current_track.start_time.strftime(
self.current_track_end_time = start_at + timedelta( Config.TRACK_TIME_FORMAT))
milliseconds=self.current_plr.track.duration)
self.label_end_time.setText( self.label_end_time.setText(
self.current_track_end_time.strftime(Config.TRACK_TIME_FORMAT)) self.current_track.end_time.strftime(Config.TRACK_TIME_FORMAT))
def resume(self) -> None: def resume(self) -> None:
""" """
@ -1078,24 +1119,23 @@ class Window(QMainWindow, Ui_MainWindow):
# Note resume point # Note resume point
resume_from = self.previous_track_position resume_from = self.previous_track_position
# Remember current next track # Remember what was to have been the next track
original_next_track = self.next_track original_next_plr_id = self.next_track.plr_id
original_next_track_playlist_tab = self.next_track_playlist_tab original_next_plr_playlist_tab = self.next_track.playlist_tab
with Session() as session: with Session() as session:
# Set last track to be next track # Set next track to be the last one played
self.this_is_the_next_track(session, self.next_track = self.previous_track
self.previous_track_playlist_tab, self.previous_track = PlaylistTrack()
self.previous_track)
# Resume last track # Resume last track
self.play_next(resume_from) self.play_next(resume_from)
# Reset next track if there was one # Reset next track if there was one
if original_next_track: if original_next_plr_id:
self.this_is_the_next_track(session, next_plr = Session.get(PlaylistRows, original_next_plr_id)
original_next_track_playlist_tab, self.this_is_the_next_playlist_row(
original_next_track) session, next_plr, original_next_plr_playlist_tab)
def save_as_template(self) -> None: def save_as_template(self) -> None:
"""Save current playlist as template""" """Save current playlist as template"""
@ -1193,15 +1233,15 @@ class Window(QMainWindow, Ui_MainWindow):
def show_current(self) -> None: def show_current(self) -> None:
"""Scroll to show current track""" """Scroll to show current track"""
if self.current_track_playlist_tab != self.visible_playlist_tab(): if self.current_track.playlist_tab != self.visible_playlist_tab():
self.tabPlaylist.setCurrentWidget(self.current_track_playlist_tab) self.tabPlaylist.setCurrentWidget(self.current.track_playlist_tab)
self.tabPlaylist.currentWidget().scroll_current_to_top() self.tabPlaylist.currentWidget().scroll_current_to_top()
def show_next(self) -> None: def show_next(self) -> None:
"""Scroll to show next track""" """Scroll to show next track"""
if self.next_track_playlist_tab != self.visible_playlist_tab(): if self.next_track.playlist_tab != self.visible_playlist_tab():
self.tabPlaylist.setCurrentWidget(self.next_track_playlist_tab) self.tabPlaylist.setCurrentWidget(self.next_track.playlist_tab)
self.tabPlaylist.currentWidget().scroll_next_to_top() self.tabPlaylist.currentWidget().scroll_next_to_top()
def solicit_playlist_name(self) -> Optional[str]: def solicit_playlist_name(self) -> Optional[str]:
@ -1245,11 +1285,11 @@ class Window(QMainWindow, Ui_MainWindow):
self.music.stop() self.music.stop()
# Reset playlist_tab colour # Reset playlist_tab colour
if self.current_track_playlist_tab == self.next_track_playlist_tab: if self.current_track.playlist_tab == self.next_track.playlist_tab:
self.set_tab_colour(self.current_track_playlist_tab, self.set_tab_colour(self.current_track.playlist_tab,
QColor(Config.COLOUR_NEXT_TAB)) QColor(Config.COLOUR_NEXT_TAB))
else: else:
self.set_tab_colour(self.current_track_playlist_tab, self.set_tab_colour(self.current_track.playlist_tab,
QColor(Config.COLOUR_NORMAL_TAB)) QColor(Config.COLOUR_NORMAL_TAB))
# Run end-of-track actions # Run end-of-track actions
@ -1264,12 +1304,12 @@ class Window(QMainWindow, Ui_MainWindow):
# May also be called when last tab is closed # May also be called when last tab is closed
pass pass
def this_is_the_next_track(self, session: Session, def this_is_the_next_playlist_row(self, session: Session,
playlist_tab: PlaylistTab, plr: PlaylistRows,
track: Tracks) -> None: playlist_tab: PlaylistTab) -> None:
""" """
This is notification from a playlist tab that it holds the next This is notification from a playlist tab that it holds the next
track to be played. playlist row to be played.
Actions required: Actions required:
- Clear next track if on other tab - Clear next track if on other tab
@ -1283,47 +1323,44 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
# Clear next track if on another tab # Clear next track if on another tab
if self.next_track_playlist_tab != playlist_tab: if self.next_track.playlist_tab != playlist_tab:
# We need to reset the ex-next-track playlist # We need to reset the ex-next-track playlist
if self.next_track_playlist_tab: if self.next_track.playlist_tab:
self.next_track_playlist_tab.clear_next(session) self.next_track.playlist_tab.clear_next(session)
# Reset tab colour if on other tab # Reset tab colour if on other tab
if (self.next_track_playlist_tab != if (self.next_track.playlist_tab !=
self.current_track_playlist_tab): self.current.track_playlist_tab):
self.set_tab_colour( self.set_tab_colour(
self.next_track_playlist_tab, self.next_track.playlist_tab,
QColor(Config.COLOUR_NORMAL_TAB)) QColor(Config.COLOUR_NORMAL_TAB))
# Note next playlist tab # Note next playlist tab
self.next_track_playlist_tab = playlist_tab self.next_track = PlaylistTrack()
self.next_track.set_plr(session, plr, playlist_tab)
# Set next playlist_tab tab colour if it isn't the # Set next playlist_tab tab colour if it isn't the
# currently-playing tab # currently-playing tab
if (self.next_track_playlist_tab != if (self.next_track.playlist_tab !=
self.current_track_playlist_tab): self.current_track.playlist_tab):
self.set_tab_colour( self.set_tab_colour(
self.next_track_playlist_tab, self.next_track.playlist_tab,
QColor(Config.COLOUR_NEXT_TAB)) QColor(Config.COLOUR_NEXT_TAB))
# Note next track
self.next_track = TrackData(track)
# Populate footer if we're not currently playing # Populate footer if we're not currently playing
if not self.playing and self.next_track.track_id:
if not self.playing and self.next_track:
self.label_track_length.setText( self.label_track_length.setText(
helpers.ms_to_mmss(self.next_track.duration) helpers.ms_to_mmss(self.next_track.duration)
) )
self.label_fade_length.setText(helpers.ms_to_mmss( self.label_fade_length.setText(helpers.ms_to_mmss(
self.next_track.silence_at - self.next_track.fade_at)) self.next_track.fade_length))
# Update headers # Update headers
self.update_headers() self.update_headers()
# Populate 'info' tabs with Wikipedia info, but queue it because # Populate 'info' tabs with Wikipedia info, but queue it because
# it isn't quick # it isn't quick
track_title = track.title track_title = self.next_track.title
QTimer.singleShot( QTimer.singleShot(
1, lambda: self.tabInfolist.open_in_wikipedia(track_title) 1, lambda: self.tabInfolist.open_in_wikipedia(track_title)
) )
@ -1363,10 +1400,10 @@ class Window(QMainWindow, Ui_MainWindow):
# If track is playing, update track clocks time and colours # If track is playing, update track clocks time and colours
if self.music.player and self.music.player.is_playing(): if self.music.player and self.music.player.is_playing():
playtime = self.music.get_playtime() playtime = self.music.get_playtime()
time_to_fade = (self.current_plr.track.fade_at - playtime) time_to_fade = (self.current_track.fade_at - playtime)
time_to_silence = ( time_to_silence = (
self.current_plr.track.silence_at - playtime) self.current_track.silence_at - playtime)
time_to_end = (self.current_plr.track.duration - playtime) time_to_end = (self.current_track.duration - playtime)
# Elapsed time # Elapsed time
self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime)) self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime))
@ -1408,12 +1445,6 @@ class Window(QMainWindow, Ui_MainWindow):
if self.playing: if self.playing:
self.stop_playing() self.stop_playing()
def update_current_track(self, track):
"""Update current track with passed details"""
self.current_track = TrackData(track)
self.update_headers()
def update_next_track(self, track): def update_next_track(self, track):
"""Update next track with passed details""" """Update next track with passed details"""
@ -1425,23 +1456,23 @@ class Window(QMainWindow, Ui_MainWindow):
Update last / current / next track headers Update last / current / next track headers
""" """
try: if self.previous_track.title:
self.hdrPreviousTrack.setText( self.hdrPreviousTrack.setText(
f"{self.previous_track.title} - {self.previous_track.artist}") f"{self.previous_track.title} - {self.previous_track.artist}")
except AttributeError: else:
self.hdrPreviousTrack.setText("") self.hdrPreviousTrack.setText("")
try: if self.current_track.title:
self.hdrCurrentTrack.setText( self.hdrCurrentTrack.setText(
f"{self.current_track.title} - {self.current_track.artist}") f"{self.current_track.title} - {self.current_track.artist}")
except AttributeError: else:
self.hdrCurrentTrack.setText("") self.hdrCurrentTrack.setText("")
try: if self.next_track.title:
self.hdrNextTrack.setText( self.hdrNextTrack.setText(
f"{self.next_track.title} - {self.next_track.artist}" f"{self.next_track.title} - {self.next_track.artist}"
) )
except AttributeError: else:
self.hdrNextTrack.setText("") self.hdrNextTrack.setText("")

View File

@ -419,12 +419,16 @@ class PlaylistTab(QTableWidget):
# Determin cell type changed # Determin cell type changed
with Session() as session: with Session() as session:
if self.edit_cell_type == ROW_NOTES: # Get playlistrow object
# Get playlistrow object plr_id = self._get_playlistrow_id(row)
plr_id = self._get_playlistrow_id(row) plr_item = session.get(PlaylistRows, plr_id)
plr_item = session.get(PlaylistRows, plr_id)
plr_item.note = new_text
# Note any updates needed to PlaylistTrack objects
update_current = self.musicmuster.current_track.plr_id == plr_id
update_next = self.musicmuster.next_track.plr_id == plr_id
if self.edit_cell_type == ROW_NOTES:
plr_item.note = new_text
# Set/clear row start time accordingly # Set/clear row start time accordingly
start_time = self._get_note_text_time(new_text) start_time = self._get_note_text_time(new_text)
if start_time: if start_time:
@ -436,23 +440,23 @@ class PlaylistTab(QTableWidget):
if track_id: if track_id:
track = session.get(Tracks, track_id) track = session.get(Tracks, track_id)
if track: if track:
update_current = row == self._get_current_track_row()
update_next = row == self._get_next_track_row()
if self.edit_cell_type == TITLE: if self.edit_cell_type == TITLE:
log.debug(f"KAE: _cell_changed:440, {new_text=}") log.debug(f"KAE: _cell_changed:440, {new_text=}")
track.title = new_text track.title = new_text
if update_current:
self.musicmuster.current_track.title = new_text
if update_next:
self.musicmuster.next_track.title = new_text
elif self.edit_cell_type == ARTIST: elif self.edit_cell_type == ARTIST:
track.artist = new_text track.artist = new_text
if update_current: if update_current:
self.musicmuster.update_current_track(track) self.musicmuster.current_track.artist = \
elif update_next: new_text
self.musicmuster.update_next_track(track) if update_next:
self.musicmuster.next_track.artist = new_text
# Headers will be incorrect if the edited track is if update_next or update_current:
# previous / current / next TODO: this will require self.musicmuster.update_headers()
# the stored data in musicmuster to be updated,
# which currently it isn't).
self.musicmuster.update_headers()
def closeEditor(self, def closeEditor(self,
editor: QWidget, editor: QWidget,
@ -1098,7 +1102,7 @@ class PlaylistTab(QTableWidget):
# if there's a track playing, set start time from # if there's a track playing, set start time from
# that. It may be on a different tab, so we get # that. It may be on a different tab, so we get
# start time from musicmuster. # start time from musicmuster.
start_time = self.musicmuster.current_track_end_time start_time = self.musicmuster.current_track.end_time
if start_time is None: if start_time is None:
# No current track to base from, but don't change # No current track to base from, but don't change
# time if it's already set # time if it's already set
@ -1813,7 +1817,7 @@ class PlaylistTab(QTableWidget):
def _set_next(self, session: Session, row_number: int) -> None: def _set_next(self, session: Session, row_number: int) -> None:
""" """
Set passed row as next track to play. Set passed row as next playlist row to play.
Actions required: Actions required:
- Check row has a track - Check row has a track
@ -1847,7 +1851,8 @@ class PlaylistTab(QTableWidget):
self.update_display(session) self.update_display(session)
# Notify musicmuster # Notify musicmuster
self.musicmuster.this_is_the_next_track(session, self, track) plr = session.get(PlaylistRows, self._get_playlistrow_id(row_number))
self.musicmuster.this_is_the_next_playlist_row(session, plr, self)
def _set_next_track_row(self, row: int) -> None: def _set_next_track_row(self, row: int) -> None:
"""Mark this row as next track""" """Mark this row as next track"""