]>
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
12 from .music_file
import *
14 class Mapping(RelativeLayout
):
15 expected_keys
= NumericProperty(0)
16 ready_color
= ListProperty([1, 165/255, 0, 1])
18 def __init__(self
, **kwargs
):
19 self
.key_config
, self
.channel_number
, self
.open_files
= self
.parse_config()
20 super(Mapping
, self
).__init
__(**kwargs
)
21 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
22 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
24 Clock
.schedule_interval(self
.not_all_keys_ready
, 1)
27 pygame
.mixer
.init(frequency
= 44100)
28 pygame
.mixer
.set_num_channels(self
.channel_number
)
30 def _keyboard_closed(self
):
31 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
34 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
35 key
= self
.find_by_key_code(keycode
)
36 if len(modifiers
) == 0 and key
is not None:
37 threading
.Thread(name
= "MSKeyAction", target
=key
.do_actions
).start()
38 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
39 for thread
in threading
.enumerate():
40 if thread
.getName()[0:2] != "MS":
48 def find_by_key_code(self
, key_code
):
49 if "Key_" + str(key_code
[0]) in self
.ids
:
50 return self
.ids
["Key_" + str(key_code
[0])]
53 def find_by_unicode(self
, key_sym
):
54 for key
in self
.children
:
55 if not type(key
).__name
__ == "Key":
57 print(key
.key_sym
, key_sym
)
58 if key
.key_sym
== key_sym
:
63 def not_all_keys_ready(self
, dt
):
64 for key
in self
.children
:
65 if not type(key
).__name
__ == "Key":
67 if not key
.is_key_ready
:
69 self
.ready_color
= [0, 1, 0, 1]
72 def stop_all_running(self
):
75 def start_running(self
, key
, start_time
):
76 self
.running
.append((key
, start_time
))
78 def keep_running(self
, key
, start_time
):
79 return (key
, start_time
) in self
.running
81 def finished_running(self
, key
, start_time
):
82 if (key
, start_time
) in self
.running
:
83 self
.running
.remove((key
, start_time
))
85 def parse_config(self
):
86 stream
= open("config.yml", "r")
87 config
= yaml
.load(stream
)
90 aliases
= config
['aliases']
93 file_lock
= Lock("file")
99 for key
in config
['key_properties']:
100 if key
not in key_properties
:
101 key_prop
= config
['key_properties'][key
]
102 if 'include' in key_prop
:
103 included
= key_prop
['include']
104 del(key_prop
['include'])
106 if isinstance(included
, str):
107 key_prop
.update(aliases
[included
], **key_prop
)
109 for included_
in included
:
110 key_prop
.update(aliases
[included_
], **key_prop
)
112 key_properties
[key
] = {
114 "properties": key_prop
,
118 for mapped_key
in config
['keys']:
119 if mapped_key
not in key_properties
:
120 key_properties
[mapped_key
] = {
125 for action
in config
['keys'][mapped_key
]:
126 action_name
= list(action
)[0]
128 if action
[action_name
] is None:
129 action
[action_name
] = []
131 if 'include' in action
[action_name
]:
132 included
= action
[action_name
]['include']
133 del(action
[action_name
]['include'])
135 if isinstance(included
, str):
136 action
[action_name
].update(aliases
[included
], **action
[action_name
])
138 for included_
in included
:
139 action
[action_name
].update(aliases
[included_
], **action
[action_name
])
141 for argument
in action
[action_name
]:
142 if argument
== 'file':
143 filename
= action
[action_name
]['file']
144 if filename
not in seen_files
:
145 if filename
in config
['music_properties']:
146 seen_files
[filename
] = MusicFile(
150 **config
['music_properties'][filename
])
152 seen_files
[filename
] = MusicFile(
156 channel_id
= channel_id
+ 1
158 if filename
not in key_properties
[mapped_key
]['files']:
159 key_properties
[mapped_key
]['files'].append(seen_files
[filename
])
161 action_args
['music'] = seen_files
[filename
]
164 action_args
[argument
] = action
[action_name
][argument
]
166 key_properties
[mapped_key
]['actions'].append([action_name
, action_args
])
168 return (key_properties
, channel_id
+ 1, seen_files
)