From: Ismaël Bouya Date: Mon, 20 Jun 2016 21:08:22 +0000 (+0200) Subject: Added music name, currently playing musics, pause/unpause X-Git-Tag: 1.0.0~92 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=9de92b6dd2bd906f6a64fce7c90a6aff0dbb27a2;hp=d479af33afa54fee7c22701c6012a1579ead395f;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git Added music name, currently playing musics, pause/unpause --- diff --git a/Symbola.ttf b/Symbola.ttf new file mode 100644 index 0000000..51d9a88 Binary files /dev/null and b/Symbola.ttf differ diff --git a/config.yml b/config.yml index bd67496..a27f2e1 100644 --- a/config.yml +++ b/config.yml @@ -1,36 +1,52 @@ aliases: intro_jonglacro: file: "PIRATE_01-1-intro_jonglacro.mp3" + name: Intro Jonglacro jonglacro_suite: file: "PIRATE_01-2-jonglacro_suite.mp3" + name: Jonglacro suite lancer_prisonniere: file: "PIRATE_02-lancers_de_prisonniere.mp3" + name: Lancer prisonnière quete: file: "PIRATE_03-quete.mp3" + name: Quête entree_ondine: file: "PIRATE_04-entree_ondine.mp3" + name: Entrée Ondine cour_ondine: file: "PIRATE_05-cour_a_londine.mp3" + name: Cour à l'Ondine fouille_jonglage: file: "PIRATE_07-fouille_jonglage.mp3" + name: Fouille jonglage liberation_prisonniere: file: "PIRATE_08-liberation_prisonniere.mp3" + name: Libération prisonnière duo_pirate_ondine: file: "PIRATE_09-duo_pirate_ondine.mp3" + name: Duo pirate Ondine trio_jalousie: file: "PIRATE_10-trios_jalousie.mp3" + name: Trio Jalousie piquage_2_4: file: "PIRATE_11-piquage_2_4.mp3" + name: Piquage 2-4 baston: file: "PIRATE_12-1-baston.mp3" + name: Baston baston_ascension: file: "PIRATE_12-3-baston_ascension.mp3" + name: Baston ascension bruit: file: "PIRATE_bruit.wav" + name: Bruit paf: file: "PIRATE_paf.mp3" + name: Paf ralenti: file: "PIRATE_12-2-ralenti.mp3" + name: Ralenti key_properties: 'a': @@ -42,7 +58,7 @@ key_properties: description: - #Chloé - Jonglacro - - 2 + - "2" color: [73, 221, 226] 'e': description: @@ -140,7 +156,7 @@ key_properties: description: - #Jérôme - Jeu à - - 2 4 + - "2 4" color: [240, 158, 0] 'm': description: @@ -162,12 +178,12 @@ key_properties: 'c': description: - #Christophe - - Baston 2 + - Baston "2" color: [255, 255, 0] 'v': description: - #Auréliane - - Fin baston 2 + - Fin baston "2" color: [0, 190, 27] 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: diff --git a/music_sampler.py b/music_sampler.py index 8dbbc28..ff04fd1 100644 --- a/music_sampler.py +++ b/music_sampler.py @@ -6,14 +6,14 @@ import threading pygame.mixer.pre_init(frequency = 44100) pygame.init() -size = width, height = 1024, 600 +size = width, height = 913, 563 screen = pygame.display.set_mode(size) -screen.fill((250, 250, 250)) +screen.fill((229, 228, 226)) draw_lock = helpers.Lock("draw") mapping = helpers.Mapping(screen, draw_lock) -channel_number = helpers.parse_config(mapping) +channel_number, open_files = helpers.parse_config(mapping) pygame.mixer.set_num_channels(channel_number) mapping.draw() @@ -49,12 +49,37 @@ while 1: key = mapping.find_by_key_num(event.key) if key is not None: threading.Thread(name = "MSKeyAction", target=key.do_actions).start() + threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start() elif event.type == pygame.MOUSEBUTTONUP: key = mapping.find_by_collidepoint(pygame.mouse.get_pos()) if key is not None: threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start() draw_lock.acquire() + if getattr(sys, 'frozen', False): + icon_police = pygame.font.Font(sys._MEIPASS + "/Symbola.ttf", 19) + police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) + else: + icon_police = pygame.font.Font("Symbola.ttf", 19) + police = pygame.font.Font("Ubuntu-Regular.ttf", 14) + + surface = pygame.Surface((208, 250)).convert() + surface.fill((250, 250, 250)) + offset = 0 + for music_file in open_files.values(): + police.set_bold(False) + if music_file.is_playing(): + icon = icon_police.render("⏵", True, (0,0,0)) + if music_file.is_paused(): + icon = icon_police.render("⏸", True, (0,0,0)) + else: + police.set_bold(True) + text = police.render(music_file.name, True, (0,0,0)) + surface.blit(icon, (0, offset)) + surface.blit(text, (20, offset)) + offset += police.get_linesize() + screen.blit(surface, (700, 308)) + pygame.display.flip() draw_lock.release() diff --git a/music_sampler.spec b/music_sampler.spec index 3c85a2a..a60c546 100644 --- a/music_sampler.spec +++ b/music_sampler.spec @@ -4,7 +4,11 @@ block_cipher = None a = Analysis(['music_sampler.py'], binaries=None, - datas=[('config.yml', '.'), ('Ubuntu-Regular.ttf', '.')], + datas=[ + ('config.yml', '.'), + ('Ubuntu-Regular.ttf', '.'), + ('Symbola.ttf', '.') + ], hiddenimports=[ 'six', 'packaging',