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