mirror of
https://github.com/TriM-Organization/Musicreater.git
synced 2025-09-21 19:56:23 +00:00
增加调试功能,降低容错率(不是,我真的没有反向更新(((
但是修改了算法1,算法1的响度错误解决了。但是我仍然推荐算法2
This commit is contained in:
@ -7,7 +7,7 @@
|
||||
# 若需转载或借鉴 许可声明请查看仓库目录下的 Lisence.md
|
||||
|
||||
|
||||
__version__ = '0.2.0.1'
|
||||
__version__ = '0.2.1'
|
||||
__all__ = []
|
||||
__author__ = (('金羿', 'Eilles Wan'), ('诸葛亮与八卦阵', 'bgArray'), ('鸣凤鸽子', 'MingFengPigeon'))
|
||||
|
||||
|
150
msctPkgver/magicBeing.py
Normal file
150
msctPkgver/magicBeing.py
Normal file
@ -0,0 +1,150 @@
|
||||
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
MainConsole = Console()
|
||||
|
||||
from typing import Any, Literal, Optional, TextIO
|
||||
|
||||
JustifyMethod = Literal["default", "left", "center", "right", "full"]
|
||||
OverflowMethod = Literal["fold", "crop", "ellipsis", "ignore"]
|
||||
|
||||
# 高级的打印函数
|
||||
def prt(
|
||||
*objects: Any,
|
||||
sep: str = " ",
|
||||
end: str = "\n",
|
||||
justify: Optional[JustifyMethod] = None,
|
||||
overflow: Optional[OverflowMethod] = None,
|
||||
no_wrap: Optional[bool] = None,
|
||||
emoji: Optional[bool] = None,
|
||||
markup: Optional[bool] = None,
|
||||
highlight: Optional[bool] = None,
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
crop: bool = True,
|
||||
soft_wrap: Optional[bool] = None,
|
||||
new_line_start: bool = False,
|
||||
) -> None:
|
||||
"""打印到控制台。
|
||||
|
||||
Args:
|
||||
objects (位置性的args): 要记录到终端的对象。
|
||||
sep (str, 可选): 要在打印数据之间写入的字符串。默认为""。
|
||||
end (str, optio可选nal): 在打印数据结束时写入的字符串。默认值为"\\\\n"。
|
||||
style (Union[str, Style], 可选): 应用于输出的样式。默认为`None`。
|
||||
justify (str, 可选): 校正位置,可为"default", "left", "right", "center" 或 "full". 默认为`None`。
|
||||
overflow (str, 可选): 控制溢出:"ignore"忽略, "crop"裁剪, "fold"折叠, "ellipsis"省略号。默认为`None`。
|
||||
no_wrap (Optional[bool], 可选): 禁用文字包装。默认为`None`。
|
||||
emoji (Optional[bool], 可选): 启用表情符号代码,或使用控制台默认的`None`。默认为`None`。
|
||||
markup (Optional[bool], 可选): 启用标记,或`None`使用控制台默认值。默认为`None`。
|
||||
highlight (Optional[bool], 可选): 启用自动高亮,或`None`使用控制台默认值。默认为`None`。
|
||||
width (Optional[int], 可选): 输出的宽度,或`None`自动检测。默认为`None`。
|
||||
crop (Optional[bool], 可选): 裁剪输出到终端的宽度。默认为`True`。
|
||||
soft_wrap (bool, 可选): 启用软包装模式,禁止文字包装和裁剪,或`None``用于 控制台默认值。默认为`None`。
|
||||
new_line_start (bool, False): 如果输出包含多行,在开始时插入一个新行。默认值为`False`。
|
||||
"""
|
||||
MainConsole.print(
|
||||
*objects,
|
||||
sep=sep,
|
||||
end=end,
|
||||
style="#F0F2F4 on #121110",
|
||||
justify=justify,
|
||||
overflow=overflow,
|
||||
no_wrap=no_wrap,
|
||||
emoji=emoji,
|
||||
markup=markup,
|
||||
highlight=highlight,
|
||||
width=width,
|
||||
height=height,
|
||||
crop=crop,
|
||||
soft_wrap=soft_wrap,
|
||||
new_line_start=new_line_start,
|
||||
)
|
||||
|
||||
|
||||
|
||||
# 高级的输入函数
|
||||
def ipt(
|
||||
*objects: Any,
|
||||
sep: str = " ",
|
||||
justify: Optional[JustifyMethod] = None,
|
||||
overflow: Optional[OverflowMethod] = None,
|
||||
no_wrap: Optional[bool] = None,
|
||||
emoji: Optional[bool] = None,
|
||||
markup: Optional[bool] = None,
|
||||
highlight: Optional[bool] = None,
|
||||
width: Optional[int] = None,
|
||||
height: Optional[int] = None,
|
||||
crop: bool = True,
|
||||
soft_wrap: Optional[bool] = None,
|
||||
new_line_start: bool = False,
|
||||
password: bool = False,
|
||||
stream: Optional[TextIO] = None,
|
||||
) -> str:
|
||||
"""显示一个提示并等待用户的输入。
|
||||
|
||||
它的工作方式与Python内建的 :func:`input` 函数相同,如果Python内建的 :mod:`readline` 模块先前已经加载,则提供详细的行编辑和历史功能。
|
||||
|
||||
Args:
|
||||
objects (位置性的args): 要记录到终端的对象。
|
||||
sep (str, 可选): 要在打印数据之间写入的字符串。默认为""。
|
||||
end (str, optio可选nal): 在打印数据结束时写入的字符串。默认值为"\\\\n"。
|
||||
style (Union[str, Style], 可选): 应用于输出的样式。默认为`None`。
|
||||
justify (str, 可选): 校正位置,可为"default", "left", "right", "center" 或 "full". 默认为`None`。
|
||||
overflow (str, 可选): 控制溢出:"ignore"忽略, "crop"裁剪, "fold"折叠, "ellipsis"省略号。默认为`None`。
|
||||
no_wrap (Optional[bool], 可选): 禁用文字包装。默认为`None`。
|
||||
emoji (Optional[bool], 可选): 启用表情符号代码,或使用控制台默认的`None`。默认为`None`。
|
||||
markup (Optional[bool], 可选): 启用标记,或`None`使用控制台默认值。默认为`None`。
|
||||
highlight (Optional[bool], 可选): 启用自动高亮,或`None`使用控制台默认值。默认为`None`。
|
||||
width (Optional[int], 可选): 输出的宽度,或`None`自动检测。默认为`None`。
|
||||
crop (Optional[bool], 可选): 裁剪输出到终端的宽度。默认为`True`。
|
||||
soft_wrap (bool, 可选): 启用软包装模式,禁止文字包装和裁剪,或`None``用于 控制台默认值。默认为`None`。
|
||||
new_line_start (bool, False): 如果输出包含多行,在开始时插入一个新行。默认值为`False`。
|
||||
password (bool, 可选): 隐藏已经输入的文案,默认值为`False`。
|
||||
stream (TextIO, 可选): 可选从文件中读取(而非控制台),默认为 `None`。
|
||||
|
||||
Returns:
|
||||
str: 从stdin读取的字符串
|
||||
"""
|
||||
MainConsole.print(
|
||||
*objects,
|
||||
sep=sep,
|
||||
end="",
|
||||
style="#F0F2F4 on #121110",
|
||||
justify=justify,
|
||||
overflow=overflow,
|
||||
no_wrap=no_wrap,
|
||||
emoji=emoji,
|
||||
markup=markup,
|
||||
highlight=highlight,
|
||||
width=width,
|
||||
height=height,
|
||||
crop=crop,
|
||||
soft_wrap=soft_wrap,
|
||||
new_line_start=new_line_start,
|
||||
)
|
||||
|
||||
return MainConsole.input("", password=password, stream=stream)
|
||||
|
||||
|
||||
def formatipt(
|
||||
notice: str,
|
||||
fun,
|
||||
errnote: str = "",
|
||||
*extraArg,
|
||||
):
|
||||
'''循环输入,以某种格式
|
||||
notice: 输入时的提示
|
||||
fun: 格式函数
|
||||
errnote: 输入不符格式时的提示
|
||||
*extraArg: 对于函数的其他参数'''
|
||||
while True:
|
||||
result = ipt(notice)
|
||||
try:
|
||||
funresult = fun(result, *extraArg)
|
||||
break
|
||||
except:
|
||||
prt(errnote)
|
||||
continue
|
||||
return result, funresult
|
@ -123,6 +123,10 @@ class midiConvert:
|
||||
self.methods_byDelay = [
|
||||
self._toCmdList_withDelay_m1,
|
||||
]
|
||||
if self.debugMode:
|
||||
from .magicBeing import prt,ipt
|
||||
self.prt = prt
|
||||
self.ipt = ipt
|
||||
|
||||
def convert(self, midiFile: str, outputPath: str, oldExeFormat: bool = True):
|
||||
"""转换前需要先运行此函数来获取基本信息"""
|
||||
@ -561,6 +565,8 @@ class midiConvert:
|
||||
commands = 0
|
||||
maxscore = 0
|
||||
|
||||
# 分轨的思路其实并不好,但这个算法就是这样
|
||||
# 所以我建议用第二个方法 _toCmdList_m2
|
||||
for i, track in enumerate(self.midi.tracks):
|
||||
|
||||
ticks = 0
|
||||
@ -596,8 +602,7 @@ class midiConvert:
|
||||
+ "="
|
||||
+ str(nowscore)
|
||||
+ "}"
|
||||
+ f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / volume - 1} ~ "
|
||||
f"{msg.velocity * (0.7 if msg.channel == 0 else 0.9)} {2 ** ((msg.note - 60 - _X) / 12)}"
|
||||
+ f"] ~ ~ ~ playsound {soundID} @s ^ ^ ^{1 / volume - 1} {msg.velocity/128} {2 ** ((msg.note - 60 - _X) / 12)}"
|
||||
)
|
||||
commands += 1
|
||||
if len(singleTrack) != 0:
|
||||
@ -620,10 +625,7 @@ class midiConvert:
|
||||
:return: tuple(命令列表, 命令个数, 计分板最大值)
|
||||
"""
|
||||
|
||||
if MaxVolume > 1:
|
||||
MaxVolume = 1
|
||||
if MaxVolume <= 0:
|
||||
MaxVolume = 0.001
|
||||
MaxVolume = 1 if MaxVolume > 1 else (0.001 if MaxVolume <= 0 else MaxVolume)
|
||||
|
||||
# 一个midi中仅有16通道 我们通过通道来识别而不是音轨
|
||||
channels = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
|
||||
@ -643,6 +645,8 @@ class midiConvert:
|
||||
if msg.is_meta:
|
||||
if msg.type == "set_tempo":
|
||||
tempo = msg.tempo
|
||||
if self.debugMode:
|
||||
self.prt(f"TEMPO更改:{tempo}(毫秒每拍)")
|
||||
else:
|
||||
|
||||
try:
|
||||
@ -679,6 +683,9 @@ class midiConvert:
|
||||
3 音符结束消息
|
||||
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
||||
|
||||
if self.debugMode:
|
||||
self.prt(dict(enumerate(channels)))
|
||||
|
||||
tracks = []
|
||||
cmdAmount = 0
|
||||
maxScore = 0
|
||||
@ -702,11 +709,13 @@ class midiConvert:
|
||||
InstID = msg[1]
|
||||
|
||||
elif msg[0] == "NoteS":
|
||||
|
||||
if SpecialBits:
|
||||
soundID, _X = self.__bitInst2IDwithX(InstID)
|
||||
else:
|
||||
soundID, _X = self.__Inst2soundIDwithX(InstID)
|
||||
try:
|
||||
soundID, _X = (self.__bitInst2IDwithX(InstID) if SpecialBits else self.__Inst2soundIDwithX(InstID))
|
||||
except UnboundLocalError as E:
|
||||
if self.debugMode:
|
||||
raise NotDefineProgramError(f"未定义乐器便提前演奏。\n{E}")
|
||||
else:
|
||||
soundID, _X = (self.__bitInst2IDwithX(-1) if SpecialBits else self.__Inst2soundIDwithX(-1))
|
||||
score_now = round(msg[-1] / float(speed) / 50)
|
||||
maxScore = max(maxScore, score_now)
|
||||
|
||||
@ -1117,8 +1126,10 @@ class midiConvert:
|
||||
|
||||
indexfile.close()
|
||||
|
||||
if os.path.exists(f"{self.outputPath}/{self.midFileName}.mcpack"):
|
||||
os.remove(f"{self.outputPath}/{self.midFileName}.mcpack")
|
||||
makeZip(
|
||||
f"{self.outputPath}/temp/", self.outputPath + f"/{self.midFileName}.mcpack"
|
||||
f"{self.outputPath}/temp/", f"{self.outputPath}/{self.midFileName}.mcpack"
|
||||
)
|
||||
|
||||
shutil.rmtree(f"{self.outputPath}/temp/")
|
||||
@ -1323,3 +1334,65 @@ class midiConvert:
|
||||
return (True, len(cmdlist), maxdelay, size, finalPos)
|
||||
|
||||
|
||||
def toDICT(
|
||||
self,
|
||||
) -> list:
|
||||
"""
|
||||
使用金羿的转换思路,将midi转换为字典
|
||||
:return: dict()
|
||||
"""
|
||||
|
||||
# 一个midi中仅有16通道 我们通过通道来识别而不是音轨
|
||||
channels = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
|
||||
microseconds = 0
|
||||
|
||||
# 我们来用通道统计音乐信息
|
||||
for msg in self.midi:
|
||||
|
||||
if msg.time != 0:
|
||||
try:
|
||||
microseconds += msg.time * tempo / self.midi.ticks_per_beat
|
||||
# print(microseconds)
|
||||
except NameError:
|
||||
raise NotDefineTempoError("计算当前分数时出错 未定义参量 Tempo")
|
||||
|
||||
if msg.is_meta:
|
||||
if msg.type == "set_tempo":
|
||||
tempo = msg.tempo
|
||||
else:
|
||||
|
||||
try:
|
||||
msg.channel
|
||||
channelMsg = True
|
||||
except:
|
||||
channelMsg = False
|
||||
if channelMsg:
|
||||
if msg.channel > 15:
|
||||
raise ChannelOverFlowError(f"当前消息 {msg} 的通道超限(≤15)")
|
||||
|
||||
if msg.type == "program_change":
|
||||
channels[msg.channel].append(("PgmC", msg.program, microseconds))
|
||||
|
||||
elif msg.type == "note_on" and msg.velocity != 0:
|
||||
channels[msg.channel].append(
|
||||
("NoteS", msg.note, msg.velocity, microseconds)
|
||||
)
|
||||
|
||||
elif (msg.type == "note_on" and msg.velocity == 0) or (
|
||||
msg.type == "note_off"
|
||||
):
|
||||
channels[msg.channel].append(("NoteE", msg.note, microseconds))
|
||||
|
||||
"""整合后的音乐通道格式
|
||||
每个通道包括若干消息元素其中逃不过这三种:
|
||||
|
||||
1 切换乐器消息
|
||||
("PgmC", 切换后的乐器ID: int, 距离演奏开始的毫秒)
|
||||
|
||||
2 音符开始消息
|
||||
("NoteS", 开始的音符ID, 力度(响度), 距离演奏开始的毫秒)
|
||||
|
||||
3 音符结束消息
|
||||
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
||||
|
||||
return dict(enumerate(channels))
|
Reference in New Issue
Block a user