Search code examples
pythonpython-3.xparsingmidimusic21

How to Extract Individual Chords, Rests, and Notes from a midi file?


I am making a program that should be able to extract the notes, rests, and chords from a certain midi file and write the respective pitch (in midi tone numbers - they go from 0-127) of the notes and chords to a csv file for later use.

For this project, I am using the Python Library "Music21".

 from music21 import *
 import pandas as pd


 #SETUP

 path = r"Pirates_TheCarib_midi\1225766-Pirates_of_The_Caribbean_Medley.mid"

 #create a function for taking parsing and extracting the notes
 def extract_notes(path):
     stm = converter.parse(path)

     treble = stm[0] #access the first part (if there is only one part)
     bass = stm[1]

     #note extraction
     notes_treble = []
     notes_bass = []
     for thisNote in treble.getElementsByClass("Note"):
         indiv_note = [thisNote.name, thisNote.pitch.midi, thisNote.offset]
         notes_treble.append(indiv_note)  # print's the note and the note's 
         offset

     for thisNote in bass.getElementsByClass("Note"):
         indiv_note = [thisNote.name, thisNote.pitch.midi, thisNote.offset]
         notes_bass.append(indiv_note) #add the notes to the bass

     return notes_treble, notes_bass

 #write to csv
 def to_csv(notes_array):
     df = pd.DataFrame(notes_array, index=None, columns=None)
     df.to_csv("attempt1_v1.csv")

 #using the functions
 notes_array = extract_notes(path)
 #to_csv(notes_array)

 #DEBUGGING
 stm = converter.parse(path)
 print(stm.parts)

Here is the link to the score I am using as a test. https://musescore.com/user/1699036/scores/1225766

When I run the extract_notes function, it returns two empty arrays and the line:

print(stm.parts)

it returns

<music21.stream.iterator.StreamIterator for Score:0x1b25dead550 @:0>

I am confused as to why it does this. The piece should have two parts, treble and bass. How can I get each note, chord and rest into an array so I can put it in a csv file?


Solution

  • Here is small snippet of how I did it. I needed to get all notes, chords and rests for specific instruments. So at first I iterated through part and found specific instrument and afterwards check what kind of note it is and append it.

    you can call this method like this:

    notes = get_notes_chords_rests(keyboard_instruments, "Pirates_of_The_Caribbean.mid")
    

    where keyboard_instruments is a list of instruments:

    keyboard_instruments = ["KeyboardInstrument", "Piano", "Harpsichord", "Clavichord", "Celesta", ]

    def get_notes_chords_rests(instrument_type, path):
        try:
            midi = converter.parse(path)
            parts = instrument.partitionByInstrument(midi)
            note_list = []
            for music_instrument in range(len(parts)):
                if parts.parts[music_instrument].id in instrument_type:
                    for element_by_offset in stream.iterator.OffsetIterator(parts[music_instrument]):
                        for entry in element_by_offset:
                            if isinstance(entry, note.Note):
                                note_list.append(str(entry.pitch))
                            elif isinstance(entry, chord.Chord):
                                note_list.append('.'.join(str(n) for n in entry.normalOrder))
                            elif isinstance(entry, note.Rest):
                                note_list.append('Rest')
            return note_list
        except Exception as e:
            print("failed on ", path)
            pass
    

    P.S. It is important to use try block because a lot of midi files on the web are corrupted.