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