2

所以我正在尝试使用 Music21 库解析一首 MIDI 歌曲。这首歌的音符有不同的持续时间、和弦和休止符。我希望能够解析歌曲并检查 MIDI 事件是否是音符,如果是,将其 MIDI 编号和持续时间(四分之一长度)存储到列表中。我还想检查它是否是和弦,如果是,则将和弦及其持续时间存储在列表中。

我决定和弦的格式基本上是组成它的所有 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']
4

1 回答 1

0

音符可能相互关联,因此您最好先运行

notes_to_parse.stripTies(inPlace=True)

一点建议: .notes只是音符和和弦。如果您想包括休息,请使用.notesAndRests.

于 2019-08-10T18:24:09.557 回答