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