乐谱xml文件转为VOCALOID3的输入文件格式vsqx
using
python3 xml2vsqx.py xmlpy_out_dir vsqx_out_dir
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "errrolyan"
# Date: 18-10-16
# Describe = "乐谱xml文件转为VOCALOID3的输入文件格式vsqx”
import os,re,sys
import os.path
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element
import xml.dom.minidom
def read_dic():
dictpath = "./dic/zh_CN.dict"
dic = {}
with open(dictpath,"r") as Rdic:
for i in Rdic:
list = i.split(" ")
dic[list[0]]= list[1]
return dic
def coverFiles(sourceDir, targetDir):
for files in os.listdir(targetDir):
filepath = os.path.join(targetDir,files)
os.remove(filepath)
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir, file)
targetFile = os.path.join(targetDir, file)
if os.path.isfile(sourceFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
def fileread(filepath,dic):
pathDir = os.listdir(filepath)
#print(dic)
for s in pathDir:
newDir = os.path.join(filepath, s)
if os.path.isfile(newDir):
if os.path.splitext(newDir)[1] == ".xml":
newDir1 = newDir[:-3]+"vsqx"
print(newDir1)
tree = ET.parse(newDir)
root = tree.getroot()
for child in root:
for part in child.iter('part'):
for note in part.iter("note"):
for lyric in note.iter("lyric"):
for text in lyric.iter('text'):
text.text = re.sub(u"[12345]", '', text.text)
print("歌词:" + text.text)
lyric.text= text.text
phnms = Element("phnms")
phnms.text = dic[text.text]
print(phnms.text)
text.set('updated', 'yes')
lyric.remove(text)
note.append(phnms)
for syllabic in lyric.iter("syllabic"):
lyric.remove(syllabic)
note.set('updated', 'yes')
# for measure in part.iter("measure"):
# print(measure.tag,measure.attrib)
# root.revove(measure)
os.remove(newDir)
tree.write(newDir1)
print("第" + newDir1 + "首歌完成!!!")
def xml_to_pinyin_main(xml_in_dir, xml_out_dir):
read_dic()
coverFiles(xml_in_dir, xml_out_dir)
dic = read_dic()
fileread(xml_out_dir,dic)
if __name__=="__main__":
usage = 'Usage: python3 xml2vsqx.py xmlpy_out_dir vsqx_out_dir'
if len(sys.argv) != 3:
xmlInPath = "xmlpy_out_dir"
xmlOutPath = "vsqx_out_dir"
else:
xmlInPath = sys.argv[1]
xmlOutPath = sys.argv[2]
xml_to_pinyin_main("xmlpy_out_dir","vsqx_out_dir")
print("*******XMLpinyin to vsqx 转化完成********")