I am trying to transpose to a common key (C major/A minor) like this:
def main():
print("cwd=", os.getcwd())
path="midi/bachjs"
with os.scandir(path) as it:
for entry in it:
if entry.name.startswith("Minuet") and entry.name.endswith(".mid") and entry.is_file():
filename=Path(entry.path)
print(filename)
piece=converter.parse(filename)
key=piece.analyze('key')
target=pitch.Pitch('C')
if key.type == 'minor':
target=pitch.Pitch('A')
if target.name != key.tonic.name:
move = interval.Interval(key.tonic, target)
newpiece=piece.transpose(move)
newkey=newpiece.analyze('key')
fp = newpiece.write('midi', fp=filename.with_suffix('').with_suffix('.in'+target.name+'.mid'))
if __name__ == "__main__":
main()
Transposing BWV 114:
indeed shifts the initial D down to G etc., but the result is horrible:
How could I fix this? Thanks!


This is my first time using
music21. Hopefully, this answer is helpful.I noticed some problems with your code:
music21module names. Try using more descriptive names.Below is a similar program. Instead of working from a local MIDI file, it obtained the MusicXML file (
bach/bwv114.7.mxl) from the online corpus of MusicXML files provided bymusic21at MIT. I am unsure if this is actually the same piece that you worked with - the notes do not correspond at all to the image of the score you show.I used Guitar Pro to convert the MIDI file to PDF: