因此,我正在尝试使用Music21库解析一首MIDI歌曲。这首歌的音符有不同的时长、和弦和休止符。我希望能够解析歌曲并检查MIDI事件是否为音符,如果是,则将其MIDI编号和持续时间(quarterLength)存储到列表中。我还想检查它是否是和弦,如果是,则将和弦及其持续时间存储在列表中。
我决定我想要和弦的格式基本上是组成它的所有MIDI音符,由“”连接在一起。最后,我希望包含所有和弦、音符和持续时间的列表看起来像这样:
["note/chord1 duration1",
"note/chord2 duration2",
...]例如:
song =
["65 1.0", #(note, duration)
"65.71.66 0.5", #(chord, duration)
"59 2.0",
"59.60 1.5",
...]在这方面的任何帮助都将不胜感激,或者关于我的方法中哪里出了问题的任何解释也将不胜感激。
这是我已经尝试过的。真正的问题是for循环中的第二个elif语句;我得到了一些不受欢迎的输出。
file = "song.mid"
midi_song = converter.parse(file)
print("Parsing %s" % file)
notes_to_parse = None
try: # file has instrument parts
s2 = instrument.partitionByInstrument(midi_song)
notes_to_parse = s2.parts[0].recurse()
except: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
midi_number.append(str(element.pitch.midi) + " " + str(element.quarterLength))
elif isinstance(element, chord.Chord):
midi_chord_number.append(".".join(str(n.pitch.midi) for n in element) + " " + str(element.quarterLength))
elif isinstance(element, note.Rest):
notes.append(str(element.name) + " " + str(element.quarterLength))输出应该具有已遇到的音符或和弦的MIDI数字表示,但它似乎复制了许多相同的音符:
['69.73.76',
'69.73.76',
'69.73.76 4/3',
'69.73.76 4/3',
'69.73.76 1.75',
'69.73.76 1.75',
'69.73.76 0.75',
'69.73.76 0.75',
'69.73.76 0.75',
'69.73.76 0.75',
'69.73.76 4/3',
'69.73.76 4/3',
'69.73.76 4/3',
'69.73.76 4/3',
'69.73.76 1.0',
'69.73.76 1.0',
'69.73.76 1.0',
'69.73.76 1.0',
'69.73.76 1.0',
'69.73.76 1.0',
'69.73.76 8/3',
'69.73.76 1.0',
'69.73.76 4/3',
'69.73.76 2.0',
'69.73.76 8/3',
'69.73.76 1.0',
'69.73.76 4/3',
'69.73.76 2.0',
'62.70 4/3',
'62.70 4/3',
'62.70 1.75',
'62.70 1.75',
'62.70 0.75',
'62.70 0.75',
'62.70 0.75',
'62.70 0.75',
'62.70 4/3',
'62.70 4/3',
'62.70 4/3',
'62.70 4/3',
'62.70 1.0',
'62.70 1.0',
'62.70 1.0',
'62.70 1.0',
'62.70 1.0',
'62.70 1.0',
'62.70 8/3',
'62.70 1.0',
'62.70 4/3',
'62.70 2.0',
'62.70 8/3',
'62.70 1.0',
'62.70 4/3',
'62.70 2.0',
'62.70 4/3',
'62.70 4/3',
'60.69 1.75',
'60.69 1.75',
'60.69 0.75',
'60.69 0.75',
'62.71 0.75',
'62.71 0.75',
'64.72 4/3',
'64.72 4/3',
'62.70 4/3',
'62.70 4/3',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'62.71 1.0',
'62.71 1.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0',
'62.70 4/3',
'62.70 4/3',
'60.69 1.75',
'60.69 1.75',
'60.69 0.75',
'60.69 0.75',
'62.71 0.75',
'62.71 0.75',
'64.72 4/3',
'64.72 4/3',
'62.70 4/3',
'62.70 4/3',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'62.71 1.0',
'62.71 1.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0',
'62.70 4/3',
'62.70 4/3',
'60.69 1.75',
'60.69 1.75',
'60.69 0.75',
'60.69 0.75',
'62.71 0.75',
'62.71 0.75',
'64.72 4/3',
'64.72 4/3',
'62.70 4/3',
'62.70 4/3',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'60.69 1.0',
'62.71 1.0',
'62.71 1.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0',
'55.60 8/3',
'64.72.79 1.0',
'67.60 4/3',
'36.48 2.0']发布于 2019-08-11 02:24:10
notes_to_parse.stripTies(inPlace=True)一个小建议:.notes仅仅是音符和和弦。如果您希望包含rests,请使用.notesAndRests。
https://stackoverflow.com/questions/57422408
复制相似问题