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