]>
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 . import yml_file
,gain
13 class Mapping(RelativeLayout
):
14 expected_keys
= NumericProperty(0)
15 master_volume
= NumericProperty(100)
16 ready_color
= ListProperty([1, 165/255, 0, 1])
18 def __init__(self
, **kwargs
):
19 self
.key_config
, 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)
28 def master_gain(self
):
29 return gain(self
.master_volume
)
31 def set_master_volume(self
, value
, delta
= False):
32 [db_gain
, self
.master_volume
] = gain(value
+ int(delta
) * self
.master_volume
, self
.master_volume
)
33 for music
in self
.open_files
.values():
34 music
.set_gain(db_gain
)
36 def _keyboard_closed(self
):
37 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
40 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
41 key
= self
.find_by_key_code(keycode
)
42 if len(modifiers
) == 0 and key
is not None:
43 threading
.Thread(name
= "MSKeyAction", target
=key
.do_actions
).start()
44 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
45 for thread
in threading
.enumerate():
46 if thread
.getName()[0:2] != "MS":
53 def find_by_key_code(self
, key_code
):
54 if "Key_" + str(key_code
[0]) in self
.ids
:
55 return self
.ids
["Key_" + str(key_code
[0])]
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
):
68 running
= self
.running
70 for (key
, start_time
) in running
:
71 key
.interrupt_action()
73 def start_running(self
, key
, start_time
):
74 self
.running
.append((key
, start_time
))
76 def keep_running(self
, key
, start_time
):
77 return (key
, start_time
) in self
.running
79 def finished_running(self
, key
, start_time
):
80 if (key
, start_time
) in self
.running
:
81 self
.running
.remove((key
, start_time
))
83 def parse_config(self
):
84 stream
= open(yml_file(), "r")
85 config
= yaml
.load(stream
)
88 aliases
= config
['aliases']
93 for key
in config
['key_properties']:
94 if key
not in key_properties
:
95 key_prop
= config
['key_properties'][key
]
96 if 'include' in key_prop
:
97 included
= key_prop
['include']
98 del(key_prop
['include'])
100 if isinstance(included
, str):
101 key_prop
.update(aliases
[included
], **key_prop
)
103 for included_
in included
:
104 key_prop
.update(aliases
[included_
], **key_prop
)
106 key_properties
[key
] = {
108 "properties": key_prop
,
112 for mapped_key
in config
['keys']:
113 if mapped_key
not in key_properties
:
114 key_properties
[mapped_key
] = {
119 for action
in config
['keys'][mapped_key
]:
120 action_name
= list(action
)[0]
122 if action
[action_name
] is None:
123 action
[action_name
] = []
125 if 'include' in action
[action_name
]:
126 included
= action
[action_name
]['include']
127 del(action
[action_name
]['include'])
129 if isinstance(included
, str):
130 action
[action_name
].update(aliases
[included
], **action
[action_name
])
132 for included_
in included
:
133 action
[action_name
].update(aliases
[included_
], **action
[action_name
])
135 for argument
in action
[action_name
]:
136 if argument
== 'file':
137 filename
= action
[action_name
]['file']
138 if filename
not in seen_files
:
139 if filename
in config
['music_properties']:
140 seen_files
[filename
] = MusicFile(
143 **config
['music_properties'][filename
])
145 seen_files
[filename
] = MusicFile(
149 if filename
not in key_properties
[mapped_key
]['files']:
150 key_properties
[mapped_key
]['files'].append(seen_files
[filename
])
152 action_args
['music'] = seen_files
[filename
]
155 action_args
[argument
] = action
[action_name
][argument
]
157 key_properties
[mapped_key
]['actions'].append([action_name
, action_args
])
159 return (key_properties
, seen_files
)