]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
Added music name, currently playing musics, pause/unpause
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / action.py
1 import pygame
2 import time
3
4 class Action:
5 action_types = [
6 'command',
7 'pause',
8 'play',
9 'stop',
10 'stop_all_actions',
11 'unpause',
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
24
25 def ready(self):
26 if 'music' in self.arguments:
27 return self.arguments['music'].loaded
28 else:
29 return True
30
31 def run(self):
32 print(self.description())
33 getattr(self, self.action)(**self.arguments)
34 pygame.event.post(pygame.event.Event(pygame.USEREVENT))
35
36 def description(self):
37 return getattr(self, self.action + "_print")(**self.arguments)
38
39 def command(self, command = "", **kwargs):
40 pass
41
42 def pause(self, music = None, **kwargs):
43 if music is not None:
44 music.pause()
45 else:
46 pygame.mixer.pause()
47
48 def unpause(self, music = None, **kwargs):
49 if music is not None:
50 music.unpause()
51 else:
52 pygame.mixer.unpause()
53
54 def play(self, music = None, fade_in = 0, start_at = 0,
55 restart_if_running = False, volume = 100, **kwargs):
56 if music is not None:
57 if restart_if_running:
58 if music.is_playing():
59 music.stop()
60 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
61 else:
62 if not music.is_playing():
63 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
64 else:
65 pygame.mixer.unpause()
66
67 def stop(self, music = None, fade_out = 0, **kwargs):
68 if music is not None:
69 music.stop(fade_out = fade_out)
70 else:
71 if fade_out > 0:
72 pygame.fadeout(fade_out * 1000)
73 else:
74 pygame.mixer.stop()
75
76 def stop_all_actions(self, **kwargs):
77 self.key.mapping.stop_all_running()
78
79 def volume(self, music = None, value = 100, **kwargs):
80 if music is not None:
81 music.set_volume(value)
82 else:
83 pass
84
85 def wait(self, duration = 0, music = None, **kwargs):
86 # FIXME: Make it stoppable
87 # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
88 if music is None:
89 time.sleep(duration)
90 else:
91 # TODO
92 music.wait_end()
93
94 def command_print(self, command = "", **kwargs):
95 return "running command {}".format(command)
96
97 def pause_print(self, music = None, **kwargs):
98 if music is not None:
99 return "pausing « {} »".format(music.name)
100 else:
101 return "pausing all musics"
102
103 def unpause_print(self, music = None, **kwargs):
104 if music is not None:
105 return "unpausing « {} »".format(music.name)
106 else:
107 return "unpausing all musics"
108
109 def play_print(self, music = None, fade_in = 0, start_at = 0,
110 restart_if_running = False, volume = 100, **kwargs):
111 message = "starting "
112 if music is not None:
113 message += "« {} »".format(music.name)
114 else:
115 message += "music"
116
117 if start_at != 0:
118 message += " at {}s".format(start_at)
119
120 if fade_in != 0:
121 message += " with {}s fade_in".format(fade_in)
122
123 message += " at volume {}%".format(volume)
124
125 if restart_if_running:
126 message += " (restarting if already running)"
127
128 return message
129
130 def stop_print(self, music = None, fade_out = 0, **kwargs):
131 if music is not None:
132 if fade_out == 0:
133 return "stopping music « {} »".format(music.name)
134 else:
135 return "stopping music « {} » with {}s fadeout".format(music.name, fade_out)
136 else:
137 if fade_out == 0:
138 return "stopping all musics"
139 else:
140 return "stopping all musics with {}s fadeout".format(fade_out)
141
142 def stop_all_actions_print(self, **kwargs):
143 return "stopping all actions"
144
145 def volume_print(self, music = None, value = 100, **kwargs):
146 if music is not None:
147 return "setting volume of « {} » to {}%".format(music.name, value)
148 else:
149 return "setting volume to {}%".format(value)
150
151 def wait_print(self, duration, **kwargs):
152 return "waiting {}s".format(duration)
153
154