]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/music_file.py
Use workflow for music_files
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
1 import threading
2 import pydub
3 import pygame
4 import math
5 import time
6 from transitions.extensions import HierarchicalMachine as Machine
7
8 class MusicFile(Machine):
9 def __init__(self, filename, lock, channel_id, name = None, gain = 1):
10 states = [
11 'initial',
12 'loading',
13 'failed',
14 { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused'] }
15 ]
16 transitions = [
17 { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'},
18 { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'},
19 { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'},
20 #{ 'trigger': 'play', 'source': 'loaded_stopped', 'dest': 'loaded_playing'},
21 #{ 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'},
22 #{ 'trigger': 'stop', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopped'}
23 ]
24
25 Machine.__init__(self, states=states, transitions=transitions, initial='initial')
26
27 self.filename = filename
28 self.channel_id = channel_id
29 self.name = name or filename
30 self.raw_data = None
31 self.gain = gain
32
33 self.flag_paused = False
34 threading.Thread(name = "MSMusicLoad", target = self.load, kwargs = {'lock': lock}).start()
35
36 def on_enter_loading(self, lock=None):
37 lock.acquire()
38 try:
39 print("Loading « {} »".format(self.name))
40 volume_factor = 20 * math.log10(self.gain)
41 audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).apply_gain(volume_factor)
42 self.sound_duration = audio_segment.duration_seconds
43 self.raw_data = audio_segment.raw_data
44 except Exception as e:
45 print("failed to load « {} »: {}".format(self.name, e))
46 self.loading_error = e
47 self.fail()
48 else:
49 self.success()
50 print("Loaded « {} »".format(self.name))
51 finally:
52 lock.release()
53
54 def check_is_loaded(self):
55 return self.state.startswith('loaded_')
56
57 def is_playing(self):
58 return self.channel().get_busy()
59
60 def is_paused(self):
61 return self.flag_paused
62
63 @property
64 def sound_position(self):
65 if self.is_playing() and not self.is_paused():
66 return min(time.time() - self.started_at, self.sound_duration)
67 elif self.is_playing() and self.is_paused():
68 return min(self.paused_at - self.started_at, self.sound_duration)
69 else:
70 return 0
71
72 def play(self, fade_in = 0, volume = 100, start_at = 0):
73 self.set_volume(volume)
74
75 if start_at > 0:
76 raw_data_length = len(self.raw_data)
77 start_offset = int((raw_data_length / self.sound_duration) * start_at)
78 start_offset = start_offset - (start_offset % 2)
79 sound = pygame.mixer.Sound(self.raw_data[start_offset:])
80 else:
81 sound = pygame.mixer.Sound(self.raw_data)
82
83 self.started_at = time.time()
84 self.channel().play(sound, fade_ms = int(fade_in * 1000))
85 self.flag_paused = False
86
87 def pause(self):
88 self.paused_at = time.time()
89 self.channel().pause()
90 self.flag_paused = True
91
92 def unpause(self):
93 self.started_at += (time.time() - self.paused_at)
94 self.channel().unpause()
95 self.flag_paused = False
96
97 def stop(self, fade_out = 0):
98 if fade_out > 0:
99 self.channel().fadeout(int(fade_out * 1000))
100 else:
101 self.channel().stop()
102
103 def set_volume(self, value):
104 if value < 0:
105 value = 0
106 if value > 100:
107 value = 100
108 self.channel().set_volume(value / 100)
109
110 def wait_end(self):
111 pass
112
113 def channel(self):
114 return pygame.mixer.Channel(self.channel_id)