]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
17 def __init__(self
, action
, key
, **kwargs
):
18 if action
in self
.action_types
:
21 raise Exception("Unknown action {}".format(action
))
24 self
.mapping
= key
.parent
25 self
.arguments
= kwargs
26 self
.sleep_event
= None
29 if 'music' in self
.arguments
:
30 return self
.arguments
['music'].check_is_loaded()
35 print(self
.description())
36 getattr(self
, self
.action
)(**self
.arguments
)
38 def description(self
):
39 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
42 if getattr(self
, self
.action
+ "_interrupt", None):
43 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
45 def music_list(self
, music
):
49 return self
.mapping
.open_files
.values()
52 def command(self
, command
= "", **kwargs
):
56 def pause(self
, music
= None, **kwargs
):
57 for music
in self
.music_list(music
):
58 if music
.is_loaded_playing():
61 def unpause(self
, music
= None, **kwargs
):
62 for music
in self
.music_list(music
):
63 if music
.is_loaded_paused():
66 def play(self
, music
= None, fade_in
= 0, start_at
= 0,
67 restart_if_running
= False, volume
= 100,
70 for music
in self
.music_list(music
):
71 if restart_if_running
:
72 if music
.is_not_stopped():
74 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
, loop
= loop
)
76 if not music
.is_not_stopped():
77 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
, loop
= loop
)
79 def seek(self
, music
= None, value
= 0, delta
= False, **kwargs
):
80 for music
in self
.music_list(music
):
81 music
.seek(value
= value
, delta
= delta
)
83 def stop(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
85 for music
in self
.music_list(music
):
86 if music
.is_loaded_paused() or music
.is_loaded_playing():
87 if previous
is not None:
88 previous
.stop(fade_out
= fade_out
)
91 if previous
is not None:
92 previous
.stop(fade_out
= fade_out
, wait
= wait
)
94 def stop_all_actions(self
, **kwargs
):
95 self
.mapping
.stop_all_running()
97 def volume(self
, music
= None, value
= 100, delta
= False, **kwargs
):
99 music
.set_volume(value
, delta
= delta
)
101 self
.mapping
.set_master_volume(value
, delta
= delta
)
103 def wait(self
, duration
= 0, music
= None, **kwargs
):
104 self
.sleep_event
= threading
.Event()
106 if music
is not None:
109 threading
.Timer(duration
, self
.sleep_event
.set).start()
110 self
.sleep_event
.wait()
113 def command_print(self
, command
= "", **kwargs
):
114 return "running command {}".format(command
)
116 def pause_print(self
, music
= None, **kwargs
):
117 if music
is not None:
118 return "pausing « {} »".format(music
.name
)
120 return "pausing all musics"
122 def unpause_print(self
, music
= None, **kwargs
):
123 if music
is not None:
124 return "unpausing « {} »".format(music
.name
)
126 return "unpausing all musics"
128 def play_print(self
, music
= None, fade_in
= 0, start_at
= 0,
129 restart_if_running
= False, volume
= 100, loop
= 0, **kwargs
):
130 message
= "starting "
131 if music
is not None:
132 message
+= "« {} »".format(music
.name
)
134 message
+= "all musics"
137 message
+= " at {}s".format(start_at
)
140 message
+= " with {}s fade_in".format(fade_in
)
142 message
+= " at volume {}%".format(volume
)
145 message
+= " {} times".format(loop
+ 1)
147 message
+= " in loop"
149 if restart_if_running
:
150 message
+= " (restarting if already running)"
154 def stop_print(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
155 message
= "stopping "
156 if music
is not None:
157 message
+= "music « {} »".format(music
.name
)
159 message
+= "all musics"
162 message
+= " with {}s fadeout".format(fade_out
)
164 message
+= " (waiting the end of fadeout)"
168 def stop_all_actions_print(self
, **kwargs
):
169 return "stopping all actions"
171 def seek_print(self
, music
= None, value
= 0, delta
= False, **kwargs
):
173 if music
is not None:
174 return "moving music « {} » by {:+d}s".format(music
.name
, value
)
176 return "moving all musics by {:+d}s".format(value
)
178 if music
is not None:
179 return "moving music « {} » to position {}s".format(music
.name
, value
)
181 return "moving all musics to position {}s".format(value
)
183 def volume_print(self
, music
= None, value
= 100, delta
= False, **kwargs
):
185 if music
is not None:
186 return "{:+d}% to volume of « {} »".format(value
, music
.name
)
188 return "{:+d}% to volume".format(value
)
190 if music
is not None:
191 return "setting volume of « {} » to {}%".format(music
.name
, value
)
193 return "setting volume to {}%".format(value
)
195 def wait_print(self
, duration
= 0, music
= None, **kwargs
):
197 return "waiting {}s".format(duration
)
199 return "waiting the end of « {} »".format(music
.name
)
201 return "waiting the end of « {} » + {}s".format(music
.name
, duration
)
205 def wait_interrupt(self
, duration
= 0, music
= None, **kwargs
):
206 if self
.sleep_event
is not None:
207 self
.sleep_event
.set()
208 if music
is not None:
209 music
.wait_event
.set()