问题
I have a MIDI file with marker as meta-messages.
fname = "avm.mid"
mid = MidiFile(fname) # input file of type 0
metas = [m for m in mid.tracks[0] if m.is_meta]
I have stored the meta marker times in the list "chordTimes". The first maker ( chord position) does not start at 0. I make a new MIDI file:
mo = MidiFile(type =1)# output file
track = MidiTrack()
Now I read through my list of desired chords and and add them to a new track to be added to mo.
for i in range(0, len(chords)-1):
chordInfo = chordMidiNotes(chords[i], extraBass= False)
chordNotes = chordInfo[0]
# append to track note on messages
if i != 0:
for note_value in chordNotes: # result has chord notes
track.append(Message('note_on', note=note_value, velocity=100, time=0))
else:
for j,note_value in enumerate(chordNotes): # result has chord notes
if j == 0:
track.append(Message('note_on', note=note_value, velocity=100, time=chordTimes[0]))
else:
track.append(Message('note_on', note=note_value, velocity=100, time=0))
# append to track note off messages
for k,note_value in enumerate(chordNotes): # result has chord notes
if k == 0:
track.append(Message('note_off', note=note_value, velocity=127, time=chordTimes[i+1]))
else:
track.append(Message('note_off', note=note_value, velocity=127, time=0))
# now adding track to output file
mo.tracks.append(mid.tracks[0])
mo.tracks.append(track)
mo.save("songWithChords.mid")
But when I display it, the chords seem to be incorrect and in the wrong position and look much longer ( about 3 times) than the original one. I checked the header chunk of the output:
<meta message time_signature numerator=4 denominator=4 clocks_per_click=24 notated_32nd_notes_per_beat=8 time=0>,
Any help would be greatly appreciated.
来源:https://stackoverflow.com/questions/61305523/adding-midi-chords-at-specific-metamessage-time