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