]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
4 from . import debug_print
19 def __init__(self
, action
, key
, **kwargs
):
20 if action
in self
.action_types
:
23 raise Exception("Unknown action {}".format(action
))
26 self
.mapping
= key
.parent
27 self
.arguments
= kwargs
28 self
.sleep_event
= None
31 if 'music' in self
.arguments
:
32 return self
.arguments
['music'].check_is_loaded()
37 debug_print(self
.description())
38 getattr(self
, self
.action
)(**self
.arguments
)
40 def description(self
):
41 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
44 if getattr(self
, self
.action
+ "_interrupt", None):
45 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
47 def music_list(self
, music
):
51 return self
.mapping
.open_files
.values()
54 def command(self
, command
="", **kwargs
):
58 def pause(self
, music
=None, **kwargs
):
59 for music
in self
.music_list(music
):
60 if music
.is_loaded_playing():
63 def unpause(self
, music
=None, **kwargs
):
64 for music
in self
.music_list(music
):
65 if music
.is_loaded_paused():
68 def play(self
, music
=None, fade_in
=0, start_at
=0,
69 restart_if_running
=False, volume
=100,
71 for music
in self
.music_list(music
):
72 if restart_if_running
:
73 if music
.is_not_stopped():
80 elif not music
.is_not_stopped():
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
)
91 def stop(self
, music
=None, fade_out
=0, wait
=False, **kwargs
):
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
)
99 if previous
is not None:
100 previous
.stop(fade_out
=fade_out
, wait
=wait
)
102 def stop_all_actions(self
, **kwargs
):
103 self
.mapping
.stop_all_running()
105 def volume(self
, music
=None, value
=100, fade
=0, delta
=False, **kwargs
):
106 if music
is not None:
107 music
.set_volume(value
, delta
=delta
, fade
=fade
)
109 self
.mapping
.set_master_volume(value
, delta
=delta
)
111 def wait(self
, duration
=0, music
=None, **kwargs
):
112 self
.sleep_event
= threading
.Event()
114 if music
is not None:
117 threading
.Timer(duration
, self
.sleep_event
.set).start()
118 self
.sleep_event
.wait()
121 def command_print(self
, command
="", **kwargs
):
122 return "running command {}".format(command
)
124 def pause_print(self
, music
=None, **kwargs
):
125 if music
is not None:
126 return "pausing « {} »".format(music
.name
)
128 return "pausing all musics"
130 def unpause_print(self
, music
=None, **kwargs
):
131 if music
is not None:
132 return "unpausing « {} »".format(music
.name
)
134 return "unpausing all musics"
136 def play_print(self
, music
=None, fade_in
=0, start_at
=0,
137 restart_if_running
=False, volume
=100, loop
=0, **kwargs
):
138 message
= "starting "
139 if music
is not None:
140 message
+= "« {} »".format(music
.name
)
142 message
+= "all musics"
145 message
+= " at {}s".format(start_at
)
148 message
+= " with {}s fade_in".format(fade_in
)
150 message
+= " at volume {}%".format(volume
)
153 message
+= " {} times".format(loop
+ 1)
155 message
+= " in loop"
157 if restart_if_running
:
158 message
+= " (restarting if already running)"
162 def stop_print(self
, music
=None, fade_out
=0, wait
=False, **kwargs
):
163 message
= "stopping "
164 if music
is not None:
165 message
+= "music « {} »".format(music
.name
)
167 message
+= "all musics"
170 message
+= " with {}s fadeout".format(fade_out
)
172 message
+= " (waiting the end of fadeout)"
176 def stop_all_actions_print(self
, **kwargs
):
177 return "stopping all actions"
179 def seek_print(self
, music
=None, value
=0, delta
=False, **kwargs
):
181 if music
is not None:
182 return "moving music « {} » by {:+d}s" \
183 .format(music
.name
, value
)
185 return "moving all musics by {:+d}s" \
188 if music
is not None:
189 return "moving music « {} » to position {}s" \
190 .format(music
.name
, value
)
192 return "moving all musics to position {}s" \
195 def volume_print(self
, music
=None,
196 value
=100, delta
=False, fade
=0, **kwargs
):
199 if music
is not None:
200 message
+= "{:+d}% to volume of « {} »" \
201 .format(value
, music
.name
)
203 message
+= "{:+d}% to volume" \
206 if music
is not None:
207 message
+= "setting volume of « {} » to {}%" \
208 .format(music
.name
, value
)
210 message
+= "setting volume to {}%" \
213 if music
is not None and fade
> 0:
214 message
+= " with {}s fade".format(fade
)
218 def wait_print(self
, duration
=0, music
=None, **kwargs
):
220 return "waiting {}s" \
223 return "waiting the end of « {} »" \
226 return "waiting the end of « {} » + {}s" \
227 .format(music
.name
, duration
)
231 def wait_interrupt(self
, duration
=0, music
=None, **kwargs
):
232 if self
.sleep_event
is not None:
233 self
.sleep_event
.set()
234 if music
is not None:
235 music
.wait_event
.set()