]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Add fading
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 18 Jul 2016 19:17:12 +0000 (21:17 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 20 Jul 2016 19:48:54 +0000 (21:48 +0200)
helpers/__init__.py
helpers/action.py
helpers/mapping.py
helpers/music_effect.py [new file with mode: 0644]
helpers/music_file.py

index 9d6663893bc32634b1163f7a525b8b04a7bdf9a5..f5ad848b855324c7a9133a29533d4535bb9f210d 100644 (file)
@@ -131,7 +131,7 @@ def duration_to_min_sec(duration):
 
 def gain(volume, old_volume=None):
     if old_volume is None:
-        return 20 * math.log10(volume / 100)
+        return 20 * math.log10(max(volume, 0.1) / 100)
     else:
         return [
                 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)),
index 218c3167c934ada44a8213d22a5400d80215fe19..b8e44e6783267fcfd9781f3817cf1fbd58f550ee 100644 (file)
@@ -102,9 +102,9 @@ class Action:
     def stop_all_actions(self, **kwargs):
         self.mapping.stop_all_running()
 
-    def volume(self, music=None, value=100, delta=False, **kwargs):
+    def volume(self, music=None, value=100, fade=0, delta=False, **kwargs):
         if music is not None:
-            music.set_volume(value, delta=delta)
+            music.set_volume(value, delta=delta, fade=fade)
         else:
             self.mapping.set_master_volume(value, delta=delta)
 
@@ -192,22 +192,29 @@ class Action:
                 return "moving all musics to position {}s" \
                         .format(value)
 
-    def volume_print(self, music=None, value=100, delta=False, **kwargs):
+    def volume_print(self, music=None,
+            value=100, delta=False, fade=0, **kwargs):
+        message = ""
         if delta:
             if music is not None:
-                return "{:+d}% to volume of « {} »" \
+                message += "{:+d}% to volume of « {} »" \
                         .format(value, music.name)
             else:
-                return "{:+d}% to volume" \
+                message += "{:+d}% to volume" \
                         .format(value)
         else:
             if music is not None:
-                return "setting volume of « {} » to {}%" \
+                message += "setting volume of « {} » to {}%" \
                         .format(music.name, value)
             else:
-                return "setting volume to {}%" \
+                message += "setting volume to {}%" \
                         .format(value)
 
+        if music is not None and fade > 0:
+            message += " with {}s fade".format(fade)
+
+        return message
+
     def wait_print(self, duration=0, music=None, **kwargs):
         if music is None:
             return "waiting {}s" \
index 19fc3c5f2140ec9ba64feac2984b97d720e9f05b..9420acff8b4f07ab69023cab62af0965eb2f30d8 100644 (file)
@@ -91,7 +91,7 @@ class Mapping(RelativeLayout):
         stream = open(Config.yml_file, "r")
         try:
             config = yaml.load(stream)
-        except yaml.scanner.ScannerError as e:
+        except Exception as e:
             error_print("Error while loading config file: {}".format(e))
             sys.exit()
         stream.close()
diff --git a/helpers/music_effect.py b/helpers/music_effect.py
new file mode 100644 (file)
index 0000000..ef8dc90
--- /dev/null
@@ -0,0 +1,50 @@
+class GainEffect:
+    effect_types = [
+        'fade'
+    ]
+
+    def __init__(self, effect, audio_segment, start, end, **kwargs):
+        if effect in self.effect_types:
+            self.effect = effect
+        else:
+            raise Exception("Unknown effect {}".format(effect))
+
+        self.start = start
+        self.end = end
+        self.audio_segment = audio_segment
+        getattr(self, self.effect + "_init")(**kwargs)
+
+    def get_last_gain(self):
+        return getattr(self, self.effect + "_get_last_gain")()
+
+    def get_next_gain(self, current_frame, frame_count):
+        # This returns two values:
+        # - The first one is the gain to apply on that frame
+        # - The last one is True or False depending on whether it is the last
+        #   call to the function and the last gain should be saved permanently
+        return getattr(self, self.effect + "_get_next_gain")(
+                current_frame,
+                frame_count)
+
+    # Fading
+    def fade_init(self, gain=0, **kwargs):
+        self.first_frame = int(self.audio_segment.frame_rate * self.start)
+        self.last_frame = int(self.audio_segment.frame_rate * self.end)
+        self.gain= gain
+
+    def fade_get_last_gain(self):
+        return self.gain
+
+    def fade_get_next_gain(self, current_frame, frame_count):
+        if current_frame >= self.last_frame:
+            return [self.gain, True]
+        elif current_frame < self.first_frame:
+            return [0, False]
+        else:
+            return [
+                    (current_frame - self.first_frame) / \
+                            (self.last_frame - self.first_frame) * self.gain,
+                    False
+                    ]
+
+
index 3053008c0a281adedf65761ab205b5de8bb01d84..810bc2266dbe807d26d00727931cd4daddb12619 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()
 
@@ -134,7 +135,6 @@ class MusicFile(Machine):
             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)
@@ -213,7 +213,10 @@ 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)
+        # FIXME: self.effects_next_gain should take into account the loop number
+        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,6 +225,7 @@ class MusicFile(Machine):
         if not (self.is_loaded_playing() or self.is_loaded_paused()):
             return
         with self.music_lock:
+            self.abandon_all_effects()
             self.a_s_with_effect = None
             self.current_frame = max(
                     0,
@@ -229,6 +233,28 @@ class MusicFile(Machine):
                         + int(value * self.audio_segment.frame_rate))
         # FIXME: si on fait un seek + delta, adapter le "loop"
 
+    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,
+                    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():
             ms = int(self.sound_position * 1000)
@@ -245,19 +271,29 @@ 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.sound_position,
+                self.sound_position + fade,
+                gain=db_gain))
+        else:
+            self.set_gain(db_gain)
 
     def wait_end(self):
         self.wait_event.clear()