]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/mapping.py
Add exit key
[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_properties[key] = {
90 "actions": [],
91 "properties": config['key_properties'][key],
92 "files": []
93 }
94
95 for mapped_key in config['keys']:
96 if mapped_key not in key_properties:
97 key_properties[mapped_key] = {
98 "actions": [],
99 "properties": {},
100 "files": []
101 }
102 for action in config['keys'][mapped_key]:
103 action_name = list(action)[0]
104 action_args = {}
105 if action[action_name] is None:
106 action[action_name] = []
107
108 if 'include' in action[action_name]:
109 included = action[action_name]['include']
110 del(action[action_name]['include'])
111
112 if isinstance(included, str):
113 action[action_name].update(aliases[included], **action[action_name])
114 else:
115 for included_ in included:
116 action[action_name].update(aliases[included_], **action[action_name])
117
118 for argument in action[action_name]:
119 if argument == 'file':
120 filename = action[action_name]['file']
121 if filename not in seen_files:
122 if filename in config['music_properties']:
123 seen_files[filename] = MusicFile(
124 filename,
125 file_lock,
126 channel_id,
127 **config['music_properties'][filename])
128 else:
129 seen_files[filename] = MusicFile(
130 filename,
131 file_lock,
132 channel_id)
133 channel_id = channel_id + 1
134
135 if filename not in key_properties[mapped_key]['files']:
136 key_properties[mapped_key]['files'].append(seen_files[filename])
137
138 action_args['music'] = seen_files[filename]
139
140 else:
141 action_args[argument] = action[action_name][argument]
142
143 key_properties[mapped_key]['actions'].append([action_name, action_args])
144
145 return (key_properties, channel_id + 1, seen_files)
146
147