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