-
Notifications
You must be signed in to change notification settings - Fork 2
/
meicreate.py
451 lines (378 loc) · 16.3 KB
/
meicreate.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""
Copyright (c) 2013, Gregory Burlet
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sample usage:
python meicreate.py -b C_07a_ED-Kl_1_A-Wn_SHWeber90_S_009_bar_position_2.txt -s C_07a_ED-Kl_1_A-Wn_SHWeber90_S_009_staff_vertices.txt -f detmoldbars.mei -g '(2|)x2 (4(2|))' -v
"""
from __future__ import division
import argparse
from pyparsing import nestedExpr
import os
import re
import sys
import datetime
from pymei import MeiDocument, MeiElement, XmlExport
# set up command line argument structure
parser = argparse.ArgumentParser(description='Convert text file of OMR barline data to mei.')
parser.add_argument('-b', '--barfilein', help='barline data input file')
parser.add_argument('-s', '--stafffilein', help='staff data input file')
parser.add_argument('-g', '--staffgroups', help='staffgroups')
parser.add_argument('-f', '--fileout', help='output file')
parser.add_argument('-v', '--verbose', help='increase output verbosity', action='store_true')
class BarlineDataConverter:
'''
Convert the output of the barline detection algorithm
to MEI.
'''
def __init__(self, staff_bb, bar_bb, verbose):
'''
Initialize the converter
'''
self.staff_bb = staff_bb
self.bar_bb = bar_bb
self.verbose = verbose
def bardata_to_mei(self, sg_hint, image_path, image_width, image_height, image_dpi):
'''
Perform the data conversion to mei
'''
self.meidoc = MeiDocument()
mei = MeiElement('mei')
self.meidoc.setRootElement(mei)
###########################
# MetaData #
###########################
mei_head = self._create_header()
mei.addChild(mei_head)
###########################
# Body #
###########################
music = MeiElement('music')
body = MeiElement('body')
mdiv = MeiElement('mdiv')
score = MeiElement('score')
score_def = MeiElement('scoreDef')
section = MeiElement('section')
# physical location data
facsimile = MeiElement('facsimile')
surface = MeiElement('surface')
graphic = self._create_graphic(image_path, image_width, image_height)
surface.addChild(graphic)
# parse staff group hint to generate staff group
sg_hint = sg_hint.split(" ")
systems = []
for s in sg_hint:
parser = nestedExpr()
sg_list = parser.parseString(s).asList()[0]
staff_grp, n = self._create_staff_group(sg_list, MeiElement('staffGrp'), 0)
# parse repeating staff groups (systems)
num_sb = 1
match = re.search('(?<=x)(\d+)$', s)
if match is not None:
# there are multiple systems of this staff grouping
num_sb = int(match.group(0))
for i in range(num_sb):
systems.append(staff_grp)
if self.verbose:
print "number of staves in system: %d x %d system(s)" % (n, num_sb)
# there may be hidden staves in a system
# make the encoded staff group the largest number of staves in a system
final_staff_grp = max(systems, key=lambda x: len(x.getDescendantsByName('staffDef')))
mei.addChild(music)
music.addChild(facsimile)
facsimile.addChild(surface)
# list of staff bounding boxes within a system
staves = []
for staff_bb in self.staff_bb:
# get bounding box of the staff
# parse bounding box integers
#staff_bb = [int(x) for x in staff_bb]
staves.append(staff_bb[1:])
music.addChild(body)
body.addChild(mdiv)
mdiv.addChild(score)
score.addChild(score_def)
score_def.addChild(final_staff_grp)
score.addChild(section)
# parse barline data file [staffnum][barlinenum_ulx]
barlines = []
for i, bar in enumerate(self.bar_bb):
staff_num = int(bar[0])
ulx = bar[1]
try:
barlines[staff_num-1].append(ulx)
except IndexError:
barlines.append([ulx])
staff_offset = 0
n_measure = 1
for s_ind, s in enumerate(systems):
# measures in a system
s_measures = []
staff_defs = s.getDescendantsByName('staffDef')
# for each staff in the system
for i in range(len(staff_defs)):
staff_num = staff_offset + i
s_bb = staves[staff_num]
# bounding box of the staff
s_ulx = s_bb[0]
s_uly = s_bb[1]
s_lrx = s_bb[2]
s_lry = s_bb[3]
# for each barline on this staff
try:
staff_bars = barlines[staff_num]
except IndexError:
# a staff was found, but no bar candidates have been found on the staff
continue
# for each barline on this staff
for n, b in enumerate(staff_bars[:-1]):
# calculate bounding box of the measure
m_uly = s_uly
m_lry = s_lry
m_ulx = b
m_lrx = staff_bars[n+1]
zone = self._create_zone(m_ulx, m_uly, m_lrx, m_lry)
surface.addChild(zone)
if len(sg_hint) == 1 or len(staff_defs) == len(final_staff_grp.getDescendantsByName('staffDef')):
staff_n = str(i+1)
else:
# take into consideration hidden staves
staff_n = i + self._calc_staff_num(len(staff_defs), [final_staff_grp]) + 1
staff = self._create_staff(staff_n, zone)
#print ' ', staff_n, m_ulx, m_uly, m_lrx, m_lry
try:
s_measures[n].addChild(staff)
except IndexError:
# create a new measure
measure = self._create_measure(str(n_measure))
s_measures.append(measure)
section.addChild(measure)
measure.addChild(staff)
n_measure += 1
# calculate min/max of measure/staff bounding boxes to get measure zone
self._calc_measure_zone(s_measures)
staff_offset += len(staff_defs)
# add a system break, if necessary
if s_ind+1 < len(systems):
sb = MeiElement('sb')
section.addChild(sb)
def _calc_staff_num(self, num_staves, staff_grps):
'''
In the case where there are hidden staves,
search for the correct staff number within the staff
group definition.
'''
if len(staff_grps) == 0:
# termination condition (or no match found)
return 0
else:
sg_staves = staff_grps[0].getChildrenByName('staffDef')
sgs = staff_grps[0].getChildrenByName('staffGrp')
if num_staves == len(sg_staves):
# no need to look at subsequent staff groups
n = int(sg_staves[0].getAttribute('n').value)
else:
n = self._calc_staff_num(num_staves, sgs)
return n + self._calc_staff_num(num_staves, staff_grps[1:])
def _calc_measure_zone(self, measures):
'''
Calculate the bounding box of the provided measures
by calculating the min and max of the bounding boxes
of the staves which compose the measure.
'''
# for each measure
for m in measures:
staff_measure_zones = []
min_ulx = sys.maxint
min_uly = sys.maxint
max_lrx = -sys.maxint - 1
max_lry = -sys.maxint - 1
for s in m.getChildrenByName('staff'):
# have to skip # at the beginning of the id ref since using URIs
s_zone = self.meidoc.getElementById(s.getAttribute('facs').value[1:])
ulx = int(s_zone.getAttribute('ulx').value)
if ulx < min_ulx:
min_ulx = ulx
uly = int(s_zone.getAttribute('uly').value)
if uly < min_uly:
min_uly = uly
lrx = int(s_zone.getAttribute('lrx').value)
if lrx > max_lrx:
max_lrx = lrx
lry = int(s_zone.getAttribute('lry').value)
if lry > max_lry:
max_lry = lry
m_zone = self._create_zone(min_ulx, min_uly, max_lrx, max_lry)
m.addAttribute('facs', '#'+m_zone.getId())
surface = self.meidoc.getElementsByName('surface')[0]
surface.addChild(m_zone)
def _create_header(self, rodan_version='0.1'):
'''
Create a meiHead element
'''
mei_head = MeiElement('meiHead')
today = datetime.date.today().isoformat()
app_name = 'RODAN/barlineFinder'
# file description
file_desc = MeiElement('fileDesc')
title_stmt = MeiElement('titleStmt')
title = MeiElement('title')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
title_stmt.addChild(title)
title_stmt.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
pub_stmt = MeiElement('pubStmt')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
pub_stmt.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
mei_head.addChild(file_desc)
file_desc.addChild(title_stmt)
file_desc.addChild(pub_stmt)
# encoding description
encoding_desc = MeiElement('encodingDesc')
app_info = MeiElement('appInfo')
application = MeiElement('application')
application.addAttribute('version', rodan_version)
name = MeiElement('name')
name.setValue(app_name)
ptr = MeiElement('ptr')
ptr.addAttribute('target', 'https://github.com/DDMAL/barlineFinder')
mei_head.addChild(encoding_desc)
encoding_desc.addChild(app_info)
app_info.addChild(application)
application.addChild(name)
application.addChild(ptr)
# revision description
revision_desc = MeiElement('revisionDesc')
change = MeiElement('change')
change.addAttribute('n', '1')
resp_stmt = MeiElement('respStmt')
corp_name = MeiElement('corpName')
corp_name.setValue('Distributed Digital Music Archives and Libraries Lab (DDMAL)')
change_desc = MeiElement('changeDesc')
ref = MeiElement('ref')
ref.addAttribute('target', '#'+application.getId())
ref.setValue(app_name)
ref.setTail('.')
p = MeiElement('p')
p.addChild(ref)
p.setValue('Encoded using ')
date = MeiElement('date')
date.setValue(today)
mei_head.addChild(revision_desc)
revision_desc.addChild(change)
change.addChild(resp_stmt)
resp_stmt.addChild(corp_name)
change.addChild(change_desc)
change_desc.addChild(p)
change.addChild(date)
return mei_head
def _create_graphic(self, image_path, image_width, image_height):
'''
Create a graphic element.
'''
graphic = MeiElement('graphic')
graphic.addAttribute('height', str(image_height))
graphic.addAttribute('width', str(image_width))
graphic.addAttribute('target', image_path)
graphic.addAttribute('unit', 'px')
return graphic
def _create_staff_group(self, sg_list, staff_grp, n):
'''
Recursively create the staff group element from the parsed
user input of the staff groupings
'''
if not sg_list:
return staff_grp, n
else:
if type(sg_list[0]) is list:
new_staff_grp, n = self._create_staff_group(sg_list[0], MeiElement('staffGrp'), n)
staff_grp.addChild(new_staff_grp)
else:
# check for barthrough character
if sg_list[0][-1] == '|':
# the barlines go through all the staves in the staff group
staff_grp.addAttribute('barthru', 'true')
# remove the barthrough character, should now only be an integer
sg_list[0] = sg_list[0][:-1]
n_staff_defs = int(sg_list[0])
# get current staffDef number
for i in range(n_staff_defs):
staff_def = MeiElement('staffDef')
staff_def.addAttribute('n', str(n+i+1))
staff_def.addAttribute('lines', '5')
staff_grp.addChild(staff_def)
n += n_staff_defs
return self._create_staff_group(sg_list[1:], staff_grp, n)
def _create_staff(self, n, zone):
'''
Create a staff element, and attach a zone reference to it
'''
staff = MeiElement('staff')
staff.addAttribute('n', str(n))
staff.addAttribute('facs', '#'+zone.getId())
return staff
def _create_measure(self, n, zone = None):
'''
Create a measure element and attach a zone reference to it.
The zone element is optional, since the zone of the measure is
calculated once all of the staves within a measure have been added
to the MEI.
'''
measure = MeiElement('measure')
measure.addAttribute('n', str(n))
if zone is not None:
measure.addAttribute('facs', '#'+zone.getId())
return measure
def _create_zone(self, ulx, uly, lrx, lry):
'''
Create a zone element
'''
zone = MeiElement('zone')
zone.addAttribute('ulx', str(ulx))
zone.addAttribute('uly', str(uly))
zone.addAttribute('lrx', str(lrx))
zone.addAttribute('lry', str(lry))
return zone
def output_mei(self, output_path):
'''
Write the generated mei to disk
'''
# output mei file
XmlExport.meiDocumentToFile(self.meidoc, output_path)
if __name__ == "__main__":
# parse command line arguments
args = parser.parse_args()
bar_input_path = args.barfilein
staff_input_path = args.stafffilein
if not os.path.exists(bar_input_path) or not os.path.exists(staff_input_path):
raise ValueError('The input file does not exist')
output_path = args.fileout
sg_hint = args.staffgroups
verbose = args.verbose
bar_converter = BarlineDataConverter(bar_input_path, staff_input_path, verbose)
bar_converter.bardata_to_mei(sg_hint)
bar_converter.output_mei(output_path)