]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/action.py
327d42fe3a2f3e6df02617630922d980e4cfc476
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
)
35 def description(self
):
36 return getattr(self
, self
.action
+ "_print")(**self
.arguments
)
38 def command(self
, command
= "", **kwargs
):
41 def pause(self
, music
= None, **kwargs
):
45 for music
in self
.key
.parent
.open_files
.values():
46 if music
.is_playing() and not music
.is_paused():
49 def unpause(self
, music
= None, **kwargs
):
53 for music
in self
.key
.parent
.open_files
.values():
54 if music
.is_playing() and music
.is_paused():
57 def play(self
, music
= None, fade_in
= 0, start_at
= 0,
58 restart_if_running
= False, volume
= 100, **kwargs
):
60 if restart_if_running
:
61 if music
.is_playing():
63 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
65 if not music
.is_playing():
66 music
.play(volume
= volume
, fade_in
= fade_in
, start_at
= start_at
)
68 pygame
.mixer
.unpause()
70 def stop(self
, music
= None, fade_out
= 0, **kwargs
):
72 music
.stop(fade_out
= fade_out
)
75 pygame
.mixer
.fadeout(int(fade_out
* 1000))
79 def stop_all_actions(self
, **kwargs
):
80 self
.key
.parent
.stop_all_running()
82 def volume(self
, music
= None, value
= 100, **kwargs
):
84 music
.set_volume(value
)
88 def wait(self
, duration
= 0, music
= None, **kwargs
):
89 # FIXME: Make it stoppable
90 # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
97 def command_print(self
, command
= "", **kwargs
):
98 return "running command {}".format(command
)
100 def pause_print(self
, music
= None, **kwargs
):
101 if music
is not None:
102 return "pausing « {} »".format(music
.name
)
104 return "pausing all musics"
106 def unpause_print(self
, music
= None, **kwargs
):
107 if music
is not None:
108 return "unpausing « {} »".format(music
.name
)
110 return "unpausing all musics"
112 def play_print(self
, music
= None, fade_in
= 0, start_at
= 0,
113 restart_if_running
= False, volume
= 100, **kwargs
):
114 message
= "starting "
115 if music
is not None:
116 message
+= "« {} »".format(music
.name
)
121 message
+= " at {}s".format(start_at
)
124 message
+= " with {}s fade_in".format(fade_in
)
126 message
+= " at volume {}%".format(volume
)
128 if restart_if_running
:
129 message
+= " (restarting if already running)"
133 def stop_print(self
, music
= None, fade_out
= 0, **kwargs
):
134 if music
is not None:
136 return "stopping music « {} »".format(music
.name
)
138 return "stopping music « {} » with {}s fadeout".format(music
.name
, fade_out
)
141 return "stopping all musics"
143 return "stopping all musics with {}s fadeout".format(fade_out
)
145 def stop_all_actions_print(self
, **kwargs
):
146 return "stopping all actions"
148 def volume_print(self
, music
= None, value
= 100, **kwargs
):
149 if music
is not None:
150 return "setting volume of « {} » to {}%".format(music
.name
, value
)
152 return "setting volume to {}%".format(value
)
154 def wait_print(self
, duration
, **kwargs
):
155 return "waiting {}s".format(duration
)