]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/mapping.py
Use machine for key handling
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / mapping.py
index 19fc3c5f2140ec9ba64feac2984b97d720e9f05b..ba2c3401cb3966278c6fd07e9093f5a8c23e980d 100644 (file)
@@ -7,9 +7,10 @@ import threading
 import yaml
 import sys
 
-from .music_file import *
+from .music_file import MusicFile
 from .mixer import Mixer
 from . import Config, gain, error_print
+from .action import Action
 
 class Mapping(RelativeLayout):
     expected_keys = NumericProperty(0)
@@ -21,24 +22,43 @@ class Mapping(RelativeLayout):
             self.mixer = Mixer()
         else:
             self.mixer = None
-        self.key_config, self.open_files = self.parse_config()
+
+        try:
+            self.key_config, self.open_files = self.parse_config()
+        except Exception as e:
+            error_print("Error while loading configuration: {}".format(e))
+            sys.exit()
+
         super(Mapping, self).__init__(**kwargs)
         self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
         self._keyboard.bind(on_key_down=self._on_keyboard_down)
         self.running = []
+        self.wait_ids = {}
         Clock.schedule_interval(self.not_all_keys_ready, 1)
 
     @property
     def master_gain(self):
         return gain(self.master_volume)
 
-    def set_master_volume(self, value, delta=False):
+    def set_master_volume(self, value, delta=False, fade=0):
         [db_gain, self.master_volume] = gain(
                 value + int(delta) * self.master_volume,
                 self.master_volume)
 
         for music in self.open_files.values():
-            music.set_gain(db_gain)
+            music.set_gain_with_effect(db_gain, fade=fade)
+
+    def add_wait_id(self, wait_id, action_or_wait):
+        self.wait_ids[wait_id] = action_or_wait
+
+    def interrupt_wait(self, wait_id):
+        if wait_id in self.wait_ids:
+            action_or_wait = self.wait_ids[wait_id]
+            del(self.wait_ids[wait_id])
+            if isinstance(action_or_wait, Action):
+                action_or_wait.interrupt()
+            else:
+                action_or_wait.set()
 
     def _keyboard_closed(self):
         self._keyboard.unbind(on_key_down=self._on_keyboard_down)
@@ -47,7 +67,7 @@ class Mapping(RelativeLayout):
     def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
         key = self.find_by_key_code(keycode)
         if len(modifiers) == 0 and key is not None:
-            threading.Thread(name="MSKeyAction", target=key.do_actions).start()
+            threading.Thread(name="MSKeyAction", target=key.run).start()
         elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
             for thread in threading.enumerate():
                 if thread.getName()[0:2] != "MS":
@@ -66,7 +86,7 @@ class Mapping(RelativeLayout):
         for key in self.children:
             if not type(key).__name__ == "Key":
                 continue
-            if not key.is_key_ready:
+            if not key.is_loaded_or_failed():
                 return True
         self.ready_color = [0, 1, 0, 1]
         return False
@@ -75,7 +95,7 @@ class Mapping(RelativeLayout):
         running = self.running
         self.running = []
         for (key, start_time) in running:
-            key.interrupt_action()
+            key.interrupt()
 
     def start_running(self, key, start_time):
         self.running.append((key, start_time))
@@ -91,7 +111,7 @@ class Mapping(RelativeLayout):
         stream = open(Config.yml_file, "r")
         try:
             config = yaml.load(stream)
-        except yaml.scanner.ScannerError as e:
+        except Exception as e:
             error_print("Error while loading config file: {}".format(e))
             sys.exit()
         stream.close()
@@ -158,8 +178,8 @@ class Mapping(RelativeLayout):
                                         **config['music_properties'][filename])
                             else:
                                 seen_files[filename] = MusicFile(
-                                        self,
-                                        filename)
+                                        filename,
+                                        self)
 
                         if filename not in key_properties[mapped_key]['files']:
                             key_properties[mapped_key]['files'] \