From: Ismaël Bouya Date: Mon, 27 Jun 2016 08:27:35 +0000 (+0200) Subject: Put time duration in playlist X-Git-Tag: 1.0.0~71 X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git;a=commitdiff_plain;h=98ff43054fe94f333e2deda2906cd62593ded1d8 Put time duration in playlist --- diff --git a/helpers/__init__.py b/helpers/__init__.py index 70667fd..0f819e7 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -9,3 +9,10 @@ def path(): path = os.path.dirname(os.path.realpath(__file__)) return path + "/../" +def duration_to_min_sec(duration): + minutes = int(duration / 60) + seconds = int(duration) % 60 + if minutes < 100: + return "{:2}:{:0>2}".format(minutes, seconds) + else: + return "{}:{:0>2}".format(minutes, seconds) diff --git a/helpers/action.py b/helpers/action.py index aff61e7..327d42f 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -31,7 +31,6 @@ class Action: def run(self): print(self.description()) getattr(self, self.action)(**self.arguments) - #pygame.event.post(pygame.event.Event(pygame.USEREVENT)) def description(self): return getattr(self, self.action + "_print")(**self.arguments) @@ -43,13 +42,17 @@ class Action: if music is not None: music.pause() else: - pygame.mixer.pause() + for music in self.key.parent.open_files.values(): + if music.is_playing() and not music.is_paused(): + music.pause() def unpause(self, music = None, **kwargs): if music is not None: music.unpause() else: - pygame.mixer.unpause() + for music in self.key.parent.open_files.values(): + if music.is_playing() and music.is_paused(): + music.unpause() def play(self, music = None, fade_in = 0, start_at = 0, restart_if_running = False, volume = 100, **kwargs): diff --git a/helpers/music_file.py b/helpers/music_file.py index 892519d..9acdedf 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -2,6 +2,7 @@ import threading import pydub import pygame import math +import time class MusicFile: def __init__(self, filename, lock, channel_id, name = None, gain = 1): @@ -32,6 +33,15 @@ class MusicFile: def is_paused(self): return self.flag_paused + @property + def sound_position(self): + if self.is_playing() and not self.is_paused(): + return min(time.time() - self.started_at, self.sound_duration) + elif self.is_playing() and self.is_paused(): + return min(self.paused_at - self.started_at, self.sound_duration) + else: + return 0 + def play(self, fade_in = 0, volume = 100, start_at = 0): self.channel().set_endevent() self.channel().set_endevent(pygame.USEREVENT) @@ -45,14 +55,17 @@ class MusicFile: else: sound = pygame.mixer.Sound(self.raw_data) + self.started_at = time.time() self.channel().play(sound, fade_ms = int(fade_in * 1000)) self.flag_paused = False def pause(self): + self.paused_at = time.time() self.channel().pause() self.flag_paused = True def unpause(self): + self.started_at += (time.time() - self.paused_at) self.channel().unpause() self.flag_paused = False diff --git a/music_sampler.kv b/music_sampler.kv index d21c5fb..87c6e93 100644 --- a/music_sampler.kv +++ b/music_sampler.kv @@ -171,6 +171,7 @@ text: self.parent.first_key text_size: None, None valign: "top" + halign: "center" size_hint: None, None size: self.parent.width, self.texture_size[1] pos: 0, self.parent.height - self.height @@ -182,6 +183,7 @@ text: self.parent.second_key text_size: None, None valign: "top" + halign: "center" size_hint: None, None size: self.parent.width, self.texture_size[1] pos: 0, self.parent.height - key_list_first.height - self.height @@ -193,6 +195,7 @@ text: self.parent.third_key text_size: None, None valign: "top" + halign: "center" size_hint: None, None size: self.parent.width, self.texture_size[1] pos: 0, self.parent.height - key_list_first.height - key_list_second.height - self.height @@ -204,6 +207,7 @@ text: "\n".join(self.parent.keylist[3:]) text_size: None, None valign: "top" + halign: "center" size_hint: None, None size: self.parent.width, self.texture_size[1] pos: 0, self.parent.height - key_list_first.height - key_list_second.height - key_list_third.height - self.height @@ -288,6 +292,19 @@ size_hint: None, None pos: 15, self.y size: self.texture_size[0], self.parent.height + Label: + id: playlist_times + font_name: h.path() + "fonts/Ubuntu-Regular.ttf" + line_height: self.parent.parent.ubuntu_regular_line_height or 1 + font_size: math.ceil(2 * math.sqrt(self.parent.parent.key_size or 10)) + color: 0, 0, 0, 1 + text: "\n".join(map(lambda x: x[2], self.parent.playlist)) + text_size: None, self.parent.height + halign: "left" + valign: "top" + size_hint: None, None + pos: self.parent.width - 3 * self.width / 2 - 2 * (self.parent.parent.border or 0), self.y + size: self.texture_size[0], self.parent.height : size_hint: None, None diff --git a/music_sampler.py b/music_sampler.py index 5e61466..7bd7513 100644 --- a/music_sampler.py +++ b/music_sampler.py @@ -18,7 +18,7 @@ class KeyList(RelativeLayout): third_key = StringProperty("") def append(self, value): - self.keylist = [value] + self.keylist + self.keylist.insert(0, value) def on_keylist(self, instance, new_key_list): if len(self.keylist) > 0: @@ -44,10 +44,16 @@ class PlayList(RelativeLayout): for music_file in open_files.values(): if not music_file.is_playing(): continue + + text = "{}/{}".format( + helpers.duration_to_min_sec(music_file.sound_position), + helpers.duration_to_min_sec(music_file.sound_duration) + ) + if music_file.is_paused(): - self.playlist.append(["⏸", music_file.name, False]) + self.playlist.append(["⏸", music_file.name, text, False]) else: - self.playlist.append(["⏵", music_file.name, True]) + self.playlist.append(["⏵", music_file.name, text, True]) class ActionList(RelativeLayout):