]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
28afceeb6e66c3cc193428e88c28e06959cca29d
16 def __init__(self
, action
, key
, **kwargs
):
17 if action
in self
.action_types
:
20 raise Exception("Unknown action {}".format(action
))
23 self
.mapping
= key
.parent
24 self
.arguments
= kwargs
25 self
.sleep_event
= None
28 if 'music' in self
.arguments
:
29 return self
.arguments
['music'].check_is_loaded()
34 print(self
.description())
35 getattr(self
, self
.action
)(**self
.arguments
)
37 def description(self
):
38 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
41 if getattr(self
, self
.action
+ "_interrupt", None):
42 return getattr(self
, self
.action
+ "_interrupt")(**self
.arguments
)
45 def command(self
, command
= "", **kwargs
):
49 def music_list(self
, music
):
53 return self
.mapping
.open_files
.values()
55 def pause(self
, music
= None, **kwargs
):
56 for music
in self
.music_list(music
):
57 if music
.is_loaded_playing():
60 def unpause(self
, music
= None, **kwargs
):
61 for music
in self
.music_list(music
):
62 if music
.is_loaded_paused():
65 def play(self
, music
= None, fade_in
= 0, start_at
= 0,
66 restart_if_running
= False, volume
= 100, **kwargs
):
68 if restart_if_running
:
69 if music
.is_not_stopped():
71 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
73 if not music
.is_not_stopped():
74 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
76 def stop(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
78 for music
in self
.music_list(music
):
79 if music
.is_loaded_paused() or music
.is_loaded_playing():
80 if previous
is not None:
81 previous
.stop(fade_out
= fade_out
)
84 if previous
is not None:
85 previous
.stop(fade_out
= fade_out
, wait
= wait
)
87 def stop_all_actions(self
, **kwargs
):
88 self
.mapping
.stop_all_running()
90 def volume(self
, music
= None, value
= 100, add
= False, **kwargs
):
92 music
.set_volume(value
, add
= add
)
94 self
.mapping
.set_master_volume(value
, add
= add
)
96 def wait(self
, duration
= 0, music
= None, **kwargs
):
97 self
.sleep_event
= threading
.Event()
102 threading
.Timer(duration
, self
.sleep_event
.set).start()
103 self
.sleep_event
.wait()
106 def command_print(self
, command
= "", **kwargs
):
107 return "running command {}".format(command
)
109 def pause_print(self
, music
= None, **kwargs
):
110 if music
is not None:
111 return "pausing « {} »".format(music
.name
)
113 return "pausing all musics"
115 def unpause_print(self
, music
= None, **kwargs
):
116 if music
is not None:
117 return "unpausing « {} »".format(music
.name
)
119 return "unpausing all musics"
121 def play_print(self
, music
= None, fade_in
= 0, start_at
= 0,
122 restart_if_running
= False, volume
= 100, **kwargs
):
123 message
= "starting "
124 if music
is not None:
125 message
+= "« {} »".format(music
.name
)
130 message
+= " at {}s".format(start_at
)
133 message
+= " with {}s fade_in".format(fade_in
)
135 message
+= " at volume {}%".format(volume
)
137 if restart_if_running
:
138 message
+= " (restarting if already running)"
142 def stop_print(self
, music
= None, fade_out
= 0, wait
= False, **kwargs
):
143 message
= "stopping "
144 if music
is not None:
145 message
+= "music « {} »".format(music
.name
)
147 message
+= "all musics"
150 message
+= " with {}s fadeout".format(fade_out
)
152 message
+= " (waiting the end of fadeout)"
156 def stop_all_actions_print(self
, **kwargs
):
157 return "stopping all actions"
159 def volume_print(self
, music
= None, value
= 100, add
= False, **kwargs
):
161 if music
is not None:
162 return "{:+d}% to volume of « {} »".format(value
, music
.name
)
164 return "{:+d}% to volume".format(value
)
166 if music
is not None:
167 return "setting volume of « {} » to {}%".format(music
.name
, value
)
169 return "setting volume to {}%".format(value
)
171 def wait_print(self
, duration
= 0, music
= None, **kwargs
):
173 return "waiting {}s".format(duration
)
175 return "waiting the end of « {} »".format(music
.name
)
177 return "waiting the end of « {} » + {}s".format(music
.name
, duration
)
181 def wait_interrupt(self
, duration
= 0, music
= None, **kwargs
):
182 if self
.sleep_event
is not None:
183 self
.sleep_event
.set()
184 if music
is not None:
185 music
.wait_event
.set()