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