]>
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, **kwargs): | |
71 | for music in self.music_list(music): | |
72 | if restart_if_running: | |
73 | if music.is_not_stopped(): | |
74 | music.stop() | |
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): | |
88 | for music in self.music_list(music): | |
89 | music.seek(value=value, delta=delta) | |
90 | ||
91 | def stop(self, music=None, fade_out=0, wait=False, **kwargs): | |
92 | previous = None | |
93 | for music in self.music_list(music): | |
94 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
95 | if previous is not None: | |
96 | previous.stop(fade_out=fade_out) | |
97 | previous = music | |
98 | ||
99 | if previous is not None: | |
100 | previous.stop(fade_out=fade_out, wait=wait) | |
101 | ||
102 | def stop_all_actions(self, **kwargs): | |
103 | self.mapping.stop_all_running() | |
104 | ||
105 | def volume(self, music=None, value=100, delta=False, **kwargs): | |
106 | if music is not None: | |
107 | music.set_volume(value, delta=delta) | |
108 | else: | |
109 | self.mapping.set_master_volume(value, delta=delta) | |
110 | ||
111 | def wait(self, duration=0, music=None, **kwargs): | |
112 | self.sleep_event = threading.Event() | |
113 | ||
114 | if music is not None: | |
115 | music.wait_end() | |
116 | ||
117 | threading.Timer(duration, self.sleep_event.set).start() | |
118 | self.sleep_event.wait() | |
119 | ||
120 | # Action messages | |
121 | def command_print(self, command="", **kwargs): | |
122 | return "running command {}".format(command) | |
123 | ||
124 | def pause_print(self, music=None, **kwargs): | |
125 | if music is not None: | |
126 | return "pausing « {} »".format(music.name) | |
127 | else: | |
128 | return "pausing all musics" | |
129 | ||
130 | def unpause_print(self, music=None, **kwargs): | |
131 | if music is not None: | |
132 | return "unpausing « {} »".format(music.name) | |
133 | else: | |
134 | return "unpausing all musics" | |
135 | ||
136 | def play_print(self, music=None, fade_in=0, start_at=0, | |
137 | restart_if_running=False, volume=100, loop=0, **kwargs): | |
138 | message = "starting " | |
139 | if music is not None: | |
140 | message += "« {} »".format(music.name) | |
141 | else: | |
142 | message += "all musics" | |
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 | ||
152 | if loop > 0: | |
153 | message += " {} times".format(loop + 1) | |
154 | elif loop < 0: | |
155 | message += " in loop" | |
156 | ||
157 | if restart_if_running: | |
158 | message += " (restarting if already running)" | |
159 | ||
160 | return message | |
161 | ||
162 | def stop_print(self, music=None, fade_out=0, wait=False, **kwargs): | |
163 | message = "stopping " | |
164 | if music is not None: | |
165 | message += "music « {} »".format(music.name) | |
166 | else: | |
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 | |
175 | ||
176 | def stop_all_actions_print(self, **kwargs): | |
177 | return "stopping all actions" | |
178 | ||
179 | def seek_print(self, music=None, value=0, delta=False, **kwargs): | |
180 | if delta: | |
181 | if music is not None: | |
182 | return "moving music « {} » by {:+d}s" \ | |
183 | .format(music.name, value) | |
184 | else: | |
185 | return "moving all musics by {:+d}s" \ | |
186 | .format(value) | |
187 | else: | |
188 | if music is not None: | |
189 | return "moving music « {} » to position {}s" \ | |
190 | .format(music.name, value) | |
191 | else: | |
192 | return "moving all musics to position {}s" \ | |
193 | .format(value) | |
194 | ||
195 | def volume_print(self, music=None, value=100, delta=False, **kwargs): | |
196 | if delta: | |
197 | if music is not None: | |
198 | return "{:+d}% to volume of « {} »" \ | |
199 | .format(value, music.name) | |
200 | else: | |
201 | return "{:+d}% to volume" \ | |
202 | .format(value) | |
203 | else: | |
204 | if music is not None: | |
205 | return "setting volume of « {} » to {}%" \ | |
206 | .format(music.name, value) | |
207 | else: | |
208 | return "setting volume to {}%" \ | |
209 | .format(value) | |
210 | ||
211 | def wait_print(self, duration=0, music=None, **kwargs): | |
212 | if music is None: | |
213 | return "waiting {}s" \ | |
214 | .format(duration) | |
215 | elif duration == 0: | |
216 | return "waiting the end of « {} »" \ | |
217 | .format(music.name) | |
218 | else: | |
219 | return "waiting the end of « {} » + {}s" \ | |
220 | .format(music.name, duration) | |
221 | ||
222 | ||
223 | # Interruptions | |
224 | def wait_interrupt(self, duration=0, music=None, **kwargs): | |
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 |