]>
Commit | Line | Data |
---|---|---|
1 | import threading | |
2 | import time | |
3 | ||
4 | from . import debug_print | |
5 | ||
6 | class Action: | |
7 | action_types = [ | |
8 | 'command', | |
9 | 'pause', | |
10 | 'play', | |
11 | 'seek', | |
12 | 'stop', | |
13 | 'stop_all_actions', | |
14 | 'unpause', | |
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 | |
26 | self.mapping = key.parent | |
27 | self.arguments = kwargs | |
28 | self.sleep_event = None | |
29 | ||
30 | def ready(self): | |
31 | if 'music' in self.arguments: | |
32 | return self.arguments['music'].check_is_loaded() | |
33 | else: | |
34 | return True | |
35 | ||
36 | def run(self): | |
37 | debug_print(self.description()) | |
38 | getattr(self, self.action)(**self.arguments) | |
39 | ||
40 | def description(self): | |
41 | return getattr(self, self.action + "_print")(**self.arguments) | |
42 | ||
43 | def interrupt(self): | |
44 | if getattr(self, self.action + "_interrupt", None): | |
45 | return getattr(self, self.action + "_interrupt")(**self.arguments) | |
46 | ||
47 | def music_list(self, music): | |
48 | if music is not None: | |
49 | return [music] | |
50 | else: | |
51 | return self.mapping.open_files.values() | |
52 | ||
53 | # Actions | |
54 | def command(self, command = "", **kwargs): | |
55 | # FIXME: todo | |
56 | pass | |
57 | ||
58 | def pause(self, music = None, **kwargs): | |
59 | for music in self.music_list(music): | |
60 | if music.is_loaded_playing(): | |
61 | music.pause() | |
62 | ||
63 | def unpause(self, music = None, **kwargs): | |
64 | for music in self.music_list(music): | |
65 | if music.is_loaded_paused(): | |
66 | music.unpause() | |
67 | ||
68 | def play(self, music = None, fade_in = 0, start_at = 0, | |
69 | restart_if_running = False, volume = 100, | |
70 | loop = 0, | |
71 | **kwargs): | |
72 | for music in self.music_list(music): | |
73 | if restart_if_running: | |
74 | if music.is_not_stopped(): | |
75 | music.stop() | |
76 | music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) | |
77 | else: | |
78 | if not music.is_not_stopped(): | |
79 | music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop) | |
80 | ||
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 | ||
85 | def stop(self, music = None, fade_out = 0, wait = False, **kwargs): | |
86 | previous = None | |
87 | for music in self.music_list(music): | |
88 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
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) | |
95 | ||
96 | def stop_all_actions(self, **kwargs): | |
97 | self.mapping.stop_all_running() | |
98 | ||
99 | def volume(self, music = None, value = 100, delta = False, **kwargs): | |
100 | if music is not None: | |
101 | music.set_volume(value, delta = delta) | |
102 | else: | |
103 | self.mapping.set_master_volume(value, delta = delta) | |
104 | ||
105 | def wait(self, duration = 0, music = None, **kwargs): | |
106 | self.sleep_event = threading.Event() | |
107 | ||
108 | if music is not None: | |
109 | music.wait_end() | |
110 | ||
111 | threading.Timer(duration, self.sleep_event.set).start() | |
112 | self.sleep_event.wait() | |
113 | ||
114 | # Action messages | |
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: | |
120 | return "pausing « {} »".format(music.name) | |
121 | else: | |
122 | return "pausing all musics" | |
123 | ||
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 | ||
130 | def play_print(self, music = None, fade_in = 0, start_at = 0, | |
131 | restart_if_running = False, volume = 100, loop = 0, **kwargs): | |
132 | message = "starting " | |
133 | if music is not None: | |
134 | message += "« {} »".format(music.name) | |
135 | else: | |
136 | message += "all musics" | |
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 | ||
146 | if loop > 0: | |
147 | message += " {} times".format(loop + 1) | |
148 | elif loop < 0: | |
149 | message += " in loop" | |
150 | ||
151 | if restart_if_running: | |
152 | message += " (restarting if already running)" | |
153 | ||
154 | return message | |
155 | ||
156 | def stop_print(self, music = None, fade_out = 0, wait = False, **kwargs): | |
157 | message = "stopping " | |
158 | if music is not None: | |
159 | message += "music « {} »".format(music.name) | |
160 | else: | |
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 | |
169 | ||
170 | def stop_all_actions_print(self, **kwargs): | |
171 | return "stopping all actions" | |
172 | ||
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 | ||
185 | def volume_print(self, music = None, value = 100, delta = False, **kwargs): | |
186 | if delta: | |
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) | |
191 | else: | |
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) | |
196 | ||
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) | |
204 | ||
205 | ||
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 |