我有一个带有标记作为元消息的 MIDI 文件。
fname = "avm.mid"
mid = MidiFile(fname) # input file of type 0
metas = [m for m in mid.tracks[0] if m.is_meta]
我已将元标记时间存储在列表“chordTimes”中。第一个制造商(和弦位置)不是从 0 开始。我制作了一个新的 MIDI 文件:
mo = MidiFile(type =1)# output file
track = MidiTrack()
现在我通读了我想要的和弦列表,并将它们添加到要添加到 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")
但是当我显示它时,和弦似乎不正确并且位置错误,并且看起来比原来的长得多(大约 3 倍)。我检查了输出的标题块:
<meta message time_signature numerator=4 denominator=4 clocks_per_click=24 notated_32nd_notes_per_beat=8 time=0>,
任何帮助将不胜感激。