forked from AsakuraMizu/pygros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab.py
56 lines (49 loc) · 1.9 KB
/
lab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from mido import MidiFile, tempo2bpm
# 读取MIDI文件
print("欢迎使用星雨引擎谱面启动器!图形化制谱器将在不久之后制作")
path = input("请输入MIDI文件路径:>>>")
mid = MidiFile(path)
# 定义事件列表
events = []
tempo = 500 # 默认速度(微秒每拍)
# 遍历MIDI文件中的所有消息
for track in mid.tracks:
absolute_time = 0 # 绝对时间
for msg in track:
absolute_time += msg.time
if msg.type == 'set_tempo':
tempo = msg.tempo
if msg.type == 'note_on' and msg.velocity > 0:
# 计算时间(以秒为单位)
time = absolute_time / mid.ticks_per_beat * tempo / 1000000
# 计算位置(假设音符范围为0-127,映射到-1到1)
position = (msg.note - 64) / 64
# 根据通道确定事件类型
if msg.channel == 0:
event_type = 'Click'
elif msg.channel == 1:
event_type = 'Drag'
elif msg.channel == 2:
event_type = 'Flick'
elif msg.channel == 3:
event_type = 'Hold'
else:
continue # 忽略其他通道
# 添加事件到列表
events.append((time, event_type, position, 'spd'))
# 按时间排序事件列表
events.sort()
# 将事件转换为字符串格式
event_strings = []
for event in events:
time, event_type, position, speed = event
if event_type == 'Hold':
duration = 8 # 假设Hold事件的持续时间为8秒
event_strings.append(f'{event_type}({time:.2f}, {position:.2f}, {speed}, {duration})')
else:
event_strings.append(f'{event_type}({time:.2f}, {position:.2f}, {speed})')
# 将事件写入txt文件
with open('output.txt', 'w') as f:
for event_string in event_strings:
f.write(event_string + '\n')
print("Events have been written to output.txt")