]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
16 def __init__(self
, action
, key
, **kwargs
):
17 if action
in self
.action_types
:
20 raise Exception("Unknown action {}".format(action
))
23 self
.arguments
= kwargs
26 if 'music' in self
.arguments
:
27 return self
.arguments
['music'].loaded
32 print(self
.description())
33 getattr(self
, self
.action
)(**self
.arguments
)
34 pygame
.event
.post(pygame
.event
.Event(pygame
.USEREVENT
))
36 def description(self
):
37 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
39 def command(self
, command
= "", **kwargs
):
42 def pause(self
, music
= None, **kwargs
):
48 def unpause(self
, music
= None, **kwargs
):
52 pygame
.mixer
.unpause()
54 def play(self
, music
= None, fade_in
= 0, start_at
= 0,
55 restart_if_running
= False, volume
= 100, **kwargs
):
57 if restart_if_running
:
58 if music
.is_playing():
60 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
62 if not music
.is_playing():
63 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
65 pygame
.mixer
.unpause()
67 def stop(self
, music
= None, fade_out
= 0, **kwargs
):
69 music
.stop(fade_out
= fade_out
)
72 pygame
.fadeout(int(fade_out
* 1000))
76 def stop_all_actions(self
, **kwargs
):
77 self
.key
.mapping
.stop_all_running()
79 def volume(self
, music
= None, value
= 100, **kwargs
):
81 music
.set_volume(value
)
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
94 def command_print(self
, command
= "", **kwargs
):
95 return "running command {}".format(command
)
97 def pause_print(self
, music
= None, **kwargs
):
99 return "pausing « {} »".format(music
.name
)
101 return "pausing all musics"
103 def unpause_print(self
, music
= None, **kwargs
):
104 if music
is not None:
105 return "unpausing « {} »".format(music
.name
)
107 return "unpausing all musics"
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
)
118 message
+= " at {}s".format(start_at
)
121 message
+= " with {}s fade_in".format(fade_in
)
123 message
+= " at volume {}%".format(volume
)
125 if restart_if_running
:
126 message
+= " (restarting if already running)"
130 def stop_print(self
, music
= None, fade_out
= 0, **kwargs
):
131 if music
is not None:
133 return "stopping music « {} »".format(music
.name
)
135 return "stopping music « {} » with {}s fadeout".format(music
.name
, fade_out
)
138 return "stopping all musics"
140 return "stopping all musics with {}s fadeout".format(fade_out
)
142 def stop_all_actions_print(self
, **kwargs
):
143 return "stopping all actions"
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
)
149 return "setting volume to {}%".format(value
)
151 def wait_print(self
, duration
, **kwargs
):
152 return "waiting {}s".format(duration
)