]>
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 | ||
ccda4cb9 IB |
8 | import audioop |
9 | ||
29597680 | 10 | from .lock import Lock |
a24c34bc | 11 | from . import Config, gain, debug_print, error_print |
af27d782 | 12 | from .mixer import Mixer |
aee1334c | 13 | from .music_effect import GainEffect |
1b4b78f5 | 14 | |
29597680 IB |
15 | file_lock = Lock("file") |
16 | ||
108f858f | 17 | class MusicFile: |
20586193 IB |
18 | STATES = [ |
19 | 'initial', | |
20 | 'loading', | |
21 | 'failed', | |
22 | { | |
23 | 'name': 'loaded', | |
24 | 'children': [ | |
25 | 'playing', | |
26 | 'paused', | |
27 | 'stopping' | |
28 | ] | |
29 | } | |
30 | ] | |
31 | TRANSITIONS = [ | |
32 | { | |
33 | 'trigger': 'load', | |
34 | 'source': 'initial', | |
35 | 'dest': 'loading' | |
36 | }, | |
37 | { | |
38 | 'trigger': 'fail', | |
39 | 'source': 'loading', | |
40 | 'dest': 'failed' | |
41 | }, | |
42 | { | |
43 | 'trigger': 'success', | |
44 | 'source': 'loading', | |
45 | 'dest': 'loaded' | |
46 | }, | |
47 | { | |
48 | 'trigger': 'start_playing', | |
49 | 'source': 'loaded', | |
50 | 'dest': 'loaded_playing' | |
51 | }, | |
52 | { | |
53 | 'trigger': 'pause', | |
54 | 'source': 'loaded_playing', | |
55 | 'dest': 'loaded_paused' | |
56 | }, | |
57 | { | |
58 | 'trigger': 'unpause', | |
59 | 'source': 'loaded_paused', | |
60 | 'dest': 'loaded_playing' | |
61 | }, | |
62 | { | |
63 | 'trigger': 'stop_playing', | |
64 | 'source': ['loaded_playing','loaded_paused'], | |
65 | 'dest': 'loaded_stopping' | |
66 | }, | |
67 | { | |
68 | 'trigger': 'stopped', | |
69 | 'source': '*', | |
70 | 'dest': 'loaded', | |
71 | 'before': 'trigger_stopped_events' | |
72 | } | |
73 | ] | |
74 | ||
2e404903 | 75 | def __init__(self, filename, mapping, name=None, gain=1): |
20586193 IB |
76 | Machine(model=self, states=self.STATES, |
77 | transitions=self.TRANSITIONS, initial='initial', | |
51322669 | 78 | ignore_invalid_triggers=True) |
60979de4 | 79 | |
1b4b78f5 | 80 | self.mapping = mapping |
be27763f | 81 | self.filename = filename |
9de92b6d | 82 | self.name = name or filename |
29597680 | 83 | self.audio_segment = None |
ccda4cb9 | 84 | self.initial_volume_factor = gain |
29597680 | 85 | self.music_lock = Lock("music__" + filename) |
be27763f | 86 | |
2e404903 | 87 | threading.Thread(name="MSMusicLoad", target=self.load).start() |
29597680 | 88 | |
20586193 | 89 | # Machine related events |
29597680 IB |
90 | def on_enter_loading(self): |
91 | with file_lock: | |
92 | try: | |
a24c34bc IB |
93 | debug_print("Loading « {} »".format(self.name)) |
94 | self.mixer = self.mapping.mixer or Mixer() | |
ccda4cb9 | 95 | initial_db_gain = gain(self.initial_volume_factor * 100) |
2e404903 IB |
96 | self.audio_segment = pydub.AudioSegment \ |
97 | .from_file(self.filename) \ | |
98 | .set_frame_rate(Config.frame_rate) \ | |
99 | .set_channels(Config.channels) \ | |
100 | .set_sample_width(Config.sample_width) \ | |
101 | .apply_gain(initial_db_gain) | |
29597680 IB |
102 | self.sound_duration = self.audio_segment.duration_seconds |
103 | except Exception as e: | |
a24c34bc | 104 | error_print("failed to load « {} »: {}".format(self.name, e)) |
29597680 IB |
105 | self.loading_error = e |
106 | self.fail() | |
107 | else: | |
108 | self.success() | |
a24c34bc | 109 | debug_print("Loaded « {} »".format(self.name)) |
60979de4 | 110 | |
20586193 IB |
111 | def on_enter_loaded(self): |
112 | self.gain_effects = [] | |
113 | self.set_gain(0, absolute=True) | |
114 | self.current_audio_segment = None | |
115 | self.volume = 100 | |
116 | self.wait_event = threading.Event() | |
117 | self.current_loop = 0 | |
118 | ||
119 | def on_enter_loaded_playing(self): | |
120 | self.mixer.add_file(self) | |
be27763f | 121 | |
20586193 IB |
122 | # Machine related states |
123 | def is_in_use(self): | |
124 | return self.is_loaded(allow_substates=True) and not self.is_loaded() | |
0e5d59f7 | 125 | |
20586193 IB |
126 | def is_in_use_not_stopping(self): |
127 | return self.is_loaded_playing() or self.is_loaded_paused() | |
9de92b6d | 128 | |
20586193 IB |
129 | # Machine related triggers |
130 | def trigger_stopped_events(self): | |
131 | self.mixer.remove_file(self) | |
132 | self.wait_event.set() | |
133 | ||
134 | # Actions and properties called externally | |
98ff4305 IB |
135 | @property |
136 | def sound_position(self): | |
20586193 | 137 | if self.is_in_use(): |
29597680 | 138 | return self.current_frame / self.current_audio_segment.frame_rate |
98ff4305 IB |
139 | else: |
140 | return 0 | |
141 | ||
2e404903 | 142 | def play(self, fade_in=0, volume=100, loop=0, start_at=0): |
b37c72a2 | 143 | self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) |
1b4b78f5 | 144 | self.volume = volume |
0cb786e3 IB |
145 | if loop < 0: |
146 | self.last_loop = float('inf') | |
147 | else: | |
148 | self.last_loop = loop | |
1b4b78f5 | 149 | |
29597680 | 150 | with self.music_lock: |
ccda4cb9 | 151 | self.current_audio_segment = self.audio_segment |
ccc8b4f2 | 152 | self.current_frame = int(start_at * self.audio_segment.frame_rate) |
ccc8b4f2 | 153 | |
29597680 IB |
154 | self.start_playing() |
155 | ||
20586193 IB |
156 | if fade_in > 0: |
157 | db_gain = gain(self.volume, 0)[0] | |
158 | self.set_gain(-db_gain) | |
159 | self.add_fade_effect(db_gain, fade_in) | |
29597680 | 160 | |
20586193 IB |
161 | def seek(self, value=0, delta=False): |
162 | if not self.is_in_use_not_stopping(): | |
163 | return | |
164 | ||
165 | with self.music_lock: | |
166 | self.abandon_all_effects() | |
167 | if delta: | |
168 | frame_count = int(self.audio_segment.frame_count()) | |
169 | frame_diff = int(value * self.audio_segment.frame_rate) | |
170 | self.current_frame += frame_diff | |
171 | while self.current_frame < 0: | |
172 | self.current_loop -= 1 | |
173 | self.current_frame += frame_count | |
174 | while self.current_frame > frame_count: | |
175 | self.current_loop += 1 | |
176 | self.current_frame -= frame_count | |
177 | if self.current_loop < 0: | |
178 | self.current_loop = 0 | |
179 | self.current_frame = 0 | |
180 | if self.current_loop > self.last_loop: | |
181 | self.current_loop = self.last_loop | |
182 | self.current_frame = frame_count | |
183 | else: | |
184 | self.current_frame = max( | |
185 | 0, | |
186 | int(value * self.audio_segment.frame_rate)) | |
187 | ||
188 | def stop(self, fade_out=0, wait=False, set_wait_id=None): | |
29597680 | 189 | if self.is_loaded_playing(): |
20586193 IB |
190 | ms = int(self.sound_position * 1000) |
191 | ms_fo = max(1, int(fade_out * 1000)) | |
192 | ||
193 | new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ | |
194 | .fade_out(ms_fo) | |
195 | with self.music_lock: | |
196 | self.current_audio_segment = new_audio_segment | |
29597680 | 197 | self.stop_playing() |
20586193 IB |
198 | if wait: |
199 | if set_wait_id is not None: | |
200 | self.mapping.add_wait_id(set_wait_id, self.wait_event) | |
201 | self.wait_end() | |
202 | else: | |
29597680 IB |
203 | self.stopped() |
204 | ||
20586193 IB |
205 | def abandon_all_effects(self): |
206 | db_gain = 0 | |
207 | for gain_effect in self.gain_effects: | |
208 | db_gain += gain_effect.get_last_gain() | |
209 | ||
210 | self.gain_effects = [] | |
211 | self.set_gain(db_gain) | |
212 | ||
213 | def set_volume(self, value, delta=False, fade=0): | |
214 | [db_gain, self.volume] = gain( | |
215 | value + int(delta) * self.volume, | |
216 | self.volume) | |
217 | ||
218 | self.set_gain_with_effect(db_gain, fade=fade) | |
219 | ||
220 | def set_gain_with_effect(self, db_gain, fade=0): | |
221 | if not self.is_in_use(): | |
222 | return | |
223 | ||
224 | if fade > 0: | |
225 | self.add_fade_effect(db_gain, fade) | |
226 | else: | |
227 | self.set_gain(db_gain) | |
228 | ||
229 | def wait_end(self): | |
230 | self.wait_event.clear() | |
231 | self.wait_event.wait() | |
232 | ||
233 | # Callbacks | |
234 | def finished_callback(self): | |
235 | self.stopped() | |
0deb82a5 | 236 | |
22514f3a IB |
237 | def play_callback(self, out_data_length, frame_count): |
238 | if self.is_loaded_paused(): | |
239 | return b'\0' * out_data_length | |
240 | ||
29597680 | 241 | with self.music_lock: |
ccc8b4f2 IB |
242 | [data, nb_frames] = self.get_next_sample(frame_count) |
243 | if nb_frames < frame_count: | |
0cb786e3 IB |
244 | if self.is_loaded_playing() and\ |
245 | self.current_loop < self.last_loop: | |
1e44fe7e | 246 | self.current_loop += 1 |
ccc8b4f2 | 247 | self.current_frame = 0 |
2e404903 IB |
248 | [new_data, new_nb_frames] = self.get_next_sample( |
249 | frame_count - nb_frames) | |
ccc8b4f2 IB |
250 | data += new_data |
251 | nb_frames += new_nb_frames | |
252 | elif nb_frames == 0: | |
9925ce3b | 253 | # FIXME: too slow when mixing multiple streams |
2e404903 IB |
254 | threading.Thread( |
255 | name="MSFinishedCallback", | |
256 | target=self.finished_callback).start() | |
29597680 | 257 | |
22514f3a | 258 | return data.ljust(out_data_length, b'\0') |
ccc8b4f2 | 259 | |
20586193 IB |
260 | # Helpers |
261 | def set_gain(self, db_gain, absolute=False): | |
262 | if absolute: | |
263 | self.db_gain = db_gain | |
264 | else: | |
265 | self.db_gain += db_gain | |
266 | ||
ccc8b4f2 | 267 | def get_next_sample(self, frame_count): |
20586193 | 268 | fw = self.audio_segment.frame_width |
ccc8b4f2 IB |
269 | |
270 | data = b"" | |
271 | nb_frames = 0 | |
ccc8b4f2 IB |
272 | |
273 | segment = self.current_audio_segment | |
274 | max_val = int(segment.frame_count()) | |
275 | ||
276 | start_i = max(self.current_frame, 0) | |
277 | end_i = min(self.current_frame + frame_count, max_val) | |
2e404903 | 278 | data += segment._data[start_i*fw : end_i*fw] |
ccc8b4f2 IB |
279 | nb_frames += end_i - start_i |
280 | self.current_frame += end_i - start_i | |
281 | ||
aee1334c IB |
282 | volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) |
283 | ||
284 | data = audioop.mul(data, Config.sample_width, volume_factor) | |
ccda4cb9 | 285 | |
ccc8b4f2 | 286 | return [data, nb_frames] |
be27763f | 287 | |
20586193 IB |
288 | def add_fade_effect(self, db_gain, fade_duration): |
289 | if not self.is_in_use(): | |
52d58baf | 290 | return |
20586193 IB |
291 | |
292 | self.gain_effects.append(GainEffect( | |
293 | "fade", | |
294 | self.current_audio_segment, | |
295 | self.current_loop, | |
296 | self.sound_position, | |
297 | self.sound_position + fade_duration, | |
298 | gain=db_gain)) | |
52d58baf | 299 | |
aee1334c IB |
300 | def effects_next_gain(self, frame_count): |
301 | db_gain = 0 | |
302 | for gain_effect in self.gain_effects: | |
303 | [new_gain, last_gain] = gain_effect.get_next_gain( | |
304 | self.current_frame, | |
1e44fe7e | 305 | self.current_loop, |
aee1334c IB |
306 | frame_count) |
307 | if last_gain: | |
308 | self.set_gain(new_gain) | |
309 | self.gain_effects.remove(gain_effect) | |
310 | else: | |
311 | db_gain += new_gain | |
312 | return db_gain | |
313 | ||
314 | ||
20586193 | 315 | def volume_factor(self, additional_gain=0): |
aee1334c IB |
316 | return 10 ** ( (self.db_gain + additional_gain) / 20) |
317 |