]>
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
11 from .music_file
import *
13 class Mapping(RelativeLayout
):
14 expected_keys
= NumericProperty(0)
16 def __init__(self
, **kwargs
):
17 self
.key_config
, self
.channel_number
, self
.open_files
= self
.parse_config()
18 super(Mapping
, self
).__init
__(**kwargs
)
19 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
20 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
24 pygame
.mixer
.init(frequency
= 44100)
25 pygame
.mixer
.set_num_channels(self
.channel_number
)
27 def _keyboard_closed(self
):
28 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
31 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
32 key
= self
.find_by_key_code(keycode
)
33 if len(modifiers
) == 0 and key
is not None:
34 threading
.Thread(name
= "MSKeyAction", target
=key
.do_actions
).start()
35 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
36 for thread
in threading
.enumerate():
37 if thread
.getName()[0:2] != "MS":
45 def find_by_key_code(self
, key_code
):
46 if "Key_" + str(key_code
[0]) in self
.ids
:
47 return self
.ids
["Key_" + str(key_code
[0])]
50 def find_by_unicode(self
, key_sym
):
51 for key
in self
.children
:
52 if not type(key
).__name
__ == "Key":
54 print(key
.key_sym
, key_sym
)
55 if key
.key_sym
== key_sym
:
60 def stop_all_running(self
):
63 def start_running(self
, key
, start_time
):
64 self
.running
.append((key
, start_time
))
66 def keep_running(self
, key
, start_time
):
67 return (key
, start_time
) in self
.running
69 def finished_running(self
, key
, start_time
):
70 if (key
, start_time
) in self
.running
:
71 self
.running
.remove((key
, start_time
))
73 def parse_config(self
):
74 stream
= open("config.yml", "r")
75 config
= yaml
.load(stream
)
78 aliases
= config
['aliases']
81 file_lock
= Lock("file")
87 for key
in config
['key_properties']:
88 if key
not in key_properties
:
89 key_properties
[key
] = {
91 "properties": config
['key_properties'][key
],
95 for mapped_key
in config
['keys']:
96 if mapped_key
not in key_properties
:
97 key_properties
[mapped_key
] = {
102 for action
in config
['keys'][mapped_key
]:
103 action_name
= list(action
)[0]
105 if action
[action_name
] is None:
106 action
[action_name
] = []
108 if 'include' in action
[action_name
]:
109 included
= action
[action_name
]['include']
110 del(action
[action_name
]['include'])
112 if isinstance(included
, str):
113 action
[action_name
].update(aliases
[included
], **action
[action_name
])
115 for included_
in included
:
116 action
[action_name
].update(aliases
[included_
], **action
[action_name
])
118 for argument
in action
[action_name
]:
119 if argument
== 'file':
120 filename
= action
[action_name
]['file']
121 if filename
not in seen_files
:
122 if filename
in config
['music_properties']:
123 seen_files
[filename
] = MusicFile(
127 **config
['music_properties'][filename
])
129 seen_files
[filename
] = MusicFile(
133 channel_id
= channel_id
+ 1
135 if filename
not in key_properties
[mapped_key
]['files']:
136 key_properties
[mapped_key
]['files'].append(seen_files
[filename
])
138 action_args
['music'] = seen_files
[filename
]
141 action_args
[argument
] = action
[action_name
][argument
]
143 key_properties
[mapped_key
]['actions'].append([action_name
, action_args
])
145 return (key_properties
, channel_id
+ 1, seen_files
)