From d479af33afa54fee7c22701c6012a1579ead395f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 19 Jun 2016 22:57:03 +0200 Subject: [PATCH] Fix channels and description with blank lines --- helpers/__init__.py | 8 +++++++- helpers/key.py | 6 +++++- helpers/music_file.py | 19 ++++++++++--------- music_sampler.py | 3 ++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/helpers/__init__.py b/helpers/__init__.py index b7acbf5..d0d3f46 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -14,6 +14,8 @@ def parse_config(mapping): file_lock = Lock("file") + channel_id = 0 + for mapped_key in config['keys']: key = mapping.find_by_unicode(mapped_key) if key is None: @@ -39,7 +41,8 @@ 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) + seen_files[filename] = MusicFile(filename, file_lock, channel_id) + channel_id = channel_id + 1 action_args['music'] = seen_files[filename] @@ -57,3 +60,6 @@ def parse_config(mapping): key.set_description(config['key_properties'][key_property]['description']) if 'color' in config['key_properties'][key_property]: key.set_color(config['key_properties'][key_property]['color']) + + # Return the number of channels reserved + return channel_id + 1 diff --git a/helpers/key.py b/helpers/key.py index e9e485d..7b8051e 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -72,7 +72,11 @@ class Key: ) def set_description(self, description): - self.description = [str(desc) for desc in description] + for desc in description: + if desc is None: + self.description.append("") + else: + self.description.append(str(desc)) def set_color(self, color): self.custom_color = tuple(color) diff --git a/helpers/music_file.py b/helpers/music_file.py index 5101f7f..f6b0117 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -3,9 +3,9 @@ import pydub import pygame class MusicFile: - def __init__(self, filename, lock): + def __init__(self, filename, lock, channel_id): self.filename = filename - self.channel = None + self.channel_id = channel_id self.raw_data = None self.sound = None @@ -22,7 +22,7 @@ class MusicFile: lock.release() def is_playing(self): - return self.channel is not None and self.channel.get_busy() + return self.channel().get_busy() def play(self, fade_in = 0, volume = 100, start_at = 0): self.set_volume(volume) @@ -36,18 +36,16 @@ class MusicFile: else: self.sound = pygame.mixer.Sound(self.raw_data) - self.channel = self.sound.play(fade_ms = fade_in * 1000) + self.channel().play(self.sound, fade_ms = fade_in * 1000) def pause(self): - if self.channel is not None: - self.channel.pause() + self.channel().pause() def stop(self, fade_out = 0): - self.channel = None if fade_out > 0: - self.sound.fadeout(fade_out * 1000) + self.channel().fadeout(fade_out * 1000) else: - self.sound.stop() + self.channel().stop() def set_volume(self, value): if value < 0: @@ -58,3 +56,6 @@ class MusicFile: def wait_end(self): pass + + def channel(self): + return pygame.mixer.Channel(self.channel_id) diff --git a/music_sampler.py b/music_sampler.py index d4fc2a7..8dbbc28 100644 --- a/music_sampler.py +++ b/music_sampler.py @@ -13,7 +13,8 @@ screen.fill((250, 250, 250)) draw_lock = helpers.Lock("draw") mapping = helpers.Mapping(screen, draw_lock) -helpers.parse_config(mapping) +channel_number = helpers.parse_config(mapping) +pygame.mixer.set_num_channels(channel_number) mapping.draw() -- 2.41.0