I parsed a MIDI file in music21 by using converter.parse
function in music21.
And for extracting notes and chords with duration, I have used the code as follows:
score = converter.parse('midiexample.mid')
flat = score.flat
for el in flat.notes:
if isinstance(el, music21.note.Note):
pitch = el.pitch
note_name = pitch.nameWithOctave
new_note_c = note.Note(note_name)
new_note_c.duration = el.duration
dur = round(float(el.duration.quarterLength), 1)
all_elements.append((str(note_name), dur))
elif isinstance(el, music21.chord.Chord):
chord_pitches = []
for p in el.pitches:
#note_name = p.pitch
#note_name_oct = note_name.nameWithOctave
new_note_c = note.Note(p.nameWithOctave)
#new_note_c.duration = el.duration
dur = round(float(el.duration.quarterLength), 1)
chord_pitches.append((str(new_note_c.nameWithOctave)))
chord_pitches = ".".join([i for i in chord_pitches])
all_elements.append((chord_pitches, dur))
def get_music_midi_filename_from_chords(input_chords):
midi_stream = stream.Stream()
for note_pattern, duration_pattern in input_chords:
if('.' in note_pattern):
notes_in_chord = note_pattern.split('.')
chord_notes = []
for current_note in notes_in_chord:
new_note = note.Note(current_note)
new_note.duration = duration.Duration(duration_pattern)
new_note.storedInstrument = instrument.Piano()
chord_notes.append(new_note)
new_chord = chord.Chord(chord_notes)
print(f'New_chord appended as {new_chord}')
else:
note_single = note.Note(note_pattern)
note_single.duration = duration.Duration(duration_pattern)
note_single.storedInstrument = instrument.Piano()
new_chord = note_single
print(f'Appended note {new_chord}')
midi_stream.append(new_chord)
#new_tempo = tempo.MetronomeMark(number=50)
#midi_stream.append(new_tempo)
midi_stream = midi_stream.chordify()
timestr = time.strftime("%Y%m%d-%H%M%S")
new_file = 'output-' + timestr + '.mid'
return midi_stream.write('midi', fp=new_file
)
When I call the function to get music, the MIDI file I produced has 2:51 time duration whereas the song I passed only has 1:16 of time duration. What am I doing wrong in this case? I need to make sure I am able to get the notes and chords along with their duration as correct as I can get. But an addition of a minute seems fishy. Is the duration rounding the cause? I am not able to debug this error.
Also, while comparing, I got the MIDI comparison of original MIDI and my produced MIDI and I found them as follows:
Original MIDI is dark blue, my produced one is longer. The notes are somewhat similar but the duration is prolonged in my code: