加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
museScoreClass.py 12.08 KB
一键复制 编辑 原始数据 按行查看 历史
bgArray 提交于 2022-04-23 12:49 . 修bug
from xmlJson import *
import json
import os
class MuseScore:
def __init__(self, fileName, is_debug=False):
self.filePath = fileName
self.fileName = os.path.basename(fileName)
# 读取xml文件
with open(self.filePath, 'r', encoding='utf-8') as f:
xml_str = f.read()
# 调用xml_to_json()函数
json_str = xml_to_json(xml_str)
with open(self.filePath.replace(".mscx", ".json"), 'w', encoding="utf-8") as w:
w.write(json_str)
with open(self.filePath.replace(".mscx", ".json"), 'r', encoding="utf-8") as js:
self.xmlDict = json.load(js)
if is_debug:
self.is_debug = True
# self.xmlDict = xml_to_dict(self.fileName, is_debug)
else:
self.is_debug = False
self.head = []
self.body = []
self.data = [self.head, self.body]
self.musescore_V = ""
self.musescore_programVersion = ""
self.musescore_programRevision = ""
self.musescore_scoreTag = []
self.musescore_scoreLayerTag = {}
self.musescore_scoreCurrentLayer = 0
self.musescore_scoreDivision = 480
self.musescore_scoreStyle = []
self.musescore_scorePageWidth = 33.1098
self.musescore_scorePageHeight = 46.8098
self.musescore_scorePagePrintableWidth = 31.9264
self.musescore_scorePagePrintableHeight = None
self.musescore_scorePageEvenLeftMargin = 0.592913
self.musescore_scoreEnableVerticalSpread = 1
self.musescore_scoreSpatium = 1.75
self.musescore_scoreShowInvisible = 1
self.musescore_scoreShowUnprintable = 1
self.musescore_scoreShowFrames = 1
self.musescore_scoreShowMargins = 0
self.arranger = [{'name': 'arranger'}, None]
self.composer = [{'name': 'composer'}, None]
self.copyright = [{'name': 'copyright'}, None]
self.creationDate = [{'name': 'creationDate'}, None]
self.lyricist = [{'name': 'lyricist'}, None]
self.movementNumber = [{'name': 'movementNumber'}, None]
self.movementTitle = [{'name': 'movementTitle'}, None]
self.platform = [{'name': 'platform'}, None]
self.poet = [{'name': 'poet'}, None]
self.source = [{'name': 'source'}, None]
self.translator = [{'name': 'translator'}, None]
self.workNumber = [{'name': 'workNumber'}, None]
self.workTitle = [{'name': 'workTitle'}, None]
self.originalFormat = [{'name': 'originalFormat'}, None]
self.metaTags = []
self.OrderedDict = []
self.PartDefinitions = []
self.StaffList = []
self.get_data()
self.originalData_by_get = []
#
#
#
#
#
# 获取数据
def get_data(self):
if self.is_debug is True:
print("Debugging")
print(self.xmlDict)
self.musescore_V = self.xmlDict["museScore"]["@version"]
self.musescore_programVersion = self.xmlDict["museScore"]["programVersion"]
self.musescore_programRevision = self.xmlDict["museScore"]["programRevision"]
self.musescore_scoreLayerTag = self.xmlDict["museScore"]["Score"]["LayerTag"]
self.musescore_scoreCurrentLayer = self.xmlDict["museScore"]["Score"]["currentLayer"]
self.musescore_scoreDivision = self.xmlDict["museScore"]["Score"]["Division"]
self.musescore_scoreStyle = self.xmlDict["museScore"]["Score"]["Style"]
self.musescore_scorePageWidth = self.xmlDict["museScore"]["Score"]["Style"].get("pageWidth", None)
self.musescore_scorePageHeight = self.xmlDict["museScore"]["Score"]["Style"].get("pageHeight", None)
self.musescore_scorePagePrintableWidth = self.xmlDict["museScore"]["Score"]["Style"].get("PagePrintableWidth",
None)
self.musescore_scorePagePrintableHeight = self.xmlDict["museScore"]["Score"]["Style"].get("pagePrintableHeight",
None)
self.musescore_scorePageEvenLeftMargin = self.xmlDict["museScore"]["Score"]["Style"].get("PageEvenLeftMargin",
None)
self.musescore_scoreEnableVerticalSpread = self.xmlDict["museScore"]["Score"]["Style"].get(
"EnableVerticalSpread", None)
self.musescore_scoreSpatium = self.xmlDict["museScore"]["Score"]["Style"]["Spatium"]
self.musescore_scoreShowInvisible = self.xmlDict["museScore"]["Score"]["showInvisible"]
self.musescore_scoreShowUnprintable = self.xmlDict["museScore"]["Score"]["showUnprintable"]
self.musescore_scoreShowFrames = self.xmlDict["museScore"]["Score"]["showFrames"]
self.musescore_scoreShowMargins = self.xmlDict["museScore"]["Score"]["showMargins"]
for i in self.xmlDict["museScore"]["Score"]["metaTag"]:
if i["@name"] == "arranger":
try:
self.arranger[1] = i["#text"]
except KeyError:
self.arranger[1] = None
elif i["@name"] == "composer":
try:
self.composer[1] = i["#text"]
except KeyError:
self.composer[1] = None
elif i["@name"] == "copyright":
try:
self.copyright[1] = i["#text"]
except KeyError:
self.copyright[1] = None
elif i["@name"] == "creationDate":
try:
self.creationDate[1] = i["#text"]
except KeyError:
self.creationDate[1] = None
elif i["@name"] == "lyricist":
try:
self.lyricist[1] = i["#text"]
except KeyError:
self.lyricist[1] = None
elif i["@name"] == "movementNumber":
try:
self.movementNumber[1] = i["#text"]
except KeyError:
self.movementNumber[1] = None
elif i["@name"] == "movementTitle":
try:
self.movementTitle[1] = i["#text"]
except KeyError:
self.movementTitle[1] = None
elif i["@name"] == "platform":
try:
self.platform[1] = i["#text"]
except KeyError:
self.platform[1] = None
elif i["@name"] == "poet":
try:
self.poet[1] = i["#text"]
except KeyError:
self.poet[1] = None
elif i["@name"] == "workNumber":
try:
self.workNumber[1] = i["#text"]
except KeyError:
self.workNumber[1] = None
elif i["@name"] == "workTitle":
try:
self.workTitle[1] = i["#text"]
except KeyError:
self.workTitle[1] = None
elif i["@name"] == "source":
try:
self.source[1] = i["#text"]
except KeyError:
self.source[1] = None
elif i["@name"] == "translator":
try:
self.translator[1] = i["#text"]
except KeyError:
self.translator[1] = None
try:
if i["@name"] == "originalFormat":
try:
self.originalFormat[1] = i["#text"]
except KeyError:
self.originalFormat[1] = None
except KeyError:
pass
self.metaTags = [self.arranger, self.composer, self.copyright, self.creationDate, self.lyricist,
self.movementNumber, self.movementTitle, self.platform, self.poet, self.workNumber,
self.workTitle, self.source, self.translator, self.originalFormat]
if self.is_debug:
print(self.metaTags)
self.OrderedDict = self.xmlDict["museScore"]["Score"]["Order"]
try:
for i in self.xmlDict["museScore"]["Score"]["Part"]:
identifier = 0
Staffs = {"Staffs": []}
trackName = {"trackName": ""}
Instrument = {"Instrument": {}}
Staffs["Staffs"].append(i["Staff"])
trackName["trackName"] = i["trackName"]
Instrument["Instrument"] = i["Instrument"]
identifier += 1
part = [identifier, Staffs, trackName, Instrument]
self.PartDefinitions.append(part)
except TypeError:
Staffs["Staffs"].append(self.xmlDict["museScore"]["Score"]["Part"]["Staff"])
trackName["trackName"] = self.xmlDict["museScore"]["Score"]["Part"]["trackName"]
Instrument["Instrument"] = self.xmlDict["museScore"]["Score"]["Part"]["Instrument"]
identifier += 1
part = [identifier, Staffs, trackName, Instrument]
self.PartDefinitions.append(part)
try:
for i in self.xmlDict["museScore"]["Score"]["Staff"]:
id_ = i["@id"]
VBoxes = {"VBoxes": []}
Measures = {"Measures": []}
try:
VBox = {"VBox": i["VBox"]}
VBoxes["VBoxes"].append(VBox)
except KeyError:
pass
originalMeasure = {"Measure": i["Measure"]}
Measures["Measures"].append(originalMeasure)
self.StaffList.append([id_, VBoxes, Measures])
except TypeError:
id_ = self.xmlDict["museScore"]["Score"]["Staff"]["@id"]
VBoxes = {"VBoxes": []}
Measures = {"Measures": []}
try:
VBox = {"VBox": self.xmlDict["museScore"]["Score"]["Staff"]["VBox"]}
VBoxes["VBoxes"].append(VBox)
except KeyError:
pass
originalMeasure = {"Measure": self.xmlDict["museScore"]["Score"]["Staff"]["Measure"]}
Measures["Measures"].append(originalMeasure)
self.StaffList.append([id_, VBoxes, Measures])
print("getData done")
self.originalData_by_get = [{"ALL_SETTING": [self.musescore_V, self.musescore_programVersion,
self.musescore_programRevision,
self.musescore_scoreLayerTag, self.musescore_scoreCurrentLayer,
self.musescore_scoreDivision, self.musescore_scoreStyle,
self.musescore_scorePageWidth, self.musescore_scorePageHeight,
self.musescore_scorePagePrintableWidth, self.musescore_scorePagePrintableHeight,
self.musescore_scorePageEvenLeftMargin, self.musescore_scoreEnableVerticalSpread,
self.musescore_scoreSpatium, self.musescore_scoreShowInvisible,
self.musescore_scoreShowUnprintable, self.musescore_scoreShowFrames,
self.musescore_scoreShowMargins]}, {"MetaTags": self.metaTags}, self.OrderedDict,
{"PartDefinitions": self.PartDefinitions}, {"StaffList": self.StaffList}
]
if self.is_debug:
with open("debug" + self.fileName + ".txt", "w", encoding='utf-8') as f:
f.write(str(self.originalData_by_get))
def rewriteData(self):
pass
if __name__ == '__main__':
A = MuseScore('./resources/测试精华(这是标题)1.mscx', True)
# B = MuseScore(r'C:\Users\lc\Documents\MuseScore3\乐谱\弈--二创.mscx', True)
C = MuseScore('./resources/tpc1.mscx', True)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化