]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Make seek work well with loops
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 20 Jul 2016 21:17:01 +0000 (23:17 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 21 Jul 2016 07:12:34 +0000 (09:12 +0200)
helpers/music_effect.py
helpers/music_file.py

index 23583cd07c86dba02d651cf8b1755fbdda59c673..4bdbb262ecd17d89d643f07eff1d0a9df3f4dccd 100644 (file)
@@ -3,7 +3,8 @@ class GainEffect:
         'fade'
     ]
 
-    def __init__(self, effect, audio_segment, initial_loop, start, end, **kwargs):
+    def __init__(self, effect, audio_segment, initial_loop, start, end,
+            **kwargs):
         if effect in self.effect_types:
             self.effect = effect
         else:
index cff897ad67c98ac05438de3a2d27ca0d98f01d95..aef0adce00c4f55f539b67dd07479bdae7327b7e 100644 (file)
@@ -125,6 +125,8 @@ class MusicFile(Machine):
             return 0
 
     def play(self, fade_in=0, volume=100, loop=0, start_at=0):
+        # FIXME: create a "reinitialize" method
+        self.gain_effects = []
         self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True)
         self.volume = volume
         self.current_loop = 0
@@ -178,7 +180,7 @@ class MusicFile(Machine):
                     data += new_data
                     nb_frames += new_nb_frames
                 elif nb_frames == 0:
-                    # FIXME: too slow
+                    # FIXME: too slow when mixing multiple streams
                     threading.Thread(
                             name="MSFinishedCallback",
                             target=self.finished_callback).start()
@@ -212,11 +214,26 @@ class MusicFile(Machine):
             return
         with self.music_lock:
             self.abandon_all_effects()
-            self.current_frame = max(
-                    0,
-                    int(delta) * self.current_frame
-                        + int(value * self.audio_segment.frame_rate))
-        # FIXME: si on fait un seek + delta, adapter le "loop"
+            if delta:
+                frame_count = int(self.audio_segment.frame_count())
+                frame_diff = int(value * self.audio_segment.frame_rate)
+                self.current_frame += frame_diff
+                while self.current_frame < 0:
+                    self.current_loop -= 1
+                    self.current_frame += frame_count
+                while self.current_frame > frame_count:
+                    self.current_loop += 1
+                    self.current_frame -= frame_count
+                if self.current_loop < 0:
+                    self.current_loop = 0
+                    self.current_frame = 0
+                if self.current_loop > self.last_loop:
+                    self.current_loop = self.last_loop
+                    self.current_frame = frame_count
+            else:
+                self.current_frame = max(
+                        0,
+                        int(value * self.audio_segment.frame_rate))
 
     def effects_next_gain(self, frame_count):
         db_gain = 0