]>
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 | ||
8 | from .lock import Lock | |
75d6cdba | 9 | from . import Config, gain |
af27d782 | 10 | from .mixer import Mixer |
1b4b78f5 | 11 | |
29597680 IB |
12 | file_lock = Lock("file") |
13 | ||
60979de4 | 14 | class MusicFile(Machine): |
1b4b78f5 | 15 | def __init__(self, filename, mapping, name = None, gain = 1): |
60979de4 IB |
16 | states = [ |
17 | 'initial', | |
18 | 'loading', | |
19 | 'failed', | |
29597680 | 20 | { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused', 'stopping'] } |
60979de4 IB |
21 | ] |
22 | transitions = [ | |
23 | { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'}, | |
24 | { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'}, | |
25 | { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'}, | |
29597680 IB |
26 | { 'trigger': 'start_playing', 'source': 'loaded_stopped', 'dest': 'loaded_playing'}, |
27 | { 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'}, | |
28 | { 'trigger': 'unpause', 'source': 'loaded_paused', 'dest': 'loaded_playing'}, | |
29 | { 'trigger': 'stop_playing', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopping'}, | |
22514f3a | 30 | { 'trigger': 'stopped', 'source': 'loaded_stopping', 'dest': 'loaded_stopped', 'after': 'trigger_stopped_events'} |
60979de4 IB |
31 | ] |
32 | ||
33 | Machine.__init__(self, states=states, transitions=transitions, initial='initial') | |
34 | ||
d6290f14 | 35 | # FIXME: catch error here |
af27d782 | 36 | self.mixer = mapping.mixer or Mixer() |
1b4b78f5 IB |
37 | self.volume = 100 |
38 | self.mapping = mapping | |
be27763f | 39 | self.filename = filename |
9de92b6d | 40 | self.name = name or filename |
29597680 | 41 | self.audio_segment = None |
ccc8b4f2 | 42 | self.audio_segment_frame_width = 0 |
1b4b78f5 | 43 | self.volume_factor = gain |
29597680 | 44 | self.music_lock = Lock("music__" + filename) |
0deb82a5 | 45 | self.wait_event = threading.Event() |
be27763f | 46 | |
29597680 IB |
47 | threading.Thread(name = "MSMusicLoad", target = self.load).start() |
48 | ||
49 | def on_enter_loading(self): | |
50 | with file_lock: | |
51 | try: | |
52 | print("Loading « {} »".format(self.name)) | |
1b4b78f5 | 53 | db_gain = gain(self.volume_factor * 100) |
75d6cdba | 54 | self.audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(Config.frame_rate).set_channels(Config.channels).set_sample_width(Config.sample_width).apply_gain(db_gain) |
ccc8b4f2 | 55 | self.audio_segment_frame_width = self.audio_segment.frame_width |
29597680 IB |
56 | self.sound_duration = self.audio_segment.duration_seconds |
57 | except Exception as e: | |
58 | print("failed to load « {} »: {}".format(self.name, e)) | |
59 | self.loading_error = e | |
60 | self.fail() | |
61 | else: | |
62 | self.success() | |
63 | print("Loaded « {} »".format(self.name)) | |
60979de4 IB |
64 | |
65 | def check_is_loaded(self): | |
66 | return self.state.startswith('loaded_') | |
be27763f | 67 | |
29597680 IB |
68 | def is_not_stopped(self): |
69 | return self.check_is_loaded() and not self.is_loaded_stopped() | |
0e5d59f7 | 70 | |
9de92b6d | 71 | def is_paused(self): |
29597680 | 72 | return self.is_loaded_paused() |
9de92b6d | 73 | |
98ff4305 IB |
74 | @property |
75 | def sound_position(self): | |
29597680 IB |
76 | if self.is_not_stopped(): |
77 | return self.current_frame / self.current_audio_segment.frame_rate | |
98ff4305 IB |
78 | else: |
79 | return 0 | |
80 | ||
6f4944c1 | 81 | def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0): |
1b4b78f5 IB |
82 | db_gain = gain(volume) + self.mapping.master_gain |
83 | self.volume = volume | |
6f4944c1 | 84 | self.loop = loop |
1b4b78f5 | 85 | |
29597680 | 86 | ms = int(start_at * 1000) |
ccc8b4f2 | 87 | ms_fi = int(fade_in * 1000) |
29597680 | 88 | with self.music_lock: |
ccc8b4f2 IB |
89 | self.current_audio_segment = (self.audio_segment + db_gain) |
90 | self.current_frame = int(start_at * self.audio_segment.frame_rate) | |
91 | if ms_fi > 0: | |
92 | # FIXME: apply it to repeated when looping? | |
93 | self.a_s_with_effect = self.current_audio_segment[ms:ms+ms_fi].fade_in(ms_fi) | |
94 | self.current_frame_with_effect = 0 | |
95 | else: | |
96 | self.a_s_with_effect = None | |
97 | ||
29597680 IB |
98 | self.start_playing() |
99 | ||
29597680 | 100 | def on_enter_loaded_playing(self): |
af27d782 | 101 | self.mixer.add_file(self) |
29597680 IB |
102 | |
103 | def finished_callback(self): | |
104 | if self.is_loaded_playing(): | |
105 | self.stop_playing() | |
106 | if self.is_loaded_stopping(): | |
107 | self.stopped() | |
108 | ||
22514f3a | 109 | def trigger_stopped_events(self): |
af27d782 | 110 | self.mixer.remove_file(self) |
0deb82a5 IB |
111 | self.wait_event.set() |
112 | ||
22514f3a IB |
113 | def play_callback(self, out_data_length, frame_count): |
114 | if self.is_loaded_paused(): | |
115 | return b'\0' * out_data_length | |
116 | ||
29597680 | 117 | with self.music_lock: |
ccc8b4f2 IB |
118 | [data, nb_frames] = self.get_next_sample(frame_count) |
119 | if nb_frames < frame_count: | |
6f4944c1 IB |
120 | if self.is_loaded_playing() and self.loop != 0: |
121 | self.loop -= 1 | |
ccc8b4f2 IB |
122 | self.current_frame = 0 |
123 | [new_data, new_nb_frames] = self.get_next_sample(frame_count - nb_frames) | |
124 | data += new_data | |
125 | nb_frames += new_nb_frames | |
126 | elif nb_frames == 0: | |
75d6cdba | 127 | # FIXME: too slow |
22514f3a | 128 | threading.Thread(name = "MSFinishedCallback", target=self.finished_callback).start() |
29597680 | 129 | |
22514f3a | 130 | return data.ljust(out_data_length, b'\0') |
ccc8b4f2 IB |
131 | |
132 | def get_next_sample(self, frame_count): | |
133 | fw = self.audio_segment_frame_width | |
134 | ||
135 | data = b"" | |
136 | nb_frames = 0 | |
137 | if self.a_s_with_effect is not None: | |
138 | segment = self.a_s_with_effect | |
139 | max_val = int(segment.frame_count()) | |
140 | ||
141 | start_i = max(self.current_frame_with_effect, 0) | |
142 | end_i = min(self.current_frame_with_effect + frame_count, max_val) | |
143 | ||
144 | data += segment._data[(start_i * fw):(end_i * fw)] | |
145 | ||
146 | frame_count = max(0, self.current_frame_with_effect + frame_count - max_val) | |
147 | ||
148 | self.current_frame_with_effect += end_i - start_i | |
149 | self.current_frame += end_i - start_i | |
150 | nb_frames += end_i - start_i | |
151 | ||
152 | if frame_count > 0: | |
153 | self.a_s_with_effect = None | |
154 | ||
155 | segment = self.current_audio_segment | |
156 | max_val = int(segment.frame_count()) | |
157 | ||
158 | start_i = max(self.current_frame, 0) | |
159 | end_i = min(self.current_frame + frame_count, max_val) | |
160 | data += segment._data[(start_i * fw):(end_i * fw)] | |
161 | nb_frames += end_i - start_i | |
162 | self.current_frame += end_i - start_i | |
163 | ||
164 | return [data, nb_frames] | |
be27763f | 165 | |
52d58baf IB |
166 | def seek(self, value = 0, delta = False): |
167 | # We don't want to do that while stopping | |
168 | if not (self.is_loaded_playing() or self.is_loaded_paused()): | |
169 | return | |
170 | with self.music_lock: | |
ccc8b4f2 | 171 | self.a_s_with_effect = None |
52d58baf | 172 | self.current_frame = max(0, int(delta) * self.current_frame + int(value * self.audio_segment.frame_rate)) |
ccc8b4f2 | 173 | # FIXME: si on fait un seek + delta, adapter le "loop" |
52d58baf | 174 | |
1b4b78f5 | 175 | def stop(self, fade_out = 0, wait = False): |
29597680 IB |
176 | if self.is_loaded_playing(): |
177 | ms = int(self.sound_position * 1000) | |
178 | ms_fo = max(1, int(fade_out * 1000)) | |
179 | ||
ccc8b4f2 IB |
180 | # FIXME: stop fade_out puis seek -5 -> on abandonne le fade ? (cf |
181 | # commentaire dans fonction seek | |
22514f3a | 182 | new_audio_segment = self.current_audio_segment[:ms + ms_fo].fade_out(ms_fo) |
29597680 | 183 | with self.music_lock: |
22514f3a IB |
184 | self.current_audio_segment = new_audio_segment |
185 | self.stop_playing() | |
1b4b78f5 IB |
186 | if wait: |
187 | self.wait_end() | |
0e5d59f7 | 188 | else: |
29597680 IB |
189 | self.stop_playing() |
190 | self.stopped() | |
0e5d59f7 | 191 | |
1b4b78f5 IB |
192 | def set_gain(self, db_gain): |
193 | if not self.is_not_stopped(): | |
29597680 IB |
194 | return |
195 | ||
1b4b78f5 IB |
196 | new_audio_segment = self.current_audio_segment + db_gain |
197 | ||
ccc8b4f2 IB |
198 | new_a_s_with_effect = None |
199 | if self.a_s_with_effect is not None: | |
200 | new_a_s_with_effect = self.a_s_with_effect + db_gain | |
201 | ||
29597680 IB |
202 | with self.music_lock: |
203 | self.current_audio_segment = new_audio_segment | |
ccc8b4f2 | 204 | self.a_s_with_effect = new_a_s_with_effect |
29597680 | 205 | |
8e50011c IB |
206 | def set_volume(self, value, delta = False): |
207 | [db_gain, self.volume] = gain(value + int(delta) * self.volume, self.volume) | |
1b4b78f5 IB |
208 | |
209 | self.set_gain(db_gain) | |
be27763f | 210 | |
b86db9f1 | 211 | def wait_end(self): |
0deb82a5 IB |
212 | self.wait_event.clear() |
213 | self.wait_event.wait() | |
d479af33 | 214 | |
29597680 IB |
215 | # Add some more functions to AudioSegments |
216 | def get_sample_slice_data(self, start_sample=0, end_sample=float('inf')): | |
217 | max_val = int(self.frame_count()) | |
218 | ||
219 | start_i = max(start_sample, 0) * self.frame_width | |
220 | end_i = min(end_sample, max_val) * self.frame_width | |
221 | ||
222 | return self._data[start_i:end_i] | |
223 | ||
224 | pydub.AudioSegment.get_sample_slice_data = get_sample_slice_data |