]>
Commit | Line | Data |
---|---|---|
1 | import threading | |
2 | import time | |
3 | ||
4 | from . import debug_print | |
5 | ||
6 | class Action: | |
7 | action_types = [ | |
8 | 'command', | |
9 | 'interrupt_wait', | |
10 | 'pause', | |
11 | 'play', | |
12 | 'seek', | |
13 | 'stop', | |
14 | 'stop_all_actions', | |
15 | 'unpause', | |
16 | 'volume', | |
17 | 'wait', | |
18 | ] | |
19 | ||
20 | def __init__(self, action, key, **kwargs): | |
21 | if action in self.action_types: | |
22 | self.action = action | |
23 | else: | |
24 | raise Exception("Unknown action {}".format(action)) | |
25 | ||
26 | self.key = key | |
27 | self.mapping = key.parent | |
28 | self.arguments = kwargs | |
29 | self.sleep_event = None | |
30 | ||
31 | def ready(self): | |
32 | if 'music' in self.arguments: | |
33 | return self.arguments['music'].is_loaded(allow_substates=True) | |
34 | else: | |
35 | return True | |
36 | ||
37 | def run(self): | |
38 | debug_print(self.description()) | |
39 | getattr(self, self.action)(**self.arguments) | |
40 | ||
41 | def description(self): | |
42 | return getattr(self, self.action + "_print")(**self.arguments) | |
43 | ||
44 | def interrupt(self): | |
45 | if getattr(self, self.action + "_interrupt", None): | |
46 | return getattr(self, self.action + "_interrupt")(**self.arguments) | |
47 | ||
48 | def music_list(self, music): | |
49 | if music is not None: | |
50 | return [music] | |
51 | else: | |
52 | return self.mapping.open_files.values() | |
53 | ||
54 | # Actions | |
55 | def command(self, command="", **kwargs): | |
56 | # FIXME: todo | |
57 | pass | |
58 | ||
59 | def pause(self, music=None, **kwargs): | |
60 | for music in self.music_list(music): | |
61 | if music.is_loaded_playing(): | |
62 | music.pause() | |
63 | ||
64 | def unpause(self, music=None, **kwargs): | |
65 | for music in self.music_list(music): | |
66 | if music.is_loaded_paused(): | |
67 | music.unpause() | |
68 | ||
69 | def play(self, music=None, fade_in=0, start_at=0, | |
70 | restart_if_running=False, volume=100, | |
71 | loop=0, **kwargs): | |
72 | for music in self.music_list(music): | |
73 | if restart_if_running: | |
74 | if music.is_in_use(): | |
75 | music.stop() | |
76 | music.play( | |
77 | volume=volume, | |
78 | fade_in=fade_in, | |
79 | start_at=start_at, | |
80 | loop=loop) | |
81 | elif not music.is_in_use(): | |
82 | music.play( | |
83 | volume=volume, | |
84 | fade_in=fade_in, | |
85 | start_at=start_at, | |
86 | loop=loop) | |
87 | ||
88 | def seek(self, music=None, value=0, delta=False, **kwargs): | |
89 | for music in self.music_list(music): | |
90 | music.seek(value=value, delta=delta) | |
91 | ||
92 | def interrupt_wait(self, wait_id=None): | |
93 | self.mapping.interrupt_wait(wait_id) | |
94 | ||
95 | def stop(self, music=None, fade_out=0, wait=False, | |
96 | set_wait_id=None, **kwargs): | |
97 | previous = None | |
98 | for music in self.music_list(music): | |
99 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
100 | if previous is not None: | |
101 | previous.stop(fade_out=fade_out) | |
102 | previous = music | |
103 | else: | |
104 | music.stop(fade_out=fade_out) | |
105 | ||
106 | if previous is not None: | |
107 | previous.stop( | |
108 | fade_out=fade_out, | |
109 | wait=wait, | |
110 | set_wait_id=set_wait_id) | |
111 | ||
112 | def stop_all_actions(self, **kwargs): | |
113 | self.mapping.stop_all_running() | |
114 | ||
115 | def volume(self, music=None, value=100, fade=0, delta=False, **kwargs): | |
116 | if music is not None: | |
117 | music.set_volume(value, delta=delta, fade=fade) | |
118 | else: | |
119 | self.mapping.set_master_volume(value, delta=delta, fade=fade) | |
120 | ||
121 | def wait(self, duration=0, music=None, set_wait_id=None, **kwargs): | |
122 | if set_wait_id is not None: | |
123 | self.mapping.add_wait_id(set_wait_id, self) | |
124 | ||
125 | self.sleep_event = threading.Event() | |
126 | ||
127 | if music is not None: | |
128 | music.wait_end() | |
129 | ||
130 | threading.Timer(duration, self.sleep_event.set).start() | |
131 | self.sleep_event.wait() | |
132 | ||
133 | # Action messages | |
134 | def command_print(self, command="", **kwargs): | |
135 | return "running command {}".format(command) | |
136 | ||
137 | def interrupt_wait_print(self, wait_id=None, **kwargs): | |
138 | return "interrupt wait with id {}".format(wait_id) | |
139 | ||
140 | def pause_print(self, music=None, **kwargs): | |
141 | if music is not None: | |
142 | return "pausing « {} »".format(music.name) | |
143 | else: | |
144 | return "pausing all musics" | |
145 | ||
146 | def unpause_print(self, music=None, **kwargs): | |
147 | if music is not None: | |
148 | return "unpausing « {} »".format(music.name) | |
149 | else: | |
150 | return "unpausing all musics" | |
151 | ||
152 | def play_print(self, music=None, fade_in=0, start_at=0, | |
153 | restart_if_running=False, volume=100, loop=0, **kwargs): | |
154 | message = "starting " | |
155 | if music is not None: | |
156 | message += "« {} »".format(music.name) | |
157 | else: | |
158 | message += "all musics" | |
159 | ||
160 | if start_at != 0: | |
161 | message += " at {}s".format(start_at) | |
162 | ||
163 | if fade_in != 0: | |
164 | message += " with {}s fade_in".format(fade_in) | |
165 | ||
166 | message += " at volume {}%".format(volume) | |
167 | ||
168 | if loop > 0: | |
169 | message += " {} times".format(loop + 1) | |
170 | elif loop < 0: | |
171 | message += " in loop" | |
172 | ||
173 | if restart_if_running: | |
174 | message += " (restarting if already running)" | |
175 | ||
176 | return message | |
177 | ||
178 | def stop_print(self, music=None, fade_out=0, wait=False, | |
179 | set_wait_id=None, **kwargs): | |
180 | ||
181 | message = "stopping " | |
182 | if music is not None: | |
183 | message += "music « {} »".format(music.name) | |
184 | else: | |
185 | message += "all musics" | |
186 | ||
187 | if fade_out > 0: | |
188 | message += " with {}s fadeout".format(fade_out) | |
189 | if wait: | |
190 | if set_wait_id is not None: | |
191 | message += " (waiting the end of fadeout, with id {})"\ | |
192 | .format(set_wait_id) | |
193 | else: | |
194 | message += " (waiting the end of fadeout)" | |
195 | ||
196 | return message | |
197 | ||
198 | def stop_all_actions_print(self, **kwargs): | |
199 | return "stopping all actions" | |
200 | ||
201 | def seek_print(self, music=None, value=0, delta=False, **kwargs): | |
202 | if delta: | |
203 | if music is not None: | |
204 | return "moving music « {} » by {:+d}s" \ | |
205 | .format(music.name, value) | |
206 | else: | |
207 | return "moving all musics by {:+d}s" \ | |
208 | .format(value) | |
209 | else: | |
210 | if music is not None: | |
211 | return "moving music « {} » to position {}s" \ | |
212 | .format(music.name, value) | |
213 | else: | |
214 | return "moving all musics to position {}s" \ | |
215 | .format(value) | |
216 | ||
217 | def volume_print(self, music=None, | |
218 | value=100, delta=False, fade=0, **kwargs): | |
219 | message = "" | |
220 | if delta: | |
221 | if music is not None: | |
222 | message += "{:+d}% to volume of « {} »" \ | |
223 | .format(value, music.name) | |
224 | else: | |
225 | message += "{:+d}% to volume" \ | |
226 | .format(value) | |
227 | else: | |
228 | if music is not None: | |
229 | message += "setting volume of « {} » to {}%" \ | |
230 | .format(music.name, value) | |
231 | else: | |
232 | message += "setting volume to {}%" \ | |
233 | .format(value) | |
234 | ||
235 | if fade > 0: | |
236 | message += " with {}s fade".format(fade) | |
237 | ||
238 | return message | |
239 | ||
240 | def wait_print(self, duration=0, music=None, set_wait_id=None, **kwargs): | |
241 | message = "" | |
242 | if music is None: | |
243 | message += "waiting {}s" \ | |
244 | .format(duration) | |
245 | elif duration == 0: | |
246 | message += "waiting the end of « {} »" \ | |
247 | .format(music.name) | |
248 | else: | |
249 | message += "waiting the end of « {} » + {}s" \ | |
250 | .format(music.name, duration) | |
251 | ||
252 | if set_wait_id is not None: | |
253 | message += " (setting id = {})".format(set_wait_id) | |
254 | ||
255 | return message | |
256 | ||
257 | # Interruptions | |
258 | def wait_interrupt(self, duration=0, music=None, **kwargs): | |
259 | if self.sleep_event is not None: | |
260 | self.sleep_event.set() | |
261 | if music is not None: | |
262 | music.wait_event.set() | |
263 |