]>
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():
42 if not (music
.is_loaded_playing() or music
.is_loaded_paused()):
46 music
.gain_effects
.append(GainEffect(
48 music
.current_audio_segment
,
51 music
.sound_position
+ fade
,
54 music
.set_gain(db_gain
)
56 def _keyboard_closed(self
):
57 self
._keyboard
.unbind(on_key_down
=self
._on
_keyboard
_down
)
60 def _on_keyboard_down(self
, keyboard
, keycode
, text
, modifiers
):
61 key
= self
.find_by_key_code(keycode
)
62 if len(modifiers
) == 0 and key
is not None:
63 threading
.Thread(name
="MSKeyAction", target
=key
.do_actions
).start()
64 elif 'ctrl' in modifiers
and (keycode
[0] == 113 or keycode
[0] == '99'):
65 for thread
in threading
.enumerate():
66 if thread
.getName()[0:2] != "MS":
73 def find_by_key_code(self
, key_code
):
74 if "Key_" + str(key_code
[0]) in self
.ids
:
75 return self
.ids
["Key_" + str(key_code
[0])]
78 def not_all_keys_ready(self
, dt
):
79 for key
in self
.children
:
80 if not type(key
).__name
__ == "Key":
82 if not key
.is_key_ready
:
84 self
.ready_color
= [0, 1, 0, 1]
87 def stop_all_running(self
):
88 running
= self
.running
90 for (key
, start_time
) in running
:
91 key
.interrupt_action()
93 def start_running(self
, key
, start_time
):
94 self
.running
.append((key
, start_time
))
96 def keep_running(self
, key
, start_time
):
97 return (key
, start_time
) in self
.running
99 def finished_running(self
, key
, start_time
):
100 if (key
, start_time
) in self
.running
:
101 self
.running
.remove((key
, start_time
))
103 def parse_config(self
):
104 stream
= open(Config
.yml_file
, "r")
106 config
= yaml
.load(stream
)
107 except Exception as e
:
108 error_print("Error while loading config file: {}".format(e
))
112 aliases
= config
['aliases']
117 for key
in config
['key_properties']:
118 if key
not in key_properties
:
119 key_prop
= config
['key_properties'][key
]
120 if 'include' in key_prop
:
121 included
= key_prop
['include']
122 del(key_prop
['include'])
124 if isinstance(included
, str):
125 key_prop
.update(aliases
[included
], **key_prop
)
127 for included_
in included
:
128 key_prop
.update(aliases
[included_
], **key_prop
)
130 key_properties
[key
] = {
132 "properties": key_prop
,
136 for mapped_key
in config
['keys']:
137 if mapped_key
not in key_properties
:
138 key_properties
[mapped_key
] = {
143 for action
in config
['keys'][mapped_key
]:
144 action_name
= list(action
)[0]
146 if action
[action_name
] is None:
147 action
[action_name
] = []
149 if 'include' in action
[action_name
]:
150 included
= action
[action_name
]['include']
151 del(action
[action_name
]['include'])
153 if isinstance(included
, str):
154 action
[action_name
].update(
156 **action
[action_name
])
158 for included_
in included
:
159 action
[action_name
].update(
161 **action
[action_name
])
163 for argument
in action
[action_name
]:
164 if argument
== 'file':
165 filename
= action
[action_name
]['file']
166 if filename
not in seen_files
:
167 if filename
in config
['music_properties']:
168 seen_files
[filename
] = MusicFile(
171 **config
['music_properties'][filename
])
173 seen_files
[filename
] = MusicFile(
177 if filename
not in key_properties
[mapped_key
]['files']:
178 key_properties
[mapped_key
]['files'] \
179 .append(seen_files
[filename
])
181 action_args
['music'] = seen_files
[filename
]
184 action_args
[argument
] = action
[action_name
][argument
]
186 key_properties
[mapped_key
]['actions'] \
187 .append([action_name
, action_args
])
189 return (key_properties
, seen_files
)