]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/mapping.py
95c9d670a26488684d9d803b8b8b3c66d736eba7
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 . import yml_file
13 class Mapping(RelativeLayout
):
14 expected_keys
= NumericProperty(0)
15 ready_color
= ListProperty([1, 165/255, 0, 1])
17 def __init__(self
, **kwargs
):
18 self
.key_config
, self
.open_files
= self
.parse_config()
19 super(Mapping
, self
).__init
__(**kwargs
)
20 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
21 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
23 Clock
.schedule_interval(self
.not_all_keys_ready
, 1)
26 def _keyboard_closed(self
):
27 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
30 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
31 key
= self
.find_by_key_code(keycode
)
32 if len(modifiers
) == 0 and key
is not None:
33 threading
.Thread(name
= "MSKeyAction", target
=key
.do_actions
).start()
34 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
35 for thread
in threading
.enumerate():
36 if thread
.getName()[0:2] != "MS":
43 def find_by_key_code(self
, key_code
):
44 if "Key_" + str(key_code
[0]) in self
.ids
:
45 return self
.ids
["Key_" + str(key_code
[0])]
48 def find_by_unicode(self
, key_sym
):
49 for key
in self
.children
:
50 if not type(key
).__name
__ == "Key":
52 print(key
.key_sym
, key_sym
)
53 if key
.key_sym
== key_sym
:
58 def not_all_keys_ready(self
, dt
):
59 for key
in self
.children
:
60 if not type(key
).__name
__ == "Key":
62 if not key
.is_key_ready
:
64 self
.ready_color
= [0, 1, 0, 1]
67 def stop_all_running(self
):
70 def start_running(self
, key
, start_time
):
71 self
.running
.append((key
, start_time
))
73 def keep_running(self
, key
, start_time
):
74 return (key
, start_time
) in self
.running
76 def finished_running(self
, key
, start_time
):
77 if (key
, start_time
) in self
.running
:
78 self
.running
.remove((key
, start_time
))
80 def parse_config(self
):
81 stream
= open(yml_file(), "r")
82 config
= yaml
.load(stream
)
85 aliases
= config
['aliases']
90 for key
in config
['key_properties']:
91 if key
not in key_properties
:
92 key_prop
= config
['key_properties'][key
]
93 if 'include' in key_prop
:
94 included
= key_prop
['include']
95 del(key_prop
['include'])
97 if isinstance(included
, str):
98 key_prop
.update(aliases
[included
], **key_prop
)
100 for included_
in included
:
101 key_prop
.update(aliases
[included_
], **key_prop
)
103 key_properties
[key
] = {
105 "properties": key_prop
,
109 for mapped_key
in config
['keys']:
110 if mapped_key
not in key_properties
:
111 key_properties
[mapped_key
] = {
116 for action
in config
['keys'][mapped_key
]:
117 action_name
= list(action
)[0]
119 if action
[action_name
] is None:
120 action
[action_name
] = []
122 if 'include' in action
[action_name
]:
123 included
= action
[action_name
]['include']
124 del(action
[action_name
]['include'])
126 if isinstance(included
, str):
127 action
[action_name
].update(aliases
[included
], **action
[action_name
])
129 for included_
in included
:
130 action
[action_name
].update(aliases
[included_
], **action
[action_name
])
132 for argument
in action
[action_name
]:
133 if argument
== 'file':
134 filename
= action
[action_name
]['file']
135 if filename
not in seen_files
:
136 if filename
in config
['music_properties']:
137 seen_files
[filename
] = MusicFile(
139 **config
['music_properties'][filename
])
141 seen_files
[filename
] = MusicFile(
144 if filename
not in key_properties
[mapped_key
]['files']:
145 key_properties
[mapped_key
]['files'].append(seen_files
[filename
])
147 action_args
['music'] = seen_files
[filename
]
150 action_args
[argument
] = action
[action_name
][argument
]
152 key_properties
[mapped_key
]['actions'].append([action_name
, action_args
])
154 return (key_properties
, seen_files
)