]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
4 from . import debug_print
20 def __init__(self
, action
, key
, **kwargs
):
21 if action
in self
.action_types
:
24 raise Exception("Unknown action {}".format(action
))
27 self
.mapping
= key
.parent
28 self
.arguments
= kwargs
29 self
.sleep_event
= None
32 if 'music' in self
.arguments
:
33 return self
.arguments
['music'].is_loaded(allow_substates
=True)
38 debug_print(self
.description())
39 getattr(self
, self
.action
)(**self
.arguments
)
41 def description(self
):
42 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
45 if getattr(self
, self
.action
+ "_interrupt", None):
46 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
48 def music_list(self
, music
):
52 return self
.mapping
.open_files
.values()
55 def command(self
, command
="", **kwargs
):
59 def pause(self
, music
=None, **kwargs
):
60 for music
in self
.music_list(music
):
61 if music
.is_loaded_playing():
64 def unpause(self
, music
=None, **kwargs
):
65 for music
in self
.music_list(music
):
66 if music
.is_loaded_paused():
69 def play(self
, music
=None, fade_in
=0, start_at
=0,
70 restart_if_running
=False, volume
=100,
72 for music
in self
.music_list(music
):
73 if restart_if_running
:
81 elif not music
.is_in_use():
88 def seek(self
, music
=None, value
=0, delta
=False, **kwargs
):
89 for music
in self
.music_list(music
):
90 music
.seek(value
=value
, delta
=delta
)
92 def interrupt_wait(self
, wait_id
=None):
93 self
.mapping
.interrupt_wait(wait_id
)
95 def stop(self
, music
=None, fade_out
=0, wait
=False,
96 set_wait_id
=None, **kwargs
):
98 for music
in self
.music_list(music
):
99 if music
.is_loaded_paused() or music
.is_loaded_playing():
100 if previous
is not None:
101 previous
.stop(fade_out
=fade_out
)
104 music
.stop(fade_out
=fade_out
)
106 if previous
is not None:
110 set_wait_id
=set_wait_id
)
112 def stop_all_actions(self
, **kwargs
):
113 self
.mapping
.stop_all_running()
115 def volume(self
, music
=None, value
=100, fade
=0, delta
=False, **kwargs
):
116 if music
is not None:
117 music
.set_volume(value
, delta
=delta
, fade
=fade
)
119 self
.mapping
.set_master_volume(value
, delta
=delta
, fade
=fade
)
121 def wait(self
, duration
=0, music
=None, set_wait_id
=None, **kwargs
):
122 if set_wait_id
is not None:
123 self
.mapping
.add_wait_id(set_wait_id
, self
)
125 self
.sleep_event
= threading
.Event()
127 if music
is not None:
130 threading
.Timer(duration
, self
.sleep_event
.set).start()
131 self
.sleep_event
.wait()
134 def command_print(self
, command
="", **kwargs
):
135 return "running command {}".format(command
)
137 def interrupt_wait_print(self
, wait_id
=None, **kwargs
):
138 return "interrupt wait with id {}".format(wait_id
)
140 def pause_print(self
, music
=None, **kwargs
):
141 if music
is not None:
142 return "pausing « {} »".format(music
.name
)
144 return "pausing all musics"
146 def unpause_print(self
, music
=None, **kwargs
):
147 if music
is not None:
148 return "unpausing « {} »".format(music
.name
)
150 return "unpausing all musics"
152 def play_print(self
, music
=None, fade_in
=0, start_at
=0,
153 restart_if_running
=False, volume
=100, loop
=0, **kwargs
):
154 message
= "starting "
155 if music
is not None:
156 message
+= "« {} »".format(music
.name
)
158 message
+= "all musics"
161 message
+= " at {}s".format(start_at
)
164 message
+= " with {}s fade_in".format(fade_in
)
166 message
+= " at volume {}%".format(volume
)
169 message
+= " {} times".format(loop
+ 1)
171 message
+= " in loop"
173 if restart_if_running
:
174 message
+= " (restarting if already running)"
178 def stop_print(self
, music
=None, fade_out
=0, wait
=False,
179 set_wait_id
=None, **kwargs
):
181 message
= "stopping "
182 if music
is not None:
183 message
+= "music « {} »".format(music
.name
)
185 message
+= "all musics"
188 message
+= " with {}s fadeout".format(fade_out
)
190 if set_wait_id
is not None:
191 message
+= " (waiting the end of fadeout, with id {})"\
194 message
+= " (waiting the end of fadeout)"
198 def stop_all_actions_print(self
, **kwargs
):
199 return "stopping all actions"
201 def seek_print(self
, music
=None, value
=0, delta
=False, **kwargs
):
203 if music
is not None:
204 return "moving music « {} » by {:+d}s" \
205 .format(music
.name
, value
)
207 return "moving all musics by {:+d}s" \
210 if music
is not None:
211 return "moving music « {} » to position {}s" \
212 .format(music
.name
, value
)
214 return "moving all musics to position {}s" \
217 def volume_print(self
, music
=None,
218 value
=100, delta
=False, fade
=0, **kwargs
):
221 if music
is not None:
222 message
+= "{:+d}% to volume of « {} »" \
223 .format(value
, music
.name
)
225 message
+= "{:+d}% to volume" \
228 if music
is not None:
229 message
+= "setting volume of « {} » to {}%" \
230 .format(music
.name
, value
)
232 message
+= "setting volume to {}%" \
236 message
+= " with {}s fade".format(fade
)
240 def wait_print(self
, duration
=0, music
=None, set_wait_id
=None, **kwargs
):
243 message
+= "waiting {}s" \
246 message
+= "waiting the end of « {} »" \
249 message
+= "waiting the end of « {} » + {}s" \
250 .format(music
.name
, duration
)
252 if set_wait_id
is not None:
253 message
+= " (setting id = {})".format(set_wait_id
)
258 def wait_interrupt(self
, duration
=0, music
=None, **kwargs
):
259 if self
.sleep_event
is not None:
260 self
.sleep_event
.set()
261 if music
is not None:
262 music
.wait_event
.set()