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