Created
September 27, 2020 09:59
-
-
Save dhavalsavalia/65ee96ac45425383646a27bd50e4834d to your computer and use it in GitHub Desktop.
Transposes musical scales Pythonically!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root_notes = [ | |
'A', | |
'A#', | |
'B', | |
'C', | |
'C#', | |
'D', | |
'D#', | |
'E', | |
'F', | |
'F#', | |
'G', | |
'G#' | |
] | |
full = 2 | |
half = 1 | |
intervals = dict() | |
# Please add more intervals | |
intervals['major'] = [full, full, half, full, full, full, half] | |
intervals['natural_minor'] = [full, half, full, full, half, full, full] | |
def scalify(root, interval): | |
root_number = root_notes.index(root) | |
scale_intervals = intervals[interval] | |
scale = [root_notes[root_number]] | |
# deque from collections package would be more elegant | |
for tone in scale_intervals: | |
root_number += tone | |
if root_number > 11: | |
root_number -= 12 | |
scale.append(root_notes[root_number]) | |
return scale | |
def transpose(original, new, interval): | |
original_scale = scalify(original, interval) | |
new_scale = scalify(new, interval) | |
return list(zip(original_scale, new_scale)) | |
# Demo | |
d_to_c = transpose('D', 'C', 'major') | |
for o, n in d_to_c: | |
print(f"{o:<2} -> {n}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment