]>
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 IB |
72 | Machine.__init__(self, states=states, |
73 | transitions=transitions, initial='initial') | |
60979de4 | 74 | |
1b4b78f5 IB |
75 | self.volume = 100 |
76 | self.mapping = mapping | |
be27763f | 77 | self.filename = filename |
9de92b6d | 78 | self.name = name or filename |
29597680 | 79 | self.audio_segment = None |
ccc8b4f2 | 80 | self.audio_segment_frame_width = 0 |
ccda4cb9 | 81 | self.initial_volume_factor = gain |
29597680 | 82 | self.music_lock = Lock("music__" + filename) |
0deb82a5 | 83 | self.wait_event = threading.Event() |
ccda4cb9 | 84 | self.db_gain = 0 |
aee1334c | 85 | self.gain_effects = [] |
be27763f | 86 | |
2e404903 | 87 | threading.Thread(name="MSMusicLoad", target=self.load).start() |
29597680 IB |
88 | |
89 | def on_enter_loading(self): | |
90 | with file_lock: | |
91 | try: | |
a24c34bc IB |
92 | debug_print("Loading « {} »".format(self.name)) |
93 | self.mixer = self.mapping.mixer or Mixer() | |
ccda4cb9 | 94 | initial_db_gain = gain(self.initial_volume_factor * 100) |
2e404903 IB |
95 | self.audio_segment = pydub.AudioSegment \ |
96 | .from_file(self.filename) \ | |
97 | .set_frame_rate(Config.frame_rate) \ | |
98 | .set_channels(Config.channels) \ | |
99 | .set_sample_width(Config.sample_width) \ | |
100 | .apply_gain(initial_db_gain) | |
ccc8b4f2 | 101 | self.audio_segment_frame_width = self.audio_segment.frame_width |
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 IB |
110 | |
111 | def check_is_loaded(self): | |
112 | return self.state.startswith('loaded_') | |
be27763f | 113 | |
29597680 IB |
114 | def is_not_stopped(self): |
115 | return self.check_is_loaded() and not self.is_loaded_stopped() | |
0e5d59f7 | 116 | |
9de92b6d | 117 | def is_paused(self): |
29597680 | 118 | return self.is_loaded_paused() |
9de92b6d | 119 | |
98ff4305 IB |
120 | @property |
121 | def sound_position(self): | |
29597680 IB |
122 | if self.is_not_stopped(): |
123 | return self.current_frame / self.current_audio_segment.frame_rate | |
98ff4305 IB |
124 | else: |
125 | return 0 | |
126 | ||
2e404903 | 127 | def play(self, fade_in=0, volume=100, loop=0, start_at=0): |
b37c72a2 | 128 | self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) |
1b4b78f5 | 129 | self.volume = volume |
1e44fe7e | 130 | self.current_loop = 0 |
0cb786e3 IB |
131 | if loop < 0: |
132 | self.last_loop = float('inf') | |
133 | else: | |
134 | self.last_loop = loop | |
1b4b78f5 | 135 | |
29597680 | 136 | with self.music_lock: |
ccda4cb9 | 137 | self.current_audio_segment = self.audio_segment |
ccc8b4f2 | 138 | self.current_frame = int(start_at * self.audio_segment.frame_rate) |
1e44fe7e IB |
139 | if fade_in > 0: |
140 | db_gain = gain(self.volume, 0)[0] | |
141 | self.set_gain(-db_gain) | |
142 | self.gain_effects.append(GainEffect( | |
143 | "fade", | |
144 | self.current_audio_segment, | |
145 | self.current_loop, | |
146 | self.sound_position, | |
147 | self.sound_position + fade_in, | |
148 | gain=db_gain)) | |
ccc8b4f2 | 149 | |
29597680 IB |
150 | self.start_playing() |
151 | ||
29597680 | 152 | def on_enter_loaded_playing(self): |
af27d782 | 153 | self.mixer.add_file(self) |
29597680 IB |
154 | |
155 | def finished_callback(self): | |
156 | if self.is_loaded_playing(): | |
157 | self.stop_playing() | |
158 | if self.is_loaded_stopping(): | |
159 | self.stopped() | |
160 | ||
22514f3a | 161 | def trigger_stopped_events(self): |
af27d782 | 162 | self.mixer.remove_file(self) |
0deb82a5 IB |
163 | self.wait_event.set() |
164 | ||
22514f3a IB |
165 | def play_callback(self, out_data_length, frame_count): |
166 | if self.is_loaded_paused(): | |
167 | return b'\0' * out_data_length | |
168 | ||
29597680 | 169 | with self.music_lock: |
ccc8b4f2 IB |
170 | [data, nb_frames] = self.get_next_sample(frame_count) |
171 | if nb_frames < frame_count: | |
0cb786e3 IB |
172 | if self.is_loaded_playing() and\ |
173 | self.current_loop < self.last_loop: | |
1e44fe7e | 174 | self.current_loop += 1 |
ccc8b4f2 | 175 | self.current_frame = 0 |
2e404903 IB |
176 | [new_data, new_nb_frames] = self.get_next_sample( |
177 | frame_count - nb_frames) | |
ccc8b4f2 IB |
178 | data += new_data |
179 | nb_frames += new_nb_frames | |
180 | elif nb_frames == 0: | |
75d6cdba | 181 | # FIXME: too slow |
2e404903 IB |
182 | threading.Thread( |
183 | name="MSFinishedCallback", | |
184 | target=self.finished_callback).start() | |
29597680 | 185 | |
22514f3a | 186 | return data.ljust(out_data_length, b'\0') |
ccc8b4f2 IB |
187 | |
188 | def get_next_sample(self, frame_count): | |
189 | fw = self.audio_segment_frame_width | |
190 | ||
191 | data = b"" | |
192 | nb_frames = 0 | |
ccc8b4f2 IB |
193 | |
194 | segment = self.current_audio_segment | |
195 | max_val = int(segment.frame_count()) | |
196 | ||
197 | start_i = max(self.current_frame, 0) | |
198 | end_i = min(self.current_frame + frame_count, max_val) | |
2e404903 | 199 | data += segment._data[start_i*fw : end_i*fw] |
ccc8b4f2 IB |
200 | nb_frames += end_i - start_i |
201 | self.current_frame += end_i - start_i | |
202 | ||
aee1334c IB |
203 | volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) |
204 | ||
205 | data = audioop.mul(data, Config.sample_width, volume_factor) | |
ccda4cb9 | 206 | |
ccc8b4f2 | 207 | return [data, nb_frames] |
be27763f | 208 | |
2e404903 | 209 | def seek(self, value=0, delta=False): |
52d58baf IB |
210 | # We don't want to do that while stopping |
211 | if not (self.is_loaded_playing() or self.is_loaded_paused()): | |
212 | return | |
213 | with self.music_lock: | |
aee1334c | 214 | self.abandon_all_effects() |
2e404903 IB |
215 | self.current_frame = max( |
216 | 0, | |
217 | int(delta) * self.current_frame | |
218 | + int(value * self.audio_segment.frame_rate)) | |
ccc8b4f2 | 219 | # FIXME: si on fait un seek + delta, adapter le "loop" |
52d58baf | 220 | |
aee1334c IB |
221 | def effects_next_gain(self, frame_count): |
222 | db_gain = 0 | |
223 | for gain_effect in self.gain_effects: | |
224 | [new_gain, last_gain] = gain_effect.get_next_gain( | |
225 | self.current_frame, | |
1e44fe7e | 226 | self.current_loop, |
aee1334c IB |
227 | frame_count) |
228 | if last_gain: | |
229 | self.set_gain(new_gain) | |
230 | self.gain_effects.remove(gain_effect) | |
231 | else: | |
232 | db_gain += new_gain | |
233 | return db_gain | |
234 | ||
235 | ||
236 | def abandon_all_effects(self): | |
237 | db_gain = 0 | |
238 | for gain_effect in self.gain_effects: | |
239 | db_gain += gain_effect.get_last_gain() | |
240 | ||
241 | self.gain_effects = [] | |
242 | self.set_gain(db_gain) | |
243 | ||
2e404903 | 244 | def stop(self, fade_out=0, wait=False): |
29597680 IB |
245 | if self.is_loaded_playing(): |
246 | ms = int(self.sound_position * 1000) | |
247 | ms_fo = max(1, int(fade_out * 1000)) | |
248 | ||
2e404903 IB |
249 | new_audio_segment = self.current_audio_segment[: ms+ms_fo] \ |
250 | .fade_out(ms_fo) | |
29597680 | 251 | with self.music_lock: |
22514f3a IB |
252 | self.current_audio_segment = new_audio_segment |
253 | self.stop_playing() | |
1b4b78f5 IB |
254 | if wait: |
255 | self.wait_end() | |
0e5d59f7 | 256 | else: |
29597680 IB |
257 | self.stop_playing() |
258 | self.stopped() | |
0e5d59f7 | 259 | |
aee1334c IB |
260 | def volume_factor(self, additional_gain): |
261 | return 10 ** ( (self.db_gain + additional_gain) / 20) | |
262 | ||
b37c72a2 IB |
263 | def set_gain(self, db_gain, absolute=False): |
264 | if absolute: | |
265 | self.db_gain = db_gain | |
266 | else: | |
267 | self.db_gain += db_gain | |
29597680 | 268 | |
aee1334c | 269 | def set_volume(self, value, delta=False, fade=0): |
2e404903 IB |
270 | [db_gain, self.volume] = gain( |
271 | value + int(delta) * self.volume, | |
272 | self.volume) | |
1b4b78f5 | 273 | |
aee1334c IB |
274 | if fade > 0: |
275 | self.gain_effects.append(GainEffect( | |
276 | "fade", | |
277 | self.current_audio_segment, | |
1e44fe7e | 278 | self.current_loop, |
aee1334c IB |
279 | self.sound_position, |
280 | self.sound_position + fade, | |
281 | gain=db_gain)) | |
282 | else: | |
283 | self.set_gain(db_gain) | |
be27763f | 284 | |
b86db9f1 | 285 | def wait_end(self): |
0deb82a5 IB |
286 | self.wait_event.clear() |
287 | self.wait_event.wait() | |
d479af33 | 288 |