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