]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/mapping.py
Fix master volume fade
[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, 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 not (music.is_loaded_playing() or music.is_loaded_paused()):
43 continue
44
45 if fade > 0:
46 music.gain_effects.append(GainEffect(
47 "fade",
48 music.current_audio_segment,
49 music.current_loop,
50 music.sound_position,
51 music.sound_position + fade,
52 gain=db_gain))
53 else:
54 music.set_gain(db_gain)
55
56 def _keyboard_closed(self):
57 self._keyboard.unbind(on_key_down=self._on_keyboard_down)
58 self._keyboard = None
59
60 def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
61 key = self.find_by_key_code(keycode)
62 if len(modifiers) == 0 and key is not None:
63 threading.Thread(name="MSKeyAction", target=key.do_actions).start()
64 elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
65 for thread in threading.enumerate():
66 if thread.getName()[0:2] != "MS":
67 continue
68 thread.join()
69
70 sys.exit()
71 return True
72
73 def find_by_key_code(self, key_code):
74 if "Key_" + str(key_code[0]) in self.ids:
75 return self.ids["Key_" + str(key_code[0])]
76 return None
77
78 def not_all_keys_ready(self, dt):
79 for key in self.children:
80 if not type(key).__name__ == "Key":
81 continue
82 if not key.is_key_ready:
83 return True
84 self.ready_color = [0, 1, 0, 1]
85 return False
86
87 def stop_all_running(self):
88 running = self.running
89 self.running = []
90 for (key, start_time) in running:
91 key.interrupt_action()
92
93 def start_running(self, key, start_time):
94 self.running.append((key, start_time))
95
96 def keep_running(self, key, start_time):
97 return (key, start_time) in self.running
98
99 def finished_running(self, key, start_time):
100 if (key, start_time) in self.running:
101 self.running.remove((key, start_time))
102
103 def parse_config(self):
104 stream = open(Config.yml_file, "r")
105 try:
106 config = yaml.load(stream)
107 except Exception as e:
108 error_print("Error while loading config file: {}".format(e))
109 sys.exit()
110 stream.close()
111
112 aliases = config['aliases']
113 seen_files = {}
114
115 key_properties = {}
116
117 for key in config['key_properties']:
118 if key not in key_properties:
119 key_prop = config['key_properties'][key]
120 if 'include' in key_prop:
121 included = key_prop['include']
122 del(key_prop['include'])
123
124 if isinstance(included, str):
125 key_prop.update(aliases[included], **key_prop)
126 else:
127 for included_ in included:
128 key_prop.update(aliases[included_], **key_prop)
129
130 key_properties[key] = {
131 "actions": [],
132 "properties": key_prop,
133 "files": []
134 }
135
136 for mapped_key in config['keys']:
137 if mapped_key not in key_properties:
138 key_properties[mapped_key] = {
139 "actions": [],
140 "properties": {},
141 "files": []
142 }
143 for action in config['keys'][mapped_key]:
144 action_name = list(action)[0]
145 action_args = {}
146 if action[action_name] is None:
147 action[action_name] = []
148
149 if 'include' in action[action_name]:
150 included = action[action_name]['include']
151 del(action[action_name]['include'])
152
153 if isinstance(included, str):
154 action[action_name].update(
155 aliases[included],
156 **action[action_name])
157 else:
158 for included_ in included:
159 action[action_name].update(
160 aliases[included_],
161 **action[action_name])
162
163 for argument in action[action_name]:
164 if argument == 'file':
165 filename = action[action_name]['file']
166 if filename not in seen_files:
167 if filename in config['music_properties']:
168 seen_files[filename] = MusicFile(
169 filename,
170 self,
171 **config['music_properties'][filename])
172 else:
173 seen_files[filename] = MusicFile(
174 self,
175 filename)
176
177 if filename not in key_properties[mapped_key]['files']:
178 key_properties[mapped_key]['files'] \
179 .append(seen_files[filename])
180
181 action_args['music'] = seen_files[filename]
182
183 else:
184 action_args[argument] = action[action_name][argument]
185
186 key_properties[mapped_key]['actions'] \
187 .append([action_name, action_args])
188
189 return (key_properties, seen_files)
190
191