]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/mapping.py
Use aliases in key_properties
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / mapping.py
1 from kivy.uix.relativelayout import RelativeLayout
2 from kivy.properties import NumericProperty
3 from kivy.core.window import Window
4
5 import threading
6 import pygame
7 import yaml
8 import sys
9
10 from .lock import *
11 from .music_file import *
12
13 class Mapping(RelativeLayout):
14 expected_keys = NumericProperty(0)
15
16 def __init__(self, **kwargs):
17 self.key_config, self.channel_number, self.open_files = self.parse_config()
18 super(Mapping, self).__init__(**kwargs)
19 self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
20 self._keyboard.bind(on_key_down=self._on_keyboard_down)
21 self.running = []
22
23
24 pygame.mixer.init(frequency = 44100)
25 pygame.mixer.set_num_channels(self.channel_number)
26
27 def _keyboard_closed(self):
28 self._keyboard.unbind(on_key_down=self._on_keyboard_down)
29 self._keyboard = None
30
31 def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
32 key = self.find_by_key_code(keycode)
33 if len(modifiers) == 0 and key is not None:
34 threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
35 elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
36 for thread in threading.enumerate():
37 if thread.getName()[0:2] != "MS":
38 continue
39 thread.join()
40
41 pygame.quit()
42 sys.exit()
43 return True
44
45 def find_by_key_code(self, key_code):
46 if "Key_" + str(key_code[0]) in self.ids:
47 return self.ids["Key_" + str(key_code[0])]
48 return None
49
50 def find_by_unicode(self, key_sym):
51 for key in self.children:
52 if not type(key).__name__ == "Key":
53 continue
54 print(key.key_sym, key_sym)
55 if key.key_sym == key_sym:
56 print("found")
57 return key
58 return None
59
60 def stop_all_running(self):
61 self.running = []
62
63 def start_running(self, key, start_time):
64 self.running.append((key, start_time))
65
66 def keep_running(self, key, start_time):
67 return (key, start_time) in self.running
68
69 def finished_running(self, key, start_time):
70 if (key, start_time) in self.running:
71 self.running.remove((key, start_time))
72
73 def parse_config(self):
74 stream = open("config.yml", "r")
75 config = yaml.load(stream)
76 stream.close()
77
78 aliases = config['aliases']
79 seen_files = {}
80
81 file_lock = Lock("file")
82
83 channel_id = 0
84
85 key_properties = {}
86
87 for key in config['key_properties']:
88 if key not in key_properties:
89 key_prop = config['key_properties'][key]
90 if 'include' in key_prop:
91 included = key_prop['include']
92 del(key_prop['include'])
93
94 if isinstance(included, str):
95 key_prop.update(aliases[included], **key_prop)
96 else:
97 for included_ in included:
98 key_prop.update(aliases[included_], **key_prop)
99
100 key_properties[key] = {
101 "actions": [],
102 "properties": key_prop,
103 "files": []
104 }
105
106 for mapped_key in config['keys']:
107 if mapped_key not in key_properties:
108 key_properties[mapped_key] = {
109 "actions": [],
110 "properties": {},
111 "files": []
112 }
113 for action in config['keys'][mapped_key]:
114 action_name = list(action)[0]
115 action_args = {}
116 if action[action_name] is None:
117 action[action_name] = []
118
119 if 'include' in action[action_name]:
120 included = action[action_name]['include']
121 del(action[action_name]['include'])
122
123 if isinstance(included, str):
124 action[action_name].update(aliases[included], **action[action_name])
125 else:
126 for included_ in included:
127 action[action_name].update(aliases[included_], **action[action_name])
128
129 for argument in action[action_name]:
130 if argument == 'file':
131 filename = action[action_name]['file']
132 if filename not in seen_files:
133 if filename in config['music_properties']:
134 seen_files[filename] = MusicFile(
135 filename,
136 file_lock,
137 channel_id,
138 **config['music_properties'][filename])
139 else:
140 seen_files[filename] = MusicFile(
141 filename,
142 file_lock,
143 channel_id)
144 channel_id = channel_id + 1
145
146 if filename not in key_properties[mapped_key]['files']:
147 key_properties[mapped_key]['files'].append(seen_files[filename])
148
149 action_args['music'] = seen_files[filename]
150
151 else:
152 action_args[argument] = action[action_name][argument]
153
154 key_properties[mapped_key]['actions'].append([action_name, action_args])
155
156 return (key_properties, channel_id + 1, seen_files)
157
158