]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/mapping.py
Print error when loading yaml
[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 6import threading
4b2d79ca 7import yaml
b68b4e8f 8import sys
4b2d79ca 9
4b2d79ca 10from .music_file import *
22514f3a 11from .mixer import Mixer
6c44b231 12from . import Config, gain, error_print
4b2d79ca
IB
13
14class Mapping(RelativeLayout):
15 expected_keys = NumericProperty(0)
1b4b78f5 16 master_volume = NumericProperty(100)
30d8796f 17 ready_color = ListProperty([1, 165/255, 0, 1])
4b2d79ca
IB
18
19 def __init__(self, **kwargs):
d6290f14 20 if Config.builtin_mixing:
af27d782 21 self.mixer = Mixer()
d6290f14
IB
22 else:
23 self.mixer = None
29597680 24 self.key_config, self.open_files = self.parse_config()
4b2d79ca
IB
25 super(Mapping, self).__init__(**kwargs)
26 self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
27 self._keyboard.bind(on_key_down=self._on_keyboard_down)
be27763f 28 self.running = []
30d8796f 29 Clock.schedule_interval(self.not_all_keys_ready, 1)
be27763f 30
1b4b78f5
IB
31 @property
32 def master_gain(self):
33 return gain(self.master_volume)
34
2e404903
IB
35 def set_master_volume(self, value, delta=False):
36 [db_gain, self.master_volume] = gain(
37 value + int(delta) * self.master_volume,
38 self.master_volume)
39
1b4b78f5
IB
40 for music in self.open_files.values():
41 music.set_gain(db_gain)
42
4b2d79ca
IB
43 def _keyboard_closed(self):
44 self._keyboard.unbind(on_key_down=self._on_keyboard_down)
45 self._keyboard = None
46
47 def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
48 key = self.find_by_key_code(keycode)
b68b4e8f 49 if len(modifiers) == 0 and key is not None:
2e404903 50 threading.Thread(name="MSKeyAction", target=key.do_actions).start()
b68b4e8f
IB
51 elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
52 for thread in threading.enumerate():
53 if thread.getName()[0:2] != "MS":
54 continue
55 thread.join()
56
b68b4e8f 57 sys.exit()
4b2d79ca
IB
58 return True
59
60 def find_by_key_code(self, key_code):
61 if "Key_" + str(key_code[0]) in self.ids:
62 return self.ids["Key_" + str(key_code[0])]
be27763f
IB
63 return None
64
30d8796f
IB
65 def not_all_keys_ready(self, dt):
66 for key in self.children:
67 if not type(key).__name__ == "Key":
68 continue
69 if not key.is_key_ready:
70 return True
71 self.ready_color = [0, 1, 0, 1]
72 return False
73
be27763f 74 def stop_all_running(self):
0deb82a5 75 running = self.running
be27763f 76 self.running = []
0deb82a5
IB
77 for (key, start_time) in running:
78 key.interrupt_action()
be27763f
IB
79
80 def start_running(self, key, start_time):
81 self.running.append((key, start_time))
82
83 def keep_running(self, key, start_time):
84 return (key, start_time) in self.running
85
86 def finished_running(self, key, start_time):
87 if (key, start_time) in self.running:
88 self.running.remove((key, start_time))
89
4b2d79ca 90 def parse_config(self):
75d6cdba 91 stream = open(Config.yml_file, "r")
6c44b231
IB
92 try:
93 config = yaml.load(stream)
94 except yaml.scanner.ScannerError as e:
95 error_print("Error while loading config file: {}".format(e))
96 sys.exit()
4b2d79ca
IB
97 stream.close()
98
99 aliases = config['aliases']
100 seen_files = {}
101
4b2d79ca
IB
102 key_properties = {}
103
104 for key in config['key_properties']:
105 if key not in key_properties:
e5edd8b9
IB
106 key_prop = config['key_properties'][key]
107 if 'include' in key_prop:
108 included = key_prop['include']
109 del(key_prop['include'])
110
111 if isinstance(included, str):
112 key_prop.update(aliases[included], **key_prop)
113 else:
114 for included_ in included:
115 key_prop.update(aliases[included_], **key_prop)
116
4b2d79ca
IB
117 key_properties[key] = {
118 "actions": [],
e5edd8b9 119 "properties": key_prop,
4b2d79ca
IB
120 "files": []
121 }
122
123 for mapped_key in config['keys']:
124 if mapped_key not in key_properties:
125 key_properties[mapped_key] = {
126 "actions": [],
127 "properties": {},
128 "files": []
129 }
130 for action in config['keys'][mapped_key]:
131 action_name = list(action)[0]
132 action_args = {}
133 if action[action_name] is None:
134 action[action_name] = []
135
136 if 'include' in action[action_name]:
137 included = action[action_name]['include']
138 del(action[action_name]['include'])
139
140 if isinstance(included, str):
2e404903
IB
141 action[action_name].update(
142 aliases[included],
143 **action[action_name])
4b2d79ca
IB
144 else:
145 for included_ in included:
2e404903
IB
146 action[action_name].update(
147 aliases[included_],
148 **action[action_name])
4b2d79ca
IB
149
150 for argument in action[action_name]:
151 if argument == 'file':
152 filename = action[action_name]['file']
153 if filename not in seen_files:
154 if filename in config['music_properties']:
155 seen_files[filename] = MusicFile(
156 filename,
1b4b78f5 157 self,
4b2d79ca
IB
158 **config['music_properties'][filename])
159 else:
160 seen_files[filename] = MusicFile(
1b4b78f5 161 self,
29597680 162 filename)
4b2d79ca
IB
163
164 if filename not in key_properties[mapped_key]['files']:
2e404903
IB
165 key_properties[mapped_key]['files'] \
166 .append(seen_files[filename])
4b2d79ca
IB
167
168 action_args['music'] = seen_files[filename]
169
170 else:
171 action_args[argument] = action[action_name][argument]
172
2e404903
IB
173 key_properties[mapped_key]['actions'] \
174 .append([action_name, action_args])
4b2d79ca 175
29597680 176 return (key_properties, seen_files)
4b2d79ca
IB
177
178