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