简体   繁体   English

使用带有 partitionByInstrument() 的 music21 读取 MIDI 文件以获取返回空列表的音符和和弦

[英]reading MIDI file using music21 with partitionByInstrument() to get notes and chords returning empty list

I'm trying to use the code from this github repository's .py file (the get_notes() function) to extract the notes and chords data from this midi file .我正在尝试使用这个github 存储库的 .py 文件(get_notes() 函数)中的代码从这个 midi 文件中提取音符和和弦数据。 Here is the exact code copied from the repo I'm using:这是从我正在使用的 repo 复制的确切代码:

def get_notes():
    """ Get all the notes and chords from the midi files in the ./midi_songs directory """
    notes = []

    for file in glob.glob("midi_songs/*.mid"):
        midi = converter.parse(file)

        print("Parsing %s" % file)

        notes_to_parse = None

        try: # file has instrument parts
            s2 = instrument.partitionByInstrument(midi)
            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):
                notes.append(str(element.pitch))
            elif isinstance(element, chord.Chord):
                notes.append('.'.join(str(n) for n in element.normalOrder))

    with open('data/notes', 'wb') as filepath:
        pickle.dump(notes, filepath)

    return notes

My midi file has multiple instruments, which I believe for some reason is causing nothing to be appended to the notes list.我的 MIDI 文件有多个乐器,我相信出于某种原因,这些乐器不会导致任何内容附加到音符列表中。 Seeing as I've just cloned the repository's code and run it on my own midi file, I'm not sure why it isn't working.看到我刚刚克隆了存储库的代码并在我自己的 midi 文件上运行它,我不确定为什么它不起作用。 I've looked through the documentation for music21's partitionByInstrument(), and tried to iterate through the different instruments by changing:我已经查看了 music21 的 partitionByInstrument() 的文档,并尝试通过更改来遍历不同的乐器:

notes_to_parse = s2.parts[0].recurse()

to

 notes_to_parse = s2.parts[1].recurse()

as different parts should be different instruments from the file, but it still returns an empty list.因为不同的部分应该是文件中不同的乐器,但它仍然返回一个空列表。 What do I do?我该怎么办?

v6.1 added a feature to create Instrument objects from MIDI instrument name and track name messages. v6.1 添加了一项功能,可根据 MIDI 乐器名称和轨道名称消息创建Instrument对象。 v6.5, which came out today, added a feature to remove duplicates caused when there is also an Instrument from a program change message at the same tick.今天发布的 v6.5 添加了一项功能,可以删除在同一滴答声中程序更改消息中还有一个Instrument时导致的重复项。 If you try out v6.5 you may find this to be easier to use.如果您尝试 v6.5,您可能会发现它更易于使用。

It was simply the fact that it was taking the first instrument it found, which had no data for notes, durations or offsets.原因很简单,它使用的是它发现的第一个乐器,它没有音符、时长或偏移的数据。 Iterating through the instruments I got different ones returning full lists.遍历仪器我得到了不同的返回完整列表的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM