]>
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
, error_print
13 from .music_effect
import GainEffect
15 class Mapping(RelativeLayout
):
16 expected_keys
= NumericProperty(0)
17 master_volume
= NumericProperty(100)
18 ready_color
= ListProperty([1, 165/255, 0, 1])
20 def __init__(self
, **kwargs
):
21 if Config
.builtin_mixing
:
25 self
.key_config
, self
.open_files
= self
.parse_config()
26 super(Mapping
, self
).__init
__(**kwargs
)
27 self
._keyboard
= Window
.request_keyboard(self
._keyboard
_closed
, self
)
28 self
._keyboard
.bind(on_key_down
=self
._on
_keyboard
_down
)
30 Clock
.schedule_interval(self
.not_all_keys_ready
, 1)
33 def master_gain(self
):
34 return gain(self
.master_volume
)
36 def set_master_volume(self
, value
, delta
=False, fade
=0):
37 [db_gain
, self
.master_volume
] = gain(
38 value
+ int(delta
) * self
.master_volume
,
41 for music
in self
.open_files
.values():
43 music
.gain_effects
.append(GainEffect(
45 music
.current_audio_segment
,
48 music
.sound_position
+ fade
,
51 music
.set_gain(db_gain
)
53 def _keyboard_closed(self
):
54 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
57 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
58 key
= self
.find_by_key_code(keycode
)
59 if len(modifiers
) == 0 and key
is not None:
60 threading
.Thread(name
="MSKeyAction", target
=key
.do_actions
).start()
61 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
62 for thread
in threading
.enumerate():
63 if thread
.getName()[0:2] != "MS":
70 def find_by_key_code(self
, key_code
):
71 if "Key_" + str(key_code
[0]) in self
.ids
:
72 return self
.ids
["Key_" + str(key_code
[0])]
75 def not_all_keys_ready(self
, dt
):
76 for key
in self
.children
:
77 if not type(key
).__name
__ == "Key":
79 if not key
.is_key_ready
:
81 self
.ready_color
= [0, 1, 0, 1]
84 def stop_all_running(self
):
85 running
= self
.running
87 for (key
, start_time
) in running
:
88 key
.interrupt_action()
90 def start_running(self
, key
, start_time
):
91 self
.running
.append((key
, start_time
))
93 def keep_running(self
, key
, start_time
):
94 return (key
, start_time
) in self
.running
96 def finished_running(self
, key
, start_time
):
97 if (key
, start_time
) in self
.running
:
98 self
.running
.remove((key
, start_time
))
100 def parse_config(self
):
101 stream
= open(Config
.yml_file
, "r")
103 config
= yaml
.load(stream
)
104 except Exception as e
:
105 error_print("Error while loading config file: {}".format(e
))
109 aliases
= config
['aliases']
114 for key
in config
['key_properties']:
115 if key
not in key_properties
:
116 key_prop
= config
['key_properties'][key
]
117 if 'include' in key_prop
:
118 included
= key_prop
['include']
119 del(key_prop
['include'])
121 if isinstance(included
, str):
122 key_prop
.update(aliases
[included
], **key_prop
)
124 for included_
in included
:
125 key_prop
.update(aliases
[included_
], **key_prop
)
127 key_properties
[key
] = {
129 "properties": key_prop
,
133 for mapped_key
in config
['keys']:
134 if mapped_key
not in key_properties
:
135 key_properties
[mapped_key
] = {
140 for action
in config
['keys'][mapped_key
]:
141 action_name
= list(action
)[0]
143 if action
[action_name
] is None:
144 action
[action_name
] = []
146 if 'include' in action
[action_name
]:
147 included
= action
[action_name
]['include']
148 del(action
[action_name
]['include'])
150 if isinstance(included
, str):
151 action
[action_name
].update(
153 **action
[action_name
])
155 for included_
in included
:
156 action
[action_name
].update(
158 **action
[action_name
])
160 for argument
in action
[action_name
]:
161 if argument
== 'file':
162 filename
= action
[action_name
]['file']
163 if filename
not in seen_files
:
164 if filename
in config
['music_properties']:
165 seen_files
[filename
] = MusicFile(
168 **config
['music_properties'][filename
])
170 seen_files
[filename
] = MusicFile(
174 if filename
not in key_properties
[mapped_key
]['files']:
175 key_properties
[mapped_key
]['files'] \
176 .append(seen_files
[filename
])
178 action_args
['music'] = seen_files
[filename
]
181 action_args
[argument
] = action
[action_name
][argument
]
183 key_properties
[mapped_key
]['actions'] \
184 .append([action_name
, action_args
])
186 return (key_properties
, seen_files
)