aboutsummaryrefslogtreecommitdiff
path: root/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'helpers')
-rw-r--r--helpers/music_file.py29
1 files changed, 12 insertions, 17 deletions
diff --git a/helpers/music_file.py b/helpers/music_file.py
index 5b0d0df..56060bd 100644
--- a/helpers/music_file.py
+++ b/helpers/music_file.py
@@ -5,6 +5,8 @@ from transitions.extensions import HierarchicalMachine as Machine
5 5
6import os.path 6import os.path
7 7
8import audioop
9
8from .lock import Lock 10from .lock import Lock
9from . import Config, gain, debug_print, error_print 11from . import Config, gain, debug_print, error_print
10from .mixer import Mixer 12from .mixer import Mixer
@@ -38,9 +40,10 @@ class MusicFile(Machine):
38 self.name = name or filename 40 self.name = name or filename
39 self.audio_segment = None 41 self.audio_segment = None
40 self.audio_segment_frame_width = 0 42 self.audio_segment_frame_width = 0
41 self.volume_factor = gain 43 self.initial_volume_factor = gain
42 self.music_lock = Lock("music__" + filename) 44 self.music_lock = Lock("music__" + filename)
43 self.wait_event = threading.Event() 45 self.wait_event = threading.Event()
46 self.db_gain = 0
44 47
45 threading.Thread(name = "MSMusicLoad", target = self.load).start() 48 threading.Thread(name = "MSMusicLoad", target = self.load).start()
46 49
@@ -49,8 +52,8 @@ class MusicFile(Machine):
49 try: 52 try:
50 debug_print("Loading « {} »".format(self.name)) 53 debug_print("Loading « {} »".format(self.name))
51 self.mixer = self.mapping.mixer or Mixer() 54 self.mixer = self.mapping.mixer or Mixer()
52 db_gain = gain(self.volume_factor * 100) 55 initial_db_gain = gain(self.initial_volume_factor * 100)
53 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) 56 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)
54 self.audio_segment_frame_width = self.audio_segment.frame_width 57 self.audio_segment_frame_width = self.audio_segment.frame_width
55 self.sound_duration = self.audio_segment.duration_seconds 58 self.sound_duration = self.audio_segment.duration_seconds
56 except Exception as e: 59 except Exception as e:
@@ -78,14 +81,14 @@ class MusicFile(Machine):
78 return 0 81 return 0
79 82
80 def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0): 83 def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0):
81 db_gain = gain(volume) + self.mapping.master_gain 84 self.db_gain = gain(volume) + self.mapping.master_gain
82 self.volume = volume 85 self.volume = volume
83 self.loop = loop 86 self.loop = loop
84 87
85 ms = int(start_at * 1000) 88 ms = int(start_at * 1000)
86 ms_fi = int(fade_in * 1000) 89 ms_fi = int(fade_in * 1000)
87 with self.music_lock: 90 with self.music_lock:
88 self.current_audio_segment = (self.audio_segment + db_gain) 91 self.current_audio_segment = self.audio_segment
89 self.current_frame = int(start_at * self.audio_segment.frame_rate) 92 self.current_frame = int(start_at * self.audio_segment.frame_rate)
90 if ms_fi > 0: 93 if ms_fi > 0:
91 # FIXME: apply it to repeated when looping? 94 # FIXME: apply it to repeated when looping?
@@ -160,6 +163,8 @@ class MusicFile(Machine):
160 nb_frames += end_i - start_i 163 nb_frames += end_i - start_i
161 self.current_frame += end_i - start_i 164 self.current_frame += end_i - start_i
162 165
166 data = audioop.mul(data, Config.sample_width, self.volume_factor)
167
163 return [data, nb_frames] 168 return [data, nb_frames]
164 169
165 def seek(self, value = 0, delta = False): 170 def seek(self, value = 0, delta = False):
@@ -187,18 +192,8 @@ class MusicFile(Machine):
187 self.stopped() 192 self.stopped()
188 193
189 def set_gain(self, db_gain): 194 def set_gain(self, db_gain):
190 if not self.is_not_stopped(): 195 self.db_gain += db_gain
191 return 196 self.volume_factor = 10 ** (self.db_gain / 20)
192
193 new_audio_segment = self.current_audio_segment + db_gain
194
195 new_a_s_with_effect = None
196 if self.a_s_with_effect is not None:
197 new_a_s_with_effect = self.a_s_with_effect + db_gain
198
199 with self.music_lock:
200 self.current_audio_segment = new_audio_segment
201 self.a_s_with_effect = new_a_s_with_effect
202 197
203 def set_volume(self, value, delta = False): 198 def set_volume(self, value, delta = False):
204 [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume) 199 [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume)