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