From ab47d2a1269c20d70f42942c4295c056544491f4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 27 Jul 2016 00:14:08 +0200 Subject: [PATCH] Add possibility to reload YML config file --- helpers/key.py | 3 ++- helpers/mapping.py | 50 ++++++++++++++++++++++++++++++++++--------- helpers/music_file.py | 25 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/helpers/key.py b/helpers/key.py index 363e9ce..4ec08d1 100644 --- a/helpers/key.py +++ b/helpers/key.py @@ -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("") diff --git a/helpers/mapping.py b/helpers/mapping.py index 864afbe..1256696 100644 --- a/helpers/mapping.py +++ b/helpers/mapping.py @@ -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'] \ diff --git a/helpers/music_file.py b/helpers/music_file.py index 9976306..ba86142 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -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: -- 2.41.0