]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/music_file.py
Prepare modifiers
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
index ccf60ce5eb8874f5b68dc81d48a89b9e86fd8626..9976306bcd254436f4dc030c5c51408de377b0ae 100644 (file)
@@ -32,7 +32,8 @@ class MusicFile:
         {
             'trigger': 'load',
             'source': 'initial',
-            'dest': 'loading'
+            'dest': 'loading',
+            'after': 'poll_loaded'
         },
         {
             'trigger': 'fail',
@@ -47,7 +48,10 @@ class MusicFile:
         {
             'trigger': 'start_playing',
             'source': 'loaded',
-            'dest': 'loaded_playing'
+            'dest': 'loaded_playing',
+            # if a child has no transitions, then it is bubbled to the parent,
+            # and we don't want that. Not useful in that machine precisely.
+            'conditions': ['is_loaded']
         },
         {
             'trigger': 'pause',
@@ -68,7 +72,8 @@ class MusicFile:
             'trigger': 'stopped',
             'source': '*',
             'dest': 'loaded',
-            'before': 'trigger_stopped_events'
+            'before': 'trigger_stopped_events',
+            'conditions': ['is_in_use']
         }
     ]
 
@@ -77,6 +82,7 @@ class MusicFile:
                 transitions=self.TRANSITIONS, initial='initial',
                 ignore_invalid_triggers=True)
 
+        self.loaded_callbacks = []
         self.mapping = mapping
         self.filename = filename
         self.name = name or filename
@@ -109,6 +115,9 @@ class MusicFile:
                 debug_print("Loaded « {} »".format(self.name))
 
     def on_enter_loaded(self):
+        self.cleanup()
+
+    def cleanup(self):
         self.gain_effects = []
         self.set_gain(0, absolute=True)
         self.current_audio_segment = None
@@ -130,6 +139,7 @@ class MusicFile:
     def trigger_stopped_events(self):
         self.mixer.remove_file(self)
         self.wait_event.set()
+        self.cleanup()
 
     # Actions and properties called externally
     @property
@@ -230,6 +240,22 @@ class MusicFile:
         self.wait_event.clear()
         self.wait_event.wait()
 
+    # Let other subscribe for an event when they are ready
+    def subscribe_loaded(self, callback):
+        # FIXME: should lock to be sure we have no race, but it makes the
+        # initialization screen not showing until everything is loaded
+        if self.is_loaded(allow_substates=True):
+            callback(True)
+        elif self.is_failed():
+            callback(False)
+        else:
+            self.loaded_callbacks.append(callback)
+
+    def poll_loaded(self):
+        for callback in self.loaded_callbacks:
+            callback(self.is_loaded())
+        self.loaded_callbacks = []
+
     # Callbacks
     def finished_callback(self):
         self.stopped()