]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Put time duration in playlist
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 27 Jun 2016 08:27:35 +0000 (10:27 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 27 Jun 2016 08:27:35 +0000 (10:27 +0200)
helpers/__init__.py
helpers/action.py
helpers/music_file.py
music_sampler.kv
music_sampler.py

index 70667fd21d01a1a11578030feef544c1a0be7e2e..0f819e793653744983f99702daef11df9a51af60 100644 (file)
@@ -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)
index aff61e701f76ecc72721436e5bb315aab7cf557c..327d42fe3a2f3e6df02617630922d980e4cfc476 100644 (file)
@@ -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):
index 892519d2e843be9423b98399aad37de6ce7060f4..9acdedf45026b014e39390aac7ef37a83734aa8d 100644 (file)
@@ -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
 
index d21c5fbafba0d37d9c77599b40a85a8bd7c80a4c..87c6e9338be910d4850668448dfce90880447f6f 100644 (file)
     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
     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
     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
     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
     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
  
 <Mapping>:
   size_hint: None, None
index 5e61466257fdb631ad8467e6c7bedcb16901ab0c..7bd7513d738e8c33c33d3a5e6bd9aaed8172bd76 100644 (file)
@@ -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):