]>
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', | |
9 | 'pause', | |
10 | 'play', | |
52d58baf | 11 | 'seek', |
be27763f IB |
12 | 'stop', |
13 | 'stop_all_actions', | |
9de92b6d | 14 | 'unpause', |
be27763f IB |
15 | 'volume', |
16 | 'wait', | |
17 | ] | |
18 | ||
19 | def __init__(self, action, key, **kwargs): | |
20 | if action in self.action_types: | |
21 | self.action = action | |
22 | else: | |
23 | raise Exception("Unknown action {}".format(action)) | |
24 | ||
25 | self.key = key | |
1b4b78f5 | 26 | self.mapping = key.parent |
be27763f | 27 | self.arguments = kwargs |
0deb82a5 | 28 | self.sleep_event = None |
be27763f IB |
29 | |
30 | def ready(self): | |
31 | if 'music' in self.arguments: | |
60979de4 | 32 | return self.arguments['music'].check_is_loaded() |
be27763f IB |
33 | else: |
34 | return True | |
35 | ||
36 | def run(self): | |
a24c34bc | 37 | debug_print(self.description()) |
9de92b6d | 38 | getattr(self, self.action)(**self.arguments) |
be27763f | 39 | |
ba9ea93a IB |
40 | def description(self): |
41 | return getattr(self, self.action + "_print")(**self.arguments) | |
42 | ||
0deb82a5 IB |
43 | def interrupt(self): |
44 | if getattr(self, self.action + "_interrupt", None): | |
45 | return getattr(self, self.action + "_interrupt")(**self.arguments) | |
46 | ||
29597680 | 47 | def music_list(self, music): |
be27763f | 48 | if music is not None: |
29597680 | 49 | return [music] |
be27763f | 50 | else: |
1b4b78f5 | 51 | return self.mapping.open_files.values() |
29597680 | 52 | |
c80ff6dc IB |
53 | # Actions |
54 | def command(self, command = "", **kwargs): | |
55 | # FIXME: todo | |
56 | pass | |
57 | ||
29597680 IB |
58 | def pause(self, music = None, **kwargs): |
59 | for music in self.music_list(music): | |
60 | if music.is_loaded_playing(): | |
61 | music.pause() | |
be27763f | 62 | |
9de92b6d | 63 | def unpause(self, music = None, **kwargs): |
29597680 IB |
64 | for music in self.music_list(music): |
65 | if music.is_loaded_paused(): | |
66 | music.unpause() | |
9de92b6d | 67 | |
be27763f | 68 | def play(self, music = None, fade_in = 0, start_at = 0, |
6f4944c1 IB |
69 | restart_if_running = False, volume = 100, |
70 | loop = 0, | |
71 | **kwargs): | |
c80ff6dc | 72 | for music in self.music_list(music): |
0e5d59f7 | 73 | if restart_if_running: |
29597680 | 74 | if music.is_not_stopped(): |
0e5d59f7 | 75 | music.stop() |
6f4944c1 | 76 | music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) |
0e5d59f7 | 77 | else: |
29597680 | 78 | if not music.is_not_stopped(): |
6f4944c1 | 79 | music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) |
be27763f | 80 | |
52d58baf IB |
81 | def seek(self, music = None, value = 0, delta = False, **kwargs): |
82 | for music in self.music_list(music): | |
83 | music.seek(value = value, delta = delta) | |
84 | ||
1b4b78f5 IB |
85 | def stop(self, music = None, fade_out = 0, wait = False, **kwargs): |
86 | previous = None | |
29597680 IB |
87 | for music in self.music_list(music): |
88 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
1b4b78f5 IB |
89 | if previous is not None: |
90 | previous.stop(fade_out = fade_out) | |
91 | previous = music | |
92 | ||
93 | if previous is not None: | |
94 | previous.stop(fade_out = fade_out, wait = wait) | |
be27763f IB |
95 | |
96 | def stop_all_actions(self, **kwargs): | |
1b4b78f5 | 97 | self.mapping.stop_all_running() |
be27763f | 98 | |
8e50011c | 99 | def volume(self, music = None, value = 100, delta = False, **kwargs): |
0e5d59f7 | 100 | if music is not None: |
8e50011c | 101 | music.set_volume(value, delta = delta) |
0e5d59f7 | 102 | else: |
8e50011c | 103 | self.mapping.set_master_volume(value, delta = delta) |
be27763f | 104 | |
b86db9f1 | 105 | def wait(self, duration = 0, music = None, **kwargs): |
0deb82a5 IB |
106 | self.sleep_event = threading.Event() |
107 | ||
108 | if music is not None: | |
b86db9f1 | 109 | music.wait_end() |
be27763f | 110 | |
0deb82a5 IB |
111 | threading.Timer(duration, self.sleep_event.set).start() |
112 | self.sleep_event.wait() | |
113 | ||
114 | # Action messages | |
be27763f IB |
115 | def command_print(self, command = "", **kwargs): |
116 | return "running command {}".format(command) | |
117 | ||
118 | def pause_print(self, music = None, **kwargs): | |
119 | if music is not None: | |
9de92b6d | 120 | return "pausing « {} »".format(music.name) |
be27763f IB |
121 | else: |
122 | return "pausing all musics" | |
123 | ||
9de92b6d IB |
124 | def unpause_print(self, music = None, **kwargs): |
125 | if music is not None: | |
126 | return "unpausing « {} »".format(music.name) | |
127 | else: | |
128 | return "unpausing all musics" | |
129 | ||
be27763f | 130 | def play_print(self, music = None, fade_in = 0, start_at = 0, |
6f4944c1 | 131 | restart_if_running = False, volume = 100, loop = 0, **kwargs): |
be27763f IB |
132 | message = "starting " |
133 | if music is not None: | |
9de92b6d | 134 | message += "« {} »".format(music.name) |
be27763f | 135 | else: |
c80ff6dc | 136 | message += "all musics" |
be27763f IB |
137 | |
138 | if start_at != 0: | |
139 | message += " at {}s".format(start_at) | |
140 | ||
141 | if fade_in != 0: | |
142 | message += " with {}s fade_in".format(fade_in) | |
143 | ||
144 | message += " at volume {}%".format(volume) | |
145 | ||
6f4944c1 IB |
146 | if loop > 0: |
147 | message += " {} times".format(loop + 1) | |
148 | elif loop < 0: | |
149 | message += " in loop" | |
150 | ||
be27763f IB |
151 | if restart_if_running: |
152 | message += " (restarting if already running)" | |
153 | ||
154 | return message | |
155 | ||
1b4b78f5 IB |
156 | def stop_print(self, music = None, fade_out = 0, wait = False, **kwargs): |
157 | message = "stopping " | |
be27763f | 158 | if music is not None: |
1b4b78f5 | 159 | message += "music « {} »".format(music.name) |
be27763f | 160 | else: |
1b4b78f5 IB |
161 | message += "all musics" |
162 | ||
163 | if fade_out > 0: | |
164 | message += " with {}s fadeout".format(fade_out) | |
165 | if wait: | |
166 | message += " (waiting the end of fadeout)" | |
167 | ||
168 | return message | |
be27763f | 169 | |
0e5d59f7 | 170 | def stop_all_actions_print(self, **kwargs): |
be27763f IB |
171 | return "stopping all actions" |
172 | ||
52d58baf IB |
173 | def seek_print(self, music = None, value = 0, delta = False, **kwargs): |
174 | if delta: | |
175 | if music is not None: | |
176 | return "moving music « {} » by {:+d}s".format(music.name, value) | |
177 | else: | |
178 | return "moving all musics by {:+d}s".format(value) | |
179 | else: | |
180 | if music is not None: | |
181 | return "moving music « {} » to position {}s".format(music.name, value) | |
182 | else: | |
183 | return "moving all musics to position {}s".format(value) | |
184 | ||
8e50011c IB |
185 | def volume_print(self, music = None, value = 100, delta = False, **kwargs): |
186 | if delta: | |
1b4b78f5 IB |
187 | if music is not None: |
188 | return "{:+d}% to volume of « {} »".format(value, music.name) | |
189 | else: | |
190 | return "{:+d}% to volume".format(value) | |
be27763f | 191 | else: |
1b4b78f5 IB |
192 | if music is not None: |
193 | return "setting volume of « {} » to {}%".format(music.name, value) | |
194 | else: | |
195 | return "setting volume to {}%".format(value) | |
be27763f | 196 | |
0deb82a5 IB |
197 | def wait_print(self, duration = 0, music = None, **kwargs): |
198 | if music is None: | |
199 | return "waiting {}s".format(duration) | |
200 | elif duration == 0: | |
201 | return "waiting the end of « {} »".format(music.name) | |
202 | else: | |
203 | return "waiting the end of « {} » + {}s".format(music.name, duration) | |
be27763f IB |
204 | |
205 | ||
0deb82a5 IB |
206 | # Interruptions |
207 | def wait_interrupt(self, duration = 0, music = None, **kwargs): | |
208 | if self.sleep_event is not None: | |
209 | self.sleep_event.set() | |
210 | if music is not None: | |
211 | music.wait_event.set() | |
212 |