]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
16 def __init__(self
, action
, key
, **kwargs
):
17 if action
in self
.action_types
:
20 raise Exception("Unknown action {}".format(action
))
23 self
.arguments
= kwargs
24 self
.sleep_event
= None
27 if 'music' in self
.arguments
:
28 return self
.arguments
['music'].check_is_loaded()
33 print(self
.description())
34 getattr(self
, self
.action
)(**self
.arguments
)
36 def description(self
):
37 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
40 if getattr(self
, self
.action
+ "_interrupt", None):
41 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
44 def command(self
, command
= "", **kwargs
):
48 def music_list(self
, music
):
52 return self
.key
.parent
.open_files
.values()
54 def pause(self
, music
= None, **kwargs
):
55 for music
in self
.music_list(music
):
56 if music
.is_loaded_playing():
59 def unpause(self
, music
= None, **kwargs
):
60 for music
in self
.music_list(music
):
61 if music
.is_loaded_paused():
64 def play(self
, music
= None, fade_in
= 0, start_at
= 0,
65 restart_if_running
= False, volume
= 100, **kwargs
):
67 if restart_if_running
:
68 if music
.is_not_stopped():
70 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
72 if not music
.is_not_stopped():
73 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
75 def stop(self
, music
= None, fade_out
= 0, **kwargs
):
76 for music
in self
.music_list(music
):
77 if music
.is_loaded_paused() or music
.is_loaded_playing():
78 music
.stop(fade_out
= fade_out
)
80 def stop_all_actions(self
, **kwargs
):
81 self
.key
.parent
.stop_all_running()
83 def volume(self
, music
= None, value
= 100, **kwargs
):
85 music
.set_volume(value
)
90 def wait(self
, duration
= 0, music
= None, **kwargs
):
91 self
.sleep_event
= threading
.Event()
96 threading
.Timer(duration
, self
.sleep_event
.set).start()
97 self
.sleep_event
.wait()
100 def command_print(self
, command
= "", **kwargs
):
101 return "running command {}".format(command
)
103 def pause_print(self
, music
= None, **kwargs
):
104 if music
is not None:
105 return "pausing « {} »".format(music
.name
)
107 return "pausing all musics"
109 def unpause_print(self
, music
= None, **kwargs
):
110 if music
is not None:
111 return "unpausing « {} »".format(music
.name
)
113 return "unpausing all musics"
115 def play_print(self
, music
= None, fade_in
= 0, start_at
= 0,
116 restart_if_running
= False, volume
= 100, **kwargs
):
117 message
= "starting "
118 if music
is not None:
119 message
+= "« {} »".format(music
.name
)
124 message
+= " at {}s".format(start_at
)
127 message
+= " with {}s fade_in".format(fade_in
)
129 message
+= " at volume {}%".format(volume
)
131 if restart_if_running
:
132 message
+= " (restarting if already running)"
136 def stop_print(self
, music
= None, fade_out
= 0, **kwargs
):
137 if music
is not None:
139 return "stopping music « {} »".format(music
.name
)
141 return "stopping music « {} » with {}s fadeout".format(music
.name
, fade_out
)
144 return "stopping all musics"
146 return "stopping all musics with {}s fadeout".format(fade_out
)
148 def stop_all_actions_print(self
, **kwargs
):
149 return "stopping all actions"
151 def volume_print(self
, music
= None, value
= 100, **kwargs
):
152 if music
is not None:
153 return "setting volume of « {} » to {}%".format(music
.name
, value
)
155 return "setting volume to {}%".format(value
)
157 def wait_print(self
, duration
= 0, music
= None, **kwargs
):
159 return "waiting {}s".format(duration
)
161 return "waiting the end of « {} »".format(music
.name
)
163 return "waiting the end of « {} » + {}s".format(music
.name
, duration
)
167 def wait_interrupt(self
, duration
= 0, music
= None, **kwargs
):
168 if self
.sleep_event
is not None:
169 self
.sleep_event
.set()
170 if music
is not None:
171 music
.wait_event
.set()