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