]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Put music effect in separate file
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 16 Jul 2016 14:12:06 +0000 (16:12 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 16 Jul 2016 14:12:06 +0000 (16:12 +0200)
helpers/music_file.py

index 0bfb1eca8e2d29b1b84c3ef7ae693ff8d05826ef..8b1c4804f56fdefac13698f0502d77cba5b077c0 100644 (file)
@@ -41,6 +41,7 @@ class MusicFile(Machine):
         self.stream = None
         self.name = name or filename
         self.audio_segment = None
+        self.audio_segment_frame_width = 0
         self.volume_factor = gain
         self.music_lock = Lock("music__" + filename)
         self.wait_event = threading.Event()
@@ -53,6 +54,7 @@ class MusicFile(Machine):
                 print("Loading « {} »".format(self.name))
                 db_gain = gain(self.volume_factor * 100)
                 self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).apply_gain(db_gain)
+                self.audio_segment_frame_width = self.audio_segment.frame_width
                 self.sound_duration = self.audio_segment.duration_seconds
             except Exception as e:
                 print("failed to load « {} »: {}".format(self.name, e))
@@ -84,15 +86,21 @@ class MusicFile(Machine):
         self.loop = loop
 
         ms = int(start_at * 1000)
-        ms_fi = max(1, int(fade_in * 1000))
+        ms_fi = int(fade_in * 1000)
         with self.music_lock:
-            self.current_audio_segment = (self.audio_segment + db_gain).fade(from_gain=-120, duration=ms_fi, start=ms)
-        self.initial_frame = int(start_at * self.audio_segment.frame_rate)
+            self.current_audio_segment = (self.audio_segment + db_gain)
+            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
+
         self.before_loaded_playing()
         self.start_playing()
 
     def before_loaded_playing(self):
-        self.current_frame = self.initial_frame
         with self.music_lock:
             segment = self.current_audio_segment
 
@@ -121,32 +129,69 @@ class MusicFile(Machine):
 
     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(
-                                start_sample=self.current_frame,
-                                end_sample=self.current_frame + frame_count
-                                )
-            self.current_frame += frame_count
-            if len(audio_segment) == 0:
+            [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
-                    self.current_frame = self.initial_frame
-                else:
+                    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:
                     raise sd.CallbackStop
 
-            out_data[:] = audio_segment.ljust(len(out_data), b'\0')
+            out_data[:] = data.ljust(len(out_data), b'\0')
+
+    def get_next_sample(self, frame_count):
+        fw = self.audio_segment_frame_width
+
+        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())
+
+        start_i = max(self.current_frame, 0)
+        end_i   = min(self.current_frame + frame_count, max_val)
+        data += segment._data[(start_i * fw):(end_i * fw)]
+        nb_frames += end_i - start_i
+        self.current_frame += end_i - start_i
+
+        return [data, nb_frames]
 
     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.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"
 
     def stop(self, fade_out = 0, wait = False):
         if self.is_loaded_playing():
             ms = int(self.sound_position * 1000)
             ms_fo = max(1, int(fade_out * 1000))
 
+            # FIXME: stop fade_out puis seek -5 -> on abandonne le fade ? (cf
+            # commentaire dans fonction seek
             with self.music_lock:
                 self.current_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo)
                 self.stop_playing()
@@ -162,8 +207,13 @@ class MusicFile(Machine):
 
         new_audio_segment = self.current_audio_segment + db_gain
 
+        new_a_s_with_effect = None
+        if self.a_s_with_effect is not None:
+            new_a_s_with_effect = self.a_s_with_effect + db_gain
+
         with self.music_lock:
             self.current_audio_segment = new_audio_segment
+            self.a_s_with_effect = new_a_s_with_effect
 
     def set_volume(self, value, delta = False):
         [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume)