Musicreater/nmcsup/nmcreader.py

85 lines
2.8 KiB
Python
Raw Normal View History

2021-11-21 00:59:15 +08:00
"""音创系列的文件读取功能"""
2022-01-19 17:48:47 +08:00
# 诸葛亮与八卦阵帮忙修改语法 日期:---2022年1月19日
# 统计致命三级错误0个警告二级错误3个语法一级错误22个
2021-11-21 00:59:15 +08:00
from nmcsup.log import log
from nmcsup.const import notes
2022-01-19 17:48:47 +08:00
# 从格式文本文件读入一个音轨并存入一个列表
def ReadFile(fn: str): # -> list
2021-11-21 00:59:15 +08:00
from nmcsup.trans import note2list
2022-01-19 17:48:47 +08:00
log('打开' + fn + "并读取音符")
2021-11-21 00:59:15 +08:00
try:
nat = open(fn, 'r', encoding='UTF-8').read().split(" ")
del fn
2022-01-19 17:48:47 +08:00
except FileNotFoundError:
2021-11-21 00:59:15 +08:00
log("找不到读取目标文件")
return False
Notes = []
2022-01-19 17:48:47 +08:00
log(str(nat) + "已读取")
for i in range(int(len(nat) / 2)):
Notes.append([nat[i * 2], float(nat[i * 2 + 1])])
2021-11-21 00:59:15 +08:00
Notes = note2list(Notes)
2022-01-19 17:48:47 +08:00
log('音符数据更新' + str(Notes))
return [Notes, ]
2021-11-21 00:59:15 +08:00
2022-01-19 17:48:47 +08:00
# 从midi读入多个音轨返回多个音轨列表
def ReadMidi(midfile: str): # -> list
2021-11-21 00:59:15 +08:00
import mido
from msctspt.threadOpera import NewThread
Notes = []
try:
mid = mido.MidiFile(midfile)
2022-01-19 17:48:47 +08:00
except FileNotFoundError:
log("找不到文件或无法读取文件" + midfile)
2021-11-21 00:59:15 +08:00
return False
# 解析
ks = list(notes.values())
2022-01-19 17:48:47 +08:00
def loadMidi(track1):
2021-11-21 00:59:15 +08:00
datas = []
2022-01-19 17:48:47 +08:00
for i in track1:
2021-11-21 00:59:15 +08:00
if i.is_meta:
2022-01-19 17:48:47 +08:00
log('元信息' + str(i))
2021-11-21 00:59:15 +08:00
pass # 不处理元信息
elif 'note_on' in str(i):
msg = str(i).replace("note=", '').replace("time=", '').split(" ")
2022-01-19 17:48:47 +08:00
log('音符on消息处理后' + str(msg))
2021-11-21 00:59:15 +08:00
if msg[4] == '0':
2022-01-19 17:48:47 +08:00
datas.append([ks[int(msg[2]) - 20][0], 1.0])
log('延续时间0tick--:添加音符' + str([ks[int(msg[2]) - 20][0], 1.0]))
2021-11-21 00:59:15 +08:00
else:
2022-01-19 17:48:47 +08:00
datas.append([ks[int(msg[2]) - 20][0], float(msg[4]) / 480])
log('延续时间' + msg[4] + 'tick--:添加音符' + str([ks[int(msg[2]) - 20][0], float(msg[4]) / 480]))
2021-11-21 00:59:15 +08:00
del msg
2022-01-19 17:48:47 +08:00
log('音符增加' + str(datas))
2021-11-21 00:59:15 +08:00
return datas
2022-01-19 17:48:47 +08:00
2021-11-21 00:59:15 +08:00
for j, track in enumerate(mid.tracks):
2022-01-19 17:48:47 +08:00
th = NewThread(loadMidi, (track,))
2021-11-21 00:59:15 +08:00
th.start()
Notes.append(th.getResult())
del ks
return Notes
2022-01-19 17:48:47 +08:00
def ReadOldProject(fn: str): # -> list
2021-11-21 00:59:15 +08:00
import json
from nmcsup.trans import note2list
2022-01-19 17:48:47 +08:00
log("读取文件:" + fn)
2021-11-21 00:59:15 +08:00
try:
with open(fn, 'r', encoding='UTF-8') as c:
dataset = json.load(c)
2022-01-19 17:48:47 +08:00
except FileNotFoundError:
print('找不到文件:' + fn + ",请查看您是否输入正确")
log("丢失" + fn)
2021-11-21 00:59:15 +08:00
return False
for i in range(len(dataset['musics'])):
dataset['musics'][i]['notes'] = note2list(dataset['musics'][i]['notes'])
2022-01-19 17:48:47 +08:00
# 返回 音轨列表 选择器
2021-11-21 00:59:15 +08:00
return dataset