首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何遍历music21 chord对象并将chord的每个音符附加在一起,并将整个chord作为一个元素存储到一个列表中?

如何遍历music21 chord对象并将chord的每个音符附加在一起,并将整个chord作为一个元素存储到一个列表中?
EN

Stack Overflow用户
提问于 2019-08-09 09:21:48
回答 1查看 560关注 0票数 2

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

我决定我想要和弦的格式基本上是组成它的所有MIDI音符,由“”连接在一起。最后,我希望包含所有和弦、音符和持续时间的列表看起来像这样:

代码语言:javascript
复制
["note/chord1 duration1", 
"note/chord2 duration2",
...]

例如:

代码语言:javascript
复制
song = 
["65 1.0",          #(note,  duration)
 "65.71.66  0.5",   #(chord, duration)
 "59 2.0",
 "59.60 1.5",
 ...]

在这方面的任何帮助都将不胜感激,或者关于我的方法中哪里出了问题的任何解释也将不胜感激。

这是我已经尝试过的。真正的问题是for循环中的第二个elif语句;我得到了一些不受欢迎的输出。

代码语言:javascript
复制
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数字表示,但它似乎复制了许多相同的音符:

代码语言:javascript
复制
['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']
EN

回答 1

Stack Overflow用户

发布于 2019-08-11 02:24:10

代码语言:javascript
复制
notes_to_parse.stripTies(inPlace=True)

一个小建议:.notes仅仅是音符和和弦。如果您希望包含rests,请使用.notesAndRests

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57422408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档