2022-06-07 23:43:14 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2022-06-07 23:43:14 +08:00
|
|
|
|
# 音·创 开发交流群 861684859
|
|
|
|
|
# Email EillesWan2006@163.com W-YI_DoctorYI@outlook.com EillesWan@outlook.com
|
|
|
|
|
# 版权所有 金羿("Eilles Wan") & 诸葛亮与八卦阵("bgArray") & 鸣凤鸽子("MingFengPigeon")
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# 若需转载或借鉴 许可声明请查看仓库目录下的 License.md
|
2022-06-07 23:43:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2022-06-08 01:50:14 +08:00
|
|
|
|
音·创 库版 (Musicreater Package Version)
|
|
|
|
|
是一款免费开源的针对《我的世界:基岩版》的midi音乐转换库
|
|
|
|
|
Musicreater pkgver (Package Version 音·创 库版)
|
|
|
|
|
A free open source library used for convert midi file into formats that is suitable for **Minecraft: Bedrock Edition**.
|
2022-06-07 23:43:14 +08:00
|
|
|
|
|
2023-01-27 02:19:53 +08:00
|
|
|
|
版权所有 © 2023 音·创 开发者
|
|
|
|
|
Copyright © 2023 all the developers of Musicreater
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
开源相关声明请见 ../License.md
|
|
|
|
|
Terms & Conditions: ../License.md
|
2022-06-07 23:43:14 +08:00
|
|
|
|
"""
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2022-06-19 02:04:17 +08:00
|
|
|
|
import mido
|
|
|
|
|
import brotli
|
2022-06-30 12:14:22 +08:00
|
|
|
|
import json
|
|
|
|
|
import uuid
|
|
|
|
|
import shutil
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
from .utils import *
|
2022-10-07 16:49:47 +08:00
|
|
|
|
from .exceptions import *
|
2023-01-27 23:25:28 +08:00
|
|
|
|
from typing import TypeVar, Union
|
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
T = TypeVar("T") # Declare type variable
|
2023-01-27 23:25:28 +08:00
|
|
|
|
VM = TypeVar("VM", mido.MidiFile, None) # void mido
|
2022-10-07 16:49:47 +08:00
|
|
|
|
|
2022-04-29 11:34:41 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
class SingleNote:
|
2023-01-02 14:28:25 +08:00
|
|
|
|
def __init__(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self, instrument: int, pitch: int, velocity, startTime: int, lastTime: int
|
|
|
|
|
):
|
2022-10-16 21:19:14 +08:00
|
|
|
|
"""用于存储单个音符的类
|
2022-12-29 11:09:30 +08:00
|
|
|
|
:param instrument 乐器编号
|
2022-10-07 19:25:28 +08:00
|
|
|
|
:param pitch 音符编号
|
|
|
|
|
:param velocity 力度/响度
|
|
|
|
|
:param startTime 开始之时(ms)
|
|
|
|
|
注:此处的时间是用从乐曲开始到当前的毫秒数
|
2022-10-16 21:19:14 +08:00
|
|
|
|
:param lastTime 音符延续时间(ms)"""
|
|
|
|
|
self.instrument = instrument
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"""乐器编号"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
self.note = pitch
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"""音符编号"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
self.velocity = velocity
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"""力度/响度"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
self.startTime = startTime
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"""开始之时 ms"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
self.lastTime = lastTime
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"""音符持续时间 ms"""
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
@property
|
|
|
|
|
def inst(self):
|
2023-01-27 23:25:28 +08:00
|
|
|
|
"""乐器编号"""
|
2022-10-16 21:19:14 +08:00
|
|
|
|
return self.instrument
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
@property
|
2022-10-16 21:19:14 +08:00
|
|
|
|
def pitch(self):
|
2023-01-27 23:25:28 +08:00
|
|
|
|
"""音符编号"""
|
2022-10-16 21:19:14 +08:00
|
|
|
|
return self.note
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
return (
|
|
|
|
|
f"Note(inst = {self.inst}, pitch = {self.note}, velocity = {self.velocity}, "
|
|
|
|
|
f"startTime = {self.startTime}, lastTime = {self.lastTime}, )"
|
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
def __tuple__(self):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
return self.inst, self.note, self.velocity, self.startTime, self.lastTime
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
def __dict__(self):
|
|
|
|
|
return {
|
|
|
|
|
"inst": self.inst,
|
|
|
|
|
"pitch": self.note,
|
|
|
|
|
"velocity": self.velocity,
|
|
|
|
|
"startTime": self.startTime,
|
|
|
|
|
"lastTime": self.lastTime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
class MethodList(list):
|
|
|
|
|
def __init__(self, in_=()):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self._T = [_x for _x in in_]
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, item) -> T:
|
|
|
|
|
return self._T[item]
|
2023-01-31 21:06:22 +08:00
|
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
|
return self._T.__len__()
|
2023-01-27 23:25:28 +08:00
|
|
|
|
|
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
学习笔记:
|
|
|
|
|
tempo: microseconds per quarter note 毫秒每四分音符,换句话说就是一拍占多少毫秒
|
|
|
|
|
tick: midi帧
|
|
|
|
|
ticks_per_beat: 帧每拍,即一拍多少帧
|
|
|
|
|
|
|
|
|
|
那么:
|
|
|
|
|
|
|
|
|
|
tick / ticks_per_beat => amount_of_beats 拍数(四分音符数)
|
|
|
|
|
|
|
|
|
|
tempo * amount_of_beats => 毫秒数
|
|
|
|
|
|
|
|
|
|
所以:
|
|
|
|
|
|
|
|
|
|
tempo * tick / ticks_per_beat => 毫秒数
|
|
|
|
|
|
2022-12-31 13:20:30 +08:00
|
|
|
|
###########
|
|
|
|
|
|
|
|
|
|
seconds per tick:
|
|
|
|
|
(tempo / 1000000.0) / ticks_per_beat
|
|
|
|
|
|
|
|
|
|
seconds:
|
|
|
|
|
tick * tempo / 1000000.0 / ticks_per_beat
|
|
|
|
|
|
|
|
|
|
microseconds:
|
|
|
|
|
tick * tempo / 1000.0 / ticks_per_beat
|
|
|
|
|
|
|
|
|
|
gameticks:
|
|
|
|
|
tick * tempo / 1000000.0 / ticks_per_beat * 一秒多少游戏刻
|
|
|
|
|
|
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
|
2022-04-05 23:43:03 +08:00
|
|
|
|
class midiConvert:
|
2022-10-07 16:49:47 +08:00
|
|
|
|
def __init__(self, debug: bool = False):
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""简单的midi转换类,将midi文件转换为我的世界结构或者包"""
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.debugMode: bool = debug
|
|
|
|
|
|
|
|
|
|
self.midiFile: str = ""
|
|
|
|
|
self.midi: VM = None
|
|
|
|
|
self.outputPath: str = ""
|
|
|
|
|
self.midFileName: str = ""
|
|
|
|
|
self.exeHead = ""
|
|
|
|
|
self.methods = MethodList(
|
2023-01-29 13:02:43 +08:00
|
|
|
|
[self._toCmdList_m1, self._toCmdList_m2, self._toCmdList_m3]
|
2023-01-27 23:25:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.methods_byDelay = MethodList(
|
|
|
|
|
[
|
|
|
|
|
self._toCmdList_withDelay_m1,
|
|
|
|
|
self._toCmdList_withDelay_m2,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
if self.debugMode:
|
2023-01-22 00:04:09 +08:00
|
|
|
|
from .magicBeing import prt, ipt
|
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
self.prt = prt
|
|
|
|
|
self.ipt = ipt
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
def convert(self, midiFile: str, outputPath: str, oldExeFormat: bool = True):
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""转换前需要先运行此函数来获取基本信息"""
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2022-04-05 23:43:03 +08:00
|
|
|
|
self.midiFile = midiFile
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""midi文件路径"""
|
2022-11-20 12:02:40 +08:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.midi = mido.MidiFile(self.midiFile)
|
|
|
|
|
"""MidiFile对象"""
|
|
|
|
|
except Exception as E:
|
2022-11-20 15:28:09 +08:00
|
|
|
|
raise MidiDestroyedError(f"文件{self.midiFile}损坏:{E}")
|
2022-12-29 11:09:30 +08:00
|
|
|
|
|
2022-11-20 12:02:40 +08:00
|
|
|
|
self.outputPath = os.path.abspath(outputPath)
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""输出路径"""
|
2022-04-05 23:43:03 +08:00
|
|
|
|
# 将self.midiFile的文件名,不含路径且不含后缀存入self.midiFileName
|
|
|
|
|
self.midFileName = os.path.splitext(os.path.basename(self.midiFile))[0]
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""文件名,不含路径且不含后缀"""
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
self.exeHead = (
|
|
|
|
|
"execute {} ~ ~ ~ "
|
|
|
|
|
if oldExeFormat
|
|
|
|
|
else "execute as {} at @s positioned ~ ~ ~ run "
|
|
|
|
|
)
|
|
|
|
|
"""execute指令的应用,两个版本提前决定。"""
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def __Inst2soundID_withX(instrumentID):
|
2022-06-24 13:46:46 +08:00
|
|
|
|
"""返回midi的乐器ID对应的我的世界乐器名,对于音域转换算法,如下:
|
|
|
|
|
2**( ( msg.note - 60 - X ) / 12 ) 即为MC的音高,其中
|
|
|
|
|
X的取值随乐器不同而变化:
|
|
|
|
|
竖琴harp、电钢琴pling、班卓琴banjo、方波bit、颤音琴iron_xylophone 的时候为6
|
|
|
|
|
吉他的时候为7
|
|
|
|
|
贝斯bass、迪吉里杜管didgeridoo的时候为8
|
|
|
|
|
长笛flute、牛铃cou_bell的时候为5
|
|
|
|
|
钟琴bell、管钟chime、木琴xylophone的时候为4
|
2022-12-31 13:20:30 +08:00
|
|
|
|
而存在一些打击乐器bd(basedrum)、hat、snare,没有音域,则没有X,那么我们返回7即可
|
2022-04-05 23:43:03 +08:00
|
|
|
|
:param instrumentID: midi的乐器ID
|
2022-10-05 22:18:03 +08:00
|
|
|
|
default: 如果instrumentID不在范围内,返回的默认我的世界乐器名称
|
2022-06-24 13:46:46 +08:00
|
|
|
|
:return: (str我的世界乐器名, int转换算法中的X)"""
|
2022-07-22 17:06:09 +08:00
|
|
|
|
try:
|
|
|
|
|
a = {
|
|
|
|
|
0: ("note.harp", 6),
|
|
|
|
|
1: ("note.harp", 6),
|
|
|
|
|
2: ("note.pling", 6),
|
|
|
|
|
3: ("note.harp", 6),
|
|
|
|
|
4: ("note.pling", 6),
|
|
|
|
|
5: ("note.pling", 6),
|
|
|
|
|
6: ("note.harp", 6),
|
|
|
|
|
7: ("note.harp", 6),
|
|
|
|
|
8: ("note.share", 7), # 打击乐器无音域
|
|
|
|
|
9: ("note.harp", 6),
|
|
|
|
|
10: ("note.didgeridoo", 8),
|
|
|
|
|
11: ("note.harp", 6),
|
|
|
|
|
12: ("note.xylophone", 4),
|
|
|
|
|
13: ("note.chime", 4),
|
|
|
|
|
14: ("note.harp", 6),
|
|
|
|
|
15: ("note.harp", 6),
|
|
|
|
|
16: ("note.bass", 8),
|
|
|
|
|
17: ("note.harp", 6),
|
|
|
|
|
18: ("note.harp", 6),
|
|
|
|
|
19: ("note.harp", 6),
|
|
|
|
|
20: ("note.harp", 6),
|
|
|
|
|
21: ("note.harp", 6),
|
|
|
|
|
22: ("note.harp", 6),
|
|
|
|
|
23: ("note.guitar", 7),
|
|
|
|
|
24: ("note.guitar", 7),
|
|
|
|
|
25: ("note.guitar", 7),
|
|
|
|
|
26: ("note.guitar", 7),
|
|
|
|
|
27: ("note.guitar", 7),
|
|
|
|
|
28: ("note.guitar", 7),
|
|
|
|
|
29: ("note.guitar", 7),
|
|
|
|
|
30: ("note.guitar", 7),
|
|
|
|
|
31: ("note.bass", 8),
|
|
|
|
|
32: ("note.bass", 8),
|
|
|
|
|
33: ("note.bass", 8),
|
|
|
|
|
34: ("note.bass", 8),
|
|
|
|
|
35: ("note.bass", 8),
|
|
|
|
|
36: ("note.bass", 8),
|
|
|
|
|
37: ("note.bass", 8),
|
|
|
|
|
38: ("note.bass", 8),
|
|
|
|
|
39: ("note.bass", 8),
|
|
|
|
|
40: ("note.harp", 6),
|
|
|
|
|
41: ("note.harp", 6),
|
|
|
|
|
42: ("note.harp", 6),
|
|
|
|
|
43: ("note.harp", 6),
|
|
|
|
|
44: ("note.iron_xylophone", 6),
|
|
|
|
|
45: ("note.guitar", 7),
|
|
|
|
|
46: ("note.harp", 6),
|
|
|
|
|
47: ("note.harp", 6),
|
|
|
|
|
48: ("note.guitar", 7),
|
|
|
|
|
49: ("note.guitar", 7),
|
|
|
|
|
50: ("note.bit", 6),
|
|
|
|
|
51: ("note.bit", 6),
|
|
|
|
|
52: ("note.harp", 6),
|
|
|
|
|
53: ("note.harp", 6),
|
|
|
|
|
54: ("note.bit", 6),
|
|
|
|
|
55: ("note.flute", 5),
|
|
|
|
|
56: ("note.flute", 5),
|
|
|
|
|
57: ("note.flute", 5),
|
|
|
|
|
58: ("note.flute", 5),
|
|
|
|
|
59: ("note.flute", 5),
|
|
|
|
|
60: ("note.flute", 5),
|
|
|
|
|
61: ("note.flute", 5),
|
|
|
|
|
62: ("note.flute", 5),
|
|
|
|
|
63: ("note.flute", 5),
|
|
|
|
|
64: ("note.bit", 6),
|
|
|
|
|
65: ("note.bit", 6),
|
|
|
|
|
66: ("note.bit", 6),
|
|
|
|
|
67: ("note.bit", 6),
|
|
|
|
|
68: ("note.flute", 5),
|
|
|
|
|
69: ("note.harp", 6),
|
|
|
|
|
70: ("note.harp", 6),
|
|
|
|
|
71: ("note.flute", 5),
|
|
|
|
|
72: ("note.flute", 5),
|
|
|
|
|
73: ("note.flute", 5),
|
|
|
|
|
74: ("note.harp", 6),
|
|
|
|
|
75: ("note.flute", 5),
|
|
|
|
|
76: ("note.harp", 6),
|
|
|
|
|
77: ("note.harp", 6),
|
|
|
|
|
78: ("note.harp", 6),
|
|
|
|
|
79: ("note.harp", 6),
|
|
|
|
|
80: ("note.bit", 6),
|
|
|
|
|
81: ("note.bit", 6),
|
|
|
|
|
82: ("note.bit", 6),
|
|
|
|
|
83: ("note.bit", 6),
|
|
|
|
|
84: ("note.bit", 6),
|
|
|
|
|
85: ("note.bit", 6),
|
|
|
|
|
86: ("note.bit", 6),
|
|
|
|
|
87: ("note.bit", 6),
|
|
|
|
|
88: ("note.bit", 6),
|
|
|
|
|
89: ("note.bit", 6),
|
|
|
|
|
90: ("note.bit", 6),
|
|
|
|
|
91: ("note.bit", 6),
|
|
|
|
|
92: ("note.bit", 6),
|
|
|
|
|
93: ("note.bit", 6),
|
|
|
|
|
94: ("note.bit", 6),
|
|
|
|
|
95: ("note.bit", 6),
|
|
|
|
|
96: ("note.bit", 6),
|
|
|
|
|
97: ("note.bit", 6),
|
|
|
|
|
98: ("note.bit", 6),
|
|
|
|
|
99: ("note.bit", 6),
|
|
|
|
|
100: ("note.bit", 6),
|
|
|
|
|
101: ("note.bit", 6),
|
|
|
|
|
102: ("note.bit", 6),
|
|
|
|
|
103: ("note.bit", 6),
|
|
|
|
|
104: ("note.harp", 6),
|
|
|
|
|
105: ("note.banjo", 6),
|
|
|
|
|
106: ("note.harp", 6),
|
|
|
|
|
107: ("note.harp", 6),
|
|
|
|
|
108: ("note.harp", 6),
|
|
|
|
|
109: ("note.harp", 6),
|
|
|
|
|
110: ("note.harp", 6),
|
|
|
|
|
111: ("note.guitar", 7),
|
|
|
|
|
112: ("note.harp", 6),
|
|
|
|
|
113: ("note.bell", 4),
|
|
|
|
|
114: ("note.harp", 6),
|
|
|
|
|
115: ("note.cow_bell", 5),
|
2022-12-31 13:20:30 +08:00
|
|
|
|
116: ("note.bd", 7), # 打击乐器无音域
|
2022-07-22 17:06:09 +08:00
|
|
|
|
117: ("note.bass", 8),
|
|
|
|
|
118: ("note.bit", 6),
|
2022-12-31 13:20:30 +08:00
|
|
|
|
119: ("note.bd", 7), # 打击乐器无音域
|
2022-07-22 17:06:09 +08:00
|
|
|
|
120: ("note.guitar", 7),
|
|
|
|
|
121: ("note.harp", 6),
|
|
|
|
|
122: ("note.harp", 6),
|
|
|
|
|
123: ("note.harp", 6),
|
|
|
|
|
124: ("note.harp", 6),
|
|
|
|
|
125: ("note.hat", 7), # 打击乐器无音域
|
2022-12-31 13:20:30 +08:00
|
|
|
|
126: ("note.bd", 7), # 打击乐器无音域
|
2022-07-22 17:06:09 +08:00
|
|
|
|
127: ("note.snare", 7), # 打击乐器无音域
|
|
|
|
|
}[instrumentID]
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except KeyError:
|
2022-12-31 13:20:30 +08:00
|
|
|
|
a = ("note.flute", 5)
|
2022-07-22 17:06:09 +08:00
|
|
|
|
return a
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def __bitInst2ID_withX(instrumentID):
|
2023-01-02 14:28:25 +08:00
|
|
|
|
try:
|
|
|
|
|
try:
|
|
|
|
|
return {
|
2023-01-28 11:56:09 +08:00
|
|
|
|
34: ("note.bd", 7),
|
|
|
|
|
35: ("note.bd", 7),
|
|
|
|
|
36: ("note.hat", 7),
|
|
|
|
|
37: ("note.snare", 7),
|
|
|
|
|
38: ("note.snare", 7),
|
|
|
|
|
39: ("note.snare", 7),
|
|
|
|
|
40: ("note.hat", 7),
|
|
|
|
|
41: ("note.snare", 7),
|
|
|
|
|
42: ("note.hat", 7),
|
|
|
|
|
43: ("note.snare", 7),
|
|
|
|
|
44: ("note.snare", 7),
|
|
|
|
|
45: ("note.bell", 4),
|
|
|
|
|
46: ("note.snare", 7),
|
|
|
|
|
47: ("note.snare", 7),
|
|
|
|
|
48: ("note.bell", 4),
|
|
|
|
|
49: ("note.hat", 7),
|
|
|
|
|
50: ("note.bell", 4),
|
|
|
|
|
51: ("note.bell", 4),
|
|
|
|
|
52: ("note.bell", 4),
|
|
|
|
|
53: ("note.bell", 4),
|
|
|
|
|
54: ("note.bell", 4),
|
|
|
|
|
55: ("note.bell", 4),
|
|
|
|
|
56: ("note.snare", 7),
|
|
|
|
|
57: ("note.hat", 7),
|
|
|
|
|
58: ("note.chime", 4),
|
|
|
|
|
59: ("note.iron_xylophone", 6),
|
|
|
|
|
60: ("note.bd", 7),
|
|
|
|
|
61: ("note.bd", 7),
|
|
|
|
|
62: ("note.xylophone", 4),
|
|
|
|
|
63: ("note.xylophone", 4),
|
|
|
|
|
64: ("note.xylophone", 4),
|
|
|
|
|
65: ("note.hat", 7),
|
|
|
|
|
66: ("note.bell", 4),
|
|
|
|
|
67: ("note.bell", 4),
|
|
|
|
|
68: ("note.hat", 7),
|
|
|
|
|
69: ("note.hat", 7),
|
|
|
|
|
70: ("note.flute", 5),
|
|
|
|
|
71: ("note.flute", 5),
|
|
|
|
|
72: ("note.hat", 7),
|
|
|
|
|
73: ("note.hat", 7),
|
|
|
|
|
74: ("note.xylophone", 4),
|
|
|
|
|
75: ("note.hat", 7),
|
|
|
|
|
76: ("note.hat", 7),
|
|
|
|
|
77: ("note.xylophone", 4),
|
|
|
|
|
78: ("note.xylophone", 4),
|
|
|
|
|
79: ("note.bell", 4),
|
|
|
|
|
80: ("note.bell", 4),
|
2023-01-02 14:28:25 +08:00
|
|
|
|
}[instrumentID]
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except KeyError:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return "note.bd", 7
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except KeyError:
|
2023-01-20 00:31:45 +08:00
|
|
|
|
print("WARN", "无法使用打击乐器列表库,可能是不支持当前环境,打击乐器使用Dislink算法代替。")
|
2023-01-02 14:28:25 +08:00
|
|
|
|
if instrumentID == 55:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return "note.cow_bell", 5
|
2023-01-02 14:28:25 +08:00
|
|
|
|
elif instrumentID in [41, 43, 45]:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return "note.hat", 7
|
2023-01-02 14:28:25 +08:00
|
|
|
|
elif instrumentID in [36, 37, 39]:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return "note.snare", 7
|
2023-01-02 14:28:25 +08:00
|
|
|
|
else:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return "note.bd", 7
|
2022-12-31 13:20:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def __score2time(score: int):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
return str(int(int(score / 20) / 60)) + ":" + str(int(int(score / 20) % 60))
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
|
|
|
|
def __formProgressBar(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
maxscore: int,
|
|
|
|
|
scoreboard_name: str,
|
|
|
|
|
progressbar: tuple = (
|
|
|
|
|
r"▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]",
|
|
|
|
|
("§e=§r", "§7=§r"),
|
|
|
|
|
),
|
2022-05-08 01:30:30 +08:00
|
|
|
|
) -> list:
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
pgs_style = progressbar[0]
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""用于被替换的进度条原始样式"""
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""
|
2022-05-08 01:30:30 +08:00
|
|
|
|
| 标识符 | 指定的可变量 |
|
|
|
|
|
|---------|----------------|
|
|
|
|
|
| `%%N` | 乐曲名(即传入的文件名)|
|
|
|
|
|
| `%%s` | 当前计分板值 |
|
|
|
|
|
| `%^s` | 计分板最大值 |
|
|
|
|
|
| `%%t` | 当前播放时间 |
|
|
|
|
|
| `%^t` | 曲目总时长 |
|
|
|
|
|
| `%%%` | 当前进度比率 |
|
|
|
|
|
| `_` | 用以表示进度条占位|
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"""
|
2023-01-28 11:56:09 +08:00
|
|
|
|
perEach = maxscore / pgs_style.count("_")
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result = []
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%^s" in pgs_style:
|
|
|
|
|
pgs_style = pgs_style.replace(r"%^s", str(maxscore))
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%^t" in pgs_style:
|
|
|
|
|
pgs_style = pgs_style.replace(r"%^t", self.__score2time(maxscore))
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc = scoreboard_name[:2]
|
|
|
|
|
if r"%%%" in pgs_style:
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
'scoreboard objectives add {}PercT dummy "百分比计算"'.format(sbn_pc)
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players set MaxScore {} {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
scoreboard_name, maxscore
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
|
|
|
|
+ "scoreboard players set n100 {} 100".format(scoreboard_name)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} = @s {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "PercT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} *= n100 {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "PercT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} /= MaxScore {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "PercT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%t" in pgs_style:
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
'scoreboard objectives add {}TMinT dummy "时间计算:分"'.format(sbn_pc)
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
'scoreboard objectives add {}TSecT dummy "时间计算:秒"'.format(sbn_pc)
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
|
|
|
|
+ "scoreboard players set n20 {} 20".format(scoreboard_name)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
|
|
|
|
+ "scoreboard players set n60 {} 60".format(scoreboard_name)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} = @s {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TMinT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} /= n20 {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TMinT", scoreboard_name
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} /= n60 {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TMinT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} = @s {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TSecT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} /= n20 {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TSecT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
result.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.exeHead.format("@a[scores={" + scoreboard_name + "=1..}]")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ "scoreboard players operation @s {} %= n60 {}".format(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
sbn_pc + "TSecT", scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
for i in range(pgs_style.count("_")):
|
2023-01-27 23:25:28 +08:00
|
|
|
|
npg_stl = (
|
2023-01-29 13:02:43 +08:00
|
|
|
|
pgs_style.replace("_", progressbar[1][0], i + 1)
|
|
|
|
|
.replace("_", progressbar[1][1])
|
|
|
|
|
.replace(r"%%N", self.midFileName)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%N" in pgs_style
|
2023-01-29 13:02:43 +08:00
|
|
|
|
else pgs_style.replace("_", progressbar[1][0], i + 1).replace(
|
|
|
|
|
"_", progressbar[1][1]
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%s" in npg_stl:
|
|
|
|
|
npg_stl = npg_stl.replace(
|
2023-01-20 00:31:45 +08:00
|
|
|
|
r"%%s",
|
|
|
|
|
'"},{"score":{"name":"*","objective":"'
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ '"}},{"text":"',
|
2022-10-07 16:49:47 +08:00
|
|
|
|
)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%%" in npg_stl:
|
|
|
|
|
npg_stl = npg_stl.replace(
|
2023-01-20 00:31:45 +08:00
|
|
|
|
r"%%%",
|
2023-01-29 13:02:43 +08:00
|
|
|
|
r'"},{"score":{"name":"*","objective":"'
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ sbn_pc
|
2023-01-29 13:02:43 +08:00
|
|
|
|
+ r'PercT"}},{"text":"%',
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%t" in npg_stl:
|
|
|
|
|
npg_stl = npg_stl.replace(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
r"%%t",
|
|
|
|
|
r'"},{"score":{"name":"*","objective":"{-}TMinT"}},{"text":":"},'
|
2023-01-27 23:25:28 +08:00
|
|
|
|
r'{"score":{"name":"*","objective":"{-}TSecT"}},{"text":"'.replace(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
r"{-}", sbn_pc
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
result.append(
|
|
|
|
|
self.exeHead.format(
|
2023-01-29 13:02:43 +08:00
|
|
|
|
r"@a[scores={"
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ f"={int(i * perEach)}..{math.ceil((i + 1) * perEach)}"
|
2023-01-29 13:02:43 +08:00
|
|
|
|
+ r"}]"
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
+ r'titleraw @s actionbar {"rawtext":[{"text":"'
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ npg_stl
|
2023-01-20 00:31:45 +08:00
|
|
|
|
+ r'"}]}'
|
2022-10-07 16:49:47 +08:00
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if r"%%%" in pgs_style:
|
|
|
|
|
result.append("scoreboard objectives remove {}PercT".format(sbn_pc))
|
|
|
|
|
if r"%%t" in pgs_style:
|
|
|
|
|
result.append("scoreboard objectives remove {}TMinT".format(sbn_pc))
|
|
|
|
|
result.append("scoreboard objectives remove {}TSecT".format(sbn_pc))
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
return result
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
def _toCmdList_m1(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
scoreboard_name: str = "mscplay",
|
|
|
|
|
MaxVolume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
|
|
|
|
) -> list:
|
2022-04-29 11:33:52 +08:00
|
|
|
|
"""
|
2022-06-19 01:07:07 +08:00
|
|
|
|
使用Dislink Sforza的转换思路,将midi转换为我的世界命令列表
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param scoreboard_name: 我的世界的计分板名称
|
2022-04-05 23:43:03 +08:00
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
2022-05-08 01:30:30 +08:00
|
|
|
|
:return: tuple(命令列表, 命令个数, 计分板最大值)
|
2022-04-29 11:33:52 +08:00
|
|
|
|
"""
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
2022-04-05 23:43:03 +08:00
|
|
|
|
tracks = []
|
2023-01-28 11:56:09 +08:00
|
|
|
|
MaxVolume = 1 if MaxVolume > 1 else (0.001 if MaxVolume <= 0 else MaxVolume)
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
|
|
|
|
commands = 0
|
2022-05-08 01:30:30 +08:00
|
|
|
|
maxscore = 0
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
# 分轨的思路其实并不好,但这个算法就是这样
|
|
|
|
|
# 所以我建议用第二个方法 _toCmdList_m2
|
2022-04-05 23:43:03 +08:00
|
|
|
|
for i, track in enumerate(self.midi.tracks):
|
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
ticks = 0
|
|
|
|
|
instrumentID = 0
|
2022-04-05 23:43:03 +08:00
|
|
|
|
singleTrack = []
|
|
|
|
|
|
|
|
|
|
for msg in track:
|
2022-06-19 01:07:07 +08:00
|
|
|
|
ticks += msg.time
|
2022-04-05 23:43:03 +08:00
|
|
|
|
if msg.is_meta:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "set_tempo":
|
2022-04-29 11:29:49 +08:00
|
|
|
|
tempo = msg.tempo
|
2022-06-25 23:20:56 +08:00
|
|
|
|
else:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "program_change":
|
2022-04-29 11:29:49 +08:00
|
|
|
|
instrumentID = msg.program
|
2022-12-29 11:09:30 +08:00
|
|
|
|
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "note_on" and msg.velocity != 0:
|
2022-10-07 16:49:47 +08:00
|
|
|
|
try:
|
|
|
|
|
nowscore = round(
|
|
|
|
|
(ticks * tempo)
|
|
|
|
|
/ ((self.midi.ticks_per_beat * float(speed)) * 50000)
|
|
|
|
|
)
|
|
|
|
|
except NameError:
|
2022-10-16 21:19:14 +08:00
|
|
|
|
raise NotDefineTempoError("计算当前分数时出错 未定义参量 Tempo")
|
2022-05-08 01:30:30 +08:00
|
|
|
|
maxscore = max(maxscore, nowscore)
|
2023-01-02 14:28:25 +08:00
|
|
|
|
if msg.channel == 9:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
soundID, _X = self.__bitInst2ID_withX(instrumentID)
|
2023-01-02 14:28:25 +08:00
|
|
|
|
else:
|
2023-01-28 11:56:09 +08:00
|
|
|
|
soundID, _X = self.__Inst2soundID_withX(instrumentID)
|
2022-12-29 11:09:30 +08:00
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
singleTrack.append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"execute @a[scores={"
|
|
|
|
|
+ str(scoreboard_name)
|
|
|
|
|
+ "="
|
|
|
|
|
+ str(nowscore)
|
|
|
|
|
+ "}"
|
|
|
|
|
+ f"] ~ ~ ~ playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg.velocity / 128} "
|
|
|
|
|
f"{2 ** ((msg.note - 60 - _X) / 12)}"
|
|
|
|
|
)
|
2022-04-29 11:29:49 +08:00
|
|
|
|
commands += 1
|
2022-05-08 01:30:30 +08:00
|
|
|
|
if len(singleTrack) != 0:
|
|
|
|
|
tracks.append(singleTrack)
|
|
|
|
|
|
2022-10-05 22:18:03 +08:00
|
|
|
|
return [tracks, commands, maxscore]
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
2022-12-31 13:20:30 +08:00
|
|
|
|
# 原本这个算法的转换效果应该和上面的算法相似的
|
2022-08-08 22:08:08 +08:00
|
|
|
|
def _toCmdList_m2(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
scoreboard_name: str = "mscplay",
|
|
|
|
|
MaxVolume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
2022-10-07 16:49:47 +08:00
|
|
|
|
) -> list:
|
2022-08-08 22:08:08 +08:00
|
|
|
|
"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
使用金羿的转换思路,将midi转换为我的世界命令列表
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param scoreboard_name: 我的世界的计分板名称
|
2022-10-07 19:25:28 +08:00
|
|
|
|
:param MaxVolume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
2022-08-08 22:08:08 +08:00
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
|
|
|
|
:return: tuple(命令列表, 命令个数, 计分板最大值)
|
|
|
|
|
"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
MaxVolume = 1 if MaxVolume > 1 else (0.001 if MaxVolume <= 0 else MaxVolume)
|
2022-08-08 22:08:08 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# 一个midi中仅有16个通道 我们通过通道来识别而不是音轨
|
2023-01-22 00:04:09 +08:00
|
|
|
|
channels = {
|
|
|
|
|
0: [],
|
|
|
|
|
1: [],
|
|
|
|
|
2: [],
|
|
|
|
|
3: [],
|
|
|
|
|
4: [],
|
|
|
|
|
5: [],
|
|
|
|
|
6: [],
|
|
|
|
|
7: [],
|
|
|
|
|
8: [],
|
|
|
|
|
9: [],
|
|
|
|
|
10: [],
|
|
|
|
|
11: [],
|
|
|
|
|
12: [],
|
|
|
|
|
13: [],
|
|
|
|
|
14: [],
|
|
|
|
|
15: [],
|
|
|
|
|
16: [],
|
|
|
|
|
}
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
microseconds = 0
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
# 我们来用通道统计音乐信息
|
|
|
|
|
for msg in self.midi:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
if msg.time != 0:
|
|
|
|
|
try:
|
2023-02-01 10:09:16 +08:00
|
|
|
|
microseconds += msg.time * 1000
|
2023-01-20 01:33:57 +08:00
|
|
|
|
# print(microseconds)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
except NameError:
|
2023-01-20 22:26:04 +08:00
|
|
|
|
if self.debugMode:
|
|
|
|
|
raise NotDefineTempoError("计算当前分数时出错 未定义参量 Tempo")
|
|
|
|
|
else:
|
2023-01-22 00:04:09 +08:00
|
|
|
|
microseconds += (
|
2023-02-01 10:09:16 +08:00
|
|
|
|
msg.time * 1000
|
2023-01-22 00:04:09 +08:00
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
if msg.is_meta:
|
|
|
|
|
if msg.type == "set_tempo":
|
|
|
|
|
tempo = msg.tempo
|
2023-01-20 21:34:15 +08:00
|
|
|
|
if self.debugMode:
|
|
|
|
|
self.prt(f"TEMPO更改:{tempo}(毫秒每拍)")
|
2023-01-20 00:31:45 +08:00
|
|
|
|
else:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 22:26:04 +08:00
|
|
|
|
if self.debugMode:
|
|
|
|
|
try:
|
|
|
|
|
if msg.channel > 15:
|
2023-01-28 11:56:09 +08:00
|
|
|
|
raise ChannelOverFlowError(f"当前消息 {msg} 的通道超限(≤15)")
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except AttributeError:
|
2023-01-20 22:26:04 +08:00
|
|
|
|
pass
|
2023-01-20 00:31:45 +08:00
|
|
|
|
|
|
|
|
|
if msg.type == "program_change":
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("PgmC", msg.program, microseconds))
|
2023-01-20 00:31:45 +08:00
|
|
|
|
|
|
|
|
|
elif msg.type == "note_on" and msg.velocity != 0:
|
|
|
|
|
channels[msg.channel].append(
|
|
|
|
|
("NoteS", msg.note, msg.velocity, microseconds)
|
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
elif (msg.type == "note_on" and msg.velocity == 0) or (
|
2023-01-28 11:56:09 +08:00
|
|
|
|
msg.type == "note_off"
|
2023-01-20 00:31:45 +08:00
|
|
|
|
):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("NoteE", msg.note, microseconds))
|
2023-01-22 00:04:09 +08:00
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
"""整合后的音乐通道格式
|
2022-10-07 19:25:28 +08:00
|
|
|
|
每个通道包括若干消息元素其中逃不过这三种:
|
|
|
|
|
|
|
|
|
|
1 切换乐器消息
|
|
|
|
|
("PgmC", 切换后的乐器ID: int, 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
2 音符开始消息
|
|
|
|
|
("NoteS", 开始的音符ID, 力度(响度), 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
3 音符结束消息
|
2022-10-16 21:19:14 +08:00
|
|
|
|
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
if self.debugMode:
|
2023-01-20 22:26:04 +08:00
|
|
|
|
self.prt(channels)
|
2023-01-20 21:34:15 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
tracks = []
|
|
|
|
|
cmdAmount = 0
|
|
|
|
|
maxScore = 0
|
|
|
|
|
|
|
|
|
|
# 此处 我们把通道视为音轨
|
2023-01-20 22:26:04 +08:00
|
|
|
|
for i in channels.keys():
|
2022-10-07 19:25:28 +08:00
|
|
|
|
# 如果当前通道为空 则跳过
|
2023-01-20 00:31:45 +08:00
|
|
|
|
if not channels[i]:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
continue
|
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
if i == 9:
|
2022-12-31 13:20:30 +08:00
|
|
|
|
SpecialBits = True
|
2022-10-07 19:25:28 +08:00
|
|
|
|
else:
|
2022-12-31 13:20:30 +08:00
|
|
|
|
SpecialBits = False
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
nowTrack = []
|
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
for msg in channels[i]:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
if msg[0] == "PgmC":
|
|
|
|
|
InstID = msg[1]
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
elif msg[0] == "NoteS":
|
2023-01-20 21:34:15 +08:00
|
|
|
|
try:
|
2023-01-22 00:04:09 +08:00
|
|
|
|
soundID, _X = (
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.__bitInst2ID_withX(InstID)
|
2023-01-22 00:04:09 +08:00
|
|
|
|
if SpecialBits
|
2023-01-27 23:25:28 +08:00
|
|
|
|
else self.__Inst2soundID_withX(InstID)
|
2023-01-22 00:04:09 +08:00
|
|
|
|
)
|
2023-01-20 21:34:15 +08:00
|
|
|
|
except UnboundLocalError as E:
|
|
|
|
|
if self.debugMode:
|
|
|
|
|
raise NotDefineProgramError(f"未定义乐器便提前演奏。\n{E}")
|
|
|
|
|
else:
|
2023-01-22 00:04:09 +08:00
|
|
|
|
soundID, _X = (
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.__bitInst2ID_withX(-1)
|
2023-01-22 00:04:09 +08:00
|
|
|
|
if SpecialBits
|
2023-01-27 23:25:28 +08:00
|
|
|
|
else self.__Inst2soundID_withX(-1)
|
2023-01-22 00:04:09 +08:00
|
|
|
|
)
|
2023-01-20 01:33:57 +08:00
|
|
|
|
score_now = round(msg[-1] / float(speed) / 50)
|
2022-10-16 21:19:14 +08:00
|
|
|
|
maxScore = max(maxScore, score_now)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
nowTrack.append(
|
2023-01-20 00:31:45 +08:00
|
|
|
|
self.exeHead.format(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"@a[scores=({}={})]".format(scoreboard_name, score_now)
|
|
|
|
|
.replace("(", r"{")
|
|
|
|
|
.replace(")", r"}")
|
|
|
|
|
)
|
|
|
|
|
+ f"playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg[2] / 128} "
|
|
|
|
|
f"{2 ** ((msg[1] - 60 - _X) / 12)}"
|
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
cmdAmount += 1
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
tracks.append(nowTrack)
|
|
|
|
|
|
|
|
|
|
return [tracks, cmdAmount, maxScore]
|
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
# 简单的单音填充
|
2022-10-07 19:25:28 +08:00
|
|
|
|
def _toCmdList_m3(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
scoreboard_name: str = "mscplay",
|
|
|
|
|
MaxVolume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
2022-10-07 19:25:28 +08:00
|
|
|
|
) -> list:
|
|
|
|
|
"""
|
2023-01-02 14:28:25 +08:00
|
|
|
|
使用金羿的转换思路,将midi转换为我的世界命令列表,并使用完全填充算法优化音感
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param scoreboard_name: 我的世界的计分板名称
|
2022-10-07 19:25:28 +08:00
|
|
|
|
:param MaxVolume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
|
|
|
|
:return: tuple(命令列表, 命令个数, 计分板最大值)
|
|
|
|
|
"""
|
2023-02-01 10:09:16 +08:00
|
|
|
|
# TODO: 这里的时间转换不知道有没有问题
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
if MaxVolume > 1:
|
2022-10-23 02:12:11 +08:00
|
|
|
|
MaxVolume = 1.0
|
2022-10-07 19:25:28 +08:00
|
|
|
|
if MaxVolume <= 0:
|
|
|
|
|
MaxVolume = 0.001
|
2022-08-08 22:08:08 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# 一个midi中仅有16个通道 我们通过通道来识别而不是音轨
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
|
2022-10-07 16:49:47 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
# 我们来用通道统计音乐信息
|
2022-08-08 22:08:08 +08:00
|
|
|
|
for i, track in enumerate(self.midi.tracks):
|
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
microseconds = 0
|
2022-08-08 22:08:08 +08:00
|
|
|
|
|
|
|
|
|
for msg in track:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
if msg.time != 0:
|
|
|
|
|
try:
|
|
|
|
|
microseconds += msg.time * tempo / self.midi.ticks_per_beat
|
|
|
|
|
except NameError:
|
2022-10-16 21:19:14 +08:00
|
|
|
|
raise NotDefineTempoError("计算当前分数时出错 未定义参量 Tempo")
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-08-08 22:08:08 +08:00
|
|
|
|
if msg.is_meta:
|
|
|
|
|
if msg.type == "set_tempo":
|
|
|
|
|
tempo = msg.tempo
|
|
|
|
|
else:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-29 13:02:43 +08:00
|
|
|
|
if self.debugMode:
|
|
|
|
|
try:
|
|
|
|
|
if msg.channel > 15:
|
|
|
|
|
raise ChannelOverFlowError(f"当前消息 {msg} 的通道超限(≤15)")
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-08-08 22:08:08 +08:00
|
|
|
|
if msg.type == "program_change":
|
2022-10-07 19:25:28 +08:00
|
|
|
|
channels[msg.channel].append(
|
|
|
|
|
("PgmC", msg.program, microseconds)
|
2022-08-08 22:08:08 +08:00
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
elif msg.type == "note_on" and msg.velocity != 0:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
channels[msg.channel].append(
|
|
|
|
|
("NoteS", msg.note, msg.velocity, microseconds)
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
elif (msg.type == "note_on" and msg.velocity == 0) or (
|
2023-01-28 11:56:09 +08:00
|
|
|
|
msg.type == "note_off"
|
2022-10-07 19:25:28 +08:00
|
|
|
|
):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("NoteE", msg.note, microseconds))
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
"""整合后的音乐通道格式
|
2022-10-07 19:25:28 +08:00
|
|
|
|
每个通道包括若干消息元素其中逃不过这三种:
|
|
|
|
|
|
|
|
|
|
1 切换乐器消息
|
|
|
|
|
|
|
|
|
|
("PgmC", 切换后的乐器ID: int, 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
2 音符开始消息
|
|
|
|
|
|
|
|
|
|
("NoteS", 开始的音符ID, 力度(响度), 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
3 音符结束消息
|
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
note_channels = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
# 此处 我们把通道视为音轨
|
|
|
|
|
for i in range(len(channels)):
|
|
|
|
|
# 如果当前通道为空 则跳过
|
|
|
|
|
|
|
|
|
|
noteMsgs = []
|
2022-10-23 02:12:11 +08:00
|
|
|
|
MsgIndex = []
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
for msg in channels[i]:
|
|
|
|
|
|
|
|
|
|
if msg[0] == "PgmC":
|
|
|
|
|
InstID = msg[1]
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
elif msg[0] == "NoteS":
|
|
|
|
|
noteMsgs.append(msg[1:])
|
2022-10-23 02:12:11 +08:00
|
|
|
|
MsgIndex.append(msg[1])
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
elif msg[0] == "NoteE":
|
2022-10-23 02:12:11 +08:00
|
|
|
|
if msg[1] in MsgIndex:
|
|
|
|
|
note_channels[i].append(
|
|
|
|
|
SingleNote(
|
|
|
|
|
InstID,
|
|
|
|
|
msg[1],
|
|
|
|
|
noteMsgs[MsgIndex.index(msg[1])][1],
|
|
|
|
|
noteMsgs[MsgIndex.index(msg[1])][2],
|
|
|
|
|
msg[-1] - noteMsgs[MsgIndex.index(msg[1])][2],
|
2022-10-16 21:19:14 +08:00
|
|
|
|
)
|
2022-10-23 02:12:11 +08:00
|
|
|
|
)
|
|
|
|
|
noteMsgs.pop(MsgIndex.index(msg[1]))
|
|
|
|
|
MsgIndex.pop(MsgIndex.index(msg[1]))
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-10-23 02:12:11 +08:00
|
|
|
|
tracks = []
|
2022-10-07 19:25:28 +08:00
|
|
|
|
cmdAmount = 0
|
|
|
|
|
maxScore = 0
|
|
|
|
|
CheckFirstChannel = False
|
|
|
|
|
|
2022-10-23 02:12:11 +08:00
|
|
|
|
# 临时用的插值计算函数
|
2023-01-27 23:25:28 +08:00
|
|
|
|
def _linearFun(_note: SingleNote) -> list:
|
|
|
|
|
"""传入音符数据,返回以半秒为分割的插值列表
|
|
|
|
|
:param _note: SingleNote 音符
|
|
|
|
|
:return list[tuple(int开始时间(毫秒), int乐器, int音符, int力度(内置), float音量(播放)),]"""
|
2022-12-29 11:09:30 +08:00
|
|
|
|
|
2022-10-23 02:12:11 +08:00
|
|
|
|
result = []
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
totalCount = int(_note.lastTime / 500)
|
2022-10-23 02:12:11 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for _i in range(totalCount):
|
2023-01-02 14:28:25 +08:00
|
|
|
|
result.append(
|
|
|
|
|
(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
_note.startTime + _i * 500,
|
|
|
|
|
_note.instrument,
|
|
|
|
|
_note.pitch,
|
|
|
|
|
_note.velocity,
|
|
|
|
|
MaxVolume * ((totalCount - _i) / totalCount),
|
2023-01-02 14:28:25 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-12-29 11:09:30 +08:00
|
|
|
|
|
2022-10-23 02:12:11 +08:00
|
|
|
|
return result
|
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
# 此处 我们把通道视为音轨
|
|
|
|
|
for track in note_channels:
|
|
|
|
|
# 如果当前通道为空 则跳过
|
|
|
|
|
if not track:
|
|
|
|
|
continue
|
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
if note_channels.index(track) == 0:
|
2022-10-07 19:25:28 +08:00
|
|
|
|
CheckFirstChannel = True
|
2023-01-02 14:28:25 +08:00
|
|
|
|
SpecialBits = False
|
|
|
|
|
elif note_channels.index(track) == 9:
|
|
|
|
|
SpecialBits = True
|
2022-10-07 19:25:28 +08:00
|
|
|
|
else:
|
|
|
|
|
CheckFirstChannel = False
|
2023-01-02 14:28:25 +08:00
|
|
|
|
SpecialBits = False
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
|
|
|
|
nowTrack = []
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2022-10-07 19:25:28 +08:00
|
|
|
|
for note in track:
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for every_note in _linearFun(note):
|
2022-10-16 21:19:14 +08:00
|
|
|
|
# 应该是计算的时候出了点小问题
|
|
|
|
|
# 我们应该用一个MC帧作为时间单位而不是半秒
|
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
if SpecialBits:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
soundID, _X = self.__bitInst2ID_withX(InstID)
|
2023-01-02 14:28:25 +08:00
|
|
|
|
else:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
soundID, _X = self.__Inst2soundID_withX(InstID)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
score_now = round(every_note[0] / speed / 50000)
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
|
|
|
|
maxScore = max(maxScore, score_now)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-10-07 20:23:02 +08:00
|
|
|
|
nowTrack.append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"execute @a[scores={"
|
|
|
|
|
+ str(scoreboard_name)
|
|
|
|
|
+ "="
|
|
|
|
|
+ str(score_now)
|
|
|
|
|
+ "}"
|
|
|
|
|
+ f"] ~ ~ ~ playsound {soundID} @s ~ ~{1 / every_note[4] - 1} ~ "
|
|
|
|
|
f"{note.velocity * (0.7 if CheckFirstChannel else 0.9)} {2 ** ((note.pitch - 60 - _X) / 12)}"
|
|
|
|
|
)
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-10-07 20:23:02 +08:00
|
|
|
|
cmdAmount += 1
|
2022-10-07 19:25:28 +08:00
|
|
|
|
tracks.append(nowTrack)
|
|
|
|
|
|
|
|
|
|
return [tracks, cmdAmount, maxScore]
|
|
|
|
|
|
2022-06-19 01:07:07 +08:00
|
|
|
|
def _toCmdList_withDelay_m1(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
MaxVolume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
|
|
|
|
player: str = "@a",
|
2022-06-19 01:07:07 +08:00
|
|
|
|
) -> list:
|
2022-05-08 01:30:30 +08:00
|
|
|
|
"""
|
2022-06-19 01:07:07 +08:00
|
|
|
|
使用Dislink Sforza的转换思路,将midi转换为我的世界命令列表,并输出每个音符之后的延迟
|
2022-05-08 01:30:30 +08:00
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
2022-06-19 01:07:07 +08:00
|
|
|
|
:param player: 玩家选择器,默认为`@a`
|
|
|
|
|
:return: 全部指令列表[ ( str指令, int距离上一个指令的延迟 ),...]
|
2022-05-08 01:30:30 +08:00
|
|
|
|
"""
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
2022-06-19 01:07:07 +08:00
|
|
|
|
tracks = {}
|
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
MaxVolume = 1 if MaxVolume > 1 else (0.001 if MaxVolume <= 0 else MaxVolume)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
|
2022-05-08 01:30:30 +08:00
|
|
|
|
for i, track in enumerate(self.midi.tracks):
|
|
|
|
|
|
|
|
|
|
instrumentID = 0
|
2022-06-19 01:07:07 +08:00
|
|
|
|
ticks = 0
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
|
|
|
|
for msg in track:
|
2022-06-19 01:07:07 +08:00
|
|
|
|
ticks += msg.time
|
2022-05-08 01:30:30 +08:00
|
|
|
|
if msg.is_meta:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "set_tempo":
|
2022-05-08 01:30:30 +08:00
|
|
|
|
tempo = msg.tempo
|
2022-06-26 00:17:42 +08:00
|
|
|
|
else:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "program_change":
|
2022-05-08 01:30:30 +08:00
|
|
|
|
instrumentID = msg.program
|
2022-06-30 12:14:22 +08:00
|
|
|
|
if msg.type == "note_on" and msg.velocity != 0:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
now_tick = round(
|
2022-05-08 01:30:30 +08:00
|
|
|
|
(ticks * tempo)
|
|
|
|
|
/ ((self.midi.ticks_per_beat * float(speed)) * 50000)
|
|
|
|
|
)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
soundID, _X = self.__Inst2soundID_withX(instrumentID)
|
2022-06-19 01:07:07 +08:00
|
|
|
|
try:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
tracks[now_tick].append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self.exeHead.format(player)
|
|
|
|
|
+ f"playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg.velocity / 128} "
|
|
|
|
|
f"{2 ** ((msg.note - 60 - _X) / 12)}"
|
|
|
|
|
)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except KeyError:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
tracks[now_tick] = [
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self.exeHead.format(player)
|
|
|
|
|
+ f"playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg.velocity / 128} "
|
|
|
|
|
f"{2 ** ((msg.note - 60 - _X) / 12)}"
|
|
|
|
|
]
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
all_ticks = list(tracks.keys())
|
2023-01-20 00:31:45 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for i in range(len(all_ticks)):
|
2022-06-19 01:07:07 +08:00
|
|
|
|
if i != 0:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for j in range(len(tracks[all_ticks[i]])):
|
2022-06-19 01:26:53 +08:00
|
|
|
|
if j != 0:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
results.append((tracks[all_ticks[i]][j], 0))
|
2022-06-19 01:07:07 +08:00
|
|
|
|
else:
|
|
|
|
|
results.append(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
(tracks[all_ticks[i]][j], all_ticks[i] - all_ticks[i - 1])
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
2022-06-19 01:07:07 +08:00
|
|
|
|
else:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for j in range(len(tracks[all_ticks[i]])):
|
|
|
|
|
results.append((tracks[all_ticks[i]][j], all_ticks[i]))
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return [results, max(all_ticks)]
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2023-01-27 02:19:53 +08:00
|
|
|
|
def _toCmdList_withDelay_m2(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
MaxVolume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
|
|
|
|
player: str = "@a",
|
2023-01-27 02:19:53 +08:00
|
|
|
|
) -> list:
|
|
|
|
|
"""
|
|
|
|
|
使用金羿的转换思路,将midi转换为我的世界命令列表,并输出每个音符之后的延迟
|
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
|
|
|
|
:param player: 玩家选择器,默认为`@a`
|
|
|
|
|
:return: 全部指令列表[ ( str指令, int距离上一个指令的延迟 ),...]
|
|
|
|
|
"""
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# :param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
2023-01-27 02:19:53 +08:00
|
|
|
|
tracks = {}
|
|
|
|
|
|
2023-01-28 11:56:09 +08:00
|
|
|
|
MaxVolume = 1 if MaxVolume > 1 else (0.001 if MaxVolume <= 0 else MaxVolume)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# 一个midi中仅有16个通道 我们通过通道来识别而不是音轨
|
2023-01-27 02:19:53 +08:00
|
|
|
|
channels = {
|
|
|
|
|
0: [],
|
|
|
|
|
1: [],
|
|
|
|
|
2: [],
|
|
|
|
|
3: [],
|
|
|
|
|
4: [],
|
|
|
|
|
5: [],
|
|
|
|
|
6: [],
|
|
|
|
|
7: [],
|
|
|
|
|
8: [],
|
|
|
|
|
9: [],
|
|
|
|
|
10: [],
|
|
|
|
|
11: [],
|
|
|
|
|
12: [],
|
|
|
|
|
13: [],
|
|
|
|
|
14: [],
|
|
|
|
|
15: [],
|
|
|
|
|
16: [],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
if self.debugMode:
|
|
|
|
|
raise NotDefineTempoError("计算当前分数时出错 未定义参量 Tempo")
|
|
|
|
|
else:
|
|
|
|
|
microseconds += (
|
|
|
|
|
msg.time
|
|
|
|
|
* mido.midifiles.midifiles.DEFAULT_TEMPO
|
|
|
|
|
/ self.midi.ticks_per_beat
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if msg.is_meta:
|
|
|
|
|
if msg.type == "set_tempo":
|
|
|
|
|
tempo = msg.tempo
|
|
|
|
|
if self.debugMode:
|
|
|
|
|
self.prt(f"TEMPO更改:{tempo}(毫秒每拍)")
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
if self.debugMode:
|
|
|
|
|
try:
|
|
|
|
|
if msg.channel > 15:
|
2023-01-28 11:56:09 +08:00
|
|
|
|
raise ChannelOverFlowError(f"当前消息 {msg} 的通道超限(≤15)")
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except AttributeError:
|
2023-01-27 02:19:53 +08:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if msg.type == "program_change":
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("PgmC", msg.program, microseconds))
|
2023-01-27 02:19:53 +08:00
|
|
|
|
|
|
|
|
|
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 (
|
2023-01-28 11:56:09 +08:00
|
|
|
|
msg.type == "note_off"
|
2023-01-27 02:19:53 +08:00
|
|
|
|
):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("NoteE", msg.note, microseconds))
|
2023-01-27 02:19:53 +08:00
|
|
|
|
|
|
|
|
|
"""整合后的音乐通道格式
|
|
|
|
|
每个通道包括若干消息元素其中逃不过这三种:
|
|
|
|
|
|
|
|
|
|
1 切换乐器消息
|
|
|
|
|
("PgmC", 切换后的乐器ID: int, 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
2 音符开始消息
|
|
|
|
|
("NoteS", 开始的音符ID, 力度(响度), 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
3 音符结束消息
|
|
|
|
|
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
|
|
for i in channels.keys():
|
|
|
|
|
# 如果当前通道为空 则跳过
|
|
|
|
|
if not channels[i]:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if i == 9:
|
|
|
|
|
SpecialBits = True
|
|
|
|
|
else:
|
|
|
|
|
SpecialBits = False
|
|
|
|
|
|
|
|
|
|
for msg in channels[i]:
|
|
|
|
|
|
|
|
|
|
if msg[0] == "PgmC":
|
|
|
|
|
InstID = msg[1]
|
|
|
|
|
|
|
|
|
|
elif msg[0] == "NoteS":
|
|
|
|
|
try:
|
|
|
|
|
soundID, _X = (
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.__bitInst2ID_withX(InstID)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
if SpecialBits
|
2023-01-27 23:25:28 +08:00
|
|
|
|
else self.__Inst2soundID_withX(InstID)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
)
|
|
|
|
|
except UnboundLocalError as E:
|
|
|
|
|
if self.debugMode:
|
|
|
|
|
raise NotDefineProgramError(f"未定义乐器便提前演奏。\n{E}")
|
|
|
|
|
else:
|
|
|
|
|
soundID, _X = (
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.__bitInst2ID_withX(-1)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
if SpecialBits
|
2023-01-27 23:25:28 +08:00
|
|
|
|
else self.__Inst2soundID_withX(-1)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
)
|
|
|
|
|
score_now = round(msg[-1] / float(speed) / 50)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
tracks[score_now].append(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self.exeHead.format(player)
|
|
|
|
|
+ f"playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg[2] / 128} "
|
|
|
|
|
f"{2 ** ((msg[1] - 60 - _X) / 12)}"
|
|
|
|
|
)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except KeyError:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
tracks[score_now] = [
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self.exeHead.format(player)
|
|
|
|
|
+ f"playsound {soundID} @s ^ ^ ^{1 / MaxVolume - 1} {msg[2] / 128} "
|
|
|
|
|
f"{2 ** ((msg[1] - 60 - _X) / 12)}"
|
|
|
|
|
]
|
2023-01-27 23:25:28 +08:00
|
|
|
|
|
|
|
|
|
all_ticks = list(tracks.keys())
|
|
|
|
|
if self.debugMode:
|
2023-01-27 22:16:15 +08:00
|
|
|
|
self.prt(tracks)
|
2023-01-27 02:19:53 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for i in range(len(all_ticks)):
|
|
|
|
|
for j in range(len(tracks[all_ticks[i]])):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
results.append(
|
|
|
|
|
(
|
|
|
|
|
tracks[all_ticks[i]][j],
|
|
|
|
|
(
|
|
|
|
|
0
|
|
|
|
|
if j != 0
|
|
|
|
|
else (
|
|
|
|
|
all_ticks[i] - all_ticks[i - 1]
|
|
|
|
|
if i != 0
|
|
|
|
|
else all_ticks[i]
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
|
|
|
|
|
return [results, max(all_ticks)]
|
|
|
|
|
|
|
|
|
|
def to_mcpack(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
method: int = 1,
|
|
|
|
|
volume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
2023-01-29 13:02:43 +08:00
|
|
|
|
progressbar: Union[bool, tuple] = None,
|
2023-01-28 11:56:09 +08:00
|
|
|
|
scoreboard_name: str = "mscplay",
|
|
|
|
|
isAutoReset: bool = False,
|
2023-01-20 00:31:45 +08:00
|
|
|
|
) -> tuple:
|
2022-04-29 11:33:52 +08:00
|
|
|
|
"""
|
|
|
|
|
使用method指定的转换算法,将midi转换为我的世界mcpack格式的包
|
2022-04-05 23:43:03 +08:00
|
|
|
|
:param method: 转换算法
|
2022-05-08 01:30:30 +08:00
|
|
|
|
:param isAutoReset: 是否自动重置计分板
|
|
|
|
|
:param progressbar: 进度条,(当此参数为True时使用默认进度条,为其他的值为真的参数时识别为进度条自定义参数,为其他值为假的时候不生成进度条)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param scoreboard_name: 我的世界的计分板名称
|
2022-04-05 23:43:03 +08:00
|
|
|
|
:param volume: 音量,注意:这里的音量范围为(0,1],其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
2022-04-29 11:33:52 +08:00
|
|
|
|
:return 成功与否,成功返回(True,True),失败返回(False,str失败原因)
|
|
|
|
|
"""
|
2022-11-20 15:28:09 +08:00
|
|
|
|
|
2022-10-16 21:19:14 +08:00
|
|
|
|
# try:
|
2023-01-28 11:56:09 +08:00
|
|
|
|
cmdlist, maxlen, maxscore = self.methods[method - 1](
|
|
|
|
|
scoreboard_name, volume, speed
|
|
|
|
|
)
|
2022-10-16 21:19:14 +08:00
|
|
|
|
# except:
|
|
|
|
|
# return (False, f"无法找到算法ID{method}对应的转换算法")
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2022-11-20 15:28:09 +08:00
|
|
|
|
# 当文件f夹{self.outputPath}/temp/functions存在时清空其下所有项目,然后创建
|
|
|
|
|
if os.path.exists(f"{self.outputPath}/temp/functions/"):
|
|
|
|
|
shutil.rmtree(f"{self.outputPath}/temp/functions/")
|
|
|
|
|
os.makedirs(f"{self.outputPath}/temp/functions/mscplay")
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
|
|
|
|
# 写入manifest.json
|
2022-11-20 15:28:09 +08:00
|
|
|
|
if not os.path.exists(f"{self.outputPath}/temp/manifest.json"):
|
2022-05-08 01:30:30 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8"
|
2022-05-08 01:30:30 +08:00
|
|
|
|
) as f:
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f.write(
|
|
|
|
|
'{\n "format_version": 1,\n "header": {\n "description": "'
|
|
|
|
|
+ self.midFileName
|
|
|
|
|
+ ' Pack : behavior pack",\n "version": [ 0, 0, 1 ],\n "name": "'
|
|
|
|
|
+ self.midFileName
|
|
|
|
|
+ 'Pack",\n "uuid": "'
|
|
|
|
|
+ str(uuid.uuid4())
|
|
|
|
|
+ '"\n },\n "modules": [\n {\n "description": "'
|
|
|
|
|
+ f"the Player of the Music {self.midFileName}"
|
|
|
|
|
+ '",\n "type": "data",\n "version": [ 0, 0, 1 ],\n "uuid": "'
|
|
|
|
|
+ str(uuid.uuid4())
|
|
|
|
|
+ '"\n }\n ]\n}'
|
|
|
|
|
)
|
2022-04-05 23:43:03 +08:00
|
|
|
|
else:
|
2022-05-08 01:30:30 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/manifest.json", "r", encoding="utf-8"
|
2022-05-08 01:30:30 +08:00
|
|
|
|
) as manifest:
|
2022-04-29 11:29:49 +08:00
|
|
|
|
data = json.loads(manifest.read())
|
2022-06-30 12:14:22 +08:00
|
|
|
|
data["header"][
|
|
|
|
|
"description"
|
2022-04-29 11:29:49 +08:00
|
|
|
|
] = f"the Player of the Music {self.midFileName}"
|
2022-06-30 12:14:22 +08:00
|
|
|
|
data["header"]["name"] = self.midFileName
|
|
|
|
|
data["header"]["uuid"] = str(uuid.uuid4())
|
|
|
|
|
data["modules"][0]["description"] = "None"
|
|
|
|
|
data["modules"][0]["uuid"] = str(uuid.uuid4())
|
2022-04-29 11:29:49 +08:00
|
|
|
|
manifest.close()
|
2023-01-28 11:56:09 +08:00
|
|
|
|
open(f"{self.outputPath}/temp/manifest.json", "w", encoding="utf-8").write(
|
|
|
|
|
json.dumps(data)
|
|
|
|
|
)
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
|
|
|
|
# 将命令列表写入文件
|
2023-01-27 23:25:28 +08:00
|
|
|
|
index_file = open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/functions/index.mcfunction", "w", encoding="utf-8"
|
|
|
|
|
)
|
2022-04-05 23:43:03 +08:00
|
|
|
|
for track in cmdlist:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
index_file.write(
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"function mscplay/track" + str(cmdlist.index(track) + 1) + "\n"
|
2022-04-29 11:29:49 +08:00
|
|
|
|
)
|
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/functions/mscplay/track{cmdlist.index(track) + 1}.mcfunction",
|
|
|
|
|
"w",
|
|
|
|
|
encoding="utf-8",
|
2022-04-29 11:29:49 +08:00
|
|
|
|
) as f:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
f.write("\n".join(track))
|
2023-01-27 23:25:28 +08:00
|
|
|
|
index_file.writelines(
|
2022-05-08 01:30:30 +08:00
|
|
|
|
(
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"scoreboard players add @a[scores={"
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2022-06-30 12:14:22 +08:00
|
|
|
|
+ "=1..}] "
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2022-06-30 12:14:22 +08:00
|
|
|
|
+ " 1\n",
|
2022-05-08 01:30:30 +08:00
|
|
|
|
(
|
2023-01-02 14:28:25 +08:00
|
|
|
|
"scoreboard players reset @a[scores={"
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2023-01-02 14:28:25 +08:00
|
|
|
|
+ "="
|
|
|
|
|
+ str(maxscore + 20)
|
|
|
|
|
+ "..}]"
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ f" {scoreboard_name}\n"
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
|
|
|
|
if isAutoReset
|
2022-06-30 12:14:22 +08:00
|
|
|
|
else "",
|
|
|
|
|
f"function mscplay/progressShow\n" if progressbar else "",
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
2022-04-29 11:29:49 +08:00
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
|
|
|
|
|
if progressbar:
|
2022-10-05 22:18:03 +08:00
|
|
|
|
if progressbar:
|
2022-05-08 01:30:30 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction",
|
|
|
|
|
"w",
|
|
|
|
|
encoding="utf-8",
|
2022-05-08 01:30:30 +08:00
|
|
|
|
) as f:
|
|
|
|
|
f.writelines(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
"\n".join(self.__formProgressBar(maxscore, scoreboard_name))
|
|
|
|
|
)
|
2022-05-08 01:30:30 +08:00
|
|
|
|
else:
|
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/functions/mscplay/progressShow.mcfunction",
|
|
|
|
|
"w",
|
|
|
|
|
encoding="utf-8",
|
2022-05-08 01:30:30 +08:00
|
|
|
|
) as f:
|
|
|
|
|
f.writelines(
|
2022-06-30 12:14:22 +08:00
|
|
|
|
"\n".join(
|
2022-05-08 01:30:30 +08:00
|
|
|
|
self.__formProgressBar(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
maxscore, scoreboard_name, progressbar
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
index_file.close()
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
if os.path.exists(f"{self.outputPath}/{self.midFileName}.mcpack"):
|
|
|
|
|
os.remove(f"{self.outputPath}/{self.midFileName}.mcpack")
|
2023-01-29 13:02:43 +08:00
|
|
|
|
compressZipFile(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
f"{self.outputPath}/temp/", f"{self.outputPath}/{self.midFileName}.mcpack"
|
|
|
|
|
)
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2022-06-30 12:14:22 +08:00
|
|
|
|
shutil.rmtree(f"{self.outputPath}/temp/")
|
2022-04-05 23:43:03 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return True, maxlen, maxscore
|
2022-10-16 21:19:14 +08:00
|
|
|
|
|
2023-01-29 13:02:43 +08:00
|
|
|
|
def to_BDX_file(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
method: int = 1,
|
|
|
|
|
volume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
|
|
|
|
progressbar: Union[bool, tuple] = False,
|
|
|
|
|
scoreboard_name: str = "mscplay",
|
|
|
|
|
isAutoReset: bool = False,
|
|
|
|
|
author: str = "Eilles",
|
|
|
|
|
max_height: int = 64,
|
2022-04-29 18:45:06 +08:00
|
|
|
|
):
|
2022-04-29 11:33:52 +08:00
|
|
|
|
"""
|
|
|
|
|
使用method指定的转换算法,将midi转换为BDX结构文件
|
2022-04-29 11:29:49 +08:00
|
|
|
|
:param method: 转换算法
|
2022-05-08 01:30:30 +08:00
|
|
|
|
:param author: 作者名称
|
|
|
|
|
:param progressbar: 进度条,(当此参数为True时使用默认进度条,为其他的值为真的参数时识别为进度条自定义参数,为其他值为假的时候不生成进度条)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param max_height: 生成结构最大高度
|
|
|
|
|
:param scoreboard_name: 我的世界的计分板名称
|
2022-04-29 11:29:49 +08:00
|
|
|
|
:param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
2022-05-08 01:30:30 +08:00
|
|
|
|
:param isAutoReset: 是否自动重置计分板
|
2022-04-29 18:45:06 +08:00
|
|
|
|
:return 成功与否,成功返回(True,未经过压缩的源,结构占用大小),失败返回(False,str失败原因)
|
2022-04-29 11:33:52 +08:00
|
|
|
|
"""
|
2023-01-02 14:28:25 +08:00
|
|
|
|
# try:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
cmdlist, total_count, maxScore = self.methods[method - 1](
|
|
|
|
|
scoreboard_name, volume, speed
|
2023-01-02 14:28:25 +08:00
|
|
|
|
)
|
|
|
|
|
# except Exception as E:
|
|
|
|
|
# return (False, f"无法找到算法ID{method}对应的转换算法: {E}")
|
2022-10-07 19:25:28 +08:00
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
if not os.path.exists(self.outputPath):
|
|
|
|
|
os.makedirs(self.outputPath)
|
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
os.path.abspath(os.path.join(self.outputPath, f"{self.midFileName}.bdx")),
|
|
|
|
|
"w+",
|
2023-01-02 14:28:25 +08:00
|
|
|
|
) as f:
|
2022-04-29 11:29:49 +08:00
|
|
|
|
f.write("BD@")
|
|
|
|
|
|
|
|
|
|
_bytes = (
|
2023-01-02 14:28:25 +08:00
|
|
|
|
b"BDX\x00"
|
|
|
|
|
+ author.encode("utf-8")
|
|
|
|
|
+ b" & Musicreater\x00\x01command_block\x00"
|
2022-04-29 11:29:49 +08:00
|
|
|
|
)
|
|
|
|
|
|
2022-05-08 01:30:30 +08:00
|
|
|
|
commands = []
|
|
|
|
|
|
2022-04-29 11:29:49 +08:00
|
|
|
|
for track in cmdlist:
|
2022-05-08 01:30:30 +08:00
|
|
|
|
commands += track
|
|
|
|
|
|
|
|
|
|
if isAutoReset:
|
2023-01-05 17:30:36 +08:00
|
|
|
|
commands.append(
|
2023-01-02 14:28:25 +08:00
|
|
|
|
"scoreboard players reset @a[scores={"
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name
|
2023-01-02 14:28:25 +08:00
|
|
|
|
+ "="
|
|
|
|
|
+ str(maxScore + 20)
|
|
|
|
|
+ "}] "
|
2023-01-27 23:25:28 +08:00
|
|
|
|
+ scoreboard_name,
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
2022-04-29 18:45:06 +08:00
|
|
|
|
|
2023-01-29 13:02:43 +08:00
|
|
|
|
cmdBytes, size, finalPos = to_BDX_bytes(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
[(i, 0) for i in commands], max_height - 1
|
|
|
|
|
)
|
2022-10-07 16:49:47 +08:00
|
|
|
|
# 此处是对于仅有 True 的参数和自定义参数的判断
|
2022-05-08 01:30:30 +08:00
|
|
|
|
if progressbar:
|
2023-01-29 13:02:43 +08:00
|
|
|
|
pgbBytes, pgbSize, pgbNowPos = to_BDX_bytes(
|
2023-01-20 00:31:45 +08:00
|
|
|
|
[
|
|
|
|
|
(i, 0)
|
|
|
|
|
for i in (
|
2023-01-27 23:25:28 +08:00
|
|
|
|
self.__formProgressBar(maxScore, scoreboard_name)
|
|
|
|
|
if progressbar
|
2023-01-20 00:31:45 +08:00
|
|
|
|
else self.__formProgressBar(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
maxScore, scoreboard_name, progressbar
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
],
|
2023-01-27 23:25:28 +08:00
|
|
|
|
max_height - 1,
|
2022-05-08 01:30:30 +08:00
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
_bytes += pgbBytes
|
|
|
|
|
_bytes += move(y, -pgbNowPos[1])
|
|
|
|
|
_bytes += move(z, -pgbNowPos[2])
|
|
|
|
|
_bytes += move(x, 2)
|
2022-04-29 18:45:06 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
size[0] += 2 + pgbSize[0]
|
|
|
|
|
size[1] = max(size[1], pgbSize[1])
|
|
|
|
|
size[2] = max(size[2], pgbSize[2])
|
2022-04-29 18:45:06 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
_bytes += cmdBytes
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
os.path.abspath(os.path.join(self.outputPath, f"{self.midFileName}.bdx")),
|
|
|
|
|
"ab+",
|
2023-01-02 14:28:25 +08:00
|
|
|
|
) as f:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
f.write(brotli.compress(_bytes + b"XE"))
|
2022-04-29 11:29:49 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return True, total_count, maxScore, size, finalPos
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-29 13:02:43 +08:00
|
|
|
|
def to_BDX_file_with_delay(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
|
|
|
|
method: int = 1,
|
|
|
|
|
volume: float = 1.0,
|
|
|
|
|
speed: float = 1.0,
|
2023-01-29 13:02:43 +08:00
|
|
|
|
progressbar: Union[bool, tuple] = False,
|
2023-01-28 11:56:09 +08:00
|
|
|
|
player: str = "@a",
|
|
|
|
|
author: str = "Eilles",
|
|
|
|
|
max_height: int = 64,
|
2022-06-19 01:07:07 +08:00
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
使用method指定的转换算法,将midi转换为BDX结构文件
|
|
|
|
|
:param method: 转换算法
|
|
|
|
|
:param author: 作者名称
|
|
|
|
|
:param progressbar: 进度条,(当此参数为True时使用默认进度条,为其他的值为真的参数时识别为进度条自定义参数,为其他值为假的时候不生成进度条)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
:param max_height: 生成结构最大高度
|
2022-06-19 01:07:07 +08:00
|
|
|
|
:param volume: 音量,注意:这里的音量范围为(0,1],如果超出将被处理为正确值,其原理为在距离玩家 (1 / volume -1) 的地方播放音频
|
|
|
|
|
:param speed: 速度,注意:这里的速度指的是播放倍率,其原理为在播放音频的时候,每个音符的播放时间除以 speed
|
|
|
|
|
:param player: 玩家选择器,默认为`@a`
|
|
|
|
|
:return 成功与否,成功返回(True,未经过压缩的源,结构占用大小),失败返回(False,str失败原因)
|
|
|
|
|
"""
|
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
# try:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
cmdlist, max_delay = self.methods_byDelay[method - 1](
|
2023-01-20 00:31:45 +08:00
|
|
|
|
volume,
|
|
|
|
|
speed,
|
|
|
|
|
player,
|
|
|
|
|
)
|
|
|
|
|
# except Exception as E:
|
|
|
|
|
# return (False, f"无法找到算法ID{method}对应的转换算法\n{E}")
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
|
|
|
|
if not os.path.exists(self.outputPath):
|
|
|
|
|
os.makedirs(self.outputPath)
|
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
os.path.abspath(os.path.join(self.outputPath, f"{self.midFileName}.bdx")),
|
|
|
|
|
"w+",
|
2023-01-02 14:28:25 +08:00
|
|
|
|
) as f:
|
2022-06-19 01:07:07 +08:00
|
|
|
|
f.write("BD@")
|
|
|
|
|
|
|
|
|
|
_bytes = (
|
2023-01-02 14:28:25 +08:00
|
|
|
|
b"BDX\x00"
|
|
|
|
|
+ author.encode("utf-8")
|
|
|
|
|
+ b" & Musicreater\x00\x01command_block\x00"
|
2022-06-19 01:07:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
# 此处是对于仅有 True 的参数和自定义参数的判断
|
2023-01-27 23:25:28 +08:00
|
|
|
|
if progressbar:
|
2023-01-20 00:31:45 +08:00
|
|
|
|
progressbar = (
|
|
|
|
|
r"▶ %%N [ %%s/%^s %%% __________ %%t|%^t ]",
|
|
|
|
|
("§e=§r", "§7=§r"),
|
2022-06-19 01:07:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
2023-01-29 13:02:43 +08:00
|
|
|
|
cmdBytes, size, finalPos = to_BDX_bytes(cmdlist, max_height - 1)
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
if progressbar:
|
2023-01-27 23:25:28 +08:00
|
|
|
|
scb_name = self.midFileName[:5] + "Pgb"
|
2023-01-29 13:02:43 +08:00
|
|
|
|
_bytes += form_command_block_in_BDX_bytes(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
r"scoreboard objectives add {} dummy {}播放用".replace(r"{}", scb_name),
|
|
|
|
|
1,
|
|
|
|
|
customName="初始化进度条",
|
|
|
|
|
)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
_bytes += move(z, 2)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
_bytes += form_command_block_in_BDX_bytes(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
r"scoreboard players add {} {} 1".format(player, scb_name),
|
2023-01-20 00:31:45 +08:00
|
|
|
|
1,
|
|
|
|
|
1,
|
|
|
|
|
customName="显示进度条并加分",
|
|
|
|
|
)
|
|
|
|
|
_bytes += move(y, 1)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
pgbBytes, pgbSize, pgbNowPos = to_BDX_bytes(
|
2023-01-20 00:31:45 +08:00
|
|
|
|
[
|
|
|
|
|
(i, 0)
|
2023-01-27 23:25:28 +08:00
|
|
|
|
for i in self.__formProgressBar(max_delay, scb_name, progressbar)
|
2023-01-20 00:31:45 +08:00
|
|
|
|
],
|
2023-01-27 23:25:28 +08:00
|
|
|
|
max_height - 1,
|
2023-01-20 00:31:45 +08:00
|
|
|
|
)
|
|
|
|
|
_bytes += pgbBytes
|
|
|
|
|
_bytes += move(y, -1 - pgbNowPos[1])
|
|
|
|
|
_bytes += move(z, -2 - pgbNowPos[2])
|
|
|
|
|
_bytes += move(x, 2)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
_bytes += form_command_block_in_BDX_bytes(
|
2023-01-27 23:25:28 +08:00
|
|
|
|
r"scoreboard players reset {} {}".format(player, scb_name),
|
2023-01-20 00:31:45 +08:00
|
|
|
|
1,
|
|
|
|
|
customName="置零进度条",
|
|
|
|
|
)
|
|
|
|
|
_bytes += move(y, 1)
|
|
|
|
|
size[0] += 2 + pgbSize[0]
|
|
|
|
|
size[1] = max(size[1], pgbSize[1])
|
|
|
|
|
size[2] = max(size[2], pgbSize[2])
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-20 00:31:45 +08:00
|
|
|
|
size[1] += 1
|
|
|
|
|
_bytes += cmdBytes
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-02 14:28:25 +08:00
|
|
|
|
with open(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
os.path.abspath(os.path.join(self.outputPath, f"{self.midFileName}.bdx")),
|
|
|
|
|
"ab+",
|
2023-01-02 14:28:25 +08:00
|
|
|
|
) as f:
|
2022-06-30 12:14:22 +08:00
|
|
|
|
f.write(brotli.compress(_bytes + b"XE"))
|
2022-06-19 01:07:07 +08:00
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
return True, len(cmdlist), max_delay, size, finalPos
|
2022-11-20 12:02:40 +08:00
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
def toDICT(
|
2023-01-28 11:56:09 +08:00
|
|
|
|
self,
|
2023-01-27 23:25:28 +08:00
|
|
|
|
) -> dict:
|
2023-01-20 21:34:15 +08:00
|
|
|
|
"""
|
|
|
|
|
使用金羿的转换思路,将midi转换为字典
|
|
|
|
|
:return: dict()
|
|
|
|
|
"""
|
|
|
|
|
|
2023-01-27 23:25:28 +08:00
|
|
|
|
# 一个midi中仅有16个通道 我们通过通道来识别而不是音轨
|
2023-01-20 22:26:04 +08:00
|
|
|
|
channels = {}
|
2023-01-20 21:34:15 +08:00
|
|
|
|
microseconds = 0
|
|
|
|
|
|
|
|
|
|
# 我们来用通道统计音乐信息
|
|
|
|
|
for msg in self.midi:
|
|
|
|
|
|
|
|
|
|
if msg.time != 0:
|
|
|
|
|
try:
|
|
|
|
|
microseconds += msg.time * tempo / self.midi.ticks_per_beat
|
|
|
|
|
# print(microseconds)
|
2023-01-29 13:02:43 +08:00
|
|
|
|
except NameError:
|
2023-01-22 00:04:09 +08:00
|
|
|
|
microseconds += (
|
|
|
|
|
msg.time
|
|
|
|
|
* mido.midifiles.midifiles.DEFAULT_TEMPO
|
|
|
|
|
/ self.midi.ticks_per_beat
|
|
|
|
|
)
|
2023-01-20 21:34:15 +08:00
|
|
|
|
|
|
|
|
|
if msg.is_meta:
|
|
|
|
|
if msg.type == "set_tempo":
|
|
|
|
|
tempo = msg.tempo
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
if msg.type == "program_change":
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("PgmC", msg.program, microseconds))
|
2023-01-20 21:34:15 +08:00
|
|
|
|
|
|
|
|
|
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 (
|
2023-01-28 11:56:09 +08:00
|
|
|
|
msg.type == "note_off"
|
2023-01-20 21:34:15 +08:00
|
|
|
|
):
|
2023-01-28 11:56:09 +08:00
|
|
|
|
channels[msg.channel].append(("NoteE", msg.note, microseconds))
|
2023-01-22 00:04:09 +08:00
|
|
|
|
|
2023-01-20 21:34:15 +08:00
|
|
|
|
"""整合后的音乐通道格式
|
|
|
|
|
每个通道包括若干消息元素其中逃不过这三种:
|
|
|
|
|
|
|
|
|
|
1 切换乐器消息
|
|
|
|
|
("PgmC", 切换后的乐器ID: int, 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
2 音符开始消息
|
|
|
|
|
("NoteS", 开始的音符ID, 力度(响度), 距离演奏开始的毫秒)
|
|
|
|
|
|
|
|
|
|
3 音符结束消息
|
|
|
|
|
("NoteS", 结束的音符ID, 距离演奏开始的毫秒)"""
|
|
|
|
|
|
2023-01-22 00:04:09 +08:00
|
|
|
|
return channels
|