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