]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Add possibility to reload YML config file
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Tue, 26 Jul 2016 22:14:08 +0000 (00:14 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Tue, 26 Jul 2016 22:14:08 +0000 (00:14 +0200)
helpers/key.py
helpers/mapping.py
helpers/music_file.py

index 363e9ced7387577753113fda8a516b280abd9858..4ec08d19aa4c98185dcf9e12d2abb8a751c57d2c 100644 (file)
@@ -70,7 +70,7 @@ class Key(ButtonBehavior, Widget):
         },
         {
             'trigger': 'reload',
-            'source': 'loaded',
+            'source': ['loaded','failed'],
             'dest': 'configuring',
             'after': 'key_loaded_callback'
         },
@@ -190,6 +190,7 @@ class Key(ButtonBehavior, Widget):
     def set_description(self, description):
         if description[0] is not None:
             self.description_title = str(description[0])
+        self.description = []
         for desc in description[1 :]:
             if desc is None:
                 self.description.append("")
index 864afbef2eddf81c5fa791d64e1efea037a4afc4..1256696a8968bfe58ea843ff8a0cfc1874fa0471 100644 (file)
@@ -1,5 +1,5 @@
 from kivy.uix.relativelayout import RelativeLayout
-from kivy.properties import NumericProperty, ListProperty
+from kivy.properties import NumericProperty, ListProperty, StringProperty
 from kivy.core.window import Window
 from kivy.clock import Clock
 
@@ -56,13 +56,34 @@ class Mapping(RelativeLayout):
             'trigger': 'success',
             'source': 'loading',
             'dest': 'loaded'
+        },
+        {
+            'trigger': 'reload',
+            'source': 'loaded',
+            'dest': 'configuring'
         }
     ]
 
     master_volume = NumericProperty(100)
     ready_color = ListProperty([1, 165/255, 0, 1])
+    state = StringProperty("")
 
     def __init__(self, **kwargs):
+        self.keys = []
+        self.running = []
+        self.wait_ids = {}
+        self.open_files = {}
+
+        Machine(model=self, states=self.STATES,
+                transitions=self.TRANSITIONS, initial='initial',
+                ignore_invalid_triggers=True, queued=True)
+        super(Mapping, self).__init__(**kwargs)
+        self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
+        self.keyboard.bind(on_key_down=self.on_keyboard_down)
+
+        self.configure()
+
+    def on_enter_configuring(self):
         if Config.builtin_mixing:
             self.mixer = Mixer()
         else:
@@ -74,14 +95,13 @@ class Mapping(RelativeLayout):
             error_print("Error while loading configuration: {}".format(e),
                     with_trace=True)
             sys.exit()
+        else:
+            self.success()
 
-        self.keys = []
-        self.running = []
-        self.wait_ids = {}
-
-        super(Mapping, self).__init__(**kwargs)
-        self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
-        self.keyboard.bind(on_key_down=self.on_keyboard_down)
+    def on_enter_loading(self):
+        for key in self.keys:
+            key.reload()
+        self.success()
 
     # Kivy events
     def add_widget(self, widget, index=0):
@@ -112,6 +132,8 @@ class Mapping(RelativeLayout):
                 thread.join()
 
             sys.exit()
+        elif 'ctrl' in modifiers and keycode[0] == 114:
+            threading.Thread(name="MSReload", target=self.reload).start()
         return True
 
     # Helpers
@@ -143,6 +165,8 @@ class Mapping(RelativeLayout):
             self.ready_color = [0, 1, 0, 1]
         elif result == "partial":
             self.ready_color = [1, 0, 0, 1]
+        else:
+            self.ready_color = [1, 165/255, 0, 1]
 
     ## Some global actions
     def stop_all_running(self):
@@ -347,8 +371,14 @@ class Mapping(RelativeLayout):
                                     music_properties[filename],
                                     filename)
 
-                            seen_files[filename] = MusicFile(
-                                    filename, self, **music_property)
+                            if filename in self.open_files:
+                                self.open_files[filename]\
+                                        .reload_properties(**music_property)
+
+                                seen_files[filename] = self.open_files[filename]
+                            else:
+                                seen_files[filename] = MusicFile(
+                                        filename, self, **music_property)
 
                         if filename not in key_properties[mapped_key]['files']:
                             key_properties[mapped_key]['files'] \
index 9976306bcd254436f4dc030c5c51408de377b0ae..ba8614204e030423c2aa71baca8b3f084143e759 100644 (file)
@@ -92,6 +92,31 @@ class MusicFile:
 
         threading.Thread(name="MSMusicLoad", target=self.load).start()
 
+    def reload_properties(self, name=None, gain=1):
+        self.name = name or self.filename
+        if gain != self.initial_volume_factor:
+            self.initial_volume_factor = gain
+            self.reload_music_file()
+
+    def reload_music_file(self):
+        with file_lock:
+            try:
+                debug_print("Reloading « {} »".format(self.name))
+                initial_db_gain = gain(self.initial_volume_factor * 100)
+                self.audio_segment = pydub.AudioSegment \
+                        .from_file(self.filename) \
+                        .set_frame_rate(Config.frame_rate) \
+                        .set_channels(Config.channels) \
+                        .set_sample_width(Config.sample_width) \
+                        .apply_gain(initial_db_gain)
+            except Exception as e:
+                error_print("failed to reload « {} »: {}"\
+                        .format(self.name, e))
+                self.loading_error = e
+                self.to_failed()
+            else:
+                debug_print("Reloaded « {} »".format(self.name))
+
     # Machine related events
     def on_enter_loading(self):
         with file_lock: