-
Notifications
You must be signed in to change notification settings - Fork 1
/
tune_to_white_keys.py
85 lines (57 loc) · 2.41 KB
/
tune_to_white_keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from pyscales import Scale, scaleformulas, devicekeyboards, Note
from pyscales.keys import PianoKeyboard, PianoKey
def tune_to_white_keys(keyboard: PianoKeyboard, scale: Scale, print_result=False):
"""
Returns number of semitones (positive or negative) the keyboard should
be tuned so all of the keys in given chord will be on white keys.
Returns None if could not be found.
:param keyboard:
:param scale:
:param print_result:
:return:
"""
tries = 13 # TODO: do both ways - negative and positive
tune = 0
found_tune_semitones = None
next_found_tune_semitones = None
for sign in [1, -1]:
for i in range(tries):
if found_tune_semitones:
if i>found_tune_semitones:
break; # we already found the lowest shift to use
keyboard.tune(i*sign)
found = True
for key in keyboard.get_keys_for_scale(scale=scale):
if key.black():
found=False
break
if found:
next_found_tune_semitones = i*sign
break
if found_tune_semitones is None:
found_tune_semitones = next_found_tune_semitones
else:
# use the lowest semitone shift possible in either direction
if (abs(next_found_tune_semitones) < abs(found_tune_semitones)):
found_tune_semitones = next_found_tune_semitones
if print_result:
print(f"{scale.scale_name()} ; Tune semitones: { found_tune_semitones if not None else 'not found'}")
if found_tune_semitones is not None:
print(keyboard.render_keys_in_ascii())
keyboard.tune(semitones=found_tune_semitones)
print(keyboard.render_note_scale_in_ascii(scale=scale, render_octave_number=False))
print() #empty line
return found_tune_semitones
def main():
formulas = [scaleformulas.MAJOR_FORMULA, scaleformulas.MINOR_FORMULA]
notes = PianoKey.DEFAULT_NOTES_ORDER_LIST
# for debugging
# formulas = [scaleformulas.MINOR_FORMULA]
# notes = [Note('c')]
keyboard = devicekeyboards.op1_keyboard
print(f"Tuning {keyboard.name} to use white keys for different scales:")
for formula in formulas:
for note in notes:
tune_to_white_keys(keyboard=keyboard, scale=Scale(note, formula), print_result=True)
if __name__ == '__main__':
main()