]>
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 | 53 | # Actions |
2e404903 | 54 | def command(self, command="", **kwargs): |
c80ff6dc IB |
55 | # FIXME: todo |
56 | pass | |
57 | ||
2e404903 | 58 | def pause(self, music=None, **kwargs): |
29597680 IB |
59 | for music in self.music_list(music): |
60 | if music.is_loaded_playing(): | |
61 | music.pause() | |
be27763f | 62 | |
2e404903 | 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 | |
2e404903 IB |
68 | def play(self, music=None, fade_in=0, start_at=0, |
69 | restart_if_running=False, volume=100, | |
70 | loop=0, **kwargs): | |
c80ff6dc | 71 | for music in self.music_list(music): |
0e5d59f7 | 72 | if restart_if_running: |
29597680 | 73 | if music.is_not_stopped(): |
0e5d59f7 | 74 | music.stop() |
2e404903 IB |
75 | music.play( |
76 | volume=volume, | |
77 | fade_in=fade_in, | |
78 | start_at=start_at, | |
79 | loop=loop) | |
80 | elif not music.is_not_stopped(): | |
81 | music.play( | |
82 | volume=volume, | |
83 | fade_in=fade_in, | |
84 | start_at=start_at, | |
85 | loop=loop) | |
86 | ||
87 | def seek(self, music=None, value=0, delta=False, **kwargs): | |
52d58baf | 88 | for music in self.music_list(music): |
2e404903 | 89 | music.seek(value=value, delta=delta) |
52d58baf | 90 | |
2e404903 | 91 | def stop(self, music=None, fade_out=0, wait=False, **kwargs): |
1b4b78f5 | 92 | previous = None |
29597680 IB |
93 | for music in self.music_list(music): |
94 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
1b4b78f5 | 95 | if previous is not None: |
2e404903 | 96 | previous.stop(fade_out=fade_out) |
1b4b78f5 IB |
97 | previous = music |
98 | ||
99 | if previous is not None: | |
2e404903 | 100 | previous.stop(fade_out=fade_out, wait=wait) |
be27763f IB |
101 | |
102 | def stop_all_actions(self, **kwargs): | |
1b4b78f5 | 103 | self.mapping.stop_all_running() |
be27763f | 104 | |
2e404903 | 105 | def volume(self, music=None, value=100, delta=False, **kwargs): |
0e5d59f7 | 106 | if music is not None: |
2e404903 | 107 | music.set_volume(value, delta=delta) |
0e5d59f7 | 108 | else: |
2e404903 | 109 | self.mapping.set_master_volume(value, delta=delta) |
be27763f | 110 | |
2e404903 | 111 | def wait(self, duration=0, music=None, **kwargs): |
0deb82a5 IB |
112 | self.sleep_event = threading.Event() |
113 | ||
114 | if music is not None: | |
b86db9f1 | 115 | music.wait_end() |
be27763f | 116 | |
0deb82a5 IB |
117 | threading.Timer(duration, self.sleep_event.set).start() |
118 | self.sleep_event.wait() | |
119 | ||
120 | # Action messages | |
2e404903 | 121 | def command_print(self, command="", **kwargs): |
be27763f IB |
122 | return "running command {}".format(command) |
123 | ||
2e404903 | 124 | def pause_print(self, music=None, **kwargs): |
be27763f | 125 | if music is not None: |
9de92b6d | 126 | return "pausing « {} »".format(music.name) |
be27763f IB |
127 | else: |
128 | return "pausing all musics" | |
129 | ||
2e404903 | 130 | def unpause_print(self, music=None, **kwargs): |
9de92b6d IB |
131 | if music is not None: |
132 | return "unpausing « {} »".format(music.name) | |
133 | else: | |
134 | return "unpausing all musics" | |
135 | ||
2e404903 IB |
136 | def play_print(self, music=None, fade_in=0, start_at=0, |
137 | restart_if_running=False, volume=100, loop=0, **kwargs): | |
be27763f IB |
138 | message = "starting " |
139 | if music is not None: | |
9de92b6d | 140 | message += "« {} »".format(music.name) |
be27763f | 141 | else: |
c80ff6dc | 142 | message += "all musics" |
be27763f IB |
143 | |
144 | if start_at != 0: | |
145 | message += " at {}s".format(start_at) | |
146 | ||
147 | if fade_in != 0: | |
148 | message += " with {}s fade_in".format(fade_in) | |
149 | ||
150 | message += " at volume {}%".format(volume) | |
151 | ||
6f4944c1 IB |
152 | if loop > 0: |
153 | message += " {} times".format(loop + 1) | |
154 | elif loop < 0: | |
155 | message += " in loop" | |
156 | ||
be27763f IB |
157 | if restart_if_running: |
158 | message += " (restarting if already running)" | |
159 | ||
160 | return message | |
161 | ||
2e404903 | 162 | def stop_print(self, music=None, fade_out=0, wait=False, **kwargs): |
1b4b78f5 | 163 | message = "stopping " |
be27763f | 164 | if music is not None: |
1b4b78f5 | 165 | message += "music « {} »".format(music.name) |
be27763f | 166 | else: |
1b4b78f5 IB |
167 | message += "all musics" |
168 | ||
169 | if fade_out > 0: | |
170 | message += " with {}s fadeout".format(fade_out) | |
171 | if wait: | |
172 | message += " (waiting the end of fadeout)" | |
173 | ||
174 | return message | |
be27763f | 175 | |
0e5d59f7 | 176 | def stop_all_actions_print(self, **kwargs): |
be27763f IB |
177 | return "stopping all actions" |
178 | ||
2e404903 | 179 | def seek_print(self, music=None, value=0, delta=False, **kwargs): |
52d58baf IB |
180 | if delta: |
181 | if music is not None: | |
2e404903 IB |
182 | return "moving music « {} » by {:+d}s" \ |
183 | .format(music.name, value) | |
52d58baf | 184 | else: |
2e404903 IB |
185 | return "moving all musics by {:+d}s" \ |
186 | .format(value) | |
52d58baf IB |
187 | else: |
188 | if music is not None: | |
2e404903 IB |
189 | return "moving music « {} » to position {}s" \ |
190 | .format(music.name, value) | |
52d58baf | 191 | else: |
2e404903 IB |
192 | return "moving all musics to position {}s" \ |
193 | .format(value) | |
52d58baf | 194 | |
2e404903 | 195 | def volume_print(self, music=None, value=100, delta=False, **kwargs): |
8e50011c | 196 | if delta: |
1b4b78f5 | 197 | if music is not None: |
2e404903 IB |
198 | return "{:+d}% to volume of « {} »" \ |
199 | .format(value, music.name) | |
1b4b78f5 | 200 | else: |
2e404903 IB |
201 | return "{:+d}% to volume" \ |
202 | .format(value) | |
be27763f | 203 | else: |
1b4b78f5 | 204 | if music is not None: |
2e404903 IB |
205 | return "setting volume of « {} » to {}%" \ |
206 | .format(music.name, value) | |
1b4b78f5 | 207 | else: |
2e404903 IB |
208 | return "setting volume to {}%" \ |
209 | .format(value) | |
be27763f | 210 | |
2e404903 | 211 | def wait_print(self, duration=0, music=None, **kwargs): |
0deb82a5 | 212 | if music is None: |
2e404903 IB |
213 | return "waiting {}s" \ |
214 | .format(duration) | |
0deb82a5 | 215 | elif duration == 0: |
2e404903 IB |
216 | return "waiting the end of « {} »" \ |
217 | .format(music.name) | |
0deb82a5 | 218 | else: |
2e404903 IB |
219 | return "waiting the end of « {} » + {}s" \ |
220 | .format(music.name, duration) | |
be27763f IB |
221 | |
222 | ||
0deb82a5 | 223 | # Interruptions |
2e404903 | 224 | def wait_interrupt(self, duration=0, music=None, **kwargs): |
0deb82a5 IB |
225 | if self.sleep_event is not None: |
226 | self.sleep_event.set() | |
227 | if music is not None: | |
228 | music.wait_event.set() | |
229 |