]>
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
3 from kivy
.core
.window
import Window
10 from .music_file
import *
12 class Mapping(RelativeLayout
):
13 expected_keys
= NumericProperty(0)
15 def __init__(self
, **kwargs
):
16 self
.key_config
, self
.channel_number
, self
.open_files
= self
.parse_config()
17 super(Mapping
, self
).__init
__(**kwargs
)
18 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
19 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
23 pygame
.mixer
.init(frequency
= 44100)
24 pygame
.mixer
.set_num_channels(self
.channel_number
)
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
)
33 threading
.Thread(name
= "MSKeyAction", target
=key
.do_actions
).start()
36 def find_by_key_code(self
, key_code
):
37 if "Key_" + str(key_code
[0]) in self
.ids
:
38 return self
.ids
["Key_" + str(key_code
[0])]
41 def find_by_unicode(self
, key_sym
):
42 for key
in self
.children
:
43 if not type(key
).__name
__ == "Key":
45 print(key
.key_sym
, key_sym
)
46 if key
.key_sym
== key_sym
:
51 def stop_all_running(self
):
54 def start_running(self
, key
, start_time
):
55 self
.running
.append((key
, start_time
))
57 def keep_running(self
, key
, start_time
):
58 return (key
, start_time
) in self
.running
60 def finished_running(self
, key
, start_time
):
61 if (key
, start_time
) in self
.running
:
62 self
.running
.remove((key
, start_time
))
64 def parse_config(self
):
65 stream
= open("config.yml", "r")
66 config
= yaml
.load(stream
)
69 aliases
= config
['aliases']
72 file_lock
= Lock("file")
78 for key
in config
['key_properties']:
79 if key
not in key_properties
:
80 key_properties
[key
] = {
82 "properties": config
['key_properties'][key
],
86 for mapped_key
in config
['keys']:
87 if mapped_key
not in key_properties
:
88 key_properties
[mapped_key
] = {
93 for action
in config
['keys'][mapped_key
]:
94 action_name
= list(action
)[0]
96 if action
[action_name
] is None:
97 action
[action_name
] = []
99 if 'include' in action
[action_name
]:
100 included
= action
[action_name
]['include']
101 del(action
[action_name
]['include'])
103 if isinstance(included
, str):
104 action
[action_name
].update(aliases
[included
], **action
[action_name
])
106 for included_
in included
:
107 action
[action_name
].update(aliases
[included_
], **action
[action_name
])
109 for argument
in action
[action_name
]:
110 if argument
== 'file':
111 filename
= action
[action_name
]['file']
112 if filename
not in seen_files
:
113 if filename
in config
['music_properties']:
114 seen_files
[filename
] = MusicFile(
118 **config
['music_properties'][filename
])
120 seen_files
[filename
] = MusicFile(
124 channel_id
= channel_id
+ 1
126 if filename
not in key_properties
[mapped_key
]['files']:
127 key_properties
[mapped_key
]['files'].append(seen_files
[filename
])
129 action_args
['music'] = seen_files
[filename
]
132 action_args
[argument
] = action
[action_name
][argument
]
134 key_properties
[mapped_key
]['actions'].append([action_name
, action_args
])
136 return (key_properties
, channel_id
+ 1, seen_files
)