]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Make 'wait' action interruptible
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 14 Jul 2016 13:39:54 +0000 (15:39 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 14 Jul 2016 13:39:54 +0000 (15:39 +0200)
helpers/action.py
helpers/key.py
helpers/mapping.py
helpers/music_file.py

index 69ae96fb8581b5f0bc0006d7841394e37ec87500..91456299ee25c6bf072c8cdba1ac796f2c99cbcd 100644 (file)
@@ -1,4 +1,4 @@
-import pygame
+import threading
 import time
 
 class Action:
@@ -21,6 +21,7 @@ class Action:
 
         self.key = key
         self.arguments = kwargs
+        self.sleep_event = None
 
     def ready(self):
         if 'music' in self.arguments:
@@ -35,7 +36,13 @@ class Action:
     def description(self):
         return getattr(self, self.action + "_print")(**self.arguments)
 
+    def interrupt(self):
+        if getattr(self, self.action + "_interrupt", None):
+            return getattr(self, self.action + "_interrupt")(**self.arguments)
+
+    # Actions
     def command(self, command = "", **kwargs):
+        # FIXME: todo
         pass
 
     def music_list(self, music):
@@ -81,14 +88,15 @@ class Action:
             pass
 
     def wait(self, duration = 0, music = None, **kwargs):
-        # FIXME: Make it stoppable
-        # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
-        if music is None:
-            time.sleep(duration)
-        else:
-            # TODO
+        self.sleep_event = threading.Event()
+
+        if music is not None:
             music.wait_end()
 
+        threading.Timer(duration, self.sleep_event.set).start()
+        self.sleep_event.wait()
+
+    # Action messages
     def command_print(self, command = "", **kwargs):
         return "running command {}".format(command)
 
@@ -146,7 +154,19 @@ class Action:
         else:
             return "setting volume to {}%".format(value)
 
-    def wait_print(self, duration, **kwargs):
-        return "waiting {}s".format(duration)
+    def wait_print(self, duration = 0, music = None, **kwargs):
+        if music is None:
+            return "waiting {}s".format(duration)
+        elif duration == 0:
+            return "waiting the end of « {} »".format(music.name)
+        else:
+            return "waiting the end of « {} » + {}s".format(music.name, duration)
 
 
+    # Interruptions
+    def wait_interrupt(self, duration = 0, music = None, **kwargs):
+        if self.sleep_event is not None:
+            self.sleep_event.set()
+        if music is not None:
+            music.wait_event.set()
+
index ca73b87a421701661a7f76434dc866da2b860fc1..5eaf481bea86a4d2aa962be2b4bd035a0692bc8e 100644 (file)
@@ -79,6 +79,9 @@ class Key(ButtonBehavior, Widget):
     def add_action(self, action_name, **arguments):
         self.actions.append(Action(action_name, self, **arguments))
 
+    def interrupt_action(self):
+        self.current_action.interrupt()
+
     def do_actions(self):
         if not self.enabled:
             return None
@@ -88,10 +91,10 @@ class Key(ButtonBehavior, Widget):
         start_time = time.time()
         self.parent.start_running(self, start_time)
         action_number = 0
-        for action in self.actions:
+        for self.current_action in self.actions:
             if self.parent.keep_running(self, start_time):
                 self.list_actions(action_number = action_number + 0.5)
-                action.run()
+                self.current_action.run()
                 action_number += 1
                 self.list_actions(action_number = action_number)
 
index 95c9d670a26488684d9d803b8b8b3c66d736eba7..ea9d0757b0244eef6f9ef2f6d949b02790424d37 100644 (file)
@@ -65,7 +65,10 @@ class Mapping(RelativeLayout):
         return False
 
     def stop_all_running(self):
+        running = self.running
         self.running = []
+        for (key, start_time) in running:
+            key.interrupt_action()
 
     def start_running(self, key, start_time):
         self.running.append((key, start_time))
index b40de1a2f9138a852376bfcc50c058b21af82766..e6a340da53e56c97dda679a14064f056e01fd670 100644 (file)
@@ -40,8 +40,8 @@ class MusicFile(Machine):
         self.audio_segment = None
         self.gain = gain
         self.music_lock = Lock("music__" + filename)
+        self.wait_event = threading.Event()
 
-        self.flag_paused = False
         threading.Thread(name = "MSMusicLoad", target = self.load).start()
 
     def on_enter_loading(self):
@@ -109,6 +109,9 @@ class MusicFile(Machine):
         if self.is_loaded_stopping():
             self.stopped()
 
+    def on_enter_loaded_stopped(self):
+        self.wait_event.set()
+
     def play_callback(self, out_data, frame_count, time_info, status_flags):
         with self.music_lock:
             audio_segment = self.current_audio_segment.get_sample_slice_data(
@@ -147,8 +150,8 @@ class MusicFile(Machine):
         return 20 * math.log10(max(volume, 0.0001) / 100)
 
     def wait_end(self):
-        # FIXME: todo
-        pass
+        self.wait_event.clear()
+        self.wait_event.wait()
 
 # Add some more functions to AudioSegments
 def get_sample_slice_data(self, start_sample=0, end_sample=float('inf')):