music21:和弦在写入midi文件后变成音符



这就是我得到注释的方式:

def read_notes_from_file(file):
notes = []
song = converter.parse(file)
notes_in_song = None
try:
s2 = instrument.partitionByInstrument(song)
notes_in_song = s2.parts[0].recurse()
except:
notes_in_song = song.flat.notes
for element in notes_in_song:
if isinstance(element, note.Note):
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
return notes

这就是我创建midi:的方式

def create_midi(prediction_output, filename):
offset = 0
output_notes = []
for item in prediction_output:
pattern = item[0]
if ('.' in pattern) or pattern.isdigit():
notes_in_chord = pattern.split('.')
notes = []
for current_note in notes_in_chord:
new_note = note.Note(int(current_note))
new_note.storedInstrument = instrument.Piano()
notes.append(new_note)
new_chord = chord.Chord(notes)
new_chord.offset = offset
output_notes.append(new_chord)
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
offset += 0.5
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='{}.mid'.format(filename))

这是我输入create_midi:的注释

['G1', 'G6', 'C#5', '9.0.2.4', 'E-7', '2.5.7.9', 'C#6', '7.9.1.2', '11.2.4.7', '4.9.10', '3.5.7.11', '8.9.0', '11.2.3', '5.7.10', '7.0', '6.10.11', '2.3.5.9', '2.8', '5.11', '2.4.5', '5.6.7', '5.7.9.11', '1.2.5.8', '6.7.11.1.2', '1.5.8.9', '2', '3.4.8', '10.11.3.6', '4.5.8.9', '3.6.7', '4.5.7.11', '4.5.9', '3.7.11', '3.5.7.8', '5.9', '5.7.10.11', '2.4.6.10', '5.7.10.11', '10.11.0.2.3', '3.4.8', '11.4', '10.0.4.5', '5.8', '11.2.4.6', '2.6.7', '3.6.7.8.9', '4.5.8', '5.8.11.1', '2.5.7', '11.1.4', '1.4.6.9', '11.1', '4.6.7.11', '11.3.6.7', '2.4.6.9', '2.4', '2.8', '4.8.11', '11.2.5', '2.4.6.10', '10.11.3.6', '2.6.10', '5.8.10.0.1', '2.5.8.10', '0.6', '3.7.10.11', '3.6', '3.6.10', '2.4.6', '5.11', '11.0.4.7', '10.2.5', '11.1.2', '3.6.9', '5.6.9', '5.7.9.11.2', '5.8.10.1', '1.2.4.6.9', '6.7.11.2', '3.5.8.9', '2.3.7.10', '3.6', '6.8.9.11.1', '1.4.7.9.10', '2.5.8.10', '6.10.11', '1.4.7', '4.5.8', '6.8.9.1', '5.8', '2.3.6.9', '3.4.6.8.11', '11.2.4.7', '10.1', '11.3.4.6', '4.5.7', '3.6.9', '11.2.4.7', '5.7.10', '3.7.10']

这是我在将上面的注释创建的midi输入read_notes_from_file:后得到的注释

['G4', 'G4', 'C4', 'A4', 'E4', 'D4', 'C4', 'G4', 'C#4', 'E4', 'E-4', 'G#4', 'C#4', 'F4', 'G4', 'F#4', 'D4', 'D4', 'F4', 'D4', 'F4', 'F4', 'C#4', 'F#4', 'C#4', 'D4', 'E-4', 'C#4', 'E4', 'E-4', 'E4', 'E4', 'E-4', 'E-4', 'F4', 'F4', 'D4', 'F4', 'C#4', 'E-4', 'C#4', 'C#4', 'F4', 'C#4', 'D4', 'E-4', 'E4', 'F4', 'D4', 'C#4', 'C#4', 'C#4', 'E4', 'C#4', 'D4', 'D4', 'D4', 'E4', 'C#4', 'D4', 'C#4', 'D4', 'F4', 'D4', 'C4', 'E-4', 'E-4', 'E-4', 'D4', 'F4', 'C#4', 'C#4', 'C#4', 'E-4', 'F4', 'F4', 'F4', 'C#4', 'F#4', 'E-4', 'D4', 'E-4', 'F#4', 'C#4', 'D4', 'F#4', 'C#4', 'E4', 'F#4', 'F4', 'D4', 'E-4', 'C#4', 'C#4', 'C#4', 'E4', 'E-4', 'C#4', 'F4', 'E-4']

它们只是和弦中的音符,我不知道该怎么处理。代码有什么问题吗?

如果在pattern = item[0]之后添加print(pattern),您会发现您正在以一种意想不到的方式调整输入。C#变成C并且2.5.7变成2。这就是为什么和弦被导出为单个音符的原因。

最新更新