-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·291 lines (256 loc) · 9.66 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import argparse
import datetime
import xml.etree.ElementTree as ET
import os
from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
# Mapping note names and notes.
MAP_NOTE = {
'R': '0',
'C': '1',
'D': '2',
'E': '3',
'F': '4',
'G': '5',
'A': '6',
'B': '7'
}
# Mapping fifths value and pitch correction
MAP_CORRECTION = {
"-7": 0,
"-6": -4,
"-5": -1,
"-4": -5,
"-3": -2,
"-2": -6,
"-1": -3,
"0": 0,
"1": -4,
"2": -1,
"3": -5,
"4": -2,
"5": -6,
"6": -3,
"7": 0,
}
def convert_to_jianpu(note, attributes):
note_step = note["step"]
octave = note['octave']
note_type = note['type']
duration = note['duration']
dot_count = note['dot_count']
accidental = note['accidental']
fifths = attributes['fifths']
divisions = attributes['divisions']
jianpu_note = ""
note_step_cor = 0
if note_step in MAP_NOTE:
if note_step == 'R':
note_step_cor = 0
jianpu_note = '0'
else:
temp_step_cor = int(MAP_NOTE[note_step]) + MAP_CORRECTION[str(fifths)]
note_step_cor = temp_step_cor%7
note_step_cor = note_step_cor if note_step_cor != 0 else 7
jianpu_note = str(note_step_cor)
if temp_step_cor <= 0:
octave -= 1
else:
return ""
if octave >= 4:
# 处理高音音符(包括中音音符)
if octave == 5:
jianpu_note += "'"
elif octave == 6:
jianpu_note += "\""
elif octave == 7:
jianpu_note += "`"
# 加入时值标识
if note_type == 'whole' or duration / divisions == 4:
jianpu_note += ' - - -'
elif note_type == 'half' or duration / divisions == 2:
jianpu_note += ' -'
elif note_type == 'quarter' or duration / divisions == 1:
jianpu_note += ''
elif note_type == 'eighth' or duration / divisions == 0.5:
jianpu_note += '_'
elif note_type == "16th" or duration / divisions == 0.25:
jianpu_note += "="
elif note_type == "32nd" or duration / divisions == 0.125:
jianpu_note += "/"
elif note_type == "64th" or duration / divisions == 1/16:
jianpu_note += "\\"
elif octave < 4:
# TODO: 低音音符处理
# 下面 copy 了 octave > 4 的处理逻辑,写的时候删去就好。
if octave == 3:
if note_type == 'whole' or duration / divisions == 4:
jianpu_note += 'q - - -'
elif note_type == 'half' or duration / divisions == 2:
jianpu_note += 'q -'
elif note_type == 'quarter' or duration / divisions == 1:
jianpu_note += 'q'
elif note_type == 'eighth' or duration / divisions == 0.5:
jianpu_note += 'w'
elif note_type == "16th" or duration / divisions == 0.25:
jianpu_note += "e"
elif note_type == "32nd" or duration / divisions == 0.125:
jianpu_note += "r"
elif note_type == "64th" or duration / divisions == 1/16:
jianpu_note += "t"
elif octave == 2:
if note_type == 'whole' or duration / divisions == 4:
jianpu_note += 'a - - -'
elif note_type == 'half' or duration / divisions == 2:
jianpu_note += 'a -'
elif note_type == 'quarter' or duration / divisions == 1:
jianpu_note += 'a'
elif note_type == 'eighth' or duration / divisions == 0.5:
jianpu_note += 's'
elif note_type == "16th" or duration / divisions == 0.25:
jianpu_note += "d"
elif note_type == "32nd" or duration / divisions == 0.125:
jianpu_note += "f"
elif note_type == "64th" or duration / divisions == 1/16:
jianpu_note += "g"
elif octave == 1:
if note_type == 'whole' or duration / divisions == 4:
jianpu_note += 'z - - -'
elif note_type == 'half' or duration / divisions == 2:
jianpu_note += 'z -'
elif note_type == 'quarter' or duration / divisions == 1:
jianpu_note += 'z'
elif note_type == 'eighth' or duration / divisions == 0.5:
jianpu_note += 'x'
elif note_type == "16th" or duration / divisions == 0.25:
jianpu_note += "c"
elif note_type == "32nd" or duration / divisions == 0.125:
jianpu_note += "v"
elif note_type == "64th" or duration / divisions == 1/16:
jianpu_note += "g"
# 加入附点
if dot_count == 1:
jianpu_note += '.'
elif dot_count == 2:
jianpu_note += '.,'
# 加入升降号
# TODO: 参考 MusicXML 文档进行更多实现
# https://www.w3.org/2021/06/musicxml40/musicxml-reference/data-types/accidental-value/
if accidental is not None:
if accidental == 'sharp':
jianpu_note = 'i' + jianpu_note
elif accidental == 'natural':
jianpu_note = 'o' + jianpu_note
elif accidental == 'flat':
jianpu_note = 'p' + jianpu_note
return jianpu_note
def parse(file_path) -> str:
'''返回简谱排版所需要输入的字符串'''
tree = ET.parse(file_path)
root = tree.getroot()
score = ''
divisions = 1
fifths = 0
beam = False
# 遍历小节(measure)
for measure in root.findall('.//measure'):
attributes = measure.find('attributes')
notes = measure.findall('note')
if attributes is not None:
# 获得乐谱属性
div = attributes.find('divisions')
if attributes.find('key/fifths') is not None:
fifths = (attributes.find('key/fifths').text)
if div is not None:
divisions = int(div.text)
# 遍历小节中的音符
for note in notes:
rest = note.find('rest')
pitch = note.find('pitch')
if rest is not None:
# 特殊处理空拍
duration = int(note.find('duration').text)
note_type = None
if note.find('type') is not None:
note_type = note.find('type').text
dot_count = len(note.findall('dot'))
score += convert_to_jianpu(
{
'step': 'R',
'octave': 4,
'duration': duration,
'type': note_type,
'dot_count': dot_count,
'accidental': None,
},
{
'fifths': fifths,
'divisions': divisions,
}
) + ' '
elif pitch is not None:
step = pitch.find('step').text
octave = int(pitch.find('octave').text)
duration = int(note.find('duration').text)
note_type = note.find('type').text
dot_count = len(note.findall('dot'))
accidental = note.find('accidental')
beam = note.find('beam')
# 处理符杠
spacing = ' '
if beam is not None:
beam = beam.text
if beam == "begin" or beam == "continue":
spacing = ''
# 升降号
if accidental is not None:
accidental = accidental.text
score += convert_to_jianpu(
{
'step': step,
'octave': octave,
'duration': duration,
'type': note_type,
'dot_count': dot_count,
'accidental': accidental,
},
{
'fifths': fifths,
'divisions': divisions,
}
) + spacing
# 处理特殊小节线
barline = measure.find('barline')
if barline is not None:
if barline.find('bar-style').text == "light-light":
score += "| | "
elif barline.find('bar-style').text == "light-heavy":
score += "+"
else:
score += "| "
return score
def create_doc(notes, output_doc):
doc = Document()
p = doc.add_paragraph()
run = p.add_run(notes)
run.font.name = 'jpfont-nds'
r = run._element
rFonts = r.find(qn('w:rPr')).find(qn('w:rFonts'))
if rFonts is None:
rFonts = OxmlElement('w:rFonts')
r.get_or_add('w:rPr').append(rFonts)
rFonts.set(qn('w:eastAsia'), 'jpfont-nds')
run.font.size = Pt(12)
doc.save(output_doc)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('filename', type=str)
args = parser.parse_args()
musicxml_file = args.filename
output_filename = os.path.splitext(os.path.basename(musicxml_file))[0]
output_doc = 'outputs/' + output_filename + "_"+datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") + '.docx'
notes = parse(musicxml_file)
create_doc(notes, output_doc)
print("Score saved to " + output_doc)