Spaces:
Building
Building
MuGeminorum
commited on
Commit
•
d0dc12d
1
Parent(s):
e690dfb
sync ms
Browse files- app.py +7 -8
- convert.py +5 -11
- midi2abc.py +2 -2
app.py
CHANGED
@@ -28,19 +28,18 @@ def audio2midi(audio_path: str):
|
|
28 |
# Transcriptor
|
29 |
transcriptor = PianoTranscription(
|
30 |
device="cuda" if torch.cuda.is_available() else "cpu", checkpoint_path=None
|
31 |
-
)
|
32 |
-
# Transcribe and write out to MIDI file
|
33 |
-
|
34 |
-
midi_path = f"./example/{basename}.mid"
|
35 |
# midi_path = audio_path.replace(audio_path.split(".")[-1], "mid")
|
36 |
transcriptor.transcribe(audio, midi_path)
|
37 |
-
return midi_path
|
38 |
|
39 |
|
40 |
def inference(audio_path: str):
|
41 |
-
midi_path = audio2midi(audio_path)
|
42 |
-
xml_path
|
43 |
-
abc = midi2abc(midi_path, title
|
44 |
mxl_path = xml2mxl(xml_path)
|
45 |
pdf_path, jpg_path = mxl2jpg(mxl_path)
|
46 |
return midi_path, pdf_path, xml_path, mxl_path, abc, jpg_path
|
|
|
28 |
# Transcriptor
|
29 |
transcriptor = PianoTranscription(
|
30 |
device="cuda" if torch.cuda.is_available() else "cpu", checkpoint_path=None
|
31 |
+
)
|
32 |
+
# device: 'cuda' | 'cpu' Transcribe and write out to MIDI file
|
33 |
+
midi_path = f"./example/temp.mid"
|
|
|
34 |
# midi_path = audio_path.replace(audio_path.split(".")[-1], "mid")
|
35 |
transcriptor.transcribe(audio, midi_path)
|
36 |
+
return midi_path, os.path.basename(audio_path).split(".")[-2].capitalize()
|
37 |
|
38 |
|
39 |
def inference(audio_path: str):
|
40 |
+
midi_path, title = audio2midi(audio_path)
|
41 |
+
xml_path = midi2xml(midi_path, title)
|
42 |
+
abc = midi2abc(midi_path, title)
|
43 |
mxl_path = xml2mxl(xml_path)
|
44 |
pdf_path, jpg_path = mxl2jpg(mxl_path)
|
45 |
return midi_path, pdf_path, xml_path, mxl_path, abc, jpg_path
|
convert.py
CHANGED
@@ -57,25 +57,19 @@ else:
|
|
57 |
mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
|
58 |
|
59 |
|
60 |
-
def add_title_to_xml(xml_path: str):
|
61 |
"""
|
62 |
给 MIDI 文件添加标题,标题为去后缀的文件名。
|
63 |
参数:
|
64 |
- midi_file_path: MIDI 文件的路径
|
65 |
"""
|
66 |
-
# 获取文件名(不带路径)
|
67 |
-
file_name = os.path.basename(xml_path)
|
68 |
-
title = file_name.split(".")[0]
|
69 |
-
artist = "Transcripted by AI"
|
70 |
# 读取 MIDI 文件
|
71 |
midi_data = converter.parse(xml_path)
|
72 |
# 将标题添加到 MIDI 文件中
|
73 |
-
midi_data.metadata.title = title
|
74 |
midi_data.metadata.movementName = title
|
75 |
-
midi_data.metadata.composer =
|
76 |
# 保存修改后的 MIDI 文件
|
77 |
midi_data.write("musicxml", fp=xml_path)
|
78 |
-
return title, artist
|
79 |
|
80 |
|
81 |
def xml2abc(xml_path: str):
|
@@ -96,13 +90,13 @@ def xml2mxl(xml_path: str):
|
|
96 |
return mxl_file
|
97 |
|
98 |
|
99 |
-
def midi2xml(mid_file: str):
|
100 |
xml_file = mid_file.replace(".mid", ".musicxml")
|
101 |
command = [mscore, "-o", xml_file, mid_file]
|
102 |
result = subprocess.run(command)
|
103 |
-
|
104 |
print(result)
|
105 |
-
return xml_file
|
106 |
|
107 |
|
108 |
def xml2midi(xml_file: str):
|
|
|
57 |
mscore = "D:/Program Files/MuseScore 3/bin/MuseScore3.exe"
|
58 |
|
59 |
|
60 |
+
def add_title_to_xml(xml_path: str, title: str):
|
61 |
"""
|
62 |
给 MIDI 文件添加标题,标题为去后缀的文件名。
|
63 |
参数:
|
64 |
- midi_file_path: MIDI 文件的路径
|
65 |
"""
|
|
|
|
|
|
|
|
|
66 |
# 读取 MIDI 文件
|
67 |
midi_data = converter.parse(xml_path)
|
68 |
# 将标题添加到 MIDI 文件中
|
|
|
69 |
midi_data.metadata.movementName = title
|
70 |
+
midi_data.metadata.composer = "Transcripted by AI"
|
71 |
# 保存修改后的 MIDI 文件
|
72 |
midi_data.write("musicxml", fp=xml_path)
|
|
|
73 |
|
74 |
|
75 |
def xml2abc(xml_path: str):
|
|
|
90 |
return mxl_file
|
91 |
|
92 |
|
93 |
+
def midi2xml(mid_file: str, title: str):
|
94 |
xml_file = mid_file.replace(".mid", ".musicxml")
|
95 |
command = [mscore, "-o", xml_file, mid_file]
|
96 |
result = subprocess.run(command)
|
97 |
+
add_title_to_xml(xml_file, title)
|
98 |
print(result)
|
99 |
+
return xml_file
|
100 |
|
101 |
|
102 |
def xml2midi(xml_file: str):
|
midi2abc.py
CHANGED
@@ -580,7 +580,7 @@ def main(argv):
|
|
580 |
print(result)
|
581 |
|
582 |
|
583 |
-
def midi2abc(filename, title
|
584 |
key = None
|
585 |
default_len = Fraction(1, 16)
|
586 |
metre = Fraction(3, 4)
|
@@ -605,7 +605,7 @@ def midi2abc(filename, title, artist):
|
|
605 |
default_len,
|
606 |
bars_per_line,
|
607 |
title,
|
608 |
-
|
609 |
"",
|
610 |
no_triplets,
|
611 |
no_broken_rythms,
|
|
|
580 |
print(result)
|
581 |
|
582 |
|
583 |
+
def midi2abc(filename, title):
|
584 |
key = None
|
585 |
default_len = Fraction(1, 16)
|
586 |
metre = Fraction(3, 4)
|
|
|
605 |
default_len,
|
606 |
bars_per_line,
|
607 |
title,
|
608 |
+
"Transcripted by AI",
|
609 |
"",
|
610 |
no_triplets,
|
611 |
no_broken_rythms,
|