]>
Commit | Line | Data |
---|---|---|
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, error_print | |
13 | from .music_effect import GainEffect | |
14 | ||
15 | class Mapping(RelativeLayout): | |
16 | expected_keys = NumericProperty(0) | |
17 | master_volume = NumericProperty(100) | |
18 | ready_color = ListProperty([1, 165/255, 0, 1]) | |
19 | ||
20 | def __init__(self, **kwargs): | |
21 | if Config.builtin_mixing: | |
22 | self.mixer = Mixer() | |
23 | else: | |
24 | self.mixer = None | |
25 | self.key_config, self.open_files = self.parse_config() | |
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) | |
29 | self.running = [] | |
30 | Clock.schedule_interval(self.not_all_keys_ready, 1) | |
31 | ||
32 | @property | |
33 | def master_gain(self): | |
34 | return gain(self.master_volume) | |
35 | ||
36 | def set_master_volume(self, value, delta=False, fade=0): | |
37 | [db_gain, self.master_volume] = gain( | |
38 | value + int(delta) * self.master_volume, | |
39 | self.master_volume) | |
40 | ||
41 | for music in self.open_files.values(): | |
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) | |
52 | ||
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) | |
59 | if len(modifiers) == 0 and key is not None: | |
60 | threading.Thread(name="MSKeyAction", target=key.do_actions).start() | |
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 | ||
67 | sys.exit() | |
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])] | |
73 | return None | |
74 | ||
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 | ||
84 | def stop_all_running(self): | |
85 | running = self.running | |
86 | self.running = [] | |
87 | for (key, start_time) in running: | |
88 | key.interrupt_action() | |
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 | ||
100 | def parse_config(self): | |
101 | stream = open(Config.yml_file, "r") | |
102 | try: | |
103 | config = yaml.load(stream) | |
104 | except Exception as e: | |
105 | error_print("Error while loading config file: {}".format(e)) | |
106 | sys.exit() | |
107 | stream.close() | |
108 | ||
109 | aliases = config['aliases'] | |
110 | seen_files = {} | |
111 | ||
112 | key_properties = {} | |
113 | ||
114 | for key in config['key_properties']: | |
115 | if key not in key_properties: | |
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 | ||
127 | key_properties[key] = { | |
128 | "actions": [], | |
129 | "properties": key_prop, | |
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): | |
151 | action[action_name].update( | |
152 | aliases[included], | |
153 | **action[action_name]) | |
154 | else: | |
155 | for included_ in included: | |
156 | action[action_name].update( | |
157 | aliases[included_], | |
158 | **action[action_name]) | |
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, | |
167 | self, | |
168 | **config['music_properties'][filename]) | |
169 | else: | |
170 | seen_files[filename] = MusicFile( | |
171 | self, | |
172 | filename) | |
173 | ||
174 | if filename not in key_properties[mapped_key]['files']: | |
175 | key_properties[mapped_key]['files'] \ | |
176 | .append(seen_files[filename]) | |
177 | ||
178 | action_args['music'] = seen_files[filename] | |
179 | ||
180 | else: | |
181 | action_args[argument] = action[action_name][argument] | |
182 | ||
183 | key_properties[mapped_key]['actions'] \ | |
184 | .append([action_name, action_args]) | |
185 | ||
186 | return (key_properties, seen_files) | |
187 | ||
188 |