]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - 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
10 from .music_file
import *
11 from .mixer
import Mixer
12 from . import Config
, gain
, error_print
13 from .action
import Action
15 class Mapping(RelativeLayout
):
16 expected_keys
= NumericProperty(0)
17 master_volume
= NumericProperty(100)
18 ready_color
= ListProperty([1, 165/255, 0, 1])
20 def __init__(self
, **kwargs
):
21 if Config
.builtin_mixing
:
27 self
.key_config
, self
.open_files
= self
.parse_config()
28 except Exception as e
:
29 error_print("Error while loading configuration: {}".format(e
))
32 super(Mapping
, self
).__init
__(**kwargs
)
33 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
34 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
37 Clock
.schedule_interval(self
.not_all_keys_ready
, 1)
40 def master_gain(self
):
41 return gain(self
.master_volume
)
43 def set_master_volume(self
, value
, delta
=False, fade
=0):
44 [db_gain
, self
.master_volume
] = gain(
45 value
+ int(delta
) * self
.master_volume
,
48 for music
in self
.open_files
.values():
49 music
.set_gain_with_effect(db_gain
, fade
=fade
)
51 def add_wait_id(self
, wait_id
, action_or_wait
):
52 self
.wait_ids
[wait_id
] = action_or_wait
54 def interrupt_wait(self
, wait_id
):
55 if wait_id
in self
.wait_ids
:
56 action_or_wait
= self
.wait_ids
[wait_id
]
57 del(self
.wait_ids
[wait_id
])
58 if isinstance(action_or_wait
, Action
):
59 action_or_wait
.interrupt()
63 def _keyboard_closed(self
):
64 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
67 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
68 key
= self
.find_by_key_code(keycode
)
69 if len(modifiers
) == 0 and key
is not None:
70 threading
.Thread(name
="MSKeyAction", target
=key
.do_actions
).start()
71 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
72 for thread
in threading
.enumerate():
73 if thread
.getName()[0:2] != "MS":
80 def find_by_key_code(self
, key_code
):
81 if "Key_" + str(key_code
[0]) in self
.ids
:
82 return self
.ids
["Key_" + str(key_code
[0])]
85 def not_all_keys_ready(self
, dt
):
86 for key
in self
.children
:
87 if not type(key
).__name
__ == "Key":
89 if not key
.is_key_ready
:
91 self
.ready_color
= [0, 1, 0, 1]
94 def stop_all_running(self
):
95 running
= self
.running
97 for (key
, start_time
) in running
:
98 key
.interrupt_action()
100 def start_running(self
, key
, start_time
):
101 self
.running
.append((key
, start_time
))
103 def keep_running(self
, key
, start_time
):
104 return (key
, start_time
) in self
.running
106 def finished_running(self
, key
, start_time
):
107 if (key
, start_time
) in self
.running
:
108 self
.running
.remove((key
, start_time
))
110 def parse_config(self
):
111 stream
= open(Config
.yml_file
, "r")
113 config
= yaml
.load(stream
)
114 except Exception as e
:
115 error_print("Error while loading config file: {}".format(e
))
119 aliases
= config
['aliases']
124 for key
in config
['key_properties']:
125 if key
not in key_properties
:
126 key_prop
= config
['key_properties'][key
]
127 if 'include' in key_prop
:
128 included
= key_prop
['include']
129 del(key_prop
['include'])
131 if isinstance(included
, str):
132 key_prop
.update(aliases
[included
], **key_prop
)
134 for included_
in included
:
135 key_prop
.update(aliases
[included_
], **key_prop
)
137 key_properties
[key
] = {
139 "properties": key_prop
,
143 for mapped_key
in config
['keys']:
144 if mapped_key
not in key_properties
:
145 key_properties
[mapped_key
] = {
150 for action
in config
['keys'][mapped_key
]:
151 action_name
= list(action
)[0]
153 if action
[action_name
] is None:
154 action
[action_name
] = []
156 if 'include' in action
[action_name
]:
157 included
= action
[action_name
]['include']
158 del(action
[action_name
]['include'])
160 if isinstance(included
, str):
161 action
[action_name
].update(
163 **action
[action_name
])
165 for included_
in included
:
166 action
[action_name
].update(
168 **action
[action_name
])
170 for argument
in action
[action_name
]:
171 if argument
== 'file':
172 filename
= action
[action_name
]['file']
173 if filename
not in seen_files
:
174 if filename
in config
['music_properties']:
175 seen_files
[filename
] = MusicFile(
178 **config
['music_properties'][filename
])
180 seen_files
[filename
] = MusicFile(
184 if filename
not in key_properties
[mapped_key
]['files']:
185 key_properties
[mapped_key
]['files'] \
186 .append(seen_files
[filename
])
188 action_args
['music'] = seen_files
[filename
]
191 action_args
[argument
] = action
[action_name
][argument
]
193 key_properties
[mapped_key
]['actions'] \
194 .append([action_name
, action_args
])
196 return (key_properties
, seen_files
)