Last active
June 3, 2023 07:38
-
-
Save aldous-rey/68c6c43450517aa47474 to your computer and use it in GitHub Desktop.
Transpose MIDI files into C Major or A Minor
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
#converts all midi files in the current folder | |
import glob | |
import os | |
import music21 | |
#converting everything into the key of C major or A minor | |
# major conversions | |
majors = dict([("A-", 4),("A", 3),("B-", 2),("B", 1),("C", 0),("D-", -1),("D", -2),("E-", -3),("E", -4),("F", -5),("G-", 6),("G", 5)]) | |
minors = dict([("A-", 1),("A", 0),("B-", -1),("B", -2),("C", -3),("D-", -4),("D", -5),("E-", 6),("E", 5),("F", 4),("G-", 3),("G", 2)]) | |
#os.chdir("./") | |
for file in glob.glob("*.mid"): | |
score = music21.converter.parse(file) | |
key = score.analyze('key') | |
# print key.tonic.name, key.mode | |
if key.mode == "major": | |
halfSteps = majors[key.tonic.name] | |
elif key.mode == "minor": | |
halfSteps = minors[key.tonic.name] | |
newscore = score.transpose(halfSteps) | |
key = newscore.analyze('key') | |
print key.tonic.name, key.mode | |
newFileName = "C_" + file | |
newscore.write('midi',newFileName) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi! Thankls for the great work.... i feel the info on midi channels got changed through running this script?