aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-13 23:05:00 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-13 23:05:00 +0200
commit8f88a3e417d6eff7666571eccf0b6ad453c88ccd (patch)
treef67918bcb3feaa3356d45932abcd6c599db03028
parent1df30f075c186c4c3d41d5f6014817b1b6b7b1ae (diff)
downloadMusicSampler-8f88a3e417d6eff7666571eccf0b6ad453c88ccd.tar.gz
MusicSampler-8f88a3e417d6eff7666571eccf0b6ad453c88ccd.tar.zst
MusicSampler-8f88a3e417d6eff7666571eccf0b6ad453c88ccd.zip
Add actions
-rw-r--r--config.yml38
-rw-r--r--helpers.py312
-rw-r--r--run.py18
3 files changed, 251 insertions, 117 deletions
diff --git a/config.yml b/config.yml
new file mode 100644
index 0000000..1876e1e
--- /dev/null
+++ b/config.yml
@@ -0,0 +1,38 @@
1aliases:
2 jongle:
3 file: "/home/sekhmet/bla/jongle.mp3"
4 volume: 110 # pouvoir dire que "par défaut" la musique a un volume à 110%
5 acros:
6 file: "/home/sekhmet/chemin/ultra/chiant/acros.mp3"
7
8keys:
9 'bla':
10 - unknown:
11 include: "bla"
12 - play:
13 include: "coucou"
14 'a':
15 - play:
16 include: jongle
17 fade_in: 5
18 restart_if_running: False
19 'z':
20 - stop:
21 include: jongle
22 - wait:
23 time: 3
24 - play:
25 include: acros
26 - wait:
27 time: 10
28 - stop:
29 include: acros
30 fade_out: 10
31 'ESC':
32 - stop: ~
33 '-':
34 - volume:
35 value: 90
36 'p':
37 - run:
38 command: une_super_commande_de_feu_d'artifice
diff --git a/helpers.py b/helpers.py
index 76f9939..b4a0fe0 100644
--- a/helpers.py
+++ b/helpers.py
@@ -1,6 +1,30 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2from pygame import * 2from pygame import *
3from math import pi 3
4class Action:
5 action_types = [
6 'pause',
7 'play',
8 'run',
9 'stop',
10 'volume',
11 'wait',
12 ]
13
14 def __init__(self, action, **kwargs):
15 if action in self.action_types:
16 self.action = action
17 else:
18 raise Exception("Unknown action {}".format(action))
19
20 self.arguments = kwargs
21
22 def run(self, callback):
23 getattr(self, self.action)(callback, **self.arguments)
24
25 def stop(self, callback, musique = None, fade_out = 0):
26 print('stopping')
27 return callback()
4 28
5class Key: 29class Key:
6 row_positions = { 30 row_positions = {
@@ -15,6 +39,7 @@ class Key:
15 default_outer_color = (120, 120, 120) 39 default_outer_color = (120, 120, 120)
16 lighter_outer_color = (200, 200, 200) 40 lighter_outer_color = (200, 200, 200)
17 default_inner_color = (255, 255, 255) 41 default_inner_color = (255, 255, 255)
42 mapped_inner_color = ( 0, 255, 0)
18 43
19 def __init__(self, key_name, key_sym, top, left, width = 48, height = 48, disabled = False): 44 def __init__(self, key_name, key_sym, top, left, width = 48, height = 48, disabled = False):
20 self.key_name = key_name 45 self.key_name = key_name
@@ -43,8 +68,12 @@ class Key:
43 self.linewidth = 3 68 self.linewidth = 3
44 69
45 self.inner_color = self.default_inner_color 70 self.inner_color = self.default_inner_color
71 self.actions = []
46 72
47 def square(self): 73 def square(self):
74 if self.has_action():
75 self.inner_color = self.mapped_inner_color
76
48 return RoundedRect((0, 0, self.width, self.height), 77 return RoundedRect((0, 0, self.width, self.height),
49 self.outer_color, self.inner_color, self.linewidth) 78 self.outer_color, self.inner_color, self.linewidth)
50 79
@@ -62,116 +91,138 @@ class Key:
62 self.surface.blit(text, (5,5)) 91 self.surface.blit(text, (5,5))
63 background_surface.blit(self.surface, self.position) 92 background_surface.blit(self.surface, self.position)
64 93
94 def has_action(self):
95 return len(self.actions) > 0
96
97 def add_action(self, action_name, **arguments):
98 self.actions.append(Action(action_name, **arguments))
99
100 def next_action(self):
101 print("running next action")
102
65 def do_actions(self): 103 def do_actions(self):
66 print("coucou " + self.key_sym) 104 self.current_action = 0
67 pass 105 print("running actions for {}".format(self.key_sym))
68 106 if len(self.actions) > 0:
69keys = { 107 self.actions[0].run(self.next_action)
70 K_ESCAPE: Key(K_ESCAPE, 'ESC', 'first', 0), 108
71 109 def find_from_unicode(key_sym):
72 K_F1: Key(K_F1, 'F1', 'first', 100), 110 for key in Mapping.KEYS:
73 K_F2: Key(K_F2, 'F2', 'first', 150), 111 if Mapping.KEYS[key].key_sym == key_sym:
74 K_F3: Key(K_F3, 'F3', 'first', 200), 112 return Mapping.KEYS[key]
75 K_F4: Key(K_F4, 'F4', 'first', 250), 113 return None
76 114
77 K_F5: Key(K_F5, 'F5', 'first', 325), 115class Mapping:
78 K_F6: Key(K_F6, 'F6', 'first', 375), 116 KEYS = {
79 K_F7: Key(K_F7, 'F7', 'first', 425), 117 K_ESCAPE: Key(K_ESCAPE, 'ESC', 'first', 0),
80 K_F8: Key(K_F8, 'F8', 'first', 475), 118
81 119 K_F1: Key(K_F1, 'F1', 'first', 100),
82 K_F9: Key(K_F9, 'F9', 'first', 550), 120 K_F2: Key(K_F2, 'F2', 'first', 150),
83 K_F10: Key(K_F10, 'F10', 'first', 600), 121 K_F3: Key(K_F3, 'F3', 'first', 200),
84 K_F11: Key(K_F11, 'F11', 'first', 650), 122 K_F4: Key(K_F4, 'F4', 'first', 250),
85 K_F12: Key(K_F12, 'F12', 'first', 700), 123
86 124 K_F5: Key(K_F5, 'F5', 'first', 325),
87 125 K_F6: Key(K_F6, 'F6', 'first', 375),
88 178: Key(178, '²', 'second', 0), 126 K_F7: Key(K_F7, 'F7', 'first', 425),
89 K_AMPERSAND: Key(K_AMPERSAND, '&', 'second', 50), 127 K_F8: Key(K_F8, 'F8', 'first', 475),
90 233: Key(233, 'é', 'second', 100), 128
91 K_QUOTEDBL: Key(K_QUOTEDBL, '"', 'second', 150), 129 K_F9: Key(K_F9, 'F9', 'first', 550),
92 K_QUOTE: Key(K_QUOTE, "'", 'second', 200), 130 K_F10: Key(K_F10, 'F10', 'first', 600),
93 K_LEFTPAREN: Key(K_LEFTPAREN, '(', 'second', 250), 131 K_F11: Key(K_F11, 'F11', 'first', 650),
94 K_MINUS: Key(K_MINUS, '-', 'second', 300), 132 K_F12: Key(K_F12, 'F12', 'first', 700),
95 232: Key(232, 'è', 'second', 350), 133
96 K_UNDERSCORE: Key(K_UNDERSCORE, '_', 'second', 400), 134
97 231: Key(231, 'ç', 'second', 450), 135 178: Key(178, '²', 'second', 0),
98 224: Key(224, 'à', 'second', 500), 136 K_AMPERSAND: Key(K_AMPERSAND, '&', 'second', 50),
99 K_RIGHTPAREN: Key(K_RIGHTPAREN, ')', 'second', 550), 137 233: Key(233, 'é', 'second', 100),
100 K_EQUALS: Key(K_EQUALS, '=', 'second', 600), 138 K_QUOTEDBL: Key(K_QUOTEDBL, '"', 'second', 150),
101 139 K_QUOTE: Key(K_QUOTE, "'", 'second', 200),
102 K_BACKSPACE: Key(K_BACKSPACE, '<-', 'second', 650, width = 98), 140 K_LEFTPAREN: Key(K_LEFTPAREN, '(', 'second', 250),
103 141 K_MINUS: Key(K_MINUS, '-', 'second', 300),
104 142 232: Key(232, 'è', 'second', 350),
105 K_TAB: Key(K_TAB, 'tab', 'third', 0, width = 73), 143 K_UNDERSCORE: Key(K_UNDERSCORE, '_', 'second', 400),
106 K_a: Key(K_a, 'a', 'third', 75), 144 231: Key(231, 'ç', 'second', 450),
107 K_z: Key(K_z, 'z', 'third', 125), 145 224: Key(224, 'à', 'second', 500),
108 K_e: Key(K_e, 'e', 'third', 175), 146 K_RIGHTPAREN: Key(K_RIGHTPAREN, ')', 'second', 550),
109 K_r: Key(K_r, 'r', 'third', 225), 147 K_EQUALS: Key(K_EQUALS, '=', 'second', 600),
110 K_t: Key(K_t, 't', 'third', 275), 148
111 K_y: Key(K_y, 'y', 'third', 325), 149 K_BACKSPACE: Key(K_BACKSPACE, '<-', 'second', 650, width = 98),
112 K_u: Key(K_u, 'u', 'third', 375), 150
113 K_i: Key(K_i, 'i', 'third', 425), 151
114 K_o: Key(K_o, 'o', 'third', 475), 152 K_TAB: Key(K_TAB, 'tab', 'third', 0, width = 73),
115 K_p: Key(K_p, 'p', 'third', 525), 153 K_a: Key(K_a, 'a', 'third', 75),
116 K_CARET: Key(K_CARET, '^', 'third', 575), 154 K_z: Key(K_z, 'z', 'third', 125),
117 K_DOLLAR: Key(K_DOLLAR, '$', 'third', 625), 155 K_e: Key(K_e, 'e', 'third', 175),
118 156 K_r: Key(K_r, 'r', 'third', 225),
119 K_RETURN: Key(K_RETURN, 'Enter', 'third', 692, width = 56, height = 98), 157 K_t: Key(K_t, 't', 'third', 275),
120 158 K_y: Key(K_y, 'y', 'third', 325),
121 K_CAPSLOCK: Key(K_CAPSLOCK, 'CAPS', 'fourth', 0, width = 88, disabled = True), 159 K_u: Key(K_u, 'u', 'third', 375),
122 160 K_i: Key(K_i, 'i', 'third', 425),
123 K_q: Key(K_q, 'q', 'fourth', 90), 161 K_o: Key(K_o, 'o', 'third', 475),
124 K_s: Key(K_s, 's', 'fourth', 140), 162 K_p: Key(K_p, 'p', 'third', 525),
125 K_d: Key(K_d, 'd', 'fourth', 190), 163 K_CARET: Key(K_CARET, '^', 'third', 575),
126 K_f: Key(K_f, 'f', 'fourth', 240), 164 K_DOLLAR: Key(K_DOLLAR, '$', 'third', 625),
127 K_g: Key(K_g, 'g', 'fourth', 290), 165
128 K_h: Key(K_h, 'h', 'fourth', 340), 166 K_RETURN: Key(K_RETURN, 'Enter', 'third', 692, width = 56, height = 98),
129 K_j: Key(K_j, 'j', 'fourth', 390), 167
130 K_k: Key(K_k, 'k', 'fourth', 440), 168 K_CAPSLOCK: Key(K_CAPSLOCK, 'CAPS', 'fourth', 0, width = 88, disabled = True),
131 K_l: Key(K_l, 'l', 'fourth', 490), 169
132 K_m: Key(K_m, 'm', 'fourth', 540), 170 K_q: Key(K_q, 'q', 'fourth', 90),
133 249: Key(249, 'ù', 'fourth', 590), 171 K_s: Key(K_s, 's', 'fourth', 140),
134 K_ASTERISK: Key(K_ASTERISK, '*', 'fourth', 640), 172 K_d: Key(K_d, 'd', 'fourth', 190),
135 173 K_f: Key(K_f, 'f', 'fourth', 240),
136 174 K_g: Key(K_g, 'g', 'fourth', 290),
137 K_LSHIFT: Key(K_LSHIFT, 'LShift', 'fifth', 0, width = 63, disabled = True), 175 K_h: Key(K_h, 'h', 'fourth', 340),
138 176 K_j: Key(K_j, 'j', 'fourth', 390),
139 K_LESS: Key(K_LESS, '<', 'fifth', 65), 177 K_k: Key(K_k, 'k', 'fourth', 440),
140 K_w: Key(K_w, 'w', 'fifth', 115), 178 K_l: Key(K_l, 'l', 'fourth', 490),
141 K_x: Key(K_x, 'x', 'fifth', 165), 179 K_m: Key(K_m, 'm', 'fourth', 540),
142 K_c: Key(K_c, 'c', 'fifth', 215), 180 249: Key(249, 'ù', 'fourth', 590),
143 K_v: Key(K_v, 'v', 'fifth', 265), 181 K_ASTERISK: Key(K_ASTERISK, '*', 'fourth', 640),
144 K_b: Key(K_b, 'b', 'fifth', 315), 182
145 K_n: Key(K_n, 'n', 'fifth', 365), 183
146 K_COMMA: Key(K_COMMA, ',', 'fifth', 415), 184 K_LSHIFT: Key(K_LSHIFT, 'LShift', 'fifth', 0, width = 63, disabled = True),
147 K_SEMICOLON: Key(K_SEMICOLON, ';', 'fifth', 465), 185
148 K_COLON: Key(K_COLON, ':', 'fifth', 515), 186 K_LESS: Key(K_LESS, '<', 'fifth', 65),
149 K_EXCLAIM: Key(K_EXCLAIM, '!', 'fifth', 565), 187 K_w: Key(K_w, 'w', 'fifth', 115),
150 188 K_x: Key(K_x, 'x', 'fifth', 165),
151 K_RSHIFT: Key(K_RSHIFT, 'RShift', 'fifth', 615, width = 133, disabled = True), 189 K_c: Key(K_c, 'c', 'fifth', 215),
152 190 K_v: Key(K_v, 'v', 'fifth', 265),
153 K_LCTRL: Key(K_LCTRL, 'LCtrl', 'sixth', 0, width = 63, disabled = True), 191 K_b: Key(K_b, 'b', 'fifth', 315),
154 K_LSUPER: Key(K_LSUPER, 'LSuper', 'sixth', 115, disabled = True), 192 K_n: Key(K_n, 'n', 'fifth', 365),
155 K_LALT: Key(K_LALT, 'LAlt', 'sixth', 165, disabled = True), 193 K_COMMA: Key(K_COMMA, ',', 'fifth', 415),
156 K_SPACE: Key(K_SPACE, 'Espace', 'sixth', 215, width = 248), 194 K_SEMICOLON: Key(K_SEMICOLON, ';', 'fifth', 465),
157 K_MODE: Key(K_MODE, 'AltGr', 'sixth', 465, disabled = True), 195 K_COLON: Key(K_COLON, ':', 'fifth', 515),
158 314: Key(314, 'Compose', 'sixth', 515, disabled = True), 196 K_EXCLAIM: Key(K_EXCLAIM, '!', 'fifth', 565),
159 K_RCTRL: Key(K_RCTRL, 'RCtrl', 'sixth', 565, width = 63, disabled = True), 197
160 198 K_RSHIFT: Key(K_RSHIFT, 'RShift', 'fifth', 615, width = 133, disabled = True),
161 199
162 K_INSERT: Key(K_INSERT, 'ins', 'second', 755), 200 K_LCTRL: Key(K_LCTRL, 'LCtrl', 'sixth', 0, width = 63, disabled = True),
163 K_HOME: Key(K_HOME, 'home', 'second', 805), 201 K_LSUPER: Key(K_LSUPER, 'LSuper', 'sixth', 115, disabled = True),
164 K_PAGEUP: Key(K_PAGEUP, 'pg_u', 'second', 855), 202 K_LALT: Key(K_LALT, 'LAlt', 'sixth', 165, disabled = True),
165 K_DELETE: Key(K_DELETE, 'del', 'third', 755), 203 K_SPACE: Key(K_SPACE, 'Espace', 'sixth', 215, width = 248),
166 K_END: Key(K_END, 'end', 'third', 805), 204 K_MODE: Key(K_MODE, 'AltGr', 'sixth', 465, disabled = True),
167 K_PAGEDOWN: Key(K_PAGEDOWN, 'pg_d', 'third', 855), 205 314: Key(314, 'Compose', 'sixth', 515, disabled = True),
168 206 K_RCTRL: Key(K_RCTRL, 'RCtrl', 'sixth', 565, width = 63, disabled = True),
169 207
170 K_UP: Key(K_UP, 'up', 'fifth', 805), 208
171 K_DOWN: Key(K_DOWN, 'down', 'sixth', 805), 209 K_INSERT: Key(K_INSERT, 'ins', 'second', 755),
172 K_LEFT: Key(K_LEFT, 'left', 'sixth', 755), 210 K_HOME: Key(K_HOME, 'home', 'second', 805),
173 K_RIGHT: Key(K_RIGHT, 'right', 'sixth', 855), 211 K_PAGEUP: Key(K_PAGEUP, 'pg_u', 'second', 855),
174} 212 K_DELETE: Key(K_DELETE, 'del', 'third', 755),
213 K_END: Key(K_END, 'end', 'third', 805),
214 K_PAGEDOWN: Key(K_PAGEDOWN, 'pg_d', 'third', 855),
215
216
217 K_UP: Key(K_UP, 'up', 'fifth', 805),
218 K_DOWN: Key(K_DOWN, 'down', 'sixth', 805),
219 K_LEFT: Key(K_LEFT, 'left', 'sixth', 755),
220 K_RIGHT: Key(K_RIGHT, 'right', 'sixth', 855),
221 }
222
223class MusicFile:
224 def __init__(self, filename):
225 self.filename = filename
175 226
176class RoundedRect: 227class RoundedRect:
177 def __init__(self, rect, outer_color, inner_color, linewidth = 2, radius = 0.4): 228 def __init__(self, rect, outer_color, inner_color, linewidth = 2, radius = 0.4):
@@ -231,3 +282,44 @@ class RoundedRect:
231 rectangle.fill((255,255,255,alpha),special_flags=BLEND_RGBA_MIN) 282 rectangle.fill((255,255,255,alpha),special_flags=BLEND_RGBA_MIN)
232 283
233 return rectangle 284 return rectangle
285
286
287def parse_config():
288 import yaml
289 stream = open("config.yml", "r")
290 config = yaml.load(stream)
291 stream.close()
292
293 aliases = config['aliases']
294 seen_files = {}
295
296 for mapped_key in config['keys']:
297 key = Key.find_from_unicode(mapped_key)
298 if key is None:
299 continue
300
301 for action in config['keys'][mapped_key]:
302 action_name = list(action)[0]
303 action_args = {}
304 if action[action_name] is None:
305 action[action_name] = []
306
307 for argument in action[action_name]:
308 if argument == 'include':
309 included = action[action_name]['include']
310 if isinstance(included, str):
311 action_args.update(aliases[included])
312 else:
313 for included_ in included:
314 action_args.update(aliases[included_])
315 elif argument == 'file':
316 filename = action[action_name]['file']
317 if filename not in seen_files:
318 seen_files[filename] = MusicFile.new(filename)
319
320 action_args['file'] = seen_files[filename]
321
322 else:
323 action_args[argument] = action[action_name][argument]
324
325 key.add_action(action_name, **action_args)
diff --git a/run.py b/run.py
index 88afaad..69494d7 100644
--- a/run.py
+++ b/run.py
@@ -1,7 +1,9 @@
1import pygame 1import pygame
2import pydub
2import sys 3import sys
3import helpers 4import helpers
4 5
6pygame.mixer.pre_init(frequency = 44100)
5pygame.init() 7pygame.init()
6 8
7size = width, height = 1024, 600 9size = width, height = 1024, 600
@@ -11,8 +13,10 @@ background = pygame.Surface(screen.get_size())
11background = background.convert() 13background = background.convert()
12background.fill((250, 250, 250)) 14background.fill((250, 250, 250))
13 15
14for key_name in helpers.keys: 16helpers.parse_config()
15 key = helpers.keys[key_name] 17
18for key_name in helpers.Mapping.KEYS:
19 key = helpers.Mapping.KEYS[key_name]
16 key.draw(background) 20 key.draw(background)
17 21
18screen.blit(background, (0, 0)) 22screen.blit(background, (0, 0))
@@ -34,12 +38,12 @@ while 1:
34 sys.exit() 38 sys.exit()
35 39
36 if context == 'normal': 40 if context == 'normal':
37 if event.type == pygame.KEYDOWN and event.key in helpers.keys: 41 if event.type == pygame.KEYDOWN and event.key in helpers.Mapping.KEYS:
38 helpers.keys[event.key].do_actions() 42 helpers.Mapping.KEYS[event.key].do_actions()
39 if event.type == pygame.MOUSEBUTTONUP: 43 if event.type == pygame.MOUSEBUTTONUP:
40 for key in helpers.keys: 44 for key in helpers.Mapping.KEYS:
41 if helpers.keys[key].collidepoint(pygame.mouse.get_pos()): 45 if helpers.Mapping.KEYS[key].collidepoint(pygame.mouse.get_pos()):
42 helpers.keys[key].do_actions() 46 helpers.Mapping.KEYS[key].do_actions()
43 47
44 pygame.display.flip() 48 pygame.display.flip()
45 49