]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
4 from transitions
.extensions
import HierarchicalMachine
as Machine
5 from . import debug_print
, error_print
27 'children': ['running']
50 'dest': 'loaded_running',
51 'after': 'finish_action'
54 'trigger': 'interrupt',
55 'source': 'loaded_running',
57 'before': 'trigger_interrupt'
60 'trigger': 'finish_action',
61 'source': 'loaded_running',
66 def __init__(self
, action
, key
, **kwargs
):
67 Machine(model
=self
, states
=self
.STATES
,
68 transitions
=self
.TRANSITIONS
, initial
='initial',
69 ignore_invalid_triggers
=True, queued
=True)
73 self
.mapping
= key
.parent
74 self
.arguments
= kwargs
75 self
.sleep_event
= None
76 self
.waiting_music
= None
80 return self
.is_loaded(allow_substates
=True)
82 def callback_loaded(self
, success
):
88 # Machine states / events
89 def on_enter_loading(self
):
90 if self
.action
in self
.ACTION_TYPES
:
91 if 'music' in self
.arguments
:
92 self
.arguments
['music'].subscribe_loaded(self
.callback_loaded
)
96 error_print("Unknown action {}".format(self
.action
))
100 def on_enter_loaded_running(self
):
101 debug_print(self
.description())
102 getattr(self
, self
.action
)(**self
.arguments
)
104 def trigger_interrupt(self
):
105 if getattr(self
, self
.action
+ "_interrupt", None):
106 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
109 def music_list(self
, music
):
110 if music
is not None:
113 return self
.mapping
.open_files
.values()
115 def description(self
):
116 if getattr(self
, self
.action
+ "_print", None):
117 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
119 return "unknown action {}".format(self
.action
)
122 def command(self
, command
="", **kwargs
):
126 def pause(self
, music
=None, **kwargs
):
127 for music
in self
.music_list(music
):
128 if music
.is_loaded_playing():
131 def unpause(self
, music
=None, **kwargs
):
132 for music
in self
.music_list(music
):
133 if music
.is_loaded_paused():
136 def play(self
, music
=None, fade_in
=0, start_at
=0,
137 restart_if_running
=False, volume
=100,
139 for music
in self
.music_list(music
):
140 if restart_if_running
:
141 if music
.is_in_use():
148 elif not music
.is_in_use():
155 def seek(self
, music
=None, value
=0, delta
=False, **kwargs
):
156 for music
in self
.music_list(music
):
157 music
.seek(value
=value
, delta
=delta
)
159 def interrupt_wait(self
, wait_id
=None):
160 self
.mapping
.interrupt_wait(wait_id
)
162 def stop(self
, music
=None, fade_out
=0, wait
=False,
163 set_wait_id
=None, **kwargs
):
165 for music
in self
.music_list(music
):
166 if music
.is_loaded_paused() or music
.is_loaded_playing():
167 if previous
is not None:
168 previous
.stop(fade_out
=fade_out
)
171 music
.stop(fade_out
=fade_out
)
173 if previous
is not None:
174 self
.waiting_music
= previous
178 set_wait_id
=set_wait_id
)
180 def stop_all_actions(self
, **kwargs
):
181 self
.mapping
.stop_all_running()
183 def volume(self
, music
=None, value
=100, fade
=0, delta
=False, **kwargs
):
184 if music
is not None:
185 music
.set_volume(value
, delta
=delta
, fade
=fade
)
187 self
.mapping
.set_master_volume(value
, delta
=delta
, fade
=fade
)
189 def wait(self
, duration
=0, music
=None, set_wait_id
=None, **kwargs
):
190 if set_wait_id
is not None:
191 self
.mapping
.add_wait_id(set_wait_id
, self
)
193 self
.sleep_event
= threading
.Event()
195 if music
is not None:
198 threading
.Timer(duration
, self
.sleep_event
.set).start()
199 self
.sleep_event
.wait()
202 def command_print(self
, command
="", **kwargs
):
203 return "running command {}".format(command
)
205 def interrupt_wait_print(self
, wait_id
=None, **kwargs
):
206 return "interrupt wait with id {}".format(wait_id
)
208 def pause_print(self
, music
=None, **kwargs
):
209 if music
is not None:
210 return "pausing « {} »".format(music
.name
)
212 return "pausing all musics"
214 def unpause_print(self
, music
=None, **kwargs
):
215 if music
is not None:
216 return "unpausing « {} »".format(music
.name
)
218 return "unpausing all musics"
220 def play_print(self
, music
=None, fade_in
=0, start_at
=0,
221 restart_if_running
=False, volume
=100, loop
=0, **kwargs
):
222 message
= "starting "
223 if music
is not None:
224 message
+= "« {} »".format(music
.name
)
226 message
+= "all musics"
229 message
+= " at {}s".format(start_at
)
232 message
+= " with {}s fade_in".format(fade_in
)
234 message
+= " at volume {}%".format(volume
)
237 message
+= " {} times".format(loop
+ 1)
239 message
+= " in loop"
241 if restart_if_running
:
242 message
+= " (restarting if already running)"
246 def stop_print(self
, music
=None, fade_out
=0, wait
=False,
247 set_wait_id
=None, **kwargs
):
249 message
= "stopping "
250 if music
is not None:
251 message
+= "music « {} »".format(music
.name
)
253 message
+= "all musics"
256 message
+= " with {}s fadeout".format(fade_out
)
258 if set_wait_id
is not None:
259 message
+= " (waiting the end of fadeout, with id {})"\
262 message
+= " (waiting the end of fadeout)"
266 def stop_all_actions_print(self
, **kwargs
):
267 return "stopping all actions"
269 def seek_print(self
, music
=None, value
=0, delta
=False, **kwargs
):
271 if music
is not None:
272 return "moving music « {} » by {:+d}s" \
273 .format(music
.name
, value
)
275 return "moving all musics by {:+d}s" \
278 if music
is not None:
279 return "moving music « {} » to position {}s" \
280 .format(music
.name
, value
)
282 return "moving all musics to position {}s" \
285 def volume_print(self
, music
=None,
286 value
=100, delta
=False, fade
=0, **kwargs
):
289 if music
is not None:
290 message
+= "{:+d}% to volume of « {} »" \
291 .format(value
, music
.name
)
293 message
+= "{:+d}% to volume" \
296 if music
is not None:
297 message
+= "setting volume of « {} » to {}%" \
298 .format(music
.name
, value
)
300 message
+= "setting volume to {}%" \
304 message
+= " with {}s fade".format(fade
)
308 def wait_print(self
, duration
=0, music
=None, set_wait_id
=None, **kwargs
):
311 message
+= "waiting {}s" \
314 message
+= "waiting the end of « {} »" \
317 message
+= "waiting the end of « {} » + {}s" \
318 .format(music
.name
, duration
)
320 if set_wait_id
is not None:
321 message
+= " (setting id = {})".format(set_wait_id
)
325 # Interruptions (only for non-"atomic" actions)
326 def wait_interrupt(self
, duration
=0, music
=None, **kwargs
):
327 if self
.sleep_event
is not None:
328 self
.sleep_event
.set()
329 if music
is not None:
330 music
.wait_event
.set()
332 def stop_interrupt(self
, music
=None, fade_out
=0, wait
=False,
333 set_wait_id
=None, **kwargs
):
334 if self
.waiting_music
is not None:
335 self
.waiting_music
.wait_event
.set()