]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
1a2abe203567a496802824c68c1ae3113b57eb47
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,
72 for music
in self
.music_list(music
):
73 if restart_if_running
:
74 if music
.is_not_stopped():
76 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
, loop
= loop
)
78 if not music
.is_not_stopped():
79 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
, loop
= loop
)
81 def seek(self
, music
= None, value
= 0, delta
= False, **kwargs
):
82 for music
in self
.music_list(music
):
83 music
.seek(value
= value
, delta
= delta
)
85 def stop(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
87 for music
in self
.music_list(music
):
88 if music
.is_loaded_paused() or music
.is_loaded_playing():
89 if previous
is not None:
90 previous
.stop(fade_out
= fade_out
)
93 if previous
is not None:
94 previous
.stop(fade_out
= fade_out
, wait
= wait
)
96 def stop_all_actions(self
, **kwargs
):
97 self
.mapping
.stop_all_running()
99 def volume(self
, music
= None, value
= 100, delta
= False, **kwargs
):
100 if music
is not None:
101 music
.set_volume(value
, delta
= delta
)
103 self
.mapping
.set_master_volume(value
, delta
= delta
)
105 def wait(self
, duration
= 0, music
= None, **kwargs
):
106 self
.sleep_event
= threading
.Event()
108 if music
is not None:
111 threading
.Timer(duration
, self
.sleep_event
.set).start()
112 self
.sleep_event
.wait()
115 def command_print(self
, command
= "", **kwargs
):
116 return "running command {}".format(command
)
118 def pause_print(self
, music
= None, **kwargs
):
119 if music
is not None:
120 return "pausing « {} »".format(music
.name
)
122 return "pausing all musics"
124 def unpause_print(self
, music
= None, **kwargs
):
125 if music
is not None:
126 return "unpausing « {} »".format(music
.name
)
128 return "unpausing all musics"
130 def play_print(self
, music
= None, fade_in
= 0, start_at
= 0,
131 restart_if_running
= False, volume
= 100, loop
= 0, **kwargs
):
132 message
= "starting "
133 if music
is not None:
134 message
+= "« {} »".format(music
.name
)
136 message
+= "all musics"
139 message
+= " at {}s".format(start_at
)
142 message
+= " with {}s fade_in".format(fade_in
)
144 message
+= " at volume {}%".format(volume
)
147 message
+= " {} times".format(loop
+ 1)
149 message
+= " in loop"
151 if restart_if_running
:
152 message
+= " (restarting if already running)"
156 def stop_print(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
157 message
= "stopping "
158 if music
is not None:
159 message
+= "music « {} »".format(music
.name
)
161 message
+= "all musics"
164 message
+= " with {}s fadeout".format(fade_out
)
166 message
+= " (waiting the end of fadeout)"
170 def stop_all_actions_print(self
, **kwargs
):
171 return "stopping all actions"
173 def seek_print(self
, music
= None, value
= 0, delta
= False, **kwargs
):
175 if music
is not None:
176 return "moving music « {} » by {:+d}s".format(music
.name
, value
)
178 return "moving all musics by {:+d}s".format(value
)
180 if music
is not None:
181 return "moving music « {} » to position {}s".format(music
.name
, value
)
183 return "moving all musics to position {}s".format(value
)
185 def volume_print(self
, music
= None, value
= 100, delta
= False, **kwargs
):
187 if music
is not None:
188 return "{:+d}% to volume of « {} »".format(value
, music
.name
)
190 return "{:+d}% to volume".format(value
)
192 if music
is not None:
193 return "setting volume of « {} » to {}%".format(music
.name
, value
)
195 return "setting volume to {}%".format(value
)
197 def wait_print(self
, duration
= 0, music
= None, **kwargs
):
199 return "waiting {}s".format(duration
)
201 return "waiting the end of « {} »".format(music
.name
)
203 return "waiting the end of « {} » + {}s".format(music
.name
, duration
)
207 def wait_interrupt(self
, duration
= 0, music
= None, **kwargs
):
208 if self
.sleep_event
is not None:
209 self
.sleep_event
.set()
210 if music
is not None:
211 music
.wait_event
.set()