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