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