comparison musiccutter.py @ 43:c69e53b8917c

Read in per-job options from an ini file and add main funciton so you can do.. env PYTHONPATH=$HOME/lib/python python ./musiccutter.py music/Rosy\'s\ Angel\ Lullaby.mid music/Rosy\'s\ Angel\ Lullaby.ini orgues-de-barbarie-27.ini music
author Daniel O'Connor <darius@dons.net.au>
date Tue, 28 Jun 2016 17:58:32 +0930
parents 3925ac56d99e
children
comparison
equal deleted inserted replaced
42:3925ac56d99e 43:c69e53b8917c
19 pass 19 pass
20 20
21 class EVWrap(object): 21 class EVWrap(object):
22 def __init__(self, ev): 22 def __init__(self, ev):
23 self.ev = ev 23 self.ev = ev
24
25 optionlist = { 'leadin' : int, 'timemarks' : bool, 'trytranspose' : bool, 'drawrect' : bool, 'notenames' : bool,
26 'notelines' : bool, 'noteoffset' : int, 'pagesperpdf' : int, 'timescale' : float,
27 'notescale' : float, 'fontname' : str, 'fontsize' : float }
28
29 def main():
30 if len(sys.argv) != 5:
31 print('Bad usage')
32 print(' %s music.midi config.ini organconf.ini destdir' % (sys.argv[0]))
33 sys.exit(1)
34
35 filename, config, organconf, destdir = sys.argv[1:]
36 cp = ConfigParser.ConfigParser()
37 cp.read(config)
38 args = {}
39 for opt in optionlist:
40 print opt
41 if not cp.has_option('default', opt):
42 print('Missing option', opt)
43 sys.exit(1)
44 if optionlist[opt] == int:
45 fn = cp.getint
46 elif optionlist[opt] == float:
47 fn = cp.getfloat
48 elif optionlist[opt] == bool:
49 fn = cp.getboolean
50 elif optionlist[opt] == str:
51 fn = cp.get
52 else:
53 print('unknown type')
54 sys.exit(1)
55 try:
56 args[opt] = fn('default', opt)
57 except ValueError:
58 print('Error parsing', opt)
59 sys.exit(1)
60 args['organconf'] = organconf
61 m = Midi2PDF(**args)
62 base, ext = os.path.splitext(filename)
63 base = os.path.basename(base)
64 m.processMidi(filename, destdir + '/' + base + '-%02d.pdf')
24 65
25 def test(filename = None, shift = 0): 66 def test(filename = None, shift = 0):
26 if filename == None: 67 if filename == None:
27 filename = 'test.midi' 68 filename = 'test.midi'
28 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf 69 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
34 # +---+---+---+ lowest note 75 # +---+---+---+ lowest note
35 # | | | | 76 # | | | |
36 # +---+---+---+ highest note 77 # +---+---+---+ highest note
37 # 78 #
38 m = Midi2PDF(**{ 79 m = Midi2PDF(**{
39 'config' : 'orgues-de-barbarie-27.ini', 80 'organconf' : 'orgues-de-barbarie-27.ini',
40 'leadin' : 50, 81 'leadin' : 50,
41 'timemarks' : False, 82 'timemarks' : False,
42 'trytranspose' : True, 83 'trytranspose' : True,
43 'drawrect' : False, 84 'drawrect' : False,
44 'notenames' : False, 85 'notenames' : False,
45 'notelines' : False, 86 'notelines' : False,
46 'noteoffset' : shift, 87 'noteoffset' : shift,
47 'pagesperpdf' : 1, 88 'pagesperpdf' : 1,
48 'timescale' : 30, 89 'timescale' : 35,
49 'notescale' : 0.9, 90 'notescale' : 0.9,
50 'fontname' : 'Helvetica', 91 'fontname' : 'Helvetica',
51 'fontsize' : 12, 92 'fontsize' : 12,
52 }) 93 })
53 base, ext = os.path.splitext(filename) 94 base, ext = os.path.splitext(filename)
54 base = os.path.basename(base) 95 base = os.path.basename(base)
55 m.processMidi(filename, base + '-%02d.pdf') 96 m.processMidi(filename, base + '-%02d.pdf')
56 97
57 class Midi2PDF(object): 98 class Midi2PDF(object):
58 def __init__(self, config, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize): 99 def __init__(self, organconf, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize):
59 cp = ConfigParser.ConfigParser() 100 cp = ConfigParser.ConfigParser()
60 cp.read(config) 101 cp.read(organconf)
61 self.pagewidth = cp.getfloat('default', 'pagewidth') 102 self.pagewidth = cp.getfloat('default', 'pagewidth')
62 self.pageheight = cp.getfloat('default', 'pageheight') 103 self.pageheight = cp.getfloat('default', 'pageheight')
63 self.pitch = cp.getfloat('default', 'pitch') 104 self.pitch = cp.getfloat('default', 'pitch')
64 self.slotsize = cp.getfloat('default', 'slotsize') 105 self.slotsize = cp.getfloat('default', 'slotsize')
65 self.heel = cp.getfloat('default', 'heel') 106 self.heel = cp.getfloat('default', 'heel')