]>
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 music
.stop(fade_out
=fade_out
)
101 if previous
is not None:
102 previous
.stop(fade_out
=fade_out
, wait
=wait
)
104 def stop_all_actions(self
, **kwargs
):
105 self
.mapping
.stop_all_running()
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
)
111 self
.mapping
.set_master_volume(value
, delta
=delta
, fade
=fade
)
113 def wait(self
, duration
=0, music
=None, **kwargs
):
114 self
.sleep_event
= threading
.Event()
116 if music
is not None:
119 threading
.Timer(duration
, self
.sleep_event
.set).start()
120 self
.sleep_event
.wait()
123 def command_print(self
, command
="", **kwargs
):
124 return "running command {}".format(command
)
126 def pause_print(self
, music
=None, **kwargs
):
127 if music
is not None:
128 return "pausing « {} »".format(music
.name
)
130 return "pausing all musics"
132 def unpause_print(self
, music
=None, **kwargs
):
133 if music
is not None:
134 return "unpausing « {} »".format(music
.name
)
136 return "unpausing all musics"
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
)
144 message
+= "all musics"
147 message
+= " at {}s".format(start_at
)
150 message
+= " with {}s fade_in".format(fade_in
)
152 message
+= " at volume {}%".format(volume
)
155 message
+= " {} times".format(loop
+ 1)
157 message
+= " in loop"
159 if restart_if_running
:
160 message
+= " (restarting if already running)"
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
)
169 message
+= "all musics"
172 message
+= " with {}s fadeout".format(fade_out
)
174 message
+= " (waiting the end of fadeout)"
178 def stop_all_actions_print(self
, **kwargs
):
179 return "stopping all actions"
181 def seek_print(self
, music
=None, value
=0, delta
=False, **kwargs
):
183 if music
is not None:
184 return "moving music « {} » by {:+d}s" \
185 .format(music
.name
, value
)
187 return "moving all musics by {:+d}s" \
190 if music
is not None:
191 return "moving music « {} » to position {}s" \
192 .format(music
.name
, value
)
194 return "moving all musics to position {}s" \
197 def volume_print(self
, music
=None,
198 value
=100, delta
=False, fade
=0, **kwargs
):
201 if music
is not None:
202 message
+= "{:+d}% to volume of « {} »" \
203 .format(value
, music
.name
)
205 message
+= "{:+d}% to volume" \
208 if music
is not None:
209 message
+= "setting volume of « {} » to {}%" \
210 .format(music
.name
, value
)
212 message
+= "setting volume to {}%" \
216 message
+= " with {}s fade".format(fade
)
220 def wait_print(self
, duration
=0, music
=None, **kwargs
):
222 return "waiting {}s" \
225 return "waiting the end of « {} »" \
228 return "waiting the end of « {} » + {}s" \
229 .format(music
.name
, duration
)
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()