Improve clock management

- tick() implemented independently of Config.TIMER_MS
 - have tick() call periodic functions
 - don't rely on vlc get_time() (too coarse)
This commit is contained in:
Keith Edmunds 2023-06-19 09:40:32 +01:00
parent b706008101
commit 09f0e11aa7
3 changed files with 104 additions and 89 deletions

View File

@ -69,7 +69,6 @@ class Config(object):
MAX_MISSING_FILES_TO_REPORT = 10 MAX_MISSING_FILES_TO_REPORT = 10
MILLISECOND_SIGFIGS = 0 MILLISECOND_SIGFIGS = 0
MINIMUM_ROW_HEIGHT = 30 MINIMUM_ROW_HEIGHT = 30
MYSQL_CONNECT = os.environ.get('MYSQL_CONNECT') or "mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_v2" # noqa E501
NOTE_TIME_FORMAT = "%H:%M:%S" NOTE_TIME_FORMAT = "%H:%M:%S"
OBS_HOST = "localhost" OBS_HOST = "localhost"
OBS_PASSWORD = "auster" OBS_PASSWORD = "auster"

View File

@ -91,14 +91,6 @@ class Music:
fader = FadeTrack(p) fader = FadeTrack(p)
pool.start(fader) pool.start(fader)
def get_playtime(self) -> Optional[int]:
"""Return elapsed play time"""
if not self.player:
return None
return self.player.get_time()
def get_position(self) -> Optional[float]: def get_position(self) -> Optional[float]:
"""Return current position""" """Return current position"""

View File

