]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Do gain at the last moment
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Jul 2016 22:00:31 +0000 (00:00 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 17 Jul 2016 22:00:31 +0000 (00:00 +0200)
helpers/music_file.py

index 5b0d0dffdf2205d90e5c7fddbb3a2723860db659..56060bd8d9368ab8e9e08d7f2cd9a4f7b37f0c1d 100644 (file)
@@ -5,6 +5,8 @@ from transitions.extensions import HierarchicalMachine as Machine
 
 import os.path
 
+import audioop
+
 from .lock import Lock
 from . import Config, gain, debug_print, error_print
 from .mixer import Mixer
@@ -38,9 +40,10 @@ class MusicFile(Machine):
         self.name = name or filename
         self.audio_segment = None
         self.audio_segment_frame_width = 0
-        self.volume_factor = gain
+        self.initial_volume_factor = gain
         self.music_lock = Lock("music__" + filename)
         self.wait_event = threading.Event()
+        self.db_gain = 0
 
         threading.Thread(name = "MSMusicLoad", target = self.load).start()
 
@@ -49,8 +52,8 @@ class MusicFile(Machine):
             try:
                 debug_print("Loading « {} »".format(self.name))
                 self.mixer = self.mapping.mixer or Mixer()
-                db_gain = gain(self.volume_factor * 100)
-                self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(Config.frame_rate).set_channels(Config.channels).set_sample_width(Config.sample_width).apply_gain(db_gain)
+                initial_db_gain = gain(self.initial_volume_factor * 100)
+                self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(Config.frame_rate).set_channels(Config.channels).set_sample_width(Config.sample_width).apply_gain(initial_db_gain)
                 self.audio_segment_frame_width = self.audio_segment.frame_width
                 self.sound_duration = self.audio_segment.duration_seconds
             except Exception as e:
@@ -78,14 +81,14 @@ class MusicFile(Machine):
             return 0
 
     def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0):
-        db_gain = gain(volume) + self.mapping.master_gain
+        self.db_gain = gain(volume) + self.mapping.master_gain
         self.volume = volume
         self.loop = loop
 
         ms = int(start_at * 1000)
         ms_fi = int(fade_in * 1000)
         with self.music_lock:
-            self.current_audio_segment = (self.audio_segment + db_gain)
+            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?
@@ -160,6 +163,8 @@ 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)
+
         return [data, nb_frames]
 
     def seek(self, value = 0, delta = False):
@@ -187,18 +192,8 @@ class MusicFile(Machine):
             self.stopped()
 
     def set_gain(self, db_gain):
-        if not self.is_not_stopped():
-            return
-
-        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
+        self.db_gain += db_gain
+        self.volume_factor = 10 ** (self.db_gain / 20)
 
     def set_volume(self, value, delta = False):
         [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume)