]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/music_file.py
Make seek work well with loops
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
index 3053008c0a281adedf65761ab205b5de8bb01d84..aef0adce00c4f55f539b67dd07479bdae7327b7e 100644 (file)
@@ -10,6 +10,7 @@ import audioop
 from .lock import Lock
 from . import Config, gain, debug_print, error_print
 from .mixer import Mixer
+from .music_effect import GainEffect
 
 file_lock = Lock("file")
 
@@ -81,7 +82,7 @@ class MusicFile(Machine):
         self.music_lock = Lock("music__" + filename)
         self.wait_event = threading.Event()
         self.db_gain = 0
-        self.volume_factor = 1
+        self.gain_effects = []
 
         threading.Thread(name="MSMusicLoad", target=self.load).start()
 
@@ -124,23 +125,29 @@ 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.loop = loop
+        self.current_loop = 0
+        if loop < 0:
+            self.last_loop = float('inf')
+        else:
+            self.last_loop = loop
 
-        ms = int(start_at * 1000)
-        ms_fi = int(fade_in * 1000)
         with self.music_lock:
             self.current_audio_segment = self.audio_segment
             self.current_frame = int(start_at * self.audio_segment.frame_rate)
-            if ms_fi > 0:
-                # FIXME: apply it to repeated when looping?
-                self.a_s_with_effect = self \
-                        .current_audio_segment[ms : ms+ms_fi] \
-                        .fade_in(ms_fi)
-                self.current_frame_with_effect = 0
-            else:
-                self.a_s_with_effect = None
+            if fade_in > 0:
+                db_gain = gain(self.volume, 0)[0]
+                self.set_gain(-db_gain)
+                self.gain_effects.append(GainEffect(
+                    "fade",
+                    self.current_audio_segment,
+                    self.current_loop,
+                    self.sound_position,
+                    self.sound_position + fade_in,
+                    gain=db_gain))
 
         self.start_playing()
 
@@ -164,15 +171,16 @@ class MusicFile(Machine):
         with self.music_lock:
             [data, nb_frames] = self.get_next_sample(frame_count)
             if nb_frames < frame_count:
-                if self.is_loaded_playing() and self.loop != 0:
-                    self.loop -= 1
+                if self.is_loaded_playing() and\
+                        self.current_loop < self.last_loop:
+                    self.current_loop += 1
                     self.current_frame = 0
                     [new_data, new_nb_frames] = self.get_next_sample(
                             frame_count - nb_frames)
                     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()
@@ -184,25 +192,6 @@ class MusicFile(Machine):
 
         data = b""
         nb_frames = 0
-        if self.a_s_with_effect is not None:
-            segment = self.a_s_with_effect
-            max_val = int(segment.frame_count())
-
-            start_i = max(self.current_frame_with_effect, 0)
-            end_i = min(self.current_frame_with_effect + frame_count, max_val)
-
-            data += segment._data[start_i*fw : end_i*fw]
-
-            frame_count = max(
-                    0,
-                    self.current_frame_with_effect + frame_count - max_val)
-
-            self.current_frame_with_effect += end_i - start_i
-            self.current_frame += end_i - start_i
-            nb_frames += end_i - start_i
-
-            if frame_count > 0:
-                self.a_s_with_effect = None
 
         segment = self.current_audio_segment
         max_val = int(segment.frame_count())
@@ -213,7 +202,9 @@ class MusicFile(Machine):
         nb_frames += end_i - start_i
         self.current_frame += end_i - start_i
 
-        data = audioop.mul(data, Config.sample_width, self.volume_factor)
+        volume_factor = self.volume_factor(self.effects_next_gain(nb_frames))
+
+        data = audioop.mul(data, Config.sample_width, volume_factor)
 
         return [data, nb_frames]
 
@@ -222,12 +213,50 @@ class MusicFile(Machine):
         if not (self.is_loaded_playing() or self.is_loaded_paused()):
             return
         with self.music_lock:
-            self.a_s_with_effect = None
-            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"
+            self.abandon_all_effects()
+            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
+        for gain_effect in self.gain_effects:
+            [new_gain, last_gain] = gain_effect.get_next_gain(
+                    self.current_frame,
+                    self.current_loop,
+                    frame_count)
+            if last_gain:
+                self.set_gain(new_gain)
+                self.gain_effects.remove(gain_effect)
+            else:
+                db_gain += new_gain
+        return db_gain
+
+
+    def abandon_all_effects(self):
+        db_gain = 0
+        for gain_effect in self.gain_effects:
+            db_gain += gain_effect.get_last_gain()
+
+        self.gain_effects = []
+        self.set_gain(db_gain)
 
     def stop(self, fade_out=0, wait=False):
         if self.is_loaded_playing():
@@ -245,19 +274,30 @@ class MusicFile(Machine):
             self.stop_playing()
             self.stopped()
 
+    def volume_factor(self, additional_gain):
+        return 10 ** ( (self.db_gain + additional_gain) / 20)
+
     def set_gain(self, db_gain, absolute=False):
         if absolute:
             self.db_gain = db_gain
         else:
             self.db_gain += db_gain
-        self.volume_factor = 10 ** (self.db_gain / 20)
 
-    def set_volume(self, value, delta=False):
+    def set_volume(self, value, delta=False, fade=0):
         [db_gain, self.volume] = gain(
                 value + int(delta) * self.volume,
                 self.volume)
 
-        self.set_gain(db_gain)
+        if fade > 0:
+            self.gain_effects.append(GainEffect(
+                "fade",
+                self.current_audio_segment,
+                self.current_loop,
+                self.sound_position,
+                self.sound_position + fade,
+                gain=db_gain))
+        else:
+            self.set_gain(db_gain)
 
     def wait_end(self):
         self.wait_event.clear()