diff options
-rw-r--r-- | helpers/__init__.py | 8 | ||||
-rw-r--r-- | helpers/key.py | 6 | ||||
-rw-r--r-- | helpers/music_file.py | 19 | ||||
-rw-r--r-- | 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): | |||
14 | 14 | ||
15 | file_lock = Lock("file") | 15 | file_lock = Lock("file") |
16 | 16 | ||
17 | channel_id = 0 | ||
18 | |||
17 | for mapped_key in config['keys']: | 19 | for mapped_key in config['keys']: |
18 | key = mapping.find_by_unicode(mapped_key) | 20 | key = mapping.find_by_unicode(mapped_key) |
19 | if key is None: | 21 | if key is None: |
@@ -39,7 +41,8 @@ def parse_config(mapping): | |||
39 | if argument == 'file': | 41 | if argument == 'file': |
40 | filename = action[action_name]['file'] | 42 | filename = action[action_name]['file'] |
41 | if filename not in seen_files: | 43 | if filename not in seen_files: |
42 | seen_files[filename] = MusicFile(filename, file_lock) | 44 | seen_files[filename] = MusicFile(filename, file_lock, channel_id) |
45 | channel_id = channel_id + 1 | ||
43 | 46 | ||
44 | action_args['music'] = seen_files[filename] | 47 | action_args['music'] = seen_files[filename] |
45 | 48 | ||
@@ -57,3 +60,6 @@ def parse_config(mapping): | |||
57 | key.set_description(config['key_properties'][key_property]['description']) | 60 | key.set_description(config['key_properties'][key_property]['description']) |
58 | if 'color' in config['key_properties'][key_property]: | 61 | if 'color' in config['key_properties'][key_property]: |
59 | key.set_color(config['key_properties'][key_property]['color']) | 62 | key.set_color(config['key_properties'][key_property]['color']) |
63 | |||
64 | # Return the number of channels reserved | ||
65 | 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: | |||
72 | ) | 72 | ) |
73 | 73 | ||
74 | def set_description(self, description): | 74 | def set_description(self, description): |
75 | self.description = [str(desc) for desc in description] | 75 | for desc in description: |
76 | if desc is None: | ||
77 | self.description.append("") | ||
78 | else: | ||
79 | self.description.append(str(desc)) | ||
76 | 80 | ||
77 | def set_color(self, color): | 81 | def set_color(self, color): |
78 | self.custom_color = tuple(color) | 82 | 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 | |||
3 | import pygame | 3 | import pygame |
4 | 4 | ||
5 | class MusicFile: | 5 | class MusicFile: |
6 | def __init__(self, filename, lock): | 6 | def __init__(self, filename, lock, channel_id): |
7 | self.filename = filename | 7 | self.filename = filename |
8 | self.channel = None | 8 | self.channel_id = channel_id |
9 | self.raw_data = None | 9 | self.raw_data = None |
10 | self.sound = None | 10 | self.sound = None |
11 | 11 | ||
@@ -22,7 +22,7 @@ class MusicFile: | |||
22 | lock.release() | 22 | lock.release() |
23 | 23 | ||
24 | def is_playing(self): | 24 | def is_playing(self): |
25 | return self.channel is not None and self.channel.get_busy() | 25 | return self.channel().get_busy() |
26 | 26 | ||
27 | def play(self, fade_in = 0, volume = 100, start_at = 0): | 27 | def play(self, fade_in = 0, volume = 100, start_at = 0): |
28 | self.set_volume(volume) | 28 | self.set_volume(volume) |
@@ -36,18 +36,16 @@ class MusicFile: | |||
36 | else: | 36 | else: |
37 | self.sound = pygame.mixer.Sound(self.raw_data) | 37 | self.sound = pygame.mixer.Sound(self.raw_data) |
38 | 38 | ||
39 | self.channel = self.sound.play(fade_ms = fade_in * 1000) | 39 | self.channel().play(self.sound, fade_ms = fade_in * 1000) |
40 | 40 | ||
41 | def pause(self): | 41 | def pause(self): |
42 | if self.channel is not None: | 42 | self.channel().pause() |
43 | self.channel.pause() | ||
44 | 43 | ||
45 | def stop(self, fade_out = 0): | 44 | def stop(self, fade_out = 0): |
46 | self.channel = None | ||
47 | if fade_out > 0: | 45 | if fade_out > 0: |
48 | self.sound.fadeout(fade_out * 1000) | 46 | self.channel().fadeout(fade_out * 1000) |
49 | else: | 47 | else: |
50 | self.sound.stop() | 48 | self.channel().stop() |
51 | 49 | ||
52 | def set_volume(self, value): | 50 | def set_volume(self, value): |
53 | if value < 0: | 51 | if value < 0: |
@@ -58,3 +56,6 @@ class MusicFile: | |||
58 | 56 | ||
59 | def wait_end(self): | 57 | def wait_end(self): |
60 | pass | 58 | pass |
59 | |||
60 | def channel(self): | ||
61 | 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)) | |||
13 | draw_lock = helpers.Lock("draw") | 13 | draw_lock = helpers.Lock("draw") |
14 | 14 | ||
15 | mapping = helpers.Mapping(screen, draw_lock) | 15 | mapping = helpers.Mapping(screen, draw_lock) |
16 | helpers.parse_config(mapping) | 16 | channel_number = helpers.parse_config(mapping) |
17 | pygame.mixer.set_num_channels(channel_number) | ||
17 | 18 | ||
18 | mapping.draw() | 19 | mapping.draw() |
19 | 20 | ||