]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - music_sampler/music_file.py
Cleanup key and action workflows
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / music_file.py
CommitLineData
be27763f
IB
1import threading
2import pydub
98ff4305 3import time
60979de4 4from transitions.extensions import HierarchicalMachine as Machine
be27763f 5
29597680
IB
6import os.path
7
ccda4cb9
IB
8import audioop
9
29597680 10from .lock import Lock
6ebe6247 11from .helpers import Config, gain, debug_print, error_print
af27d782 12from .mixer import Mixer
aee1334c 13from .music_effect import GainEffect
1b4b78f5 14
29597680
IB
15file_lock = Lock("file")
16
108f858f 17class MusicFile:
20586193
IB
18 STATES = [
19 'initial',
20 'loading',
21 'failed',
22 {
23 'name': 'loaded',
24 'children': [
93a3e51e 25 'stopped',
20586193
IB
26 'playing',
27 'paused',
28 'stopping'
29 ]
30 }
31 ]
32 TRANSITIONS = [
33 {
34 'trigger': 'load',
93a3e51e
IB
35 'source': ['initial', 'failed'],
36 'dest': 'loading'
20586193
IB
37 },
38 {
39 'trigger': 'fail',
40 'source': 'loading',
41 'dest': 'failed'
42 },
93a3e51e
IB
43 {
44 'trigger': 'unload',
45 'source': ['failed', 'loaded_stopped'],
46 'dest': 'initial',
47 },
20586193
IB
48 {
49 'trigger': 'success',
50 'source': 'loading',
93a3e51e 51 'dest': 'loaded_stopped'
20586193
IB
52 },
53 {
54 'trigger': 'start_playing',
93a3e51e
IB
55 'source': 'loaded_stopped',
56 'dest': 'loaded_playing'
20586193
IB
57 },
58 {
59 'trigger': 'pause',
60 'source': 'loaded_playing',
61 'dest': 'loaded_paused'
62 },
63 {
64 'trigger': 'unpause',
65 'source': 'loaded_paused',
66 'dest': 'loaded_playing'
67 },
68 {
69 'trigger': 'stop_playing',
70 'source': ['loaded_playing','loaded_paused'],
71 'dest': 'loaded_stopping'
72 },
73 {
74 'trigger': 'stopped',
93a3e51e
IB
75 'source': 'loaded',
76 'dest': 'loaded_stopped',
b7ca3fc2 77 'before': 'trigger_stopped_events',
93a3e51e 78 'unless': 'is_loaded_stopped',
20586193
IB
79 }
80 ]
81
2e404903 82 def __init__(self, filename, mapping, name=None, gain=1):
93a3e51e 83 machine = Machine(model=self, states=self.STATES,
20586193 84 transitions=self.TRANSITIONS, initial='initial',
93a3e51e
IB
85 auto_transitions=False,
86 after_state_change=self.notify_state_change)
60979de4 87
93a3e51e 88 self.state_change_callbacks = []
1b4b78f5 89 self.mapping = mapping
be27763f 90 self.filename = filename
9de92b6d 91 self.name = name or filename
29597680 92 self.audio_segment = None
ccda4cb9 93 self.initial_volume_factor = gain
29597680 94 self.music_lock = Lock("music__" + filename)
be27763f 95
93a3e51e
IB
96 if Config.load_all_musics:
97 threading.Thread(name="MSMusicLoad", target=self.load).start()
29597680 98
ab47d2a1
IB
99 def reload_properties(self, name=None, gain=1):
100 self.name = name or self.filename
101 if gain != self.initial_volume_factor:
102 self.initial_volume_factor = gain
93a3e51e
IB
103 self.stopped()
104 self.unload()
105 self.load(reloading=True)
ab47d2a1 106
93a3e51e
IB
107 # Machine related events
108 def on_enter_initial(self):
109 self.audio_segment = None
dd2887c2 110
93a3e51e
IB
111 def on_enter_loading(self, reloading=False):
112 if reloading:
113 prefix = 'Rel'
114 prefix_s = 'rel'
115 else:
116 prefix = 'L'
117 prefix_s = 'l'
ab47d2a1 118
29597680 119 with file_lock:
93a3e51e
IB
120 if self.mapping.is_leaving_application:
121 self.fail()
122 return
123
29597680 124 try:
dd2887c2
IB
125 if self.filename.startswith("/"):
126 filename = self.filename
127 else:
128 filename = Config.music_path + self.filename
129
93a3e51e 130 debug_print("{}oading « {} »".format(prefix, self.name))
a24c34bc 131 self.mixer = self.mapping.mixer or Mixer()
ccda4cb9 132 initial_db_gain = gain(self.initial_volume_factor * 100)
2e404903 133 self.audio_segment = pydub.AudioSegment \
dd2887c2 134 .from_file(filename) \
2e404903
IB
135 .set_frame_rate(Config.frame_rate) \
136 .set_channels(Config.channels) \
137 .set_sample_width(Config.sample_width) \
138 .apply_gain(initial_db_gain)
29597680
IB
139 self.sound_duration = self.audio_segment.duration_seconds
140 except Exception as e:
93a3e51e
IB
141 error_print("failed to {}oad « {} »: {}".format(
142 prefix_s, self.name, e))
29597680
IB
143 self.loading_error = e
144 self.fail()
145 else:
146 self.success()
93a3e51e 147 debug_print("{}oaded « {} »".format(prefix, self.name))
60979de4 148
20586193 149 def on_enter_loaded(self):
62bce32f
IB
150 self.cleanup()
151
152 def cleanup(self):
20586193
IB
153 self.gain_effects = []
154 self.set_gain(0, absolute=True)
155 self.current_audio_segment = None
156 self.volume = 100
157 self.wait_event = threading.Event()
158 self.current_loop = 0
159
160 def on_enter_loaded_playing(self):
161 self.mixer.add_file(self)
be27763f 162
20586193
IB
163 # Machine related states
164 def is_in_use(self):
93a3e51e
IB
165 return self.is_loaded(allow_substates=True) and\
166 not self.is_loaded_stopped()
0e5d59f7 167
20586193
IB
168 def is_in_use_not_stopping(self):
169 return self.is_loaded_playing() or self.is_loaded_paused()
9de92b6d 170
93a3e51e
IB
171 def is_unloadable(self):
172 return self.is_loaded_stopped() or self.is_failed()
173
20586193
IB
174 # Machine related triggers
175 def trigger_stopped_events(self):
176 self.mixer.remove_file(self)
177 self.wait_event.set()
62bce32f 178 self.cleanup()
20586193
IB
179
180 # Actions and properties called externally
98ff4305
IB
181 @property
182 def sound_position(self):
20586193 183 if self.is_in_use():
29597680 184 return self.current_frame / self.current_audio_segment.frame_rate
98ff4305
IB
185 else:
186 return 0
187
2e404903 188 def play(self, fade_in=0, volume=100, loop=0, start_at=0):
b37c72a2 189 self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True)
1b4b78f5 190 self.volume = volume
0cb786e3
IB
191 if loop < 0:
192 self.last_loop = float('inf')
193 else:
194 self.last_loop = loop
1b4b78f5 195
29597680 196 with self.music_lock:
ccda4cb9 197 self.current_audio_segment = self.audio_segment
ccc8b4f2 198 self.current_frame = int(start_at * self.audio_segment.frame_rate)
ccc8b4f2 199
29597680
IB
200 self.start_playing()
201
20586193
IB
202 if fade_in > 0:
203 db_gain = gain(self.volume, 0)[0]
204 self.set_gain(-db_gain)
205 self.add_fade_effect(db_gain, fade_in)
29597680 206
20586193
IB
207 def seek(self, value=0, delta=False):
208 if not self.is_in_use_not_stopping():
209 return
210
211 with self.music_lock:
212 self.abandon_all_effects()
213 if delta:
214 frame_count = int(self.audio_segment.frame_count())
215 frame_diff = int(value * self.audio_segment.frame_rate)
216 self.current_frame += frame_diff
217 while self.current_frame < 0:
218 self.current_loop -= 1
219 self.current_frame += frame_count
220 while self.current_frame > frame_count:
221 self.current_loop += 1
222 self.current_frame -= frame_count
223 if self.current_loop < 0:
224 self.current_loop = 0
225 self.current_frame = 0
226 if self.current_loop > self.last_loop:
227 self.current_loop = self.last_loop
228 self.current_frame = frame_count
229 else:
230 self.current_frame = max(
231 0,
232 int(value * self.audio_segment.frame_rate))
233
234 def stop(self, fade_out=0, wait=False, set_wait_id=None):
29597680 235 if self.is_loaded_playing():
20586193
IB
236 ms = int(self.sound_position * 1000)
237 ms_fo = max(1, int(fade_out * 1000))
238
239 new_audio_segment = self.current_audio_segment[: ms+ms_fo] \
240 .fade_out(ms_fo)
241 with self.music_lock:
242 self.current_audio_segment = new_audio_segment
29597680 243 self.stop_playing()
20586193 244 if wait:
21ffec31 245 self.mapping.add_wait(self.wait_event, wait_id=set_wait_id)
20586193 246 self.wait_end()
93a3e51e 247 elif self.is_loaded(allow_substates=True):
29597680
IB
248 self.stopped()
249
20586193
IB
250 def abandon_all_effects(self):
251 db_gain = 0
252 for gain_effect in self.gain_effects:
253 db_gain += gain_effect.get_last_gain()
254
255 self.gain_effects = []
256 self.set_gain(db_gain)
257
258 def set_volume(self, value, delta=False, fade=0):
259 [db_gain, self.volume] = gain(
260 value + int(delta) * self.volume,
261 self.volume)
262
263 self.set_gain_with_effect(db_gain, fade=fade)
264
265 def set_gain_with_effect(self, db_gain, fade=0):
266 if not self.is_in_use():
267 return
268
269 if fade > 0:
270 self.add_fade_effect(db_gain, fade)
271 else:
272 self.set_gain(db_gain)
273
274 def wait_end(self):
275 self.wait_event.clear()
276 self.wait_event.wait()
277
93a3e51e
IB
278 # Let other subscribe for state change
279 def notify_state_change(self, **kwargs):
280 for callback in self.state_change_callbacks:
281 callback(self.state)
282
283 def subscribe_state_change(self, callback):
284 if callback not in self.state_change_callbacks:
285 self.state_change_callbacks.append(callback)
286 callback(self.state)
b7ca3fc2 287
93a3e51e
IB
288 def unsubscribe_state_change(self, callback):
289 if callback in self.state_change_callbacks:
290 self.state_change_callbacks.remove(callback)
b7ca3fc2 291
20586193
IB
292 # Callbacks
293 def finished_callback(self):
294 self.stopped()
0deb82a5 295
22514f3a
IB
296 def play_callback(self, out_data_length, frame_count):
297 if self.is_loaded_paused():
298 return b'\0' * out_data_length
299
29597680 300 with self.music_lock:
ccc8b4f2
IB
301 [data, nb_frames] = self.get_next_sample(frame_count)
302 if nb_frames < frame_count:
0cb786e3
IB
303 if self.is_loaded_playing() and\
304 self.current_loop < self.last_loop:
1e44fe7e 305 self.current_loop += 1
ccc8b4f2 306 self.current_frame = 0
2e404903
IB
307 [new_data, new_nb_frames] = self.get_next_sample(
308 frame_count - nb_frames)
ccc8b4f2
IB
309 data += new_data
310 nb_frames += new_nb_frames
311 elif nb_frames == 0:
9925ce3b 312 # FIXME: too slow when mixing multiple streams
2e404903
IB
313 threading.Thread(
314 name="MSFinishedCallback",
315 target=self.finished_callback).start()
29597680 316
22514f3a 317 return data.ljust(out_data_length, b'\0')
ccc8b4f2 318
20586193
IB
319 # Helpers
320 def set_gain(self, db_gain, absolute=False):
321 if absolute:
322 self.db_gain = db_gain
323 else:
324 self.db_gain += db_gain
325
ccc8b4f2 326 def get_next_sample(self, frame_count):
20586193 327 fw = self.audio_segment.frame_width
ccc8b4f2
IB
328
329 data = b""
330 nb_frames = 0
ccc8b4f2
IB
331
332 segment = self.current_audio_segment
333 max_val = int(segment.frame_count())
334
335 start_i = max(self.current_frame, 0)
336 end_i = min(self.current_frame + frame_count, max_val)
2e404903 337 data += segment._data[start_i*fw : end_i*fw]
ccc8b4f2
IB
338 nb_frames += end_i - start_i
339 self.current_frame += end_i - start_i
340
aee1334c
IB
341 volume_factor = self.volume_factor(self.effects_next_gain(nb_frames))
342
343 data = audioop.mul(data, Config.sample_width, volume_factor)
ccda4cb9 344
ccc8b4f2 345 return [data, nb_frames]
be27763f 346
20586193
IB
347 def add_fade_effect(self, db_gain, fade_duration):
348 if not self.is_in_use():
52d58baf 349 return
20586193
IB
350
351 self.gain_effects.append(GainEffect(
352 "fade",
353 self.current_audio_segment,
354 self.current_loop,
355 self.sound_position,
356 self.sound_position + fade_duration,
357 gain=db_gain))
52d58baf 358
aee1334c
IB
359 def effects_next_gain(self, frame_count):
360 db_gain = 0
361 for gain_effect in self.gain_effects:
362 [new_gain, last_gain] = gain_effect.get_next_gain(
363 self.current_frame,
1e44fe7e 364 self.current_loop,
aee1334c
IB
365 frame_count)
366 if last_gain:
367 self.set_gain(new_gain)
368 self.gain_effects.remove(gain_effect)
369 else:
370 db_gain += new_gain
371 return db_gain
372
373
20586193 374 def volume_factor(self, additional_gain=0):
aee1334c
IB
375 return 10 ** ( (self.db_gain + additional_gain) / 20)
376