python music21 解析midi 文件为音符

在这里插入图片描述

# pip install music21
import  music21 as ms21
s=ms21.converter.parse('qinghuaci.mid')
# 获取持续的时间每个音符
print([note.duration.quarterLength for note in s.flat.notesAndRests])

for note in s.flat.notesAndRests :

    if isinstance(note, ms21.note.Rest):
        print("r")

    elif isinstance(note,ms21.note.Note):
        print(note.name,note.pitch.midi,note.duration.quarterLength)
    # 取和弦
    else:
        for c_note in note.notes:
            print(c_note.name, c_note.pitch.midi,c_note.duration.quarterLength)


if __name__ == '__main__':
    pass

猜你喜欢

转载自blog.csdn.net/weixin_32759777/article/details/123700987