From d3b637a0c8065f09e26b6fe67eddd47f13cb4826 Mon Sep 17 00:00:00 2001 From: EillesWan Date: Tue, 23 Jan 2024 18:14:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=B1=BB=E5=9E=8B=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E9=87=8D=E5=A4=8D=E9=97=AE=E9=A2=98-2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Musicreater/constants.py | 9 +----- Musicreater/main.py | 61 +++++++++++++++++++------------------- Musicreater/subclass.py | 58 +++++++++++++++++++++++++++++++++++- Musicreater/types.py | 22 -------------- docs/库的生成与功能文档.md | 22 +++++++++----- 5 files changed, 104 insertions(+), 68 deletions(-) diff --git a/Musicreater/constants.py b/Musicreater/constants.py index c29c089..714c1d4 100644 --- a/Musicreater/constants.py +++ b/Musicreater/constants.py @@ -16,7 +16,7 @@ Terms & Conditions: License.md in the root directory # Email TriM-Organization@hotmail.com # 若需转载或借鉴 许可声明请查看仓库目录下的 License.md -from typing import Dict, List, Tuple +from .types import Dict, List, Tuple, MidiInstrumentTableType, MidiNoteNameTableType x = "x" """ @@ -33,13 +33,6 @@ z = "z" z """ -DEFAULT_PROGRESSBAR_STYLE = ( - r"▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]", - ("§e=§r", "§7=§r"), -) -""" -默认的进度条样式组 -""" # Midi用对照表 diff --git a/Musicreater/main.py b/Musicreater/main.py index fbd97e6..9847113 100644 --- a/Musicreater/main.py +++ b/Musicreater/main.py @@ -28,6 +28,8 @@ Terms & Conditions: License.md in the root directory import math import os +import mido + from .constants import * from .exceptions import * from .subclass import * @@ -68,6 +70,12 @@ tick * tempo / 1000000.0 / ticks_per_beat * 一秒多少游戏刻 """ +VoidMido = Union[mido.MidiFile, None] # void mido +""" +空Midi类类型 +""" + + @dataclass(init=False) class MidiConvert: """ @@ -203,7 +211,7 @@ class MidiConvert: self, max_score: int, scoreboard_name: str, - progressbar_style: ProgressBarStyleType = DEFAULT_PROGRESSBAR_STYLE, + progressbar_style: ProgressBarStyle = DEFAULT_PROGRESSBAR_STYLE, ) -> List[SingleCommand]: """ 生成进度条 @@ -223,7 +231,7 @@ class MidiConvert: ------- list[SingleCommand,] """ - pgs_style = progressbar_style[0] + pgs_style = progressbar_style.base_style """用于被替换的进度条原始样式""" """ @@ -253,7 +261,7 @@ class MidiConvert: result.append( SingleCommand( 'scoreboard objectives add {}PercT dummy "百分比计算"'.format(sbn_pc), - annotation="新增临时计算用计分板(百分比)", + annotation="新增临时百分比变量", ) ) result.append( @@ -264,7 +272,7 @@ class MidiConvert: + "scoreboard players set MaxScore {} {}".format( scoreboard_name, max_score ), - annotation="设定此音乐最大计分", + annotation="设定音乐最大延迟分数", ) ) result.append( @@ -284,7 +292,7 @@ class MidiConvert: + "scoreboard players operation @s {} = @s {}".format( sbn_pc + "PercT", scoreboard_name ), - annotation="为临时变量赋值", + annotation="赋值临时百分比", ) ) result.append( @@ -295,7 +303,7 @@ class MidiConvert: + "scoreboard players operation @s {} *= n100 {}".format( sbn_pc + "PercT", scoreboard_name ), - annotation="改变临时变量的单位为百分比(扩大精度)", + annotation="转换临时百分比之单位至%(扩大精度)", ) ) result.append( @@ -306,7 +314,7 @@ class MidiConvert: + "scoreboard players operation @s {} /= MaxScore {}".format( sbn_pc + "PercT", scoreboard_name ), - annotation="使用临时变量计算百分比", + annotation="计算百分比", ) ) @@ -318,13 +326,13 @@ class MidiConvert: result.append( SingleCommand( 'scoreboard objectives add {}TMinT dummy "时间计算:分"'.format(sbn_pc), - annotation="新增临时计算计分板(分)", + annotation="新增临时分变量", ) ) result.append( SingleCommand( 'scoreboard objectives add {}TSecT dummy "时间计算:秒"'.format(sbn_pc), - annotation="新增临时计算计分板(秒)", + annotation="新增临时秒变量", ) ) result.append( @@ -354,7 +362,7 @@ class MidiConvert: + "scoreboard players operation @s {} = @s {}".format( sbn_pc + "TMinT", scoreboard_name ), - annotation="为临时变量(分)赋值", + annotation="赋值临时分", ) ) result.append( @@ -365,7 +373,7 @@ class MidiConvert: + "scoreboard players operation @s {} /= n20 {}".format( sbn_pc + "TMinT", scoreboard_name ), - annotation="将临时变量转换单位为秒(缩减精度)", + annotation="转换临时分之单位为秒(缩减精度)", ) ) result.append( @@ -376,7 +384,7 @@ class MidiConvert: + "scoreboard players operation @s {} = @s {}".format( sbn_pc + "TSecT", sbn_pc + "TMinT" ), - annotation="为临时变量(秒)赋值", + annotation="赋值临时秒", ) ) @@ -388,7 +396,7 @@ class MidiConvert: + "scoreboard players operation @s {} /= n60 {}".format( sbn_pc + "TMinT", scoreboard_name ), - annotation="将临时变量(分)转换单位为分(缩减精度)", + annotation="转换临时分之单位为分(缩减精度)", ) ) @@ -400,42 +408,35 @@ class MidiConvert: + "scoreboard players operation @s {} %= n60 {}".format( sbn_pc + "TSecT", scoreboard_name ), - annotation="将临时变量(秒)确定下来(框定精度区间)", + annotation="确定临时秒(框定精度区间)", ) ) for i in range(pgs_style.count("_")): npg_stl = ( - pgs_style.replace("_", progressbar_style[1][0], i + 1) - .replace("_", progressbar_style[1][1]) + pgs_style.replace("_", progressbar_style.played_style, i + 1) + .replace("_", progressbar_style.to_play_style) .replace(r"%%N", self.midi_music_name) - if r"%%N" in pgs_style - else pgs_style.replace("_", progressbar_style[1][0], i + 1).replace( - "_", progressbar_style[1][1] - ) - ) - if r"%%s" in npg_stl: - npg_stl = npg_stl.replace( + .replace( r"%%s", '"},{"score":{"name":"*","objective":"' + scoreboard_name + '"}},{"text":"', ) - if r"%%%" in npg_stl: - npg_stl = npg_stl.replace( + .replace( r"%%%", r'"},{"score":{"name":"*","objective":"' + sbn_pc + r'PercT"}},{"text":"%', ) - if r"%%t" in npg_stl: - npg_stl = npg_stl.replace( + .replace( r"%%t", r'"},{"score":{"name":"*","objective":"{-}TMinT"}},{"text":":"},' r'{"score":{"name":"*","objective":"{-}TSecT"}},{"text":"'.replace( r"{-}", sbn_pc ), ) + ) result.append( SingleCommand( self.execute_cmd_head.format( @@ -455,20 +456,20 @@ class MidiConvert: result.append( SingleCommand( "scoreboard objectives remove {}PercT".format(sbn_pc), - annotation="移除临时计算计分板(百分比)", + annotation="移除临时百分比变量", ) ) if r"%%t" in pgs_style: result.append( SingleCommand( "scoreboard objectives remove {}TMinT".format(sbn_pc), - annotation="移除临时计算计分板(分)", + annotation="移除临时分变量", ) ) result.append( SingleCommand( "scoreboard objectives remove {}TSecT".format(sbn_pc), - annotation="移除临时计算计分板(秒)", + annotation="移除临时秒变量", ) ) diff --git a/Musicreater/subclass.py b/Musicreater/subclass.py index c6c7efe..e88bd1f 100644 --- a/Musicreater/subclass.py +++ b/Musicreater/subclass.py @@ -18,7 +18,8 @@ Terms & Conditions: License.md in the root directory from dataclasses import dataclass -from typing import Optional, Any +from typing import Any +from .types import Optional, Any, List, Mapping from .constants import MC_PERCUSSION_INSTRUMENT_LIST @@ -339,3 +340,58 @@ class SingleNoteBox: if not isinstance(other, self.__class__): return False return self.__str__() == other.__str__() + + +@dataclass(init=False) +class ProgressBarStyle: + """进度条样式类""" + + base_style: str + """基础样式""" + + to_play_style: str + """未播放之样式""" + + played_style: str + """已播放之样式""" + + def __init__(self, base_s: str, to_play_s: str, played_s: str): + """用于存储进度条样式的类 + :param base_s 基础样式,用以定义进度条整体 + :param to_play_s 进度条样式:尚未播放的样子 + :param played_s 已经播放的样子""" + self.base_style = base_s + self.to_play_style = to_play_s + self.played_style = played_s + + def set_base_style(self, value: str): + """设置基础样式""" + self.base_style = value + + def set_to_play_style(self, value: str): + """设置未播放之样式""" + self.to_play_style = value + + def set_played_style(self, value: str): + """设置已播放之样式""" + self.played_style = value + + +DEFAULT_PROGRESSBAR_STYLE = ProgressBarStyle( + r"▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]", + r"§e=§r", + r"§7=§r", +) +""" +默认的进度条样式 +""" + +NoteChannelType = Mapping[ + int, + List[SingleNote,], +] +""" +频道信息类型 + +Dict[int,Dict[int,List[SingleNote,],],] +""" diff --git a/Musicreater/types.py b/Musicreater/types.py index cb3b9f2..d2952e8 100644 --- a/Musicreater/types.py +++ b/Musicreater/types.py @@ -18,9 +18,6 @@ Terms & Conditions: License.md in the root directory from typing import Any, Dict, List, Literal, Optional, Tuple, Union, Iterable, Sequence, Mapping, Callable -import mido - -from .subclass import SingleNote MidiNoteNameTableType = Mapping[int, Tuple[str, ...]] """ @@ -34,26 +31,7 @@ Midi乐器对照表类型 FittingFunctionType = Callable[[float], float] -ProgressBarStyleType = Tuple[str, Tuple[str, str]] -""" -进度条样式类型 -""" -VoidMido = Union[mido.MidiFile, None] # void mido -""" -空Midi类类型 -""" - - -NoteChannelType = Dict[ - int, - List[SingleNote,], -] -""" -频道信息类型 - -Dict[int,Dict[int,List[SingleNote,],],] -""" ChannelType = Dict[ diff --git a/docs/库的生成与功能文档.md b/docs/库的生成与功能文档.md index 1f671d8..68298b2 100644 --- a/docs/库的生成与功能文档.md +++ b/docs/库的生成与功能文档.md @@ -173,7 +173,7 @@ |`InstID`|声音效果ID|不同的声音ID可以对应不同的乐器,详见转换[乐器对照表](./%E8%BD%AC%E6%8D%A2%E4%B9%90%E5%99%A8%E5%AF%B9%E7%85%A7%E8%A1%A8.md)| |`Ht`|播放点对玩家的距离|通过距离来表达声音的响度。以 $S$ 表示此参数`Ht`,以Vol表示音量百分比,则计算公式为: $S = \frac{1}{Vol}-1$ | |`Vlct`|原生我的世界中规定的播放力度|这个参数是一个谜一样的存在,似乎它的值毫不重要……因为无论这个值是多少,我们听起来都差不多。当此音符所在MIDI通道为第一通道,则这个值为 $0.7$ 倍MIDI指定力度,其他则为 $0.9$ 倍。| -|`Ptc`|音符的音高|这是决定音调的参数。以 $P$ 表示此参数, $n$ 表示其在MIDI中的编号, $x$ 表示一定的音调偏移,则计算公式为: $P = 2^\frac{n-60-x}{12}$。之所以存在音调偏移是因为在《我的世界》中,不同的[乐器存在不同的音域](https://minecraft.fandom.com/zh/wiki/%E9%9F%B3%E7%AC%A6%E7%9B%92#%E4%B9%90%E5%99%A8),我们通过音调偏移来进行调整。| +|`Ptc`|音符的音高|这是决定音调的参数。以 $P$ 表示此参数, $n$ 表示其在MIDI中的编号, $x$ 表示一定的音调偏移,则计算公式为: $P = 2^\frac{n-60-x}{12}$。之所以存在音调偏移是因为在《我的世界》中,不同的[乐器存在不同的音域](https://zh.minecraft.wiki/wiki/%E9%9F%B3%E7%AC%A6%E7%9B%92#%E4%B9%90%E5%99%A8),我们通过音调偏移来进行调整。| ### 播放器内容 @@ -285,23 +285,31 @@ 表示进度条占位的 `_` 是用来标识你的进度条的。也就是可变部分的唯一的图形部分。 -**样式定义字符串**的样例如下,这也是默认的进度条的样式: +**样式定义字符串(基础样式)**的样例如下,这也是默认进度条的基础样式: -`▶ %%N [ %%s/%^s %%% __________ %%t|%^t]` +```▶ %%N [ %%s/%^s %%% __________ %%t|%^t]``` 这是单独一行的进度条,当然你也可以制作多行的,如果是一行的,输出时所使用的指令便是 `title`,而如果是多行的话,输出就会用 `titleraw` 作为进度条字幕。 哦对了,上面的只不过是样式定义,同时还需要定义的是可变图形的部分,也就是进度条上那个真正的“条”。 -对于这个我们就采用了固定参数的方法,对于一个进度条,无非就是“已经播放过的”和“没播放过的”两种形态,所以,使用一个元组来传入这两个参数就是最简单的了。元组的格式也很简单:`(str: 播放过的部分长啥样, str: 没播放过的部分长啥样)` 。例如,我们默认的进度“条”的定义是这样的: +对于这个我们就采用了固定参数的方法,对于一个进度条,无非就是“已经播放过的”和“没播放过的”两种形态,例如,我们默认的进度“条”(**可变样式**)的定义是这样的: -`('§e=§r', '§7=§r')` +**可变样式甲(已播放样式)**:`'§e=§r'` -综合起来,把这些参数传给函数需要一个参数整合,你猜用的啥?啊对对对,我用的还是元组! +**可变样式乙(未播放样式)**:`'§7=§r'` + +综合起来,把这些参数传给函数需要一个参数整合,使用位于 `Musicreater/subclass.py` 下的 `ProgressBarStyle` 类进行定义: 我们的默认定义参数如下: -`(r'▶ %%N [ %%s/%^s %%% __________ %%t|%^t]',('§e=§r', '§7=§r'))` +```python +DEFAULT_PROGRESSBAR_STYLE = ProgressBarStyle( + r"▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]", + r"§e=§r", + r"§7=§r", +) +``` *为了避免生成错误,请尽量避免使用标识符作为定义样式字符串的其他部分*