]>
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 .mixer
import Mixer
12 from . import Config
, gain
14 class Mapping(RelativeLayout
):
15 expected_keys
= NumericProperty(0)
16 master_volume
= NumericProperty(100)
17 ready_color
= ListProperty([1, 165/255, 0, 1])
19 def __init__(self
, **kwargs
):
20 if Config
.builtin_mixing
:
24 self
.key_config
, self
.open_files
= self
.parse_config()
25 super(Mapping
, self
).__init
__(**kwargs
)
26 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
27 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
29 Clock
.schedule_interval(self
.not_all_keys_ready
, 1)
32 def master_gain(self
):
33 return gain(self
.master_volume
)
35 def set_master_volume(self
, value
, delta
=False):
36 [db_gain
, self
.master_volume
] = gain(
37 value
+ int(delta
) * self
.master_volume
,
40 for music
in self
.open_files
.values():
41 music
.set_gain(db_gain
)
43 def _keyboard_closed(self
):
44 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
47 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
48 key
= self
.find_by_key_code(keycode
)
49 if len(modifiers
) == 0 and key
is not None:
50 threading
.Thread(name
="MSKeyAction", target
=key
.do_actions
).start()
51 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
52 for thread
in threading
.enumerate():
53 if thread
.getName()[0:2] != "MS":
60 def find_by_key_code(self
, key_code
):
61 if "Key_" + str(key_code
[0]) in self
.ids
:
62 return self
.ids
["Key_" + str(key_code
[0])]
65 def not_all_keys_ready(self
, dt
):
66 for key
in self
.children
:
67 if not type(key
).__name
__ == "Key":
69 if not key
.is_key_ready
:
71 self
.ready_color
= [0, 1, 0, 1]
74 def stop_all_running(self
):
75 running
= self
.running
77 for (key
, start_time
) in running
:
78 key
.interrupt_action()
80 def start_running(self
, key
, start_time
):
81 self
.running
.append((key
, start_time
))
83 def keep_running(self
, key
, start_time
):
84 return (key
, start_time
) in self
.running
86 def finished_running(self
, key
, start_time
):
87 if (key
, start_time
) in self
.running
:
88 self
.running
.remove((key
, start_time
))
90 def parse_config(self
):
91 stream
= open(Config
.yml_file
, "r")
92 config
= yaml
.load(stream
)
95 aliases
= config
['aliases']
100 for key
in config
['key_properties']:
101 if key
not in key_properties
:
102 key_prop
= config
['key_properties'][key
]
103 if 'include' in key_prop
:
104 included
= key_prop
['include']
105 del(key_prop
['include'])
107 if isinstance(included
, str):
108 key_prop
.update(aliases
[included
], **key_prop
)
110 for included_
in included
:
111 key_prop
.update(aliases
[included_
], **key_prop
)
113 key_properties
[key
] = {
115 "properties": key_prop
,
119 for mapped_key
in config
['keys']:
120 if mapped_key
not in key_properties
:
121 key_properties
[mapped_key
] = {
126 for action
in config
['keys'][mapped_key
]:
127 action_name
= list(action
)[0]
129 if action
[action_name
] is None:
130 action
[action_name
] = []
132 if 'include' in action
[action_name
]:
133 included
= action
[action_name
]['include']
134 del(action
[action_name
]['include'])
136 if isinstance(included
, str):
137 action
[action_name
].update(
139 **action
[action_name
])
141 for included_
in included
:
142 action
[action_name
].update(
144 **action
[action_name
])
146 for argument
in action
[action_name
]:
147 if argument
== 'file':
148 filename
= action
[action_name
]['file']
149 if filename
not in seen_files
:
150 if filename
in config
['music_properties']:
151 seen_files
[filename
] = MusicFile(
154 **config
['music_properties'][filename
])
156 seen_files
[filename
] = MusicFile(
160 if filename
not in key_properties
[mapped_key
]['files']:
161 key_properties
[mapped_key
]['files'] \
162 .append(seen_files
[filename
])
164 action_args
['music'] = seen_files
[filename
]
167 action_args
[argument
] = action
[action_name
][argument
]
169 key_properties
[mapped_key
]['actions'] \
170 .append([action_name
, action_args
])
172 return (key_properties
, seen_files
)