From 9de92b6dd2bd906f6a64fce7c90a6aff0dbb27a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 20 Jun 2016 23:08:22 +0200 Subject: Added music name, currently playing musics, pause/unpause --- helpers/__init__.py | 7 +++++-- helpers/action.py | 26 ++++++++++++++++++++------ helpers/key.py | 4 ++-- helpers/music_file.py | 19 ++++++++++++++++--- 4 files changed, 43 insertions(+), 13 deletions(-) (limited to 'helpers') diff --git a/helpers/__init__.py b/helpers/__init__.py index d0d3f46..b3ab1eb 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -41,7 +41,10 @@ def parse_config(mapping): if argument == 'file': filename = action[action_name]['file'] if filename not in seen_files: - seen_files[filename] = MusicFile(filename, file_lock, channel_id) + if 'name' in action[action_name]: + seen_files[filename] = MusicFile(filename, file_lock, channel_id, name = action[action_name]['name']) + else: + seen_files[filename] = MusicFile(filename, file_lock, channel_id) channel_id = channel_id + 1 action_args['music'] = seen_files[filename] @@ -62,4 +65,4 @@ def parse_config(mapping): key.set_color(config['key_properties'][key_property]['color']) # Return the number of channels reserved - return channel_id + 1 + return (channel_id + 1, seen_files) diff --git a/helpers/action.py b/helpers/action.py index 5afe437..1cb1686 100644 --- a/helpers/action.py +++ b/helpers/action.py @@ -8,6 +8,7 @@ class Action: 'play', 'stop', 'stop_all_actions', + 'unpause', 'volume', 'wait', ] @@ -29,7 +30,8 @@ class Action: def run(self): print(self.description()) - return getattr(self, self.action)(**self.arguments) + 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,6 +45,12 @@ class Action: else: pygame.mixer.pause() + def unpause(self, music = None, **kwargs): + if music is not None: + music.unpause() + else: + pygame.mixer.unpause() + def play(self, music = None, fade_in = 0, start_at = 0, restart_if_running = False, volume = 100, **kwargs): if music is not None: @@ -88,15 +96,21 @@ class Action: def pause_print(self, music = None, **kwargs): if music is not None: - return "pausing {}".format(music.filename) + return "pausing « {} »".format(music.name) else: return "pausing all musics" + def unpause_print(self, music = None, **kwargs): + if music is not None: + return "unpausing « {} »".format(music.name) + else: + return "unpausing all musics" + def play_print(self, music = None, fade_in = 0, start_at = 0, restart_if_running = False, volume = 100, **kwargs): message = "starting " if music is not None: - message += music.filename + message += "« {} »".format(music.name) else: message += "music" @@ -116,9 +130,9 @@ class Action: def stop_print(self, music = None, fade_out = 0, **kwargs): if music is not None: if fade_out == 0: - return "stopping music {}".format(music.filename) + return "stopping music « {} »".format(music.name) else: - return "stopping music {} with {}s fadeout".format(music.filename, fade_out) + return "stopping music « {} » with {}s fadeout".format(music.name, fade_out) else: if fade_out == 0: return "stopping all musics" @@ -130,7 +144,7 @@ class Action: def volume_print(self, music = None, value = 100, **kwargs): if music is not None: - return "setting volume of {} to {}%".format(music.filename, value) + return "setting volume of « {} » to {}%".format(music.name, value) else: return "setting volume to {}%".format(value) diff --git a/helpers/key.py b/helpers/key.py index 7b8051e..57fdef1 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -149,7 +149,7 @@ class Key: #print("actions linked to key {}:".format(self.key_sym)) #print("\t" + "\n\t".join(action_descriptions)) self.draw_lock.acquire() - surface = pygame.Surface((800, 250)).convert() + surface = pygame.Surface((690, 250)).convert() surface.fill((250, 250, 250)) if getattr(sys, 'frozen', False): police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) @@ -168,7 +168,7 @@ class Key: surface.blit(text, (0, offset)) offset += police.get_linesize() - screen.blit(surface, (10, 330)) + screen.blit(surface, (5, 308)) pygame.display.flip() self.draw_lock.release() diff --git a/helpers/music_file.py b/helpers/music_file.py index f6b0117..d40a132 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -3,28 +3,35 @@ import pydub import pygame class MusicFile: - def __init__(self, filename, lock, channel_id): + def __init__(self, filename, lock, channel_id, name = None): self.filename = filename self.channel_id = channel_id + self.name = name or filename self.raw_data = None self.sound = None self.loaded = False + self.flag_paused = False threading.Thread(name = "MSMusicLoad", target = self.load_sound, args = [lock]).start() def load_sound(self, lock): lock.acquire() - print("Loading {}".format(self.filename)) + print("Loading « {} »".format(self.name)) self.raw_data = pydub.AudioSegment.from_file(self.filename).raw_data self.sound = pygame.mixer.Sound(self.raw_data) - print("Loaded {}".format(self.filename)) + print("Loaded « {} »".format(self.name)) self.loaded = True lock.release() def is_playing(self): return self.channel().get_busy() + def is_paused(self): + return self.flag_paused + def play(self, fade_in = 0, volume = 100, start_at = 0): + self.channel().set_endevent() + self.channel().set_endevent(pygame.USEREVENT) self.set_volume(volume) if start_at > 0: @@ -37,9 +44,15 @@ class MusicFile: self.sound = pygame.mixer.Sound(self.raw_data) self.channel().play(self.sound, fade_ms = fade_in * 1000) + self.flag_paused = False def pause(self): self.channel().pause() + self.flag_paused = True + + def unpause(self): + self.channel().unpause() + self.flag_paused = False def stop(self, fade_out = 0): if fade_out > 0: -- cgit v1.2.3