python - 有没有办法使用 music21 从 musicxml 文件中读取 Rests?
问题描述
我正在尝试将带有 music21 的 musicxml 文件读入列表。我保持非常简单。 乐谱
我已经尝试了下面的代码,但即使它毫无问题地添加了注释,它也会跳过其余部分。
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']]
如何更改它以便我也可以获得有关其余部分的信息?
解决方案
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
推荐阅读
- swift - 如何在视图控制器代码中获得文本字段输入?
- c# - 将两个相同类型结构的值相加
- sql - 值必须至少填写一个,触发
- python - python中的Optparse:当file.dat用作命令行中的参数时如何在file.dat中使用整数
- php - laravel - 如何在控制器中实现 Heartland 支付方式
- python - python从字典词典中制作平均值列表:所有平均值相同
- c# - 如何在 C# 的轮播视图中显示下一张图片?
- regex - 如何在python中使用正则表达式从文本中删除项目符号
- spring-boot - SpringBoot中的单元测试Freemarker模板 - 无法初始化freemarker配置
- string - 错误类型“字符串”不是类型转换颤振 sqflite 中“num”类型的子类型