【问题标题】:Is there a way to read Rests from a musicxml file using music21?有没有办法使用 music21 从 musicxml 文件中读取 Rests?
【发布时间】:2021-05-09 18:38:23
【问题描述】:

我正在尝试将带有 music21 的 musicxml 文件读入列表。我保持非常简单。 sheetmusic

我已经尝试了下面的代码,但即使它毫无问题地添加了注释,它也会跳过其余的。

def xml_to_list():
    fn = "Untitled.xml"
    xml_data = m12.converter.parse(fn)
    score = []
    for part in xml_data.parts:
        instrument = part.getInstrument().instrumentName
        for note in part.recurse().notes:
            start = note.offset
            duration = note.quarterLength
            pitch = note.pitch.ps
            score.append([start, duration, pitch, instrument])
    print(score)

我目前的输出是这样的:

[[0.0, 1.0, 72.0, 'Piano'], [1.0, 1.0, 74.0, 'Piano'], [2.0, 1.0, 76.0, 'Piano'], [0.0, 1.0, 79.0, 'Piano'], [1.0, 1.0, 79.0, 'Piano'], [2.0, 1.0, 79.0, 'Piano'], [3.0, 1.0, 77.0, 'Piano']]

我怎样才能改变它,以便我也能得到关于其余部分的信息?

【问题讨论】:

    标签: python music21 musicxml


    【解决方案1】:
    def xml_to_list(xml):
        xml_data = m21.converter.parse(xml)
        score = []
        for part in xml_data.parts:
            for note in part.recurse().notesAndRests:
                if note.isRest:
                    start = note.offset
                    duration = note.quarterLength
                    score.append([start, duration, -1])
                else:
                    start = note.offset
                    duration = note.quarterLength
                    pitch = note.nameWithOctave
                    score.append([start, duration, pitch])
        return score
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 2020-08-24
      • 2018-06-10
      相关资源
      最近更新 更多