首页 > 解决方案 > 使用 Music 21 和 Python 输出不止一种乐器

问题描述

我正在使用 Python 和 Music21 编写一种算法,该算法从输入的小提琴音乐文件和钢琴作品组成旋律。我的问题是,当我输入一个包含两种乐器的 midi 文件时,输出只有一种乐器。我目前可以将输出乐器更改为吉他、小号等,即使这些乐器不在我的原始输入文件中。我想知道我是否可以编写一些代码来识别输入文件中的仪器并输出那些特定的仪器。或者,有什么方法可以为两个输出仪器而不是一个编码?我已经厌倦了用另一个仪器复制现有代码,但算法只输出代码中检测到的最后一个仪器。下面是我当前运行的代码:

def convert_to_midi(prediction_output):

    offset=0
    output_notes=[]

     #Create note and chord objects based on the values generated by the model
    for pattern in prediction_output:

         #Pattern is a chord
         if ('.' in pattern) or pattern.isdigit():
             notes_in_chord=pattern.split('.')
             notes=[]
             for current_note in notes_in_chord:
                 output_notes.append(instrument.Guitar())

                 cn=int(current_note) 
                 new_note=note.Note(cn)
                 notes.append(new_note)

             new_chord=chord.Chord(notes)
             new_chord.offset=offset
             output_notes.append(new_note)

         #Pattern is a note
         else:
             output_notes.append(instrument.Guitar())

             new_note=note.Note(pattern)
             new_note.offset=offset
             output_notes.append(new_note)

标签: pythonmusic21

解决方案


Instrument 对象直接进入 Stream 对象,而不是 Note,并且每个 Part 一次只能有一个 Instrument 对象处于活动状态。


推荐阅读