diff options
-rw-r--r-- | helpers/__init__.py | 7 | ||||
-rw-r--r-- | helpers/action.py | 9 | ||||
-rw-r--r-- | helpers/music_file.py | 13 | ||||
-rw-r--r-- | music_sampler.kv | 17 | ||||
-rw-r--r-- | music_sampler.py | 12 |
5 files changed, 52 insertions, 6 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 | ||
12 | def 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 | |||
2 | import pydub | 2 | import pydub |
3 | import pygame | 3 | import pygame |
4 | import math | 4 | import math |
5 | import time | ||
5 | 6 | ||
6 | class MusicFile: | 7 | class 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 | ||
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 @@ | |||
171 | text: self.parent.first_key | 171 | text: self.parent.first_key |
172 | text_size: None, None | 172 | text_size: None, None |
173 | valign: "top" | 173 | valign: "top" |
174 | halign: "center" | ||
174 | size_hint: None, None | 175 | size_hint: None, None |
175 | size: self.parent.width, self.texture_size[1] | 176 | size: self.parent.width, self.texture_size[1] |
176 | pos: 0, self.parent.height - self.height | 177 | pos: 0, self.parent.height - self.height |
@@ -182,6 +183,7 @@ | |||
182 | text: self.parent.second_key | 183 | text: self.parent.second_key |
183 | text_size: None, None | 184 | text_size: None, None |
184 | valign: "top" | 185 | valign: "top" |
186 | halign: "center" | ||
185 | size_hint: None, None | 187 | size_hint: None, None |
186 | size: self.parent.width, self.texture_size[1] | 188 | size: self.parent.width, self.texture_size[1] |
187 | pos: 0, self.parent.height - key_list_first.height - self.height | 189 | pos: 0, self.parent.height - key_list_first.height - self.height |
@@ -193,6 +195,7 @@ | |||
193 | text: self.parent.third_key | 195 | text: self.parent.third_key |
194 | text_size: None, None | 196 | text_size: None, None |
195 | valign: "top" | 197 | valign: "top" |
198 | halign: "center" | ||
196 | size_hint: None, None | 199 | size_hint: None, None |
197 | size: self.parent.width, self.texture_size[1] | 200 | size: self.parent.width, self.texture_size[1] |
198 | pos: 0, self.parent.height - key_list_first.height - key_list_second.height - self.height | 201 | pos: 0, self.parent.height - key_list_first.height - key_list_second.height - self.height |
@@ -204,6 +207,7 @@ | |||
204 | text: "\n".join(self.parent.keylist[3:]) | 207 | text: "\n".join(self.parent.keylist[3:]) |
205 | text_size: None, None | 208 | text_size: None, None |
206 | valign: "top" | 209 | valign: "top" |
210 | halign: "center" | ||
207 | size_hint: None, None | 211 | size_hint: None, None |
208 | size: self.parent.width, self.texture_size[1] | 212 | size: self.parent.width, self.texture_size[1] |
209 | pos: 0, self.parent.height - key_list_first.height - key_list_second.height - key_list_third.height - self.height | 213 | pos: 0, self.parent.height - key_list_first.height - key_list_second.height - key_list_third.height - self.height |
@@ -288,6 +292,19 @@ | |||
288 | size_hint: None, None | 292 | size_hint: None, None |
289 | pos: 15, self.y | 293 | pos: 15, self.y |
290 | size: self.texture_size[0], self.parent.height | 294 | size: self.texture_size[0], self.parent.height |
295 | Label: | ||
296 | id: playlist_times | ||
297 | font_name: h.path() + "fonts/Ubuntu-Regular.ttf" | ||
298 | line_height: self.parent.parent.ubuntu_regular_line_height or 1 | ||
299 | font_size: math.ceil(2 * math.sqrt(self.parent.parent.key_size or 10)) | ||
300 | color: 0, 0, 0, 1 | ||
301 | text: "\n".join(map(lambda x: x[2], self.parent.playlist)) | ||
302 | text_size: None, self.parent.height | ||
303 | halign: "left" | ||
304 | valign: "top" | ||
305 | size_hint: None, None | ||
306 | pos: self.parent.width - 3 * self.width / 2 - 2 * (self.parent.parent.border or 0), self.y | ||
307 | size: self.texture_size[0], self.parent.height | ||
291 | 308 | ||
292 | <Mapping>: | 309 | <Mapping>: |
293 | size_hint: None, None | 310 | 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): | |||
18 | third_key = StringProperty("") | 18 | third_key = StringProperty("") |
19 | 19 | ||
20 | def append(self, value): | 20 | def append(self, value): |
21 | self.keylist = [value] + self.keylist | 21 | self.keylist.insert(0, value) |
22 | 22 | ||
23 | def on_keylist(self, instance, new_key_list): | 23 | def on_keylist(self, instance, new_key_list): |
24 | if len(self.keylist) > 0: | 24 | if len(self.keylist) > 0: |
@@ -44,10 +44,16 @@ class PlayList(RelativeLayout): | |||
44 | for music_file in open_files.values(): | 44 | for music_file in open_files.values(): |
45 | if not music_file.is_playing(): | 45 | if not music_file.is_playing(): |
46 | continue | 46 | continue |
47 | |||
48 | text = "{}/{}".format( | ||
49 | helpers.duration_to_min_sec(music_file.sound_position), | ||
50 | helpers.duration_to_min_sec(music_file.sound_duration) | ||
51 | ) | ||
52 | |||
47 | if music_file.is_paused(): | 53 | if music_file.is_paused(): |
48 | self.playlist.append(["⏸", music_file.name, False]) | 54 | self.playlist.append(["⏸", music_file.name, text, False]) |
49 | else: | 55 | else: |
50 | self.playlist.append(["⏵", music_file.name, True]) | 56 | self.playlist.append(["⏵", music_file.name, text, True]) |
51 | 57 | ||
52 | 58 | ||
53 | class ActionList(RelativeLayout): | 59 | class ActionList(RelativeLayout): |