0

我正在尝试使用此github 存储库的 .py 文件中的代码(get_notes() 函数)从该 midi 文件中提取音符和和弦数据。这是从我正在使用的存储库中复制的确切代码:

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

我的 midi 文件有多种乐器,我相信由于某种原因,没有任何东西被附加到笔记列表中。看到我刚刚克隆了存储库的代码并在我自己的 midi 文件上运行它,我不确定它为什么不起作用。我查看了 music21 的 partitionByInstrument() 的文档,并尝试通过更改来迭代不同的乐器:

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

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

因为不同的部分应该是文件中的不同工具,但它仍然返回一个空列表。我该怎么办?

4

2 回答 2

2

Instrumentv6.1 添加了从 MIDI 乐器名称和轨道名称消息创建对象的功能。Instrument今天发布的 v6.5 添加了一项功能,用于删除在同一滴答声中还有来自程序更改消息时导致的重复项。如果您试用 v6.5,您可能会发现它更易于使用。

于 2021-01-01T04:41:06.287 回答
0

只是它使用了它发现的第一个乐器,它没有关于音符、持续时间或偏移的数据。遍历工具,我得到了不同的返回完整列表的工具。

于 2020-10-23T10:57:56.070 回答