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