2

I need to loop through a given directory as transpose the midi files from their respective keys into a universal key (C). How can I do this in python?

Current Code:

This will be used to transpose the key in which each midi file is play in such that each file will be a universal key. This is used because if each midi file is in different keys the model will output a midi file that contains instructions that doesn't sound pleasing.

import music21 as m21
import glob


def main():
    #Get midi files
    fileHandler = FileHandler("./BluesMidi")
    nonTransposedMidi = fileHandler.GetSongFromDirectory()
    #print(nonProcessedMidi) prints a list of midi files

    transposer = TransposeMidiKey(nonTransposedMidi)
    transposer.TransposeMidi()


class FileHandler:
    #Attributes
    songs = []

    #Constructor
    def __init__(self, filePath):
        self._filePath = filePath

    #Properties

    #Methods
    def GetSongFromDirectory(self):
        for i in glob.iglob(self._filePath + '/*.mid'):
            self.songs.append(i)

        return self.songs

    def CreateNewTransposedMidi():
        pass


class TransposeMidiKey:
    #Attributes


    #Constructor
    def __init__(self, nonTransposedMidi):
        self._nonTransposedMidi = nonTransposedMidi

    #Properties


    #Methods
    def TransposeMidi(self):
        for i in self._nonTransposedMidi:
            # Change midi into stream objects
            musicScorData = m21.converter.parse(i)
            # Find current key of each midi file
            key = musicScorData.analyze('key')
            # Print the key and the mode (major or minor)
            #print(key.tonic.name, key.mode)

            # If the key is already major
            if key.mode == 'major':
                # Transpose it into the key of C
                interval = m21.interval.Interval(key.tonic, m21.pitch.Pitch('C'))


if __name__=='__main__':
    main()
dspencer
  • 4,297
  • 4
  • 22
  • 43
AndrewCooper
  • 47
  • 1
  • 4

0 Answers0