mms-lib 0.2.1__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mms_lib-0.2.1 → mms_lib-0.3.1}/PKG-INFO +1 -1
- {mms_lib-0.2.1 → mms_lib-0.3.1}/pyproject.toml +1 -1
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/__init__.py +1 -1
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/assets/text/profile.json +32 -32
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/core.py +24 -15
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/dataclass.py +30 -13
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/midi_reader.py +31 -12
- mms_lib-0.3.1/src/mms/nbs_reader.py +63 -0
- mms_lib-0.3.1/src/mms/pynbs/__init__.py +4 -0
- mms_lib-0.3.1/src/mms/pynbs/file.py +306 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/tools.py +6 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/PKG-INFO +1 -1
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/SOURCES.txt +3 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/LICENSE +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/README.md +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/setup.cfg +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/assets/text/features.json +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/assets/text/mapping.json +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/assets/text/settings.json +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/cli.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/evaltools.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/__init__.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/frozen.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/__init__.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/checks.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/decode.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/encode.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/messages.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/specs.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/messages/strings.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/midifiles/__init__.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/midifiles/meta.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/midifiles/midifiles.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/midifiles/tracks.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/midifiles/units.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/parser.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/tokenizer.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms/mido/version.py +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/dependency_links.txt +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/entry_points.txt +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/requires.txt +0 -0
- {mms_lib-0.2.1 → mms_lib-0.3.1}/src/mms_lib.egg-info/top_level.txt +0 -0
|
@@ -77,22 +77,22 @@
|
|
|
77
77
|
["note.bd", 1.5, 0, 0]
|
|
78
78
|
],
|
|
79
79
|
"cymbal": [
|
|
80
|
-
["
|
|
80
|
+
["dig.sand", 1.0, 19, 0]
|
|
81
81
|
],
|
|
82
82
|
"open_cymbal": [
|
|
83
|
-
["
|
|
83
|
+
["dig.sand", 1.5, 19, 0]
|
|
84
84
|
],
|
|
85
85
|
"pedal_cymbal": [
|
|
86
|
-
["
|
|
86
|
+
["dig.sand", 1.5, 31, 0]
|
|
87
87
|
],
|
|
88
88
|
"closed_cymbal": [
|
|
89
|
-
["
|
|
89
|
+
["dig.sand", 1.5, 38, 0]
|
|
90
90
|
],
|
|
91
91
|
"cabasa": [
|
|
92
|
-
["
|
|
92
|
+
["dig.sand", 1.2, 19, 0]
|
|
93
93
|
],
|
|
94
94
|
"shaker": [
|
|
95
|
-
["
|
|
95
|
+
["dig.sand", 1.2, 31, 0]
|
|
96
96
|
],
|
|
97
97
|
"parrot": [
|
|
98
98
|
["mob.parrot.idle", 1.0, 0, 0]
|
|
@@ -122,16 +122,16 @@
|
|
|
122
122
|
["note.didgeridoo", 0.8, 0, 0]
|
|
123
123
|
],
|
|
124
124
|
"trumpet": [
|
|
125
|
-
["note.trumpet", 0.
|
|
125
|
+
["note.trumpet", 0.5, 0, 0]
|
|
126
126
|
],
|
|
127
127
|
"french_horn": [
|
|
128
|
-
["note.trumpet_exposed", 0.
|
|
128
|
+
["note.trumpet_exposed", 0.5, 0, 0]
|
|
129
129
|
],
|
|
130
130
|
"trombone": [
|
|
131
|
-
["note.trumpet_weathered", 0.
|
|
131
|
+
["note.trumpet_weathered", 0.5, 0, 0]
|
|
132
132
|
],
|
|
133
133
|
"tuba": [
|
|
134
|
-
["note.trumpet_oxidized", 0.
|
|
134
|
+
["note.trumpet_oxidized", 0.5, 0, 0]
|
|
135
135
|
],
|
|
136
136
|
"orchestra_hit": [
|
|
137
137
|
["note.bass", 0.4, 0, 0],
|
|
@@ -235,22 +235,22 @@
|
|
|
235
235
|
["block.note_block.basedrum", 1.5, 0, 0]
|
|
236
236
|
],
|
|
237
237
|
"cymbal": [
|
|
238
|
-
["block.
|
|
238
|
+
["block.sand.break", 1.0, 19, 0]
|
|
239
239
|
],
|
|
240
240
|
"open_cymbal": [
|
|
241
|
-
["block.
|
|
241
|
+
["block.sand.break", 1.5, 19, 0]
|
|
242
242
|
],
|
|
243
243
|
"pedal_cymbal": [
|
|
244
|
-
["block.
|
|
244
|
+
["block.sand.break", 1.5, 31, 0]
|
|
245
245
|
],
|
|
246
246
|
"closed_cymbal": [
|
|
247
|
-
["block.
|
|
247
|
+
["block.sand.break", 1.5, 38, 0]
|
|
248
248
|
],
|
|
249
249
|
"cabasa": [
|
|
250
|
-
["block.
|
|
250
|
+
["block.sand.break", 1.2, 19, 0]
|
|
251
251
|
],
|
|
252
252
|
"shaker": [
|
|
253
|
-
["block.
|
|
253
|
+
["block.sand.break", 1.2, 31, 0]
|
|
254
254
|
],
|
|
255
255
|
"parrot": [
|
|
256
256
|
["entity.parrot.ambient", 1.0, 0, 0]
|
|
@@ -280,16 +280,16 @@
|
|
|
280
280
|
["block.note_block.didgeridoo", 0.8, 0, 0]
|
|
281
281
|
],
|
|
282
282
|
"trumpet": [
|
|
283
|
-
["block.note_block.trumpet", 0.
|
|
283
|
+
["block.note_block.trumpet", 0.5, 0, 0]
|
|
284
284
|
],
|
|
285
285
|
"french_horn": [
|
|
286
|
-
["block.note_block.trumpet_exposed", 0.
|
|
286
|
+
["block.note_block.trumpet_exposed", 0.5, 0, 0]
|
|
287
287
|
],
|
|
288
288
|
"trombone": [
|
|
289
|
-
["block.note_block.trumpet_weathered", 0.
|
|
289
|
+
["block.note_block.trumpet_weathered", 0.5, 0, 0]
|
|
290
290
|
],
|
|
291
291
|
"tuba": [
|
|
292
|
-
["block.note_block.trumpet_oxidized", 0.
|
|
292
|
+
["block.note_block.trumpet_oxidized", 0.5, 0, 0]
|
|
293
293
|
],
|
|
294
294
|
"orchestra_hit": [
|
|
295
295
|
["block.note_block.bass", 0.4, 0, 0],
|
|
@@ -381,22 +381,22 @@
|
|
|
381
381
|
["note.bd", 1.5, 0, 0]
|
|
382
382
|
],
|
|
383
383
|
"cymbal": [
|
|
384
|
-
["
|
|
384
|
+
["dig.sand", 1.0, 19, 0]
|
|
385
385
|
],
|
|
386
386
|
"open_cymbal": [
|
|
387
|
-
["
|
|
387
|
+
["dig.sand", 1.5, 19, 0]
|
|
388
388
|
],
|
|
389
389
|
"pedal_cymbal": [
|
|
390
|
-
["
|
|
390
|
+
["dig.sand", 1.5, 31, 0]
|
|
391
391
|
],
|
|
392
392
|
"closed_cymbal": [
|
|
393
|
-
["
|
|
393
|
+
["dig.sand", 1.5, 38, 0]
|
|
394
394
|
],
|
|
395
395
|
"cabasa": [
|
|
396
|
-
["
|
|
396
|
+
["dig.sand", 1.2, 19, 0]
|
|
397
397
|
],
|
|
398
398
|
"shaker": [
|
|
399
|
-
["
|
|
399
|
+
["dig.sand", 1.2, 31, 0]
|
|
400
400
|
],
|
|
401
401
|
"parrot": [
|
|
402
402
|
["mob.parrot.idle", 1.0, 0, 0]
|
|
@@ -501,22 +501,22 @@
|
|
|
501
501
|
["block.note.basedrum", 1.5, 0, 0]
|
|
502
502
|
],
|
|
503
503
|
"cymbal": [
|
|
504
|
-
["block.
|
|
504
|
+
["block.sand.break", 1.0, 19, 0]
|
|
505
505
|
],
|
|
506
506
|
"open_cymbal": [
|
|
507
|
-
["block.
|
|
507
|
+
["block.sand.break", 1.5, 19, 0]
|
|
508
508
|
],
|
|
509
509
|
"pedal_cymbal": [
|
|
510
|
-
["block.
|
|
510
|
+
["block.sand.break", 1.5, 31, 0]
|
|
511
511
|
],
|
|
512
512
|
"closed_cymbal": [
|
|
513
|
-
["block.
|
|
513
|
+
["block.sand.break", 1.5, 38, 0]
|
|
514
514
|
],
|
|
515
515
|
"cabasa": [
|
|
516
|
-
["block.
|
|
516
|
+
["block.sand.break", 1.2, 19, 0]
|
|
517
517
|
],
|
|
518
518
|
"shaker": [
|
|
519
|
-
["block.
|
|
519
|
+
["block.sand.break", 1.2, 31, 0]
|
|
520
520
|
],
|
|
521
521
|
"parrot": [
|
|
522
522
|
["entity.parrot.ambient", 1.0, 0, 0]
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import os
|
|
2
2
|
from . import evaltools
|
|
3
3
|
from math import ceil, isclose
|
|
4
|
-
from .tools import round_int, round_45, is_number, limit
|
|
4
|
+
from .tools import round_int, round_45, is_number, limit, is_midi
|
|
5
5
|
from .dataclass import LyricsList, Note, Lyrics, Eval, AverageNumber, Profiles, Settings
|
|
6
|
+
from .nbs_reader import NBSReader
|
|
6
7
|
from .midi_reader import MIDIReader
|
|
7
8
|
from importlib.resources import files
|
|
8
9
|
|
|
@@ -41,7 +42,7 @@ def load_lrc(lines: list[str], time_per_tick: int = 50):
|
|
|
41
42
|
elif tag_type == "offset":
|
|
42
43
|
offset = int(value)
|
|
43
44
|
|
|
44
|
-
def read_midi(midi_reader: MIDIReader, platform_profile: dict, lyrics_data: list[str] | None = None) -> dict[int, set[Note | Lyrics]]:
|
|
45
|
+
def read_midi(midi_reader: MIDIReader | NBSReader, platform_profile: dict, lyrics_data: list[str] | None = None) -> dict[int, set[Note | Lyrics]]:
|
|
45
46
|
if profile.settings.max_time_error:
|
|
46
47
|
tempo = profile.settings.time_per_tick
|
|
47
48
|
min_error = float("INF")
|
|
@@ -105,13 +106,18 @@ def read_midi(midi_reader: MIDIReader, platform_profile: dict, lyrics_data: list
|
|
|
105
106
|
# 歌词数据处理
|
|
106
107
|
if lyrics_data is not None:
|
|
107
108
|
lyrics_buffer.clear()
|
|
109
|
+
|
|
108
110
|
for k, i in load_lrc(lyrics_data, best_tempo):
|
|
109
111
|
if k in lyrics_buffer:
|
|
110
112
|
lyrics_buffer[k] += " " + i
|
|
111
113
|
else:
|
|
112
114
|
lyrics_buffer[k] = i
|
|
113
115
|
|
|
114
|
-
|
|
116
|
+
render_lyrics(lyrics_buffer, result)
|
|
117
|
+
|
|
118
|
+
return result
|
|
119
|
+
|
|
120
|
+
def render_lyrics(lyrics_buffer: dict[int, str], music_data: dict[int, set[Note | Lyrics]]) -> None:
|
|
115
121
|
if lyrics_buffer:
|
|
116
122
|
last_l = None
|
|
117
123
|
for k, l in LyricsList(lyrics_buffer, profile.settings.smooth_progress, profile.settings.auto_join_lyrics):
|
|
@@ -120,25 +126,28 @@ def read_midi(midi_reader: MIDIReader, platform_profile: dict, lyrics_data: list
|
|
|
120
126
|
continue
|
|
121
127
|
|
|
122
128
|
# 将歌词数据合并到结果中
|
|
123
|
-
if k not in
|
|
124
|
-
|
|
129
|
+
if k not in music_data:
|
|
130
|
+
music_data[k] = set()
|
|
125
131
|
|
|
126
|
-
|
|
132
|
+
music_data[k].add(l)
|
|
127
133
|
last_l = l
|
|
128
134
|
|
|
129
|
-
return result
|
|
130
|
-
|
|
131
135
|
def convertor(file_data: bytes, platform: str, lyrics_data: list[str] | None = None):
|
|
132
136
|
platform_profile = profile.profile[platform]
|
|
133
137
|
|
|
134
|
-
|
|
138
|
+
if is_midi(file_data):
|
|
139
|
+
file_data = MIDIReader(file_data)
|
|
140
|
+
else:
|
|
141
|
+
file_data = NBSReader(file_data)
|
|
142
|
+
|
|
143
|
+
music_data = read_midi(file_data, platform_profile, lyrics_data)
|
|
135
144
|
|
|
136
145
|
if profile.settings.skip:
|
|
137
|
-
time_offset = min(
|
|
146
|
+
time_offset = min(music_data.keys())
|
|
138
147
|
else:
|
|
139
148
|
time_offset = 0
|
|
140
149
|
|
|
141
|
-
return cmd_convertor(platform_profile, time_offset,
|
|
150
|
+
return cmd_convertor(platform_profile, time_offset, music_data)
|
|
142
151
|
|
|
143
152
|
def get_notes(midi_file: MIDIReader, platform_profile: dict, tempo: float | int):
|
|
144
153
|
if not profile.settings.enable_extension:
|
|
@@ -163,7 +172,7 @@ def get_notes(midi_file: MIDIReader, platform_profile: dict, tempo: float | int)
|
|
|
163
172
|
if data.percussion:
|
|
164
173
|
program = platform_profile["sound_list"].get(profile.mapping.get_p(data.program), platform_profile["sound_list"][profile.mapping.p_mapping["undefined"]])
|
|
165
174
|
|
|
166
|
-
if "linear_note" in profile.features["percussion"].get(str(data.
|
|
175
|
+
if "linear_note" in profile.features["percussion"].get(str(data.program), profile.features["percussion"]["undefined"]):
|
|
167
176
|
linear_note = True
|
|
168
177
|
else:
|
|
169
178
|
if data.program == -1:
|
|
@@ -171,13 +180,13 @@ def get_notes(midi_file: MIDIReader, platform_profile: dict, tempo: float | int)
|
|
|
171
180
|
else:
|
|
172
181
|
program = platform_profile["sound_list"].get(profile.mapping.get(data.program), platform_profile["sound_list"][profile.mapping.mapping["undefined"]])
|
|
173
182
|
|
|
174
|
-
if "linear_note" in profile.features.get(str(data.
|
|
183
|
+
if "linear_note" in profile.features.get(str(data.program), profile.features["undefined"]):
|
|
175
184
|
linear_note = True
|
|
176
185
|
|
|
177
186
|
if program is None: continue
|
|
178
187
|
|
|
179
188
|
delay_time = 0
|
|
180
|
-
while data.time + delay_time
|
|
189
|
+
while data.time + delay_time <= time:
|
|
181
190
|
# 一个音符可以对应多个我的世界乐器,因此这里遍历一下从配置文件中获取的数据
|
|
182
191
|
for note in program:
|
|
183
192
|
# 累加配置文件中我的世界乐器之间的时间间隔
|
|
@@ -240,7 +249,7 @@ def cmd_convertor(platform_profile: dict, start_time: int, note_data: dict[int,
|
|
|
240
249
|
cmd = cmd.replace("{ADDRESS}", str(profile.settings.id))
|
|
241
250
|
cmd = cmd.replace("{ID_SCOREBOARD}", profile.settings.id_scoreboard_name)
|
|
242
251
|
cmd = cmd.replace("{TIMER_SCOREBOARD}", profile.settings.timer_scoreboard_name)
|
|
243
|
-
yield time, cmd
|
|
252
|
+
yield time, cmd.lstrip("/")
|
|
244
253
|
|
|
245
254
|
def cmd_generator(platform_profile: dict, start_time: int, note_data: dict[int, set[Note | Lyrics]]):
|
|
246
255
|
raw_cmd: str = platform_profile["command"][profile.settings.command_type][0]
|
|
@@ -227,7 +227,6 @@ class Lyrics:
|
|
|
227
227
|
|
|
228
228
|
class NoteData:
|
|
229
229
|
__slots__ = (
|
|
230
|
-
"source_program",
|
|
231
230
|
"percussion",
|
|
232
231
|
"velocity",
|
|
233
232
|
"panning",
|
|
@@ -237,15 +236,14 @@ class NoteData:
|
|
|
237
236
|
"type"
|
|
238
237
|
)
|
|
239
238
|
|
|
240
|
-
def __init__(self, time: float, pitch: float, program:
|
|
239
|
+
def __init__(self, time: float, pitch: float, program: int, panning: tuple[float, float], velocity: float):
|
|
241
240
|
self.type = "note"
|
|
242
241
|
self.time = time
|
|
243
242
|
self.pitch = pitch
|
|
244
|
-
self.program = program
|
|
243
|
+
self.program = program
|
|
245
244
|
self.panning = panning
|
|
246
245
|
self.velocity = velocity
|
|
247
246
|
self.percussion = False
|
|
248
|
-
self.source_program = program[1]
|
|
249
247
|
|
|
250
248
|
class LyricsData:
|
|
251
249
|
__slots__ = (
|
|
@@ -380,29 +378,48 @@ class LyricsList:
|
|
|
380
378
|
class Stack:
|
|
381
379
|
__slots__ = (
|
|
382
380
|
"__data",
|
|
381
|
+
"__p_data"
|
|
383
382
|
)
|
|
384
383
|
|
|
385
384
|
def __init__(self):
|
|
386
385
|
self.__data: dict[int, dict[float, list[NoteData]]] = {}
|
|
386
|
+
self.__p_data: dict[int, dict[float, list[NoteData]]] = {}
|
|
387
387
|
|
|
388
388
|
def put(self, channel: int, data: NoteData):
|
|
389
|
-
|
|
389
|
+
if channel == 9:
|
|
390
|
+
base = self.__p_data
|
|
391
|
+
note_id = data.program
|
|
392
|
+
data.percussion = True
|
|
393
|
+
else:
|
|
394
|
+
base = self.__data
|
|
395
|
+
note_id = data.pitch
|
|
396
|
+
data.percussion = False
|
|
397
|
+
|
|
398
|
+
if channel not in base:
|
|
399
|
+
base[channel] = {note_id: [data]}
|
|
400
|
+
elif note_id not in base[channel]:
|
|
401
|
+
base[channel][note_id] = [data]
|
|
402
|
+
else:
|
|
403
|
+
base[channel][note_id].append(data)
|
|
390
404
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
self.__data[channel][data.pitch] = [data]
|
|
405
|
+
def get(self, channel: int, note_id: int) -> NoteData | None:
|
|
406
|
+
if channel == 9:
|
|
407
|
+
base = self.__p_data
|
|
395
408
|
else:
|
|
396
|
-
self.__data
|
|
409
|
+
base = self.__data
|
|
397
410
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
return self.__data[channel][pitch].pop()
|
|
411
|
+
if note_id in base.get(channel, {}) and base[channel][note_id]:
|
|
412
|
+
return base[channel][note_id].pop()
|
|
401
413
|
else:
|
|
402
414
|
return None
|
|
403
415
|
|
|
404
416
|
def __iter__(self):
|
|
405
417
|
for channel in self.__data.values():
|
|
418
|
+
for notes in channel.values():
|
|
419
|
+
for note in notes:
|
|
420
|
+
yield note
|
|
421
|
+
|
|
422
|
+
for channel in self.__p_data.values():
|
|
406
423
|
for notes in channel.values():
|
|
407
424
|
for note in notes:
|
|
408
425
|
yield note
|
|
@@ -134,9 +134,6 @@ class MIDIReader:
|
|
|
134
134
|
def get_time_accuracy(self, time_per_tick: float) -> float:
|
|
135
135
|
return sum(map(lambda i: (round_int(i / time_per_tick) - i / time_per_tick) ** 2, self._scan_time_nodes()))
|
|
136
136
|
|
|
137
|
-
def override_mapping(self, mapping: dict[int, dict[int, int]]) -> None:
|
|
138
|
-
self.__instruments_mapping = mapping
|
|
139
|
-
|
|
140
137
|
def __iter__(self):
|
|
141
138
|
stack = Stack()
|
|
142
139
|
tempo_info = TempoList(self.__midi_file.ticks_per_beat)
|
|
@@ -154,14 +151,12 @@ class MIDIReader:
|
|
|
154
151
|
# 判断该事件是否有通道数据,如果有并且通道没有初始化数据就初始化该通道
|
|
155
152
|
if hasattr(message, "channel"):
|
|
156
153
|
channel = message.channel
|
|
157
|
-
mapping: dict[int, int] = self.__instruments_mapping.get(channel, {})
|
|
158
154
|
if channel not in channel_info:
|
|
159
155
|
channel_info[channel] = {
|
|
160
156
|
"program": InfoList(-1),
|
|
161
157
|
"volume": InfoList(1),
|
|
162
158
|
"panning": InfoList((0, 1))}
|
|
163
159
|
else:
|
|
164
|
-
mapping = {}
|
|
165
160
|
channel = -1
|
|
166
161
|
# 获取tempo信息
|
|
167
162
|
if message.type == "set_tempo":
|
|
@@ -189,7 +184,7 @@ class MIDIReader:
|
|
|
189
184
|
# 获取歌词数据
|
|
190
185
|
data = LyricsData(message.text)
|
|
191
186
|
# 获取音符信息
|
|
192
|
-
elif message.type == "note_on" and message.velocity
|
|
187
|
+
elif message.type == "note_on" and message.velocity > 0:
|
|
193
188
|
# 对音符力度(音量)进行归一化处理
|
|
194
189
|
note_velocity = message.velocity / 127
|
|
195
190
|
# 音符音量再乘以音符所在的通道的音量
|
|
@@ -201,17 +196,41 @@ class MIDIReader:
|
|
|
201
196
|
# 打击乐器保持原声
|
|
202
197
|
note_pitch = 66
|
|
203
198
|
note_program = message.note
|
|
199
|
+
|
|
200
|
+
data = stack.get(channel, note_program)
|
|
201
|
+
|
|
202
|
+
stack.put(
|
|
203
|
+
channel,
|
|
204
|
+
NoteData(
|
|
205
|
+
tempo_info.compute_time(time),
|
|
206
|
+
note_pitch,
|
|
207
|
+
note_program,
|
|
208
|
+
note_panning,
|
|
209
|
+
note_velocity
|
|
210
|
+
)
|
|
211
|
+
)
|
|
204
212
|
else:
|
|
205
213
|
note_pitch = message.note
|
|
206
214
|
note_program = channel_info[channel]["program"].match_info(time)
|
|
207
215
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
216
|
+
if note_data := stack.get(channel, note_pitch):
|
|
217
|
+
data = note_data
|
|
218
|
+
|
|
219
|
+
stack.put(
|
|
220
|
+
channel,
|
|
221
|
+
NoteData(
|
|
222
|
+
tempo_info.compute_time(time),
|
|
223
|
+
note_pitch,
|
|
224
|
+
note_program,
|
|
225
|
+
note_panning,
|
|
226
|
+
note_velocity
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
elif message.type == "note_off" or message.type == "note_on":
|
|
212
231
|
data = stack.get(channel, message.note)
|
|
213
232
|
|
|
214
|
-
#
|
|
233
|
+
# 将音符时间转为绝对时间并返回结果,时间单位为毫秒
|
|
215
234
|
if data is not None: yield tempo_info.compute_time(time), data
|
|
216
235
|
|
|
217
|
-
for data in stack: yield data.time
|
|
236
|
+
for data in stack: yield data.time, data
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import math
|
|
3
|
+
from . import pynbs
|
|
4
|
+
from .tools import round_45
|
|
5
|
+
from .dataclass import NoteData
|
|
6
|
+
|
|
7
|
+
NBS_TO_GM_MAPPING = {
|
|
8
|
+
0: 0,
|
|
9
|
+
1: 43,
|
|
10
|
+
2: 116,
|
|
11
|
+
3: 118,
|
|
12
|
+
4: 115,
|
|
13
|
+
5: 24,
|
|
14
|
+
6: 73,
|
|
15
|
+
7: 14,
|
|
16
|
+
8: 8,
|
|
17
|
+
9: 13,
|
|
18
|
+
10: 9,
|
|
19
|
+
11: 113,
|
|
20
|
+
12: 111,
|
|
21
|
+
13: 80,
|
|
22
|
+
14: 105,
|
|
23
|
+
15: 98,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class NBSReader:
|
|
27
|
+
def __init__(self, data: bytes):
|
|
28
|
+
self.__nbs_file = pynbs.Parser(io.BytesIO(data)).read_file()
|
|
29
|
+
self.__nodes_cache = []
|
|
30
|
+
self.__instruments_mapping = {}
|
|
31
|
+
|
|
32
|
+
def get_time_accuracy(self, _) -> float:
|
|
33
|
+
return 0.0
|
|
34
|
+
|
|
35
|
+
def override_mapping(self, mapping: dict[int, dict[int, int]]) -> None:
|
|
36
|
+
self.__instruments_mapping = mapping
|
|
37
|
+
|
|
38
|
+
def __iter__(self):
|
|
39
|
+
volume_info = {}
|
|
40
|
+
panning_info = {}
|
|
41
|
+
for layer in self.__nbs_file.layers:
|
|
42
|
+
volume_info[layer.id] = layer.volume
|
|
43
|
+
panning_info[layer.id] = layer.panning
|
|
44
|
+
|
|
45
|
+
for tick, notes in self.__nbs_file:
|
|
46
|
+
for note in notes:
|
|
47
|
+
if panning_info[note.layer] == 0:
|
|
48
|
+
panning = note.panning
|
|
49
|
+
else:
|
|
50
|
+
panning = (note.panning + panning_info[note.layer]) / 2
|
|
51
|
+
|
|
52
|
+
radian = math.radians((panning + 100) * 0.9)
|
|
53
|
+
|
|
54
|
+
note_data = NoteData(
|
|
55
|
+
tick * 50,
|
|
56
|
+
note.key + 21 + note.pitch / 100,
|
|
57
|
+
NBS_TO_GM_MAPPING.get(note.instrument, 0),
|
|
58
|
+
(round_45(math.cos(radian), 2), round_45(math.sin(radian), 2)),
|
|
59
|
+
note.velocity * volume_info[note.layer]
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
yield tick * 50, note_data
|
|
63
|
+
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"read",
|
|
3
|
+
"new_file",
|
|
4
|
+
"Parser",
|
|
5
|
+
"Writer",
|
|
6
|
+
"File",
|
|
7
|
+
"Header",
|
|
8
|
+
"Note",
|
|
9
|
+
"Layer",
|
|
10
|
+
"Instrument",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from struct import Struct
|
|
16
|
+
|
|
17
|
+
CURRENT_NBS_VERSION = 5
|
|
18
|
+
|
|
19
|
+
BYTE = Struct("<B")
|
|
20
|
+
SHORT = Struct("<H")
|
|
21
|
+
SSHORT = Struct("<h")
|
|
22
|
+
INT = Struct("<I")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class Instrument:
|
|
27
|
+
id: int
|
|
28
|
+
name: str
|
|
29
|
+
file: str
|
|
30
|
+
pitch: int = 45
|
|
31
|
+
press_key: bool = True
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class Note:
|
|
36
|
+
tick: int
|
|
37
|
+
layer: int
|
|
38
|
+
instrument: int
|
|
39
|
+
key: int
|
|
40
|
+
velocity: int = 100
|
|
41
|
+
panning: int = 0
|
|
42
|
+
pitch: int = 0
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class Layer:
|
|
47
|
+
id: int
|
|
48
|
+
name: str = ""
|
|
49
|
+
lock: bool = False
|
|
50
|
+
volume: int = 100
|
|
51
|
+
panning: int = 0
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def read(filename):
|
|
55
|
+
with open(filename, "rb") as fileobj:
|
|
56
|
+
return Parser(fileobj).read_file()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def new_file(**header):
|
|
60
|
+
return File(Header(**header), [], [Layer(0, "", False, 100, 0)], [])
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass
|
|
64
|
+
class Header:
|
|
65
|
+
version: int = CURRENT_NBS_VERSION
|
|
66
|
+
default_instruments: int = 16
|
|
67
|
+
song_length: int = 0
|
|
68
|
+
song_layers: int = 0
|
|
69
|
+
song_name: str = ""
|
|
70
|
+
song_author: str = ""
|
|
71
|
+
original_author: str = ""
|
|
72
|
+
description: str = ""
|
|
73
|
+
tempo: float = 10.0
|
|
74
|
+
auto_save: bool = False
|
|
75
|
+
auto_save_duration: int = 10
|
|
76
|
+
time_signature: int = 4
|
|
77
|
+
minutes_spent: int = 0
|
|
78
|
+
left_clicks: int = 0
|
|
79
|
+
right_clicks: int = 0
|
|
80
|
+
blocks_added: int = 0
|
|
81
|
+
blocks_removed: int = 0
|
|
82
|
+
song_origin: str = ""
|
|
83
|
+
loop: bool = False
|
|
84
|
+
max_loop_count: int = 0
|
|
85
|
+
loop_start: int = 0
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class File:
|
|
89
|
+
def __init__(self, header, notes, layers, instruments):
|
|
90
|
+
self.header = header
|
|
91
|
+
self.notes = notes
|
|
92
|
+
self.layers = layers
|
|
93
|
+
self.instruments = instruments
|
|
94
|
+
|
|
95
|
+
def update_header(self, version):
|
|
96
|
+
self.header.version = version
|
|
97
|
+
if self.notes:
|
|
98
|
+
self.header.song_length = self.notes[-1].tick
|
|
99
|
+
self.header.song_layers = len(self.layers)
|
|
100
|
+
|
|
101
|
+
def save(self, filename, version=CURRENT_NBS_VERSION):
|
|
102
|
+
self.update_header(version)
|
|
103
|
+
with open(filename, "wb") as fileobj:
|
|
104
|
+
Writer(fileobj).encode_file(self, version)
|
|
105
|
+
|
|
106
|
+
def __iter__(self):
|
|
107
|
+
if not self.notes:
|
|
108
|
+
return
|
|
109
|
+
chord = []
|
|
110
|
+
current_tick = self.notes[0].tick
|
|
111
|
+
|
|
112
|
+
for note in sorted(self.notes, key=lambda n: n.tick):
|
|
113
|
+
if note.tick == current_tick:
|
|
114
|
+
chord.append(note)
|
|
115
|
+
else:
|
|
116
|
+
chord.sort(key=lambda n: n.layer)
|
|
117
|
+
yield current_tick, chord
|
|
118
|
+
current_tick, chord = note.tick, [note]
|
|
119
|
+
yield current_tick, chord
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class Parser:
|
|
123
|
+
def __init__(self, fileobj):
|
|
124
|
+
self.fileobj = fileobj
|
|
125
|
+
|
|
126
|
+
def read_file(self):
|
|
127
|
+
header = self.parse_header()
|
|
128
|
+
version = header.version
|
|
129
|
+
return File(
|
|
130
|
+
header,
|
|
131
|
+
list(self.parse_notes(version)),
|
|
132
|
+
list(self.parse_layers(header.song_layers, version)),
|
|
133
|
+
list(self.parse_instruments(version)),
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
def read_numeric(self, fmt):
|
|
137
|
+
return fmt.unpack(self.fileobj.read(fmt.size))[0]
|
|
138
|
+
|
|
139
|
+
def read_string(self):
|
|
140
|
+
length = self.read_numeric(INT)
|
|
141
|
+
return self.fileobj.read(length).decode(encoding="cp1252")
|
|
142
|
+
|
|
143
|
+
def jump(self):
|
|
144
|
+
value = -1
|
|
145
|
+
while True:
|
|
146
|
+
jump = self.read_numeric(SHORT)
|
|
147
|
+
if not jump:
|
|
148
|
+
break
|
|
149
|
+
value += jump
|
|
150
|
+
yield value
|
|
151
|
+
|
|
152
|
+
def parse_header(self):
|
|
153
|
+
song_length = self.read_numeric(SHORT)
|
|
154
|
+
if song_length == 0:
|
|
155
|
+
# A song length of 0 indicates the Open Note Block Studio format
|
|
156
|
+
version = self.read_numeric(BYTE)
|
|
157
|
+
else:
|
|
158
|
+
version = 0
|
|
159
|
+
|
|
160
|
+
return Header(
|
|
161
|
+
version=version,
|
|
162
|
+
default_instruments=self.read_numeric(BYTE) if version > 0 else 10,
|
|
163
|
+
song_length=self.read_numeric(SHORT) if version >= 3 else song_length,
|
|
164
|
+
song_layers=self.read_numeric(SHORT),
|
|
165
|
+
song_name=self.read_string(),
|
|
166
|
+
song_author=self.read_string(),
|
|
167
|
+
original_author=self.read_string(),
|
|
168
|
+
description=self.read_string(),
|
|
169
|
+
tempo=self.read_numeric(SHORT) / 100.0,
|
|
170
|
+
auto_save=self.read_numeric(BYTE) == 1,
|
|
171
|
+
auto_save_duration=self.read_numeric(BYTE),
|
|
172
|
+
time_signature=self.read_numeric(BYTE),
|
|
173
|
+
minutes_spent=self.read_numeric(INT),
|
|
174
|
+
left_clicks=self.read_numeric(INT),
|
|
175
|
+
right_clicks=self.read_numeric(INT),
|
|
176
|
+
blocks_added=self.read_numeric(INT),
|
|
177
|
+
blocks_removed=self.read_numeric(INT),
|
|
178
|
+
song_origin=self.read_string(),
|
|
179
|
+
loop=self.read_numeric(BYTE) == 1 if version >= 4 else False,
|
|
180
|
+
max_loop_count=self.read_numeric(BYTE) if version >= 4 else 0,
|
|
181
|
+
loop_start=self.read_numeric(SHORT) if version >= 4 else 0,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
def parse_notes(self, version):
|
|
185
|
+
for current_tick in self.jump():
|
|
186
|
+
for current_layer in self.jump():
|
|
187
|
+
instrument = self.read_numeric(BYTE)
|
|
188
|
+
key = self.read_numeric(BYTE)
|
|
189
|
+
velocity = self.read_numeric(BYTE) if version >= 4 else 100
|
|
190
|
+
panning = self.read_numeric(BYTE) - 100 if version >= 4 else 0
|
|
191
|
+
pitch = self.read_numeric(SSHORT) if version >= 4 else 0
|
|
192
|
+
yield Note(
|
|
193
|
+
current_tick,
|
|
194
|
+
current_layer,
|
|
195
|
+
instrument,
|
|
196
|
+
key,
|
|
197
|
+
velocity,
|
|
198
|
+
panning,
|
|
199
|
+
pitch,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
def parse_layers(self, layers_count, version):
|
|
203
|
+
for i in range(layers_count):
|
|
204
|
+
name = self.read_string()
|
|
205
|
+
lock = self.read_numeric(BYTE) == 1 if version >= 4 else False
|
|
206
|
+
volume = self.read_numeric(BYTE)
|
|
207
|
+
panning = self.read_numeric(BYTE) - 100 if version >= 2 else 0
|
|
208
|
+
yield Layer(i, name, lock, volume, panning)
|
|
209
|
+
|
|
210
|
+
def parse_instruments(self, version):
|
|
211
|
+
for i in range(self.read_numeric(BYTE)):
|
|
212
|
+
name = self.read_string()
|
|
213
|
+
sound_file = self.read_string()
|
|
214
|
+
pitch = self.read_numeric(BYTE)
|
|
215
|
+
press_key = self.read_numeric(BYTE) == 1
|
|
216
|
+
yield Instrument(i, name, sound_file, pitch, press_key)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
class Writer:
|
|
220
|
+
def __init__(self, fileobj):
|
|
221
|
+
self.fileobj = fileobj
|
|
222
|
+
|
|
223
|
+
def encode_file(self, nbs_file, version):
|
|
224
|
+
self.write_header(nbs_file, version)
|
|
225
|
+
self.write_notes(nbs_file, version)
|
|
226
|
+
self.write_layers(nbs_file, version)
|
|
227
|
+
self.write_instruments(nbs_file, version)
|
|
228
|
+
|
|
229
|
+
def encode_numeric(self, fmt, value):
|
|
230
|
+
self.fileobj.write(fmt.pack(value))
|
|
231
|
+
|
|
232
|
+
def encode_string(self, value):
|
|
233
|
+
self.encode_numeric(INT, len(value))
|
|
234
|
+
self.fileobj.write(value.encode(encoding="cp1252"))
|
|
235
|
+
|
|
236
|
+
def write_header(self, nbs_file, version):
|
|
237
|
+
header = nbs_file.header
|
|
238
|
+
|
|
239
|
+
if version > 0:
|
|
240
|
+
self.encode_numeric(SHORT, 0)
|
|
241
|
+
self.encode_numeric(BYTE, version)
|
|
242
|
+
self.encode_numeric(BYTE, header.default_instruments)
|
|
243
|
+
else:
|
|
244
|
+
self.encode_numeric(SHORT, header.song_length)
|
|
245
|
+
if version >= 3:
|
|
246
|
+
self.encode_numeric(SHORT, header.song_length)
|
|
247
|
+
self.encode_numeric(SHORT, header.song_layers)
|
|
248
|
+
self.encode_string(header.song_name)
|
|
249
|
+
self.encode_string(header.song_author)
|
|
250
|
+
self.encode_string(header.original_author)
|
|
251
|
+
self.encode_string(header.description)
|
|
252
|
+
|
|
253
|
+
self.encode_numeric(SHORT, int(header.tempo * 100))
|
|
254
|
+
self.encode_numeric(BYTE, int(header.auto_save))
|
|
255
|
+
self.encode_numeric(BYTE, header.auto_save_duration)
|
|
256
|
+
self.encode_numeric(BYTE, header.time_signature)
|
|
257
|
+
|
|
258
|
+
self.encode_numeric(INT, header.minutes_spent)
|
|
259
|
+
self.encode_numeric(INT, header.left_clicks)
|
|
260
|
+
self.encode_numeric(INT, header.right_clicks)
|
|
261
|
+
self.encode_numeric(INT, header.blocks_added)
|
|
262
|
+
self.encode_numeric(INT, header.blocks_removed)
|
|
263
|
+
self.encode_string(header.song_origin)
|
|
264
|
+
|
|
265
|
+
if version >= 4:
|
|
266
|
+
self.encode_numeric(BYTE, int(header.loop))
|
|
267
|
+
self.encode_numeric(BYTE, header.max_loop_count)
|
|
268
|
+
self.encode_numeric(SHORT, header.loop_start)
|
|
269
|
+
|
|
270
|
+
def write_notes(self, nbs_file, version):
|
|
271
|
+
current_tick = -1
|
|
272
|
+
|
|
273
|
+
for tick, chord in nbs_file:
|
|
274
|
+
self.encode_numeric(SHORT, tick - current_tick)
|
|
275
|
+
current_tick = tick
|
|
276
|
+
current_layer = -1
|
|
277
|
+
|
|
278
|
+
for note in chord:
|
|
279
|
+
self.encode_numeric(SHORT, note.layer - current_layer)
|
|
280
|
+
current_layer = note.layer
|
|
281
|
+
self.encode_numeric(BYTE, note.instrument)
|
|
282
|
+
self.encode_numeric(BYTE, note.key)
|
|
283
|
+
if version >= 4:
|
|
284
|
+
self.encode_numeric(BYTE, note.velocity)
|
|
285
|
+
self.encode_numeric(BYTE, note.panning + 100)
|
|
286
|
+
self.encode_numeric(SSHORT, note.pitch)
|
|
287
|
+
|
|
288
|
+
self.encode_numeric(SHORT, 0)
|
|
289
|
+
self.encode_numeric(SHORT, 0)
|
|
290
|
+
|
|
291
|
+
def write_layers(self, nbs_file, version):
|
|
292
|
+
for layer in nbs_file.layers:
|
|
293
|
+
self.encode_string(layer.name)
|
|
294
|
+
if version >= 4:
|
|
295
|
+
self.encode_numeric(BYTE, int(layer.lock))
|
|
296
|
+
self.encode_numeric(BYTE, layer.volume)
|
|
297
|
+
if version >= 2:
|
|
298
|
+
self.encode_numeric(BYTE, layer.panning + 100)
|
|
299
|
+
|
|
300
|
+
def write_instruments(self, nbs_file, version):
|
|
301
|
+
self.encode_numeric(BYTE, len(nbs_file.instruments))
|
|
302
|
+
for instrument in nbs_file.instruments:
|
|
303
|
+
self.encode_string(instrument.name)
|
|
304
|
+
self.encode_string(instrument.file)
|
|
305
|
+
self.encode_numeric(BYTE, instrument.pitch)
|
|
306
|
+
self.encode_numeric(BYTE, int(instrument.press_key))
|
|
@@ -25,3 +25,9 @@ def is_number(text: str) -> bool:
|
|
|
25
25
|
length = len(str)
|
|
26
26
|
return all(text[i] in "0123456789" or (text[i] == "." and 1 < i + 1 < length) for i in range(length)) and text.count(".") <= 1
|
|
27
27
|
|
|
28
|
+
def is_midi(data: bytes) -> bool:
|
|
29
|
+
return data[:4] == b"MThd"
|
|
30
|
+
|
|
31
|
+
def replace_item(d: dict, k, v):
|
|
32
|
+
old_v, d[k] = d[k], v
|
|
33
|
+
return old_v
|
|
@@ -7,6 +7,7 @@ src/mms/core.py
|
|
|
7
7
|
src/mms/dataclass.py
|
|
8
8
|
src/mms/evaltools.py
|
|
9
9
|
src/mms/midi_reader.py
|
|
10
|
+
src/mms/nbs_reader.py
|
|
10
11
|
src/mms/tools.py
|
|
11
12
|
src/mms/assets/text/features.json
|
|
12
13
|
src/mms/assets/text/mapping.json
|
|
@@ -29,6 +30,8 @@ src/mms/mido/midifiles/meta.py
|
|
|
29
30
|
src/mms/mido/midifiles/midifiles.py
|
|
30
31
|
src/mms/mido/midifiles/tracks.py
|
|
31
32
|
src/mms/mido/midifiles/units.py
|
|
33
|
+
src/mms/pynbs/__init__.py
|
|
34
|
+
src/mms/pynbs/file.py
|
|
32
35
|
src/mms_lib.egg-info/PKG-INFO
|
|
33
36
|
src/mms_lib.egg-info/SOURCES.txt
|
|
34
37
|
src/mms_lib.egg-info/dependency_links.txt
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|