]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/action.py
Make 'wait' action interruptible
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
CommitLineData
0deb82a5 1import threading
be27763f
IB
2import time
3
4class Action:
5 action_types = [
6 'command',
7 'pause',
8 'play',
9 'stop',
10 'stop_all_actions',
9de92b6d 11 'unpause',
be27763f
IB
12 'volume',
13 'wait',
14 ]
15
16 def __init__(self, action, key, **kwargs):
17 if action in self.action_types:
18 self.action = action
19 else:
20 raise Exception("Unknown action {}".format(action))
21
22 self.key = key
23 self.arguments = kwargs
0deb82a5 24 self.sleep_event = None
be27763f
IB
25
26 def ready(self):
27 if 'music' in self.arguments:
60979de4 28 return self.arguments['music'].check_is_loaded()
be27763f
IB
29 else:
30 return True
31
32 def run(self):
ba9ea93a 33 print(self.description())
9de92b6d 34 getattr(self, self.action)(**self.arguments)
be27763f 35
ba9ea93a
IB
36 def description(self):
37 return getattr(self, self.action + "_print")(**self.arguments)
38
0deb82a5
IB
39 def interrupt(self):
40 if getattr(self, self.action + "_interrupt", None):
41 return getattr(self, self.action + "_interrupt")(**self.arguments)
42
43 # Actions
be27763f 44 def command(self, command = "", **kwargs):
0deb82a5 45 # FIXME: todo
be27763f
IB
46 pass
47
29597680 48 def music_list(self, music):
be27763f 49 if music is not None:
29597680 50 return [music]
be27763f 51 else:
29597680
IB
52 return self.key.parent.open_files.values()
53
54 def pause(self, music = None, **kwargs):
55 for music in self.music_list(music):
56 if music.is_loaded_playing():
57 music.pause()
be27763f 58
9de92b6d 59 def unpause(self, music = None, **kwargs):
29597680
IB
60 for music in self.music_list(music):
61 if music.is_loaded_paused():
62 music.unpause()
9de92b6d 63
be27763f
IB
64 def play(self, music = None, fade_in = 0, start_at = 0,
65 restart_if_running = False, volume = 100, **kwargs):
66 if music is not None:
0e5d59f7 67 if restart_if_running:
29597680 68 if music.is_not_stopped():
0e5d59f7
IB
69 music.stop()
70 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
71 else:
29597680 72 if not music.is_not_stopped():
0e5d59f7 73 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
be27763f
IB
74
75 def stop(self, music = None, fade_out = 0, **kwargs):
29597680
IB
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)
be27763f
IB
79
80 def stop_all_actions(self, **kwargs):
8612c5f8 81 self.key.parent.stop_all_running()
be27763f
IB
82
83 def volume(self, music = None, value = 100, **kwargs):
0e5d59f7
IB
84 if music is not None:
85 music.set_volume(value)
86 else:
29597680 87 # FIXME: todo
0e5d59f7 88 pass
be27763f 89
b86db9f1 90 def wait(self, duration = 0, music = None, **kwargs):
0deb82a5
IB
91 self.sleep_event = threading.Event()
92
93 if music is not None:
b86db9f1 94 music.wait_end()
be27763f 95
0deb82a5
IB
96 threading.Timer(duration, self.sleep_event.set).start()
97 self.sleep_event.wait()
98
99 # Action messages
be27763f
IB
100 def command_print(self, command = "", **kwargs):
101 return "running command {}".format(command)
102
103 def pause_print(self, music = None, **kwargs):
104 if music is not None:
9de92b6d 105 return "pausing « {} »".format(music.name)
be27763f
IB
106 else:
107 return "pausing all musics"
108
9de92b6d
IB
109 def unpause_print(self, music = None, **kwargs):
110 if music is not None:
111 return "unpausing « {} »".format(music.name)
112 else:
113 return "unpausing all musics"
114
be27763f
IB
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:
9de92b6d 119 message += "« {} »".format(music.name)
be27763f
IB
120 else:
121 message += "music"
122
123 if start_at != 0:
124 message += " at {}s".format(start_at)
125
126 if fade_in != 0:
127 message += " with {}s fade_in".format(fade_in)
128
129 message += " at volume {}%".format(volume)
130
131 if restart_if_running:
132 message += " (restarting if already running)"
133
134 return message
135
136 def stop_print(self, music = None, fade_out = 0, **kwargs):
137 if music is not None:
138 if fade_out == 0:
9de92b6d 139 return "stopping music « {} »".format(music.name)
be27763f 140 else:
9de92b6d 141 return "stopping music « {} » with {}s fadeout".format(music.name, fade_out)
be27763f
IB
142 else:
143 if fade_out == 0:
144 return "stopping all musics"
145 else:
146 return "stopping all musics with {}s fadeout".format(fade_out)
147
0e5d59f7 148 def stop_all_actions_print(self, **kwargs):
be27763f
IB
149 return "stopping all actions"
150
0e5d59f7 151 def volume_print(self, music = None, value = 100, **kwargs):
be27763f 152 if music is not None:
9de92b6d 153 return "setting volume of « {} » to {}%".format(music.name, value)
be27763f
IB
154 else:
155 return "setting volume to {}%".format(value)
156
0deb82a5
IB
157 def wait_print(self, duration = 0, music = None, **kwargs):
158 if music is None:
159 return "waiting {}s".format(duration)
160 elif duration == 0:
161 return "waiting the end of « {} »".format(music.name)
162 else:
163 return "waiting the end of « {} » + {}s".format(music.name, duration)
be27763f
IB
164
165
0deb82a5
IB
166 # Interruptions
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()
172