forked from mariaalfaroc/ScoreGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
324 lines (254 loc) · 11.2 KB
/
manager.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import os
import agnostictranslate as agnostic
import dictionary as internal
import image_processing as imgprocess
import kerntranslate as kern
OUT_FOLDER = 'OUT'
INTERNAL_FOLDER = 'INTERNAL'
AGNOSTIC_FOLDER = 'AGNOSTIC'
KERN_FOLDER = 'KERN'
SVG_FOLDER = 'SVG'
IMAGE_FOLDER = 'PNG'
extensionKern = '.krn'
extensionAgnosticInternal = '.txt'
extensionImage = '.png'
def create_folds():
global out_path
global internal_path
global kern_path
global agnostic_path
global svg_path
global image_path
main_path = os.path.dirname(os.path.realpath(__file__))
out_path = os.path.join(main_path, OUT_FOLDER)
internal_path = os.path.join(out_path, INTERNAL_FOLDER)
kern_path = os.path.join(out_path, KERN_FOLDER)
agnostic_path = os.path.join(out_path, AGNOSTIC_FOLDER)
svg_path = os.path.join(out_path, SVG_FOLDER)
image_path = os.path.join(out_path, IMAGE_FOLDER)
os.makedirs(out_path, exist_ok=True)
os.makedirs(internal_path, exist_ok=True)
os.makedirs(kern_path, exist_ok=True)
os.makedirs(agnostic_path, exist_ok=True)
os.makedirs(svg_path, exist_ok=True)
os.makedirs(image_path, exist_ok=True)
def init(i: int, typeAgnostic: str):
global fInternal
global fKern
global fAgnostic
filenameAgnosticInternal = str(i) + extensionAgnosticInternal
filenameKern = str(i) + extensionKern
fInternal = open(os.path.join(internal_path, filenameAgnosticInternal), "w")
fKern = open(os.path.join(kern_path, filenameKern), "w")
fKern.write(kern.init + kern.advance)
fAgnostic = open(os.path.join(agnostic_path, filenameAgnosticInternal), "w")
agnostic.valueSeparator(typeAgnostic=typeAgnostic)
def piano_init(i: int, typeAgnostic: str):
# Clefs are fixed to piano clefs
# The order is conditioned to the appearance of a Humdrum **kern file
# Staves are ordered from lowest to highest pitch
# Therefore, 1 -> ClefF4; 2 -> ClefG2
global fInternal
global fKern
global fAgnostic1_path
global fAgnostic1
global fAgnostic2
filenameAgnosticInternal = str(i) + extensionAgnosticInternal
filenameKern = str(i) + extensionKern
fInternal = open(os.path.join(internal_path, filenameAgnosticInternal), "w")
fKern = open(os.path.join(kern_path, filenameKern), "w")
fKern.write(kern.init + kern.not_advance + kern.init + kern.advance)
fAgnostic1_path = os.path.join(agnostic_path, '-' + filenameAgnosticInternal)
fAgnostic1 = open(fAgnostic1_path, "w")
fAgnostic2 = open(os.path.join(agnostic_path, filenameAgnosticInternal), "w")
agnostic.valueSeparator(typeAgnostic=typeAgnostic)
def polyss_init(i: int, typeAgnostic: str):
# Spines: left, lowest voice; right, highest voice
global fInternal
global fKern
global fAgnostic1_path
global fAgnostic1
global fAgnostic2
filenameAgnosticInternal = str(i) + extensionAgnosticInternal
filenameKern = str(i) + extensionKern
fInternal = open(os.path.join(internal_path, filenameAgnosticInternal), "w")
fKern = open(os.path.join(kern_path, filenameKern), "w")
fKern.write(kern.init + kern.advance)
fAgnostic1_path = os.path.join(agnostic_path, '-' + filenameAgnosticInternal)
fAgnostic1 = open(fAgnostic1_path, "w")
fAgnostic2 = open(os.path.join(agnostic_path, filenameAgnosticInternal), "w")
agnostic.valueSeparator(typeAgnostic=typeAgnostic)
def write_clef(c):
global clef
clef = c
fInternal.write(clef + internal.advance)
fKern.write(kern.clefs[clef] + kern.advance)
fAgnostic.write(agnostic.clefs(clef) + agnostic.advance)
def write_pianoclef(c1, c2):
global clef1
global clef2
clef1 = c1
clef2 = c2
fInternal.write(clef1 + internal.not_advance + clef2 + internal.advance)
fKern.write(kern.clefs[clef1] + kern.not_advance + kern.clefs[clef2] + kern.advance)
fAgnostic1.write(agnostic.clefs(clef1) + agnostic.advance)
fAgnostic2.write(agnostic.clefs(clef2) + agnostic.advance)
def write_polyssclef(c):
global clef1
global clef2
clef1 = clef2 = c
fInternal.write(clef1 + internal.advance)
fKern.write(kern.clefs[clef1] + kern.advance)
fAgnostic1.write(agnostic.clefs(clef1) + agnostic.advance)
fAgnostic2.write(agnostic.clefs(clef2) + agnostic.advance)
def write_key(k):
global key
key = k
fInternal.write(key + internal.advance)
fKern.write(kern.accidentals[key] + kern.advance)
fAgnostic.write(agnostic.accidentals(key, clef))
def write_pianokey(k):
global key
key = k
fInternal.write(key + internal.not_advance + key + internal.advance)
fKern.write(kern.accidentals[key] + kern.not_advance + kern.accidentals[key] + kern.advance)
fAgnostic1.write(agnostic.accidentals(key, clef1))
fAgnostic2.write(agnostic.accidentals(key, clef2))
def write_polysskey(k):
global key
key = k
fInternal.write(key + internal.advance)
fKern.write(kern.accidentals[key] + kern.advance)
fAgnostic1.write(agnostic.accidentals(key, clef1))
fAgnostic2.write(agnostic.accidentals(key, clef2))
def write_timeSig(m):
fInternal.write(m[0] + internal.advance)
fKern.write(kern.bars[m[0]] + kern.advance)
fAgnostic.write(agnostic.timeSig(m[0]) + agnostic.advance)
def write_pianotimeSig(m):
fInternal.write(m[0] + internal.not_advance + m[0] + internal.advance)
fKern.write(kern.bars[m[0]] + kern.not_advance + kern.bars[m[0]] + kern.advance)
fAgnostic1.write(agnostic.timeSig(m[0]) + agnostic.advance)
fAgnostic2.write(agnostic.timeSig(m[0]) + agnostic.advance)
def write_polysstimeSig(m):
fInternal.write(m[0] + internal.advance)
fKern.write(kern.bars[m[0]] + kern.advance)
fAgnostic1.write(agnostic.timeSig(m[0]) + agnostic.advance)
fAgnostic2.write(agnostic.timeSig(m[0]) + agnostic.advance)
def write_openspine():
# When musicTexture=='poly-ss'
fKern.write(kern.spine + kern.advance)
def write_barLine(n):
fInternal.write('bar' + str(n) + internal.advance)
fKern.write(kern.barLine(n) + kern.advance)
fAgnostic.write(agnostic.barLine(n)) # agnostic.advance is handled in the barLine(n) function
def write_pianobarLine(n):
fInternal.write('bar' + str(n) + internal.not_advance + 'bar' + str(n) + internal.advance)
fKern.write(kern.barLine(n) + kern.not_advance + kern.barLine(n) + kern.advance)
fAgnostic1.write(agnostic.barLine(n))
fAgnostic2.write(agnostic.barLine(n)) # agnostic.advance is handled in the barLine(n) function
def write_symbol(s: list, bar_accidentals: dict):
# s is the internal representation of the symbol
# We only have chord as musical groupings
size = len(s)
for id, note in enumerate(s):
fInternal.write(note)
fKern.write(kern.kern_symbol(line=note, clef=clef, key=key, bar_accidentals=bar_accidentals))
if id != size - 1:
fInternal.write(internal.separator)
fKern.write(kern.separator)
fInternal.write(internal.advance)
fKern.write(kern.advance)
s_ag = agnostic.ag_chord(notes=s) if size > 1 else agnostic.ag_symbol(line=s[0])
fAgnostic.write(s_ag)
if size == 1:
fAgnostic.write(agnostic.advance)
def write_pianosymbol(s1: list, s2: list, bar_accidentals1: dict, bar_accidentals2: dict):
# s is the internal representation of the symbol
# We only have chord as musical groupings
size1, size2 = len(s1), len(s2)
for id1, note1 in enumerate(s1):
fInternal.write(note1)
fKern.write(kern.kern_symbol(line=note1, clef=clef1, key=key, bar_accidentals=bar_accidentals1))
if id1 != size1 - 1:
fInternal.write(internal.separator)
fKern.write(kern.separator)
fInternal.write(internal.not_advance)
fKern.write(kern.not_advance)
for id2, note2 in enumerate(s2):
fInternal.write(note2)
fKern.write(kern.kern_symbol(line=note2, clef=clef2, key=key, bar_accidentals=bar_accidentals2))
if id2 != size2 - 1:
fInternal.write(internal.separator)
fKern.write(kern.separator)
fInternal.write(internal.advance)
fKern.write(kern.advance)
s1_ag = agnostic.ag_chord(notes=s1) if size1 > 1 else agnostic.ag_symbol(line=s1[0])
s2_ag = agnostic.ag_chord(notes=s2) if size2 > 1 else agnostic.ag_symbol(line=s2[0])
if s1_ag != '':
fAgnostic1.write(s1_ag)
if size1 == 1:
fAgnostic1.write(agnostic.advance)
if s2_ag != '':
fAgnostic2.write(s2_ag)
if size2 == 1:
fAgnostic2.write(agnostic.advance)
def write_end():
fInternal.close()
fKern.write(kern.end_barLine() + kern.advance + kern.end)
fKern.close()
fAgnostic.write(agnostic.end_barLine())
fAgnostic.close()
def write_pianoend():
fInternal.close()
fKern.write(kern.end_barLine() + kern.not_advance + kern.end_barLine() +
kern.advance +
kern.end + kern.not_advance + kern.end)
fKern.close()
fAgnostic1.write(agnostic.end_barLine())
fAgnostic1.close()
fAgnostic2.write(agnostic.end_barLine())
with open(fAgnostic1_path, 'r') as f1:
fAgnostic2.write('\n' + f1.read())
fAgnostic2.close()
os.remove(fAgnostic1_path)
# ----------------------------------------------
# -- DATA PROCESSING FOR END-TO-END OMR -- #
def removefiles(files: list, extension: str):
del_size = len(files)
for file in files:
# INTERNAL
i_file = file.replace(SVG_FOLDER, INTERNAL_FOLDER)
i_file = i_file.split(extension)[0] + extensionAgnosticInternal
os.remove(i_file)
# KERN
k_file = file.replace(SVG_FOLDER, KERN_FOLDER)
k_file = k_file.split(extension)[0] + extensionKern
os.remove(k_file)
# AGNOSTIC
ag_file = file.replace(SVG_FOLDER, AGNOSTIC_FOLDER)
ag_file = ag_file.split(extension)[0] + extensionAgnosticInternal
os.remove(ag_file)
# SVG
os.remove(file)
print("{} scores have been deleted".format(del_size))
def preprareforOMR(musicTexture: str, typeAgnostic: str, nstaves: int):
# Convert HUMDRUM **KERN files to SVG
imgprocess.kerntosvg(kern_path=kern_path, kern_folder=KERN_FOLDER, svg_folder=SVG_FOLDER, extensionKern=extensionKern)
# Check for those that do not meet end-to-end conditions (single-staff section or pair-staff section, as appropriate)
del_files = imgprocess.getMultiStaff(svg_path=svg_path, mode=musicTexture, numStaves=nstaves)
# Remove those files
removefiles(files=del_files, extension='.svg')
# Convert to PNG to be able to crop images to standard size
imgprocess.svgtopng(svg_path=svg_path, svg_folder=SVG_FOLDER, image_folder=IMAGE_FOLDER,
extensionImage=extensionImage)
# Crop to centered the music information on the image
crop_path = imgprocess.crop_and_filter(image_path=image_path, extensionImage=extensionImage)
# Distort to simulate as if they were pictures taken with a camera
imgprocess.distort(crop_path=crop_path, extensionImage=extensionImage)
# ----
# TODO
# See if there are any measure rest and write its corresponding agnostic token
# ----
# Processing agnostic files to add specific music texture features if needed
agnostic.encoding_reformat(agnostic_path=agnostic_path, mode=musicTexture, typeAgnostic=typeAgnostic)