]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
Fix master volume fade
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
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 else:
99 music.stop(fade_out=fade_out)
100
101 if previous is not None:
102 previous.stop(fade_out=fade_out, wait=wait)
103
104 def stop_all_actions(self, **kwargs):
105 self.mapping.stop_all_running()
106
107 def volume(self, music=None, value=100, fade=0, delta=False, **kwargs):
108 if music is not None:
109 music.set_volume(value, delta=delta, fade=fade)
110 else:
111 self.mapping.set_master_volume(value, delta=delta, fade=fade)
112
113 def wait(self, duration=0, music=None, **kwargs):
114 self.sleep_event = threading.Event()
115
116 if music is not None:
117 music.wait_end()
118
119 threading.Timer(duration, self.sleep_event.set).start()
120 self.sleep_event.wait()
121
122 # Action messages
123 def command_print(self, command="", **kwargs):
124 return "running command {}".format(command)
125
126 def pause_print(self, music=None, **kwargs):
127 if music is not None:
128 return "pausing « {} »".format(music.name)
129 else:
130 return "pausing all musics"
131
132 def unpause_print(self, music=None, **kwargs):
133 if music is not None:
134 return "unpausing « {} »".format(music.name)
135 else:
136 return "unpausing all musics"
137
138 def play_print(self, music=None, fade_in=0, start_at=0,
139 restart_if_running=False, volume=100, loop=0, **kwargs):
140 message = "starting "
141 if music is not None:
142 message += "« {} »".format(music.name)
143 else:
144 message += "all musics"
145
146 if start_at != 0:
147 message += " at {}s".format(start_at)
148
149 if fade_in != 0:
150 message += " with {}s fade_in".format(fade_in)
151
152 message += " at volume {}%".format(volume)
153
154 if loop > 0:
155 message += " {} times".format(loop + 1)
156 elif loop < 0:
157 message += " in loop"
158
159 if restart_if_running:
160 message += " (restarting if already running)"
161
162 return message
163
164 def stop_print(self, music=None, fade_out=0, wait=False, **kwargs):
165 message = "stopping "
166 if music is not None:
167 message += "music « {} »".format(music.name)
168 else:
169 message += "all musics"
170
171 if fade_out > 0:
172 message += " with {}s fadeout".format(fade_out)
173 if wait:
174 message += " (waiting the end of fadeout)"
175
176 return message
177
178 def stop_all_actions_print(self, **kwargs):
179 return "stopping all actions"
180
181 def seek_print(self, music=None, value=0, delta=False, **kwargs):
182 if delta:
183 if music is not None:
184 return "moving music « {} » by {:+d}s" \
185 .format(music.name, value)
186 else:
187 return "moving all musics by {:+d}s" \
188 .format(value)
189 else:
190 if music is not None:
191 return "moving music « {} » to position {}s" \
192 .format(music.name, value)
193 else:
194 return "moving all musics to position {}s" \
195 .format(value)
196
197 def volume_print(self, music=None,
198 value=100, delta=False, fade=0, **kwargs):
199 message = ""
200 if delta:
201 if music is not None:
202 message += "{:+d}% to volume of « {} »" \
203 .format(value, music.name)
204 else:
205 message += "{:+d}% to volume" \
206 .format(value)
207 else:
208 if music is not None:
209 message += "setting volume of « {} » to {}%" \
210 .format(music.name, value)
211 else:
212 message += "setting volume to {}%" \
213 .format(value)
214
215 if fade > 0:
216 message += " with {}s fade".format(fade)
217
218 return message
219
220 def wait_print(self, duration=0, music=None, **kwargs):
221 if music is None:
222 return "waiting {}s" \
223 .format(duration)
224 elif duration == 0:
225 return "waiting the end of « {} »" \
226 .format(music.name)
227 else:
228 return "waiting the end of « {} » + {}s" \
229 .format(music.name, duration)
230
231
232 # Interruptions
233 def wait_interrupt(self, duration=0, music=None, **kwargs):
234 if self.sleep_event is not None:
235 self.sleep_event.set()
236 if music is not None:
237 music.wait_event.set()
238