aboutsummaryrefslogtreecommitdiff
path: root/helpers
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-27 10:27:35 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-27 10:27:35 +0200
commit98ff43054fe94f333e2deda2906cd62593ded1d8 (patch)
treee8cfe3df72c6a02bd1d8fbe565b1a8b9185c79b2 /helpers
parent65ec4d2a87bfe0dcf1250ec8dc61225d4ed66325 (diff)
downloadMusicSampler-98ff43054fe94f333e2deda2906cd62593ded1d8.tar.gz
MusicSampler-98ff43054fe94f333e2deda2906cd62593ded1d8.tar.zst
MusicSampler-98ff43054fe94f333e2deda2906cd62593ded1d8.zip
Put time duration in playlist
Diffstat (limited to 'helpers')
-rw-r--r--helpers/__init__.py7
-rw-r--r--helpers/action.py9
-rw-r--r--helpers/music_file.py13
3 files changed, 26 insertions, 3 deletions
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():
9 path = os.path.dirname(os.path.realpath(__file__)) 9 path = os.path.dirname(os.path.realpath(__file__))
10 return path + "/../" 10 return path + "/../"
11 11
12def duration_to_min_sec(duration):
13 minutes = int(duration / 60)
14 seconds = int(duration) % 60
15 if minutes < 100:
16 return "{:2}:{:0>2}".format(minutes, seconds)
17 else:
18 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:
31 def run(self): 31 def run(self):
32 print(self.description()) 32 print(self.description())
33 getattr(self, self.action)(**self.arguments) 33 getattr(self, self.action)(**self.arguments)
34 #pygame.event.post(pygame.event.Event(pygame.USEREVENT))
35 34
36 def description(self): 35 def description(self):
37 return getattr(self, self.action + "_print")(**self.arguments) 36 return getattr(self, self.action + "_print")(**self.arguments)
@@ -43,13 +42,17 @@ class Action:
43 if music is not None: 42 if music is not None:
44 music.pause() 43 music.pause()
45 else: 44 else:
46 pygame.mixer.pause() 45 for music in self.key.parent.open_files.values():
46 if music.is_playing() and not music.is_paused():
47 music.pause()
47 48
48 def unpause(self, music = None, **kwargs): 49 def unpause(self, music = None, **kwargs):
49 if music is not None: 50 if music is not None:
50 music.unpause() 51 music.unpause()
51 else: 52 else:
52 pygame.mixer.unpause() 53 for music in self.key.parent.open_files.values():
54 if music.is_playing() and music.is_paused():
55 music.unpause()
53 56
54 def play(self, music = None, fade_in = 0, start_at = 0, 57 def play(self, music = None, fade_in = 0, start_at = 0,
55 restart_if_running = False, volume = 100, **kwargs): 58 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
2import pydub 2import pydub
3import pygame 3import pygame
4import math 4import math
5import time
5 6
6class MusicFile: 7class MusicFile:
7 def __init__(self, filename, lock, channel_id, name = None, gain = 1): 8 def __init__(self, filename, lock, channel_id, name = None, gain = 1):
@@ -32,6 +33,15 @@ class MusicFile:
32 def is_paused(self): 33 def is_paused(self):
33 return self.flag_paused 34 return self.flag_paused
34 35
36 @property
37 def sound_position(self):
38 if self.is_playing() and not self.is_paused():
39 return min(time.time() - self.started_at, self.sound_duration)
40 elif self.is_playing() and self.is_paused():
41 return min(self.paused_at - self.started_at, self.sound_duration)
42 else:
43 return 0
44
35 def play(self, fade_in = 0, volume = 100, start_at = 0): 45 def play(self, fade_in = 0, volume = 100, start_at = 0):
36 self.channel().set_endevent() 46 self.channel().set_endevent()
37 self.channel().set_endevent(pygame.USEREVENT) 47 self.channel().set_endevent(pygame.USEREVENT)
@@ -45,14 +55,17 @@ class MusicFile:
45 else: 55 else:
46 sound = pygame.mixer.Sound(self.raw_data) 56 sound = pygame.mixer.Sound(self.raw_data)
47 57
58 self.started_at = time.time()
48 self.channel().play(sound, fade_ms = int(fade_in * 1000)) 59 self.channel().play(sound, fade_ms = int(fade_in * 1000))
49 self.flag_paused = False 60 self.flag_paused = False
50 61
51 def pause(self): 62 def pause(self):
63 self.paused_at = time.time()
52 self.channel().pause() 64 self.channel().pause()
53 self.flag_paused = True 65 self.flag_paused = True
54 66
55 def unpause(self): 67 def unpause(self):
68 self.started_at += (time.time() - self.paused_at)
56 self.channel().unpause() 69 self.channel().unpause()
57 self.flag_paused = False 70 self.flag_paused = False
58 71