]>
Commit | Line | Data |
---|---|---|
be27763f IB |
1 | import threading |
2 | import pydub | |
98ff4305 | 3 | import time |
60979de4 | 4 | from transitions.extensions import HierarchicalMachine as Machine |
be27763f | 5 | |
29597680 IB |
6 | import os.path |
7 | ||
8 | from .lock import Lock | |
75d6cdba | 9 | from . import Config, gain |
1b4b78f5 | 10 | |
29597680 IB |
11 | file_lock = Lock("file") |
12 | ||
60979de4 | 13 | class MusicFile(Machine): |
1b4b78f5 | 14 | def __init__(self, filename, mapping, name = None, gain = 1): |
60979de4 IB |
15 | states = [ |
16 | 'initial', | |
17 | 'loading', | |
18 | 'failed', | |
29597680 | 19 | { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] } |
60979de4 IB |
20 | ] |
21 | transitions = [ | |
22 | { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}, | |
23 | { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}, | |
24 | { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}, | |
29597680 IB |
25 | { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}, |
26 | { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}, | |
27 | { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}, | |
28 | { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}, | |
22514f3a | 29 | { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped', 'after': 'trigger_stopped_events'} |
60979de4 IB |
30 | ] |
31 | ||
32 | Machine.__init__(self, states=states, transitions=transitions, initial='initial') | |
33 | ||
1b4b78f5 IB |
34 | self.volume = 100 |
35 | self.mapping = mapping | |
be27763f | 36 | self.filename = filename |
9de92b6d | 37 | self.name = name or filename |
29597680 | 38 | self.audio_segment = None |
ccc8b4f2 | 39 | self.audio_segment_frame_width = 0 |
1b4b78f5 | 40 | self.volume_factor = gain |
29597680 | 41 | self.music_lock = Lock("music__" + filename) |
0deb82a5 | 42 | self.wait_event = threading.Event() |
be27763f | 43 | |
29597680 IB |
44 | threading.Thread(name = "MSMusicLoad", target = self.load).start() |
45 | ||
46 | def on_enter_loading(self): | |
47 | with file_lock: | |
48 | try: | |
49 | print("Loading « {} »".format(self.name)) | |
1b4b78f5 | 50 | db_gain = gain(self.volume_factor * 100) |
75d6cdba | 51 | 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) |
ccc8b4f2 | 52 | self.audio_segment_frame_width = self.audio_segment.frame_width |
29597680 IB |
53 | self.sound_duration = self.audio_segment.duration_seconds |
54 | except Exception as e: | |
55 | print("failed to load « {} »: {}".format(self.name, e)) | |
56 | self.loading_error = e | |
57 | self.fail() | |
58 | else: | |
59 | self.success() | |
60 | print("Loaded « {} »".format(self.name)) | |
60979de4 IB |
61 | |
62 | def check_is_loaded(self): | |
63 | return self.state.startswith('loaded_') | |
be27763f | 64 | |
29597680 IB |
65 | def is_not_stopped(self): |
66 | return self.check_is_loaded() and not self.is_loaded_stopped() | |
0e5d59f7 | 67 | |
9de92b6d | 68 | def is_paused(self): |
29597680 | 69 | return self.is_loaded_paused() |
9de92b6d | 70 | |
98ff4305 IB |
71 | @property |
72 | def sound_position(self): | |
29597680 IB |
73 | if self.is_not_stopped(): |
74 | return self.current_frame / self.current_audio_segment.frame_rate | |
98ff4305 IB |
75 | else: |
76 | return 0 | |
77 | ||
6f4944c1 | 78 | def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0): |
1b4b78f5 IB |
79 | db_gain = gain(volume) + self.mapping.master_gain |
80 | self.volume = volume | |
6f4944c1 | 81 | self.loop = loop |
1b4b78f5 | 82 | |
29597680 | 83 | ms = int(start_at * 1000) |
ccc8b4f2 | 84 | ms_fi = int(fade_in * 1000) |
29597680 | 85 | with self.music_lock: |
ccc8b4f2 IB |
86 | self.current_audio_segment = (self.audio_segment + db_gain) |
87 | self.current_frame = int(start_at * self.audio_segment.frame_rate) | |
88 | if ms_fi > 0: | |
89 | # FIXME: apply it to repeated when looping? | |
90 | self.a_s_with_effect = self.current_audio_segment[ms:ms+ms_fi].fade_in(ms_fi) | |
91 | self.current_frame_with_effect = 0 | |
92 | else: | |
93 | self.a_s_with_effect = None | |
94 | ||
29597680 IB |
95 | self.start_playing() |
96 | ||
29597680 | 97 | def on_enter_loaded_playing(self): |
22514f3a | 98 | self.mapping.mixer.add_file(self) |
29597680 IB |
99 | |
100 | def finished_callback(self): | |
101 | if self.is_loaded_playing(): | |
102 | self.stop_playing() | |
103 | if self.is_loaded_stopping(): | |
104 | self.stopped() | |
105 | ||
22514f3a IB |
106 | def trigger_stopped_events(self): |
107 | self.mapping.mixer.remove_file(self) | |
0deb82a5 IB |
108 | self.wait_event.set() |
109 | ||
22514f3a IB |
110 | def play_callback(self, out_data_length, frame_count): |
111 | if self.is_loaded_paused(): | |
112 | return b'\0' * out_data_length | |
113 | ||
29597680 | 114 | with self.music_lock: |
ccc8b4f2 IB |
115 | [data, nb_frames] = self.get_next_sample(frame_count) |
116 | if nb_frames < frame_count: | |
6f4944c1 IB |
117 | if self.is_loaded_playing() and self.loop != 0: |
118 | self.loop -= 1 | |
ccc8b4f2 IB |
119 | self.current_frame = 0 |
120 | [new_data, new_nb_frames] = self.get_next_sample(frame_count - nb_frames) | |
121 | data += new_data | |
122 | nb_frames += new_nb_frames | |
123 | elif nb_frames == 0: | |
75d6cdba | 124 | # FIXME: too slow |
22514f3a | 125 | threading.Thread(name = "MSFinishedCallback", target=self.finished_callback).start() |
29597680 | 126 | |
22514f3a | 127 | return data.ljust(out_data_length, b'\0') |
ccc8b4f2 IB |
128 | |
129 | def get_next_sample(self, frame_count): | |
130 | fw = self.audio_segment_frame_width | |
131 | ||
132 | data = b"" | |
133 | nb_frames = 0 | |
134 | if self.a_s_with_effect is not None: | |
135 | segment = self.a_s_with_effect | |
136 | max_val = int(segment.frame_count()) | |
137 | ||
138 | start_i = max(self.current_frame_with_effect, 0) | |
139 | end_i = min(self.current_frame_with_effect + frame_count, max_val) | |
140 | ||
141 | data += segment._data[(start_i * fw):(end_i * fw)] | |
142 | ||
143 | frame_count = max(0, self.current_frame_with_effect + frame_count - max_val) | |
144 | ||
145 | self.current_frame_with_effect += end_i - start_i | |
146 | self.current_frame += end_i - start_i | |
147 | nb_frames += end_i - start_i | |
148 | ||
149 | if frame_count > 0: | |
150 | self.a_s_with_effect = None | |
151 | ||
152 | segment = self.current_audio_segment | |
153 | max_val = int(segment.frame_count()) | |
154 | ||
155 | start_i = max(self.current_frame, 0) | |
156 | end_i = min(self.current_frame + frame_count, max_val) | |
157 | data += segment._data[(start_i * fw):(end_i * fw)] | |
158 | nb_frames += end_i - start_i | |
159 | self.current_frame += end_i - start_i | |
160 | ||
161 | return [data, nb_frames] | |
be27763f | 162 | |
52d58baf IB |
163 | def seek(self, value = 0, delta = False): |
164 | # We don't want to do that while stopping | |
165 | if not (self.is_loaded_playing() or self.is_loaded_paused()): | |
166 | return | |
167 | with self.music_lock: | |
ccc8b4f2 | 168 | self.a_s_with_effect = None |
52d58baf | 169 | self.current_frame = max(0, int(delta) * self.current_frame + int(value * self.audio_segment.frame_rate)) |
ccc8b4f2 | 170 | # FIXME: si on fait un seek + delta, adapter le "loop" |
52d58baf | 171 | |
1b4b78f5 | 172 | def stop(self, fade_out = 0, wait = False): |
29597680 IB |
173 | if self.is_loaded_playing(): |
174 | ms = int(self.sound_position * 1000) | |
175 | ms_fo = max(1, int(fade_out * 1000)) | |
176 | ||
ccc8b4f2 IB |
177 | # FIXME: stop fade_out puis seek -5 -> on abandonne le fade ? (cf |
178 | # commentaire dans fonction seek | |
22514f3a | 179 | new_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo) |
29597680 | 180 | with self.music_lock: |
22514f3a IB |
181 | self.current_audio_segment = new_audio_segment |
182 | self.stop_playing() | |
1b4b78f5 IB |
183 | if wait: |
184 | self.wait_end() | |
0e5d59f7 | 185 | else: |
29597680 IB |
186 | self.stop_playing() |
187 | self.stopped() | |
0e5d59f7 | 188 | |
1b4b78f5 IB |
189 | def set_gain(self, db_gain): |
190 | if not self.is_not_stopped(): | |
29597680 IB |
191 | return |
192 | ||
1b4b78f5 IB |
193 | new_audio_segment = self.current_audio_segment + db_gain |
194 | ||
ccc8b4f2 IB |
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 | ||
29597680 IB |
199 | with self.music_lock: |
200 | self.current_audio_segment = new_audio_segment | |
ccc8b4f2 | 201 | self.a_s_with_effect = new_a_s_with_effect |
29597680 | 202 | |
8e50011c IB |
203 | def set_volume(self, value, delta = False): |
204 | [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume) | |
1b4b78f5 IB |
205 | |
206 | self.set_gain(db_gain) | |
be27763f | 207 | |
b86db9f1 | 208 | def wait_end(self): |
0deb82a5 IB |
209 | self.wait_event.clear() |
210 | self.wait_event.wait() | |
d479af33 | 211 | |
29597680 IB |
212 | # Add some more functions to AudioSegments |
213 | def get_sample_slice_data(self, start_sample=0, end_sample=float('inf')): | |
214 | max_val = int(self.frame_count()) | |
215 | ||
216 | start_i = max(start_sample, 0) * self.frame_width | |
217 | end_i = min(end_sample, max_val) * self.frame_width | |
218 | ||
219 | return self._data[start_i:end_i] | |
220 | ||
221 | pydub.AudioSegment.get_sample_slice_data = get_sample_slice_data |