@ -915,6 +915,24 @@ class Window(QMainWindow, Ui_MainWindow):
else: else:
return None return None
def get_playtime(self) -> int:
"""
Return number of milliseconds current track has been playing or
zero if not playing. The vlc function get_time() only updates 3-4
times a second; this function has much better resolution.
"""
if (
self.current_track.track_id is None or
self.current_track.start_time is None
):
return 0
now = datetime.now()
track_start = self.current_track.start_time
elapsed_seconds = (now - track_start).total_seconds()
return int(elapsed_seconds * 1000)
def hide_played(self): def hide_played(self):
"""Toggle hide played tracks""" """Toggle hide played tracks"""
@ -1671,29 +1689,25 @@ class Window(QMainWindow, Ui_MainWindow):
def tick(self) -> None: def tick(self) -> None:
""" """
Carry out clock tick actions. Called every Config.TIMER_MS milliseconds. Call periodic functions
as required.
"""
self.clock_counter is incrememted at each tick (100ms), and this # Get current number of milliseconds
value is used to determine the actions to take. self.clock_counter += Config.TIMER_MS
self.clock_counter %= 1000
The Fade Volume graph is updated every 10ms. # Call periodic functions
if self.clock_counter % 10 == 0:
self.tick_10ms()
if self.clock_counter % 500 == 0:
self.tick_500ms()
if self.clock_counter == 0:
self.tick_1000ms()
The Time of Day clock and any cart progress bars are updated def tick_10ms(self) -> None:
every 500ms. """
Called every 10ms
All other timers are updated every second. As the timer displays
have a one-second resolution, updating every 500ms can result in
some timers updating and then, 500ms later, other timers
updating. That looks odd.
Actions required:
- Update Fade Volume graph
- Update TOD clock
- Call cart_tick
- If track is playing:
update track clocks time and colours
- Else:
run stop_track
""" """
# Update volume fade curve # Update volume fade curve
@ -1707,74 +1721,84 @@ class Window(QMainWindow, Ui_MainWindow):
).total_seconds() * 1000 ).total_seconds() * 1000
self.current_track.fade_graph.tick(play_time) self.current_track.fade_graph.tick(play_time)
if self.clock_counter % 20 == 0: def tick_500ms(self) -> None:
# Update TOD clock """
self.lblTOD.setText(datetime.now().strftime( Called every 500ms
Config.TOD_TIME_FORMAT)) """
# Update carts
self.cart_tick()
if self.clock_counter % 50 == 0: self.lblTOD.setText(datetime.now().strftime(
if not self.playing: Config.TOD_TIME_FORMAT))
return # Update carts
self.cart_tick()
# If track is playing, update track clocks time and colours def tick_1000ms(self) -> None:
# There is a discrete time between starting playing a track and """
# player.is_playing() returning True, so assume playing if less Called every 1000ms
# than Config.PLAY_SETTLE microseconds have passed since """
# starting play.
if self.music.player and self.current_track.start_time and (
self.music.player.is_playing() or
(datetime.now() - self.current_track.start_time)
< timedelta(microseconds=Config.PLAY_SETTLE)):
playtime = self.music.get_playtime()
time_to_fade = (self.current_track.fade_at - playtime)
time_to_silence = (
self.current_track.silence_at - playtime)
time_to_end = (self.current_track.duration - playtime)
# Elapsed time # Only update play clocks once a second so that their updates
self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime)) # are synchronised (otherwise it looks odd)
# Time to fade if not self.playing:
self.label_fade_timer.setText(helpers.ms_to_mmss(time_to_fade)) return
# If silent in the next 5 seconds, put warning colour on # If track is playing, update track clocks time and colours
# time to silence box and enable play controls # There is a discrete time between starting playing a track and
if time_to_silence <= 5500: # player.is_playing() returning True, so assume playing if less
self.frame_silent.setStyleSheet( # than Config.PLAY_SETTLE microseconds have passed since
f"background: {Config.COLOUR_ENDING_TIMER}" # starting play.
) if self.music.player and self.current_track.start_time and (
self.enable_play_next_controls() self.music.player.is_playing() or
# Set warning colour on time to silence box when fade starts (datetime.now() - self.current_track.start_time)
elif time_to_fade <= 500: < timedelta(microseconds=Config.PLAY_SETTLE)):
self.frame_silent.setStyleSheet( playtime = self.get_playtime()
f"background: {Config.COLOUR_WARNING_TIMER}" time_to_fade = (self.current_track.fade_at - playtime)
) time_to_silence = (
# Five seconds before fade starts, set warning colour on self.current_track.silence_at - playtime)
# time to silence box and enable play controls time_to_end = (self.current_track.duration - playtime)
elif time_to_fade <= 5500:
self.frame_fade.setStyleSheet(
f"background: {Config.COLOUR_WARNING_TIMER}"
)
self.enable_play_next_controls()
else:
self.frame_silent.setStyleSheet("")
self.frame_fade.setStyleSheet("")
self.label_silent_timer.setText( # Elapsed time
helpers.ms_to_mmss(time_to_silence) self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime))
# Time to fade
self.label_fade_timer.setText(helpers.ms_to_mmss(time_to_fade))
# If silent in the next 5 seconds, put warning colour on
# time to silence box and enable play controls
if time_to_silence <= 5500:
self.frame_silent.setStyleSheet(
f"background: {Config.COLOUR_ENDING_TIMER}"
) )
self.enable_play_next_controls()
# Time to end # Set warning colour on time to silence box when fade starts
self.label_end_timer.setText(helpers.ms_to_mmss(time_to_end)) elif time_to_fade <= 500:
self.frame_silent.setStyleSheet(
# Autoplay next track f"background: {Config.COLOUR_WARNING_TIMER}"
# if time_to_silence <= 1500: )
# self.play_next() # Five seconds before fade starts, set warning colour on
# time to silence box and enable play controls
elif time_to_fade <= 5500:
self.frame_fade.setStyleSheet(
f"background: {Config.COLOUR_WARNING_TIMER}"
)
self.enable_play_next_controls()
else: else:
if self.playing: self.frame_silent.setStyleSheet("")
self.stop_playing() self.frame_fade.setStyleSheet("")
self.label_silent_timer.setText(
helpers.ms_to_mmss(time_to_silence)
)
# Time to end
self.label_end_timer.setText(helpers.ms_to_mmss(time_to_end))
# Autoplay next track
# if time_to_silence <= 1500:
# self.play_next()
else:
if self.playing:
self.stop_playing()
def update_headers(self) -> None: def update_headers(self) -> None:
""" """