import os import threading import vlc from config import Config from time import sleep from log import DEBUG, ERROR class Music: """ Manage the playing of music tracks """ def __init__(self, max_volume=100): self.fading = False self.player = None self.track_path = None self.max_volume = max_volume def fade(self): """ Fade the currently playing track. Return the current track position. The actual management of fading runs in its own thread so as not to hold up the UI during the fade. """ DEBUG("fade()") if not self.playing(): return None self.fading = True position = self.player.get_position() thread = threading.Thread(target=self._fade) thread.start() return position def _fade(self): """ Implementation of fading the current track in a separate thread. """ DEBUG("_fade()") fade_time = Config.FADE_TIME / 1000 steps = Config.FADE_STEPS sleep_time = fade_time / steps # We reduce volume by one mesure first, then by two measures, # then three, and so on. # The sum of the arithmetic sequence 1, 2, 3, ..n is # (n**2 + n) / 2 total_measures_count = (steps**2 + steps) / 2 # Take a copy of current player to allow another track to be # started without interfering here p = self.player measures_to_reduce_by = 0 for i in range(1, steps + 1): measures_to_reduce_by += i DEBUG(f"measures_to_reduce_by={measures_to_reduce_by}") volume_factor = 1 - (measures_to_reduce_by / total_measures_count) DEBUG(f"volume_factor={volume_factor}") p.audio_set_volume(int(self.max_volume * volume_factor)) sleep(sleep_time) p.pause() p.release() self.fading = False def get_playtime(self): "Return elapsed play time" return self.player.get_time() def play(self, path): """ Start playing the track at path. Log and return if path not found. """ DEBUG(f"play({path})") if not os.access(path, os.R_OK): ERROR(f"play({path}): path not found") return self.track_path = path self.player = vlc.MediaPlayer(path) self.player.audio_set_volume(self.max_volume) self.player.play() def playing(self): """ Return True if currently playing a track, else False vlc.is_playing() returns True if track was faded out. get_position seems more reliable. """ if self.player: if self.player.get_position() > 0 and self.player.is_playing(): return True # We take a copy of the player when fading, so we could be # playing in a fade nowFalse return self.fading def set_position(self, ms): "Set current play time in milliseconds from start" return self.player.set_time(ms) def set_volume(self, volume): "Set maximum volume used for player" self.max_volume = volume self.player.audio_set_volume(volume) def stop(self): "Immediately stop playing" position = self.player.get_position() self.player.stop() self.player.release() return position