Skip to content

Instantly share code, notes, and snippets.

@aldous-rey
Last active June 3, 2023 07:38
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aldous-rey/68c6c43450517aa47474 to your computer and use it in GitHub Desktop.
Save aldous-rey/68c6c43450517aa47474 to your computer and use it in GitHub Desktop.
Transpose MIDI files into C Major or A Minor
#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)
@chyiochan
Copy link

I feel very silly, this is my first time ever using python. I'm trying to transpose a midi file to C but I'm unsure as to how! I've installed python and typed the location of this .py file, and even dragged and dropped. It states "SyntaxError: unexpected character after line continuation character"

File is in a folder on desktop with the Midi file wanting to be changed.

@kikomayorga
Copy link

hi! Thankls for the great work.... i feel the info on midi channels got changed through running this script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment