]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Add seek action
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 14 Jul 2016 21:06:05 +0000 (23:06 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 14 Jul 2016 21:06:05 +0000 (23:06 +0200)
helpers/action.py
helpers/music_file.py

index 2f294a01d1c83e3c3ad50e0aad1fe2e50f05ec0e..3dc1b8daf396f1316049b2d707ed2f47316d551e 100644 (file)
@@ -6,6 +6,7 @@ class Action:
         'command',
         'pause',
         'play',
+        'seek',
         'stop',
         'stop_all_actions',
         'unpause',
@@ -73,6 +74,10 @@ class Action:
                 if not music.is_not_stopped():
                     music.play(volume = volume, fade_in = fade_in, start_at = start_at)
 
+    def seek(self, music = None, value = 0, delta = False, **kwargs):
+        for music in self.music_list(music):
+            music.seek(value = value, delta = delta)
+
     def stop(self, music = None, fade_out = 0, wait = False, **kwargs):
         previous = None
         for music in self.music_list(music):
@@ -156,6 +161,18 @@ class Action:
     def stop_all_actions_print(self, **kwargs):
         return "stopping all actions"
 
+    def seek_print(self, music = None, value = 0, delta = False, **kwargs):
+        if delta:
+            if music is not None:
+                return "moving music « {} » by {:+d}s".format(music.name, value)
+            else:
+                return "moving all musics by {:+d}s".format(value)
+        else:
+            if music is not None:
+                return "moving music « {} » to position {}s".format(music.name, value)
+            else:
+                return "moving all musics to position {}s".format(value)
+
     def volume_print(self, music = None, value = 100, delta = False, **kwargs):
         if delta:
             if music is not None:
index b926c64c63a20e1adcc689a70c0e15d69a32ca1b..32869b6891aed831a8b2768016aedf3a83a7fe45 100644 (file)
@@ -129,6 +129,13 @@ class MusicFile(Machine):
 
             out_data[:] = audio_segment.ljust(len(out_data), b'\0')
 
+    def seek(self, value = 0, delta = False):
+        # We don't want to do that while stopping
+        if not (self.is_loaded_playing() or self.is_loaded_paused()):
+            return
+        with self.music_lock:
+            self.current_frame = max(0, int(delta) * self.current_frame + int(value * self.audio_segment.frame_rate))
+
     def stop(self, fade_out = 0, wait = False):
         if self.is_loaded_playing():
             ms = int(self.sound_position * 1000)