]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/action.py
Fix master volume fade
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
CommitLineData
0deb82a5 1import threading
be27763f
IB
2import time
3
a24c34bc
IB
4from . import debug_print
5
be27763f
IB
6class Action:
7 action_types = [
8 'command',
9 'pause',
10 'play',
52d58baf 11 'seek',
be27763f
IB
12 'stop',
13 'stop_all_actions',
9de92b6d 14 'unpause',
be27763f
IB
15 'volume',
16 'wait',
17 ]
18
19 def __init__(self, action, key, **kwargs):
20 if action in self.action_types:
21 self.action = action
22 else:
23 raise Exception("Unknown action {}".format(action))
24
25 self.key = key
1b4b78f5 26 self.mapping = key.parent
be27763f 27 self.arguments = kwargs
0deb82a5 28 self.sleep_event = None
be27763f
IB
29
30 def ready(self):
31 if 'music' in self.arguments:
60979de4 32 return self.arguments['music'].check_is_loaded()
be27763f
IB
33 else:
34 return True
35
36 def run(self):
a24c34bc 37 debug_print(self.description())
9de92b6d 38 getattr(self, self.action)(**self.arguments)
be27763f 39
ba9ea93a
IB
40 def description(self):
41 return getattr(self, self.action + "_print")(**self.arguments)
42
0deb82a5
IB
43 def interrupt(self):
44 if getattr(self, self.action + "_interrupt", None):
45 return getattr(self, self.action + "_interrupt")(**self.arguments)
46
29597680 47 def music_list(self, music):
be27763f 48 if music is not None:
29597680 49 return [music]
be27763f 50 else:
1b4b78f5 51 return self.mapping.open_files.values()
29597680 52
c80ff6dc 53 # Actions
2e404903 54 def command(self, command="", **kwargs):
c80ff6dc
IB
55 # FIXME: todo
56 pass
57
2e404903 58 def pause(self, music=None, **kwargs):
29597680
IB
59 for music in self.music_list(music):
60 if music.is_loaded_playing():
61 music.pause()
be27763f 62
2e404903 63 def unpause(self, music=None, **kwargs):
29597680
IB
64 for music in self.music_list(music):
65 if music.is_loaded_paused():
66 music.unpause()
9de92b6d 67
2e404903
IB
68 def play(self, music=None, fade_in=0, start_at=0,
69 restart_if_running=False, volume=100,
70 loop=0, **kwargs):
c80ff6dc 71 for music in self.music_list(music):
0e5d59f7 72 if restart_if_running:
29597680 73 if music.is_not_stopped():
0e5d59f7 74 music.stop()
2e404903
IB
75 music.play(
76 volume=volume,
77 fade_in=fade_in,
78 start_at=start_at,
79 loop=loop)
80 elif not music.is_not_stopped():
81 music.play(
82 volume=volume,
83 fade_in=fade_in,
84 start_at=start_at,
85 loop=loop)
86
87 def seek(self, music=None, value=0, delta=False, **kwargs):
52d58baf 88 for music in self.music_list(music):
2e404903 89 music.seek(value=value, delta=delta)
52d58baf 90
2e404903 91 def stop(self, music=None, fade_out=0, wait=False, **kwargs):
1b4b78f5 92 previous = None
29597680
IB
93 for music in self.music_list(music):
94 if music.is_loaded_paused() or music.is_loaded_playing():
1b4b78f5 95 if previous is not None:
2e404903 96 previous.stop(fade_out=fade_out)
1b4b78f5 97 previous = music
51322669
IB
98 else:
99 music.stop(fade_out=fade_out)
1b4b78f5
IB
100
101 if previous is not None:
2e404903 102 previous.stop(fade_out=fade_out, wait=wait)
be27763f
IB
103
104 def stop_all_actions(self, **kwargs):
1b4b78f5 105 self.mapping.stop_all_running()
be27763f 106
aee1334c 107 def volume(self, music=None, value=100, fade=0, delta=False, **kwargs):
0e5d59f7 108 if music is not None:
aee1334c 109 music.set_volume(value, delta=delta, fade=fade)
0e5d59f7 110 else:
a8340c5d 111 self.mapping.set_master_volume(value, delta=delta, fade=fade)
be27763f 112
2e404903 113 def wait(self, duration=0, music=None, **kwargs):
0deb82a5
IB
114 self.sleep_event = threading.Event()
115
116 if music is not None:
b86db9f1 117 music.wait_end()
be27763f 118
0deb82a5
IB
119 threading.Timer(duration, self.sleep_event.set).start()
120 self.sleep_event.wait()
121
122 # Action messages
2e404903 123 def command_print(self, command="", **kwargs):
be27763f
IB
124 return "running command {}".format(command)
125
2e404903 126 def pause_print(self, music=None, **kwargs):
be27763f 127 if music is not None:
9de92b6d 128 return "pausing « {} »".format(music.name)
be27763f
IB
129 else:
130 return "pausing all musics"
131
2e404903 132 def unpause_print(self, music=None, **kwargs):
9de92b6d
IB
133 if music is not None:
134 return "unpausing « {} »".format(music.name)
135 else:
136 return "unpausing all musics"
137
2e404903
IB
138 def play_print(self, music=None, fade_in=0, start_at=0,
139 restart_if_running=False, volume=100, loop=0, **kwargs):
be27763f
IB
140 message = "starting "
141 if music is not None:
9de92b6d 142 message += "« {} »".format(music.name)
be27763f 143 else:
c80ff6dc 144 message += "all musics"
be27763f
IB
145
146 if start_at != 0:
147 message += " at {}s".format(start_at)
148
149 if fade_in != 0:
150 message += " with {}s fade_in".format(fade_in)
151
152 message += " at volume {}%".format(volume)
153
6f4944c1
IB
154 if loop > 0:
155 message += " {} times".format(loop + 1)
156 elif loop < 0:
157 message += " in loop"
158
be27763f
IB
159 if restart_if_running:
160 message += " (restarting if already running)"
161
162 return message
163
2e404903 164 def stop_print(self, music=None, fade_out=0, wait=False, **kwargs):
1b4b78f5 165 message = "stopping "
be27763f 166 if music is not None:
1b4b78f5 167 message += "music « {} »".format(music.name)
be27763f 168 else:
1b4b78f5
IB
169 message += "all musics"
170
171 if fade_out > 0:
172 message += " with {}s fadeout".format(fade_out)
173 if wait:
174 message += " (waiting the end of fadeout)"
175
176 return message
be27763f 177
0e5d59f7 178 def stop_all_actions_print(self, **kwargs):
be27763f
IB
179 return "stopping all actions"
180
2e404903 181 def seek_print(self, music=None, value=0, delta=False, **kwargs):
52d58baf
IB
182 if delta:
183 if music is not None:
2e404903
IB
184 return "moving music « {} » by {:+d}s" \
185 .format(music.name, value)
52d58baf 186 else:
2e404903
IB
187 return "moving all musics by {:+d}s" \
188 .format(value)
52d58baf
IB
189 else:
190 if music is not None:
2e404903
IB
191 return "moving music « {} » to position {}s" \
192 .format(music.name, value)
52d58baf 193 else:
2e404903
IB
194 return "moving all musics to position {}s" \
195 .format(value)
52d58baf 196
aee1334c
IB
197 def volume_print(self, music=None,
198 value=100, delta=False, fade=0, **kwargs):
199 message = ""
8e50011c 200 if delta:
1b4b78f5 201 if music is not None:
aee1334c 202 message += "{:+d}% to volume of « {} »" \
2e404903 203 .format(value, music.name)
1b4b78f5 204 else:
aee1334c 205 message += "{:+d}% to volume" \
2e404903 206 .format(value)
be27763f 207 else:
1b4b78f5 208 if music is not None:
aee1334c 209 message += "setting volume of « {} » to {}%" \
2e404903 210 .format(music.name, value)
1b4b78f5 211 else:
aee1334c 212 message += "setting volume to {}%" \
2e404903 213 .format(value)
be27763f 214
a8340c5d 215 if fade > 0:
aee1334c
IB
216 message += " with {}s fade".format(fade)
217
218 return message
219
2e404903 220 def wait_print(self, duration=0, music=None, **kwargs):
0deb82a5 221 if music is None:
2e404903
IB
222 return "waiting {}s" \
223 .format(duration)
0deb82a5 224 elif duration == 0:
2e404903
IB
225 return "waiting the end of « {} »" \
226 .format(music.name)
0deb82a5 227 else:
2e404903
IB
228 return "waiting the end of « {} » + {}s" \
229 .format(music.name, duration)
be27763f
IB
230
231
0deb82a5 232 # Interruptions
2e404903 233 def wait_interrupt(self, duration=0, music=None, **kwargs):
0deb82a5
IB
234 if self.sleep_event is not None:
235 self.sleep_event.set()
236 if music is not None:
237 music.wait_event.set()
238