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