Search code examples
pythonmidimusic21

Finding notes sounding at the same time in a different voice in a midi file


I have a midi file consisting of two parts. Now I need to print out, for each note in part 0 (including rests), which note sounds at the same time in part 1 (and the note that proceeds this).

I can go through all of the notes in part 0 with music21, but how do I find the note at that time in part 1. Will I need to use end times? Or is there a function for this?

for thisNote in s.parts[0].notes:
    print thisNote.ps

Solution

  • Here is a very interesting function that let me solve this:

    secondPart = s.parts[1]
    
    for thisNote in s.parts[0].notes:
    
    
       sys.stdout.write('at ' + str(thisNote.offset) + ' toppitch: ' + str(thisNote.ps))  
    
       soundingNotes = secondPart.getElementsByOffset(thisNote.offset,  mustFinishInSpan=False, mustBeginInSpan=False)
    
    
       try:
         if soundingNotes > 0:
           basslength = len(soundingNotes)
    
       except: 
         basslength = 0
    
       if basslength > 1:
         print "oh ow, more then one note sounding in the bass."
    
       if basslength > 0:
         if soundingNotes[0].isRest:
           sys.stdout.write(' bottompitch: R')
         else:
           sys.stdout.write(' bottompitch: ' + str(soundingNotes[0].ps)) # + ' from ' + str(soundingNotes[0].offset))
    
         # print the previous note
    
         soundingNotes2 = secondPart.getElementsByOffset(thisNote.offset-1,  mustFinishInSpan=False, mustBeginInSpan=False)