]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Fix channels and description with blank lines
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 19 Jun 2016 20:57:03 +0000 (22:57 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 19 Jun 2016 20:57:03 +0000 (22:57 +0200)
helpers/__init__.py
helpers/key.py
helpers/music_file.py
music_sampler.py

index b7acbf55ee89683d447daee097d4bc78effb0d5b..d0d3f46f5588d92677ecd7bc3e4a072fc103a9b3 100644 (file)
@@ -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
index e9e485d11d52d45a61c0d73060aa66895f611a7a..7b8051eca028ea049b0c0c8daa889c99cc1b9722 100644 (file)
@@ -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)
index 5101f7f4c7232175a243d5445e8c9551bc58e950..f6b0117d1f1bf71f67d6d9973c1e9b80b7ecf226 100644 (file)
@@ -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)
index d4fc2a726ee6923772bfd36d782ca5ff0014fc9f..8dbbc28de8e35a9e10e3ba08f08a1a2a96a89c1c 100644 (file)
@@ -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()