From 21ffec315b8fa8a6b46351021da915236148a7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 12 Aug 2016 14:51:32 +0200 Subject: Make 'interrupt_wait' able to interrupt all waits If 'wait_id' is not specified, interrupt_wait will now stop all wait events. This fixes https://git.immae.eu/mantisbt/view.php?id=4 --- documentation_en.md | 6 ++-- documentation_fr.md | 7 ++--- music_sampler/actions/interrupt_wait.py | 5 +++- music_sampler/actions/wait.py | 3 +- .../locales/fr/LC_MESSAGES/music_sampler.po | 4 +++ music_sampler/mapping.py | 35 +++++++++++++++------- music_sampler/music_file.py | 3 +- 7 files changed, 40 insertions(+), 23 deletions(-) diff --git a/documentation_en.md b/documentation_en.md index d65e7ad..899ef1a 100644 --- a/documentation_en.md +++ b/documentation_en.md @@ -386,9 +386,9 @@ actions. action. When false, it is thus useless to add actions after that one. - `interrupt_wait`: stop a wait event (normal `wait` or fade out wait). The keys that were waiting will move to the next actions. Parameters: - * `wait_id: name` : gives the id of the `wait` to interrupt (defined with - `set_wait_id`, see actions `wait` and `stop`). To interrupt several waits, - use the same action several times. + * `wait_id: name` (optional) gives the id of the `wait` to interrupt (defined with + `set_wait_id`, see actions `wait` and `stop`). If not given, interrupts + all wait events. - `run_command` : Run a command. Parameters: * `command: my_command` : Gives the command to run. * `wait: true/false` (optional, default false) if true, waits for the diff --git a/documentation_fr.md b/documentation_fr.md index 64e3a4f..ff3cfe8 100644 --- a/documentation_fr.md +++ b/documentation_fr.md @@ -432,10 +432,9 @@ successivement mais sans attendre (donc presque simultanément) : ne pas hésite suite de celle-ci puisqu'elles seront systématiquement interrompues. - `interrupt_wait`: interrompt l'attente (de `wait` ou fin d'un fondu avec attente) et passe directement à l'action suivante. Paramètre : - * `wait_id: name` : précise l'identifiant du `wait` à stopper (défini par - `set_wait_id`, voir les actions `wait` et `stop`). Pour interrompre - plusieurs `wait` d'un seul coup, il faut mettre plusieurs - `interrupt_wait`. + * `wait_id: name` (facultatif) précise l'identifiant du `wait` à stopper + (défini par `set_wait_id`, voir les actions `wait` et `stop`). Si absent, + interrompt toutes les attentes. - `run_command` : lance une commande. Paramètres : * `command: my_command` : précise la commande à lancer. * `wait: true/false` (facultatif, défaut : false) : si `wait` est true, diff --git a/music_sampler/actions/interrupt_wait.py b/music_sampler/actions/interrupt_wait.py index f85a3c4..b367628 100644 --- a/music_sampler/actions/interrupt_wait.py +++ b/music_sampler/actions/interrupt_wait.py @@ -2,4 +2,7 @@ def run(action, wait_id=None, **kwargs): action.mapping.interrupt_wait(wait_id) def description(action, wait_id=None, **kwargs): - return _("interrupt wait with id {}").format(wait_id) + if wait_id is None: + return _("interrupt all waits") + else: + return _("interrupt wait with id {}").format(wait_id) diff --git a/music_sampler/actions/wait.py b/music_sampler/actions/wait.py index e6d07f2..bcee649 100644 --- a/music_sampler/actions/wait.py +++ b/music_sampler/actions/wait.py @@ -1,8 +1,7 @@ import threading def run(action, duration=0, music=None, set_wait_id=None, **kwargs): - if set_wait_id is not None: - action.mapping.add_wait_id(set_wait_id, action) + action.mapping.add_wait(action, wait_id=set_wait_id) action.sleep_event = threading.Event() action.sleep_event_timer = threading.Timer( diff --git a/music_sampler/locales/fr/LC_MESSAGES/music_sampler.po b/music_sampler/locales/fr/LC_MESSAGES/music_sampler.po index f603a3d..888d7a5 100644 --- a/music_sampler/locales/fr/LC_MESSAGES/music_sampler.po +++ b/music_sampler/locales/fr/LC_MESSAGES/music_sampler.po @@ -91,6 +91,10 @@ msgstr "L'argument '-V' ne peut être utilisé que dans la version compilée" msgid "interrupt wait with id {}" msgstr "Interrompre l'attente d'identifiant {}" +#: music_sampler/actions/interrupt_wait.py:5 +msgid "interrupt all waits" +msgstr "Interrompre toutes les attentes" + #: music_sampler/actions/pause.py:8 msgid "pausing « {} »" msgstr "mise en pause de « {} »" diff --git a/music_sampler/mapping.py b/music_sampler/mapping.py index 50b68a9..a04c2f6 100644 --- a/music_sampler/mapping.py +++ b/music_sampler/mapping.py @@ -197,17 +197,30 @@ class Mapping(RelativeLayout): music.set_gain_with_effect(db_gain, fade=fade) # Wait handler methods - 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 add_wait(self, action_or_wait, wait_id=None): + if wait_id is not None: + self.wait_ids[wait_id] = [action_or_wait] + else: + if None not in self.wait_ids: + self.wait_ids[None] = [] + self.wait_ids[None].append(action_or_wait) + + def interrupt_wait(self, wait_id=None): + if wait_id is None: + ids_to_interrupt = list(self.wait_ids.keys()) + elif wait_id in self.wait_ids: + ids_to_interrupt = [wait_id] + else: + ids_to_interrupt = [] + + for _wait_id in ids_to_interrupt: + action_or_waits = self.wait_ids[_wait_id] + del(self.wait_ids[_wait_id]) + for action_or_wait in action_or_waits: + if isinstance(action_or_wait, Action): + action_or_wait.interrupt() + else: + action_or_wait.set() # Methods to control running keys def start_running(self, key, start_time): diff --git a/music_sampler/music_file.py b/music_sampler/music_file.py index fa6293d..4ba65e3 100644 --- a/music_sampler/music_file.py +++ b/music_sampler/music_file.py @@ -241,8 +241,7 @@ class MusicFile: self.current_audio_segment = new_audio_segment self.stop_playing() if wait: - if set_wait_id is not None: - self.mapping.add_wait_id(set_wait_id, self.wait_event) + self.mapping.add_wait(self.wait_event, wait_id=set_wait_id) self.wait_end() else: self.stopped() -- cgit v1.2.3