]>
Commit | Line | Data |
---|---|---|
1 | import threading | |
2 | import pydub | |
3 | import time | |
4 | from transitions.extensions import HierarchicalMachine as Machine | |
5 | ||
6 | import os.path | |
7 | ||
8 | import audioop | |
9 | ||
10 | from .lock import Lock | |
11 | from . import Config, gain, debug_print, error_print | |
12 | from .mixer import Mixer | |
13 | from .music_effect import GainEffect | |
14 | ||
15 | file_lock = Lock("file") | |
16 | ||
17 | class MusicFile: | |
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 | ||
75 | def __init__(self, filename, mapping, name=None, gain=1): | |
76 | Machine(model=self, states=self.STATES, | |
77 | transitions=self.TRANSITIONS, initial='initial', | |
78 | ignore_invalid_triggers=True) | |
79 | ||
80 | self.mapping = mapping | |
81 | self.filename = filename | |
82 | self.name = name or filename | |
83 | self.audio_segment = None | |
84 | self.initial_volume_factor = gain | |
85 | self.music_lock = Lock("music__" + filename) | |
86 | ||
87 | threading.Thread(name="MSMusicLoad", target=self.load).start() | |
88 | ||
89 | # Machine related events | |
90 | def on_enter_loading(self): | |
91 | with file_lock: | |
92 | try: | |
93 | debug_print("Loading « {} »".format(self.name)) | |
94 | self.mixer = self.mapping.mixer or Mixer() | |
95 | initial_db_gain = gain(self.initial_volume_factor * 100) | |
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) | |
102 | self.sound_duration = self.audio_segment.duration_seconds | |
103 | except Exception as e: | |
104 | error_print("failed to load « {} »: {}".format(self.name, e)) | |
105 | self.loading_error = e | |
106 | self.fail() | |
107 | else: | |
108 | self.success() | |
109 | debug_print("Loaded « {} »".format(self.name)) | |
110 | ||
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) | |
121 | ||
122 | # Machine related states | |
123 | def is_in_use(self): | |
124 | return self.is_loaded(allow_substates=True) and not self.is_loaded() | |
125 | ||
126 | def is_in_use_not_stopping(self): | |
127 | return self.is_loaded_playing() or self.is_loaded_paused() | |
128 | ||
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 | |
135 | @property | |
136 | def sound_position(self): | |
137 | if self.is_in_use(): | |
138 | return self.current_frame / self.current_audio_segment.frame_rate | |
139 | else: | |
140 | return 0 | |
141 | ||
142 | def play(self, fade_in=0, volume=100, loop=0, start_at=0): | |
143 | self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) | |
144 | self.volume = volume | |
145 | if loop < 0: | |
146 | self.last_loop = float('inf') | |
147 | else: | |
148 | self.last_loop = loop | |
149 | ||
150 | with self.music_lock: | |
151 | self.current_audio_segment = self.audio_segment | |
152 | self.current_frame = int(start_at * self.audio_segment.frame_rate) | |
153 | ||
154 | self.start_playing() | |
155 | ||
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) | |
160 | ||
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): | |
189 | if self.is_loaded_playing(): | |
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 | |
197 | self.stop_playing() | |
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: | |
203 | self.stopped() | |
204 | ||
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() | |
236 | ||
237 | def play_callback(self, out_data_length, frame_count): | |
238 | if self.is_loaded_paused(): | |
239 | return b'\0' * out_data_length | |
240 | ||
241 | with self.music_lock: | |
242 | [data, nb_frames] = self.get_next_sample(frame_count) | |
243 | if nb_frames < frame_count: | |
244 | if self.is_loaded_playing() and\ | |
245 | self.current_loop < self.last_loop: | |
246 | self.current_loop += 1 | |
247 | self.current_frame = 0 | |
248 | [new_data, new_nb_frames] = self.get_next_sample( | |
249 | frame_count - nb_frames) | |
250 | data += new_data | |
251 | nb_frames += new_nb_frames | |
252 | elif nb_frames == 0: | |
253 | # FIXME: too slow when mixing multiple streams | |
254 | threading.Thread( | |
255 | name="MSFinishedCallback", | |
256 | target=self.finished_callback).start() | |
257 | ||
258 | return data.ljust(out_data_length, b'\0') | |
259 | ||
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 | ||
267 | def get_next_sample(self, frame_count): | |
268 | fw = self.audio_segment.frame_width | |
269 | ||
270 | data = b"" | |
271 | nb_frames = 0 | |
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) | |
278 | data += segment._data[start_i*fw : end_i*fw] | |
279 | nb_frames += end_i - start_i | |
280 | self.current_frame += end_i - start_i | |
281 | ||
282 | volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) | |
283 | ||
284 | data = audioop.mul(data, Config.sample_width, volume_factor) | |
285 | ||
286 | return [data, nb_frames] | |
287 | ||
288 | def add_fade_effect(self, db_gain, fade_duration): | |
289 | if not self.is_in_use(): | |
290 | return | |
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)) | |
299 | ||
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, | |
305 | self.current_loop, | |
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 | ||
315 | def volume_factor(self, additional_gain=0): | |
316 | return 10 ** ( (self.db_gain + additional_gain) / 20) | |
317 |