]>
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', | |
9 | 'stop', | |
10 | 'stop_all_actions', | |
9de92b6d | 11 | 'unpause', |
be27763f IB |
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 | |
1b4b78f5 | 23 | self.mapping = key.parent |
be27763f | 24 | self.arguments = kwargs |
0deb82a5 | 25 | self.sleep_event = None |
be27763f IB |
26 | |
27 | def ready(self): | |
28 | if 'music' in self.arguments: | |
60979de4 | 29 | return self.arguments['music'].check_is_loaded() |
be27763f IB |
30 | else: |
31 | return True | |
32 | ||
33 | def run(self): | |
ba9ea93a | 34 | print(self.description()) |
9de92b6d | 35 | getattr(self, self.action)(**self.arguments) |
be27763f | 36 | |
ba9ea93a IB |
37 | def description(self): |
38 | return getattr(self, self.action + "_print")(**self.arguments) | |
39 | ||
0deb82a5 IB |
40 | def interrupt(self): |
41 | if getattr(self, self.action + "_interrupt", None): | |
42 | return getattr(self, self.action + "_interrupt")(**self.arguments) | |
43 | ||
44 | # Actions | |
be27763f | 45 | def command(self, command = "", **kwargs): |
0deb82a5 | 46 | # FIXME: todo |
be27763f IB |
47 | pass |
48 | ||
29597680 | 49 | def music_list(self, music): |
be27763f | 50 | if music is not None: |
29597680 | 51 | return [music] |
be27763f | 52 | else: |
1b4b78f5 | 53 | return self.mapping.open_files.values() |
29597680 IB |
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() | |
be27763f | 59 | |
9de92b6d | 60 | def unpause(self, music = None, **kwargs): |
29597680 IB |
61 | for music in self.music_list(music): |
62 | if music.is_loaded_paused(): | |
63 | music.unpause() | |
9de92b6d | 64 | |
be27763f IB |
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: | |
0e5d59f7 | 68 | if restart_if_running: |
29597680 | 69 | if music.is_not_stopped(): |
0e5d59f7 IB |
70 | music.stop() |
71 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) | |
72 | else: | |
29597680 | 73 | if not music.is_not_stopped(): |
0e5d59f7 | 74 | music.play(volume = volume, fade_in = fade_in, start_at = start_at) |
be27763f | 75 | |
1b4b78f5 IB |
76 | def stop(self, music = None, fade_out = 0, wait = False, **kwargs): |
77 | previous = None | |
29597680 IB |
78 | for music in self.music_list(music): |
79 | if music.is_loaded_paused() or music.is_loaded_playing(): | |
1b4b78f5 IB |
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) | |
be27763f IB |
86 | |
87 | def stop_all_actions(self, **kwargs): | |
1b4b78f5 | 88 | self.mapping.stop_all_running() |
be27763f | 89 | |
1b4b78f5 | 90 | def volume(self, music = None, value = 100, add = False, **kwargs): |
0e5d59f7 | 91 | if music is not None: |
1b4b78f5 | 92 | music.set_volume(value, add = add) |
0e5d59f7 | 93 | else: |
1b4b78f5 | 94 | self.mapping.set_master_volume(value, add = add) |
be27763f | 95 | |
b86db9f1 | 96 | def wait(self, duration = 0, music = None, **kwargs): |
0deb82a5 IB |
97 | self.sleep_event = threading.Event() |
98 | ||
99 | if music is not None: | |
b86db9f1 | 100 | music.wait_end() |
be27763f | 101 | |
0deb82a5 IB |
102 | threading.Timer(duration, self.sleep_event.set).start() |
103 | self.sleep_event.wait() | |
104 | ||
105 | # Action messages | |
be27763f IB |
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: | |
9de92b6d | 111 | return "pausing « {} »".format(music.name) |
be27763f IB |
112 | else: |
113 | return "pausing all musics" | |
114 | ||
9de92b6d IB |
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 | ||
be27763f IB |
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: | |
9de92b6d | 125 | message += "« {} »".format(music.name) |
be27763f IB |
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 | ||
1b4b78f5 IB |
142 | def stop_print(self, music = None, fade_out = 0, wait = False, **kwargs): |
143 | message = "stopping " | |
be27763f | 144 | if music is not None: |
1b4b78f5 | 145 | message += "music « {} »".format(music.name) |
be27763f | 146 | else: |
1b4b78f5 IB |
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 | |
be27763f | 155 | |
0e5d59f7 | 156 | def stop_all_actions_print(self, **kwargs): |
be27763f IB |
157 | return "stopping all actions" |
158 | ||
1b4b78f5 IB |
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) | |
be27763f | 165 | else: |
1b4b78f5 IB |
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) | |
be27763f | 170 | |
0deb82a5 IB |
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) | |
be27763f IB |
178 | |
179 | ||
0deb82a5 IB |
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 |