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