From 7d6faebc5bc64c90072b74ab00cebcce612547da Mon Sep 17 00:00:00 2001 From: EillesWan Date: Fri, 20 Jan 2023 21:34:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E9=99=8D=E4=BD=8E=E5=AE=B9=E9=94=99=E7=8E=87?= =?UTF-8?q?=EF=BC=88=E4=B8=8D=E6=98=AF=EF=BC=8C=E6=88=91=E7=9C=9F=E7=9A=84?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=8F=8D=E5=90=91=E6=9B=B4=E6=96=B0=EF=BC=88?= =?UTF-8?q?=EF=BC=88=EF=BC=88=20=E4=BD=86=E6=98=AF=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E7=AE=97=E6=B3=951=EF=BC=8C=E7=AE=97=E6=B3=951?= =?UTF-8?q?=E7=9A=84=E5=93=8D=E5=BA=A6=E9=94=99=E8=AF=AF=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E4=BA=86=E3=80=82=E4=BD=86=E6=98=AF=E6=88=91=E4=BB=8D=E7=84=B6?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E7=AE=97=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + README.md | 34 ++-- docs/download&atart/Android.md | 5 +- docs/download&atart/Linux.md | 5 +- docs/download&atart/Windows.md | 9 +- docs/库的生成与功能文档.md | 35 +++++ magicDemo.py | 275 ++++++++++----------------------- msctPkgver/__init__.py | 2 +- msctPkgver/magicBeing.py | 150 ++++++++++++++++++ msctPkgver/main.py | 97 ++++++++++-- requirements.txt | 2 + 11 files changed, 379 insertions(+), 238 deletions(-) create mode 100644 msctPkgver/magicBeing.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index acc23ef..2df5f24 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ /.vscode *.mid *.midi +*.mcpack +*.bdx +demo_config.json # Byte-compiled / optimized __pycache__/ diff --git a/README.md b/README.md index d96a16f..fa31d8c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ **此分支为音·创的包版本,即便于其他软件使用的可被import版本** -### **看这看这!看这看这!看这看这!教程:[教程链接](./docs/%E5%8A%9F%E8%83%BD%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.md)** +### **如果希望直接使用演示程序转换MIDI音乐,请:[看这看这!看这看这!看这看这!](./docs/%E5%8A%9F%E8%83%BD%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.md)** ## 软件作者✒ @@ -40,32 +40,26 @@ ## 致谢🙏 +本致谢列表排名无顺序。 -- 感谢 **昀梦**\ 找出指令生成错误bug并指正 -- 感谢由 **Charlie_Ping “查理平”** 带来的bdx文件转换参考, -以及mid转我的世界乐器参考表格 -- 感谢由 **CMA_2401PT** 为我们的软件开发进行指导 -- 感谢由 **Dislink Sforza “断联·斯福尔扎”**\带来的midi音色解析以及转换指令的算法,我们将其改编并应用;同时,感谢他的[网页版转换器](https://dislink.github.io/midi2bdx/)给我们的开发与更新带来巨大的压力和动力,让我们在原本一骑绝尘的摸鱼道路上转向开发 -- 感谢 **Touch “偷吃”**\提供的测试支持,并对程序的改进提供了丰富的意见;同时也感谢他的不断尝试新的内容,使我们的排错更进一步 -- 感谢 **Mono**\反馈安装时的问题 -- 感谢 **Ammelia** 敦促我们进行新的功能开发,并为新功能提出了非常优秀的大量建议 +- 感谢 **昀梦**\ 找出指令生成错误bug并指正 +- 感谢由 **Charlie_Ping “查理平”** 带来的BDX文件转换参考,以及MIDI-我的世界对应乐器参考表格 +- 感谢由 **CMA_2401PT** 为我们的软件开发的一些方面进行指导,同时我们参考了他的BDXworkshop作为BDX结构编辑的参考 +- 感谢由 **Dislink Sforza “断联·斯福尔扎”**\ 带来的midi音色解析以及转换指令的算法,我们将其改编并应用;同时,感谢他的[网页版转换器](https://dislink.github.io/midi2bdx/)给我们的开发与更新带来巨大的压力和动力,让我们在原本一骑绝尘的摸鱼道路上转向开发,希望他能考上一个理想的大学! +- 感谢 **Touch “偷吃”**\ 提供的BDX导入测试支持,并对程序的改进提供了丰富的意见;同时也感谢他的不断尝试新的内容,使我们的排错更进一步 +- 感谢 **Mono**\ 反馈安装时的问题,辅助我们找到了视窗操作系统下的兼容性问题 +- 感谢 **Ammelia**\ 敦促我们进行新的功能开发,并为新功能提出了非常优秀的大量建议,以及提供的BDX导入测试支持,为我们的新结构生成算法提供了大量的实际理论支持 +- 感谢 **神羽** 对我们项目的支持与宣传 -> 感谢广大群友为此程序提供的测试等支持 +> 感谢广大群友为此程序提供的测试等支持 > -> 若您对我们有所贡献但您的名字没有显示在此列表中,请联系我! +> 若您对我们有所贡献但您的名字没有显示在此列表中,请联系我们! ## 联系我们📞 -### 作者\<*金羿*\>(Eilles)联系方式 +若遇到库中的问题,欢迎在[此](https://gitee.com/EillesWan/Musicreater/issues/new)提出你的issue。 -1. QQ 2647547478 -2. 电邮 EillesWan2006@163.com W-YI_DoctorYI@outlook.com EillesWan@outlook.com -3. 微信 WYI_DoctorYI -4. Telegram [@EillesWan](https://t.me/EillesWan) - -### 作者\<*诸葛亮与八卦阵*\>(bgArray)联系方式 - -1. QQ 4740437765 +如果需要与开发组进行交流,欢迎加入我们的[开发闲聊Q群](https://jq.qq.com/?_wv=1027&k=hpeRxrYr)。 diff --git a/docs/download&atart/Android.md b/docs/download&atart/Android.md index f4e885e..cf87b78 100644 --- a/docs/download&atart/Android.md +++ b/docs/download&atart/Android.md @@ -154,11 +154,10 @@ pkg install git 1. 开始使用演示程序 - 依照你的需要,执行以下命令之一: + 依照你的需要,执行以下命令以运行库的演示程序: ```bash - python demo_convert.py #计分板播放器,支持mcpack与BDX - python demo_convert_bdx_byDelay.py #延迟播放器,仅支持BDX + python magicDemo.py ``` 运行成功了,哦耶! diff --git a/docs/download&atart/Linux.md b/docs/download&atart/Linux.md index 3158b97..592e207 100644 --- a/docs/download&atart/Linux.md +++ b/docs/download&atart/Linux.md @@ -95,10 +95,9 @@ 1. 开始使用 - 在目录下打开终端,执行以下命令:(选择你需要的) + 在目录下打开终端,执行以下命令以运行演示程序: ```bash - python demo_convert.py #计分板播放器,支持mcpack与BDX - python demo_convert_bdx_byDelay.py #延迟播放器,仅支持BDX + python magicDemo.py ``` diff --git a/docs/download&atart/Windows.md b/docs/download&atart/Windows.md index 38c8ddb..6ba245e 100644 --- a/docs/download&atart/Windows.md +++ b/docs/download&atart/Windows.md @@ -60,18 +60,17 @@ 1. 开始使用 + 您可以直接双击 `magicDemo.py` 以运行演示程序,或者按照以下步骤使用终端应用运行。 + 在目录下打开终端。 例如:打开命令行:请进入到目录下,在文件资源管理器的地址框内输入`cmd`: - - 执行以下命令:(选择你需要的) ```bash - python demo_convert.py #计分板播放器,支持mcpack与BDX - python demo_convert_bdx_byDelay.py #延迟播放器,仅支持BDX + python ./magicDemo.py ``` ## 三、安装时错误的补充说明 @@ -86,4 +85,4 @@ > [下载64位VCREDIST安装包](https://aka.ms/vs/17/release/vc_redist.x64.exe) > [下载32位VCREDIST安装包](https://aka.ms/vs/17/release/vc_redist.x86.exe) - 感谢群友Mono帮我们发现这个问题。 \ No newline at end of file + 感谢群友*Mono*帮我们发现这个问题。 \ No newline at end of file diff --git a/docs/库的生成与功能文档.md b/docs/库的生成与功能文档.md index 124306c..91bdb93 100644 --- a/docs/库的生成与功能文档.md +++ b/docs/库的生成与功能文档.md @@ -8,6 +8,41 @@ **此为开发相关文档,内容包括:所生成文件结构的详细说明、特殊参数的详细解释** +# 库的简单调用 + +参见[magicDemo.py的相关部分](../magicDemo.py#L436),使用此库进行MIDI转换非常简单。 + +```python +import msctPkgver # 导入转换库 + +# 首先新建转换对象。 +conversion = msctPkgver.midiConvert() +# 值得注意的是,一个转换对象可以转换多个文件。 +# 也就是在实例化的时候不进行对文件的绑定。 +# 如果有调试需要,可以在实例化时传入参数 debug = True +# 如:conversion = msctPkgver.midiConvert(debug=True) + +# 设置输入输出地址,并指定execute指令语法 +# 地址都为字符串类型,不能传入文件流 +midi_path = "./where/you/place/.midi/files.mid" +output_folder = "./where/you/want2/convert/into/" +old_execute_format = False # 指定是否使用旧的execute指令语法(即1.18及以前的《我的世界:基岩版》语法) +conversion.convert(midi_path,output_folder,old_execute_format) + +# 进行转换并接受输出,具体的参数均在文档中有相关说明 +method_id = 2 # 指定使用的转换算法 +convertion_result = conversion.tomcpack(method_id,*prompts) + +# 转换结果是一个元组。 +# 若其转换成功,则前三位必为 +# True, 指令数量, 最大延迟 +# 其中,最大延迟可以理解为计分板的最大值 +# 如果转换失败,暂时还没有定返回值的规则 +# 但是有一点是肯定的,数据结构必定是元组 +print(convertion_result) +``` + + # 生成文件结构 ## 名词解释 diff --git a/magicDemo.py b/magicDemo.py index 53ecc85..3ee92a1 100644 --- a/magicDemo.py +++ b/magicDemo.py @@ -82,38 +82,35 @@ import os import random import datetime -from msctPkgver.main import * - try: - from rich.console import Console + import msctPkgver except ModuleNotFoundError as E: - if input("您需要安装 Rich 模块才能使用这个样例\n请问是否安装?(y/n)").lower() in ('y', '1'): - os.system("pip install Rich -i https://mirrors.aliyun.com/pypi/") - from rich.console import Console + if input("您需要安装 mido、Brotli 模块才能使用这个样例\n请问是否安装?(y/n):").lower() in ('y', '1'): + os.system("pip install -r requirements.txt") + import msctPkgver else: raise E try: + from msctPkgver.magicBeing import * + import requests import zhdate except ModuleNotFoundError as E: - if input("您需要安装 zhdate 模块才能使用这个样例\n请问是否安装?(y/n)").lower() in ('y', '1'): - os.system("pip install zhdate -i https://mirrors.aliyun.com/pypi/") + if input( + "您需要安装以下模块才能使用这个样例\nrequests==2.28.1\nrich==12.6.0\nzhdate==0.1\n请问是否安装?(y/n):" + ).lower() in ('y', '1'): + open("Demo_Requirements.txt", 'r').write( + "requests==2.28.1\nrich==12.6.0\nzhdate==0.1" + ) + os.system("pip install -r Demo_Requirements.txt") + os.remove("./Demo_Requirements.txt") + from rich.console import Console + import requests import zhdate else: raise E -try: - import requests -except ModuleNotFoundError as E: - if input("您需要安装 requests 模块才能使用这个样例\n请问是否安装?(y/n)").lower() in ('y', '1'): - os.system("pip install requests -i https://mirrors.aliyun.com/pypi/") - import requests - else: - raise E - - -MainConsole = Console() MainConsole.print( "[#121110 on #F0F2F4] ", @@ -173,132 +170,8 @@ else: justify="center", ) - -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, - ) - - prt(f"{_('LangChd')}{_(':')}{_(currentLang)}") -# 高级的输入函数 -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, @@ -319,7 +192,7 @@ def formatipt( prt(errnote) continue return result, funresult - + # 获取midi列表 while True: @@ -389,57 +262,64 @@ def boolstr(sth: str) -> bool: else: raise "布尔字符串啊?" - -prompts = [] -# 提示语 检测函数 错误提示语 -for args in [ - ( - f'{_("EnterVolume")}{_(":")}', - float, - ), - ( - f'{_("EnterSpeed")}{_(":")}', - float, - ), - ( - f'{_("WhetherPgb")}{_(":")}', - boolstr, - ), - ( - f'{_("EnterSbName")}{_(":")}', - str, - ) - if playerFormat == 1 - else ( - f'{_("EnterSelecter")}{_(":")}', - str, - ), - ( - f'{_("WhetherSbReset")}{_(":")}', - boolstr, - ) - if playerFormat == 1 - else (), - ( - f'{_("EnterAuthor")}{_(":")}', - str, - ) - if fileFormat == 1 - else (), - ( - f'{_("EnterMaxHeight")}{_(":")}', - int, - ) - if fileFormat == 1 - else (), -]: - if args: - prompts.append(formatipt(*args)[1]) +if os.path.exists("./demo_config.json"): + import json + prompts = json.load(open("./demo_config.json",'r',encoding="utf-8")) +else: + prompts = [] + # 提示语 检测函数 错误提示语 + for args in [ + ( + f'{_("EnterVolume")}{_(":")}', + float, + ), + ( + f'{_("EnterSpeed")}{_(":")}', + float, + ), + ( + f'{_("WhetherPgb")}{_(":")}', + boolstr, + ), + ( + f'{_("EnterSbName")}{_(":")}', + str, + ) + if playerFormat == 1 + else ( + f'{_("EnterSelecter")}{_(":")}', + str, + ), + ( + f'{_("WhetherSbReset")}{_(":")}', + boolstr, + ) + if playerFormat == 1 + else (), + ( + f'{_("EnterAuthor")}{_(":")}', + str, + ) + if fileFormat == 1 + else (), + ( + f'{_("EnterMaxHeight")}{_(":")}', + int, + ) + if fileFormat == 1 + else (), + ]: + if args: + prompts.append(formatipt(*args)[1]) -newLine = '\n' -conversion = midiConvert() + + + + +conversion = msctPkgver.midiConvert() for singleMidi in midis: + prt("\n"f"{_('Dealing')} {singleMidi} {_(':')}") conversion.convert(singleMidi, outpath) conversion_result = ( conversion.tomcpack(2, *prompts) @@ -450,11 +330,18 @@ for singleMidi in midis: else conversion.toBDXfile_withDelay(1, *prompts) ) ) + if prompts[-1] == "debug": + with open("./records.json",'a',encoding="utf-8") as f: + json.dump(prompts,f) + if conversion_result[0]: prt( - f"{newLine}{_('Dealing')} {singleMidi} {_(':')}{newLine} {_('CmdLength')}{_(':')}{conversion_result[1]}{_(',')}{_('MaxDelay')}{_(':')}{conversion_result[2]}{f'''{_(',')}{_('PlaceSize')}{_(':')}{conversion_result[3]}{_(',')}{_('LastPos')}{_(':')}{conversion_result[4]}''' if fileFormat == 1 else ''}" + f" {_('CmdLength')}{_(':')}{conversion_result[1]}{_(',')}{_('MaxDelay')}{_(':')}{conversion_result[2]}{f'''{_(',')}{_('PlaceSize')}{_(':')}{conversion_result[3]}{_(',')}{_('LastPos')}{_(':')}{conversion_result[4]}''' if fileFormat == 1 else ''}" ) else: prt(f"{_('Failed')}") -ipt(_("PressEnterExit")) +if ipt(_("PressEnterExit")).lower() == "record": + import json + with open("./demo_config.json",'w',encoding="utf-8") as f: + json.dump(prompts,f) diff --git a/msctPkgver/__init__.py b/msctPkgver/__init__.py index 6087cd8..ae4fdd0 100644 --- a/msctPkgver/__init__.py +++ b/msctPkgver/__init__.py @@ -7,7 +7,7 @@ # 若需转载或借鉴 许可声明请查看仓库目录下的 Lisence.md -__version__ = '0.2.0.1' +__version__ = '0.2.1' __all__ = [] __author__ = (('金羿', 'Eilles Wan'), ('诸葛亮与八卦阵', 'bgArray'), ('鸣凤鸽子', 'MingFengPigeon')) diff --git a/msctPkgver/magicBeing.py b/msctPkgver/magicBeing.py new file mode 100644 index 0000000..0f2c7bf --- /dev/null +++ b/msctPkgver/magicBeing.py @@ -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 diff --git a/msctPkgver/main.py b/msctPkgver/main.py index 65250fe..99b9655 100644 --- a/msctPkgver/main.py +++ b/msctPkgver/main.py @@ -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)) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8838e78 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Brotli==1.0.9 +mido==1.2.10