Mercurial > ~darius > hgwebdir.cgi > musiccutter
comparison musiccutter.py @ 41:21da8af1cdd2
Use ini file to hold organ specific details.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 24 May 2016 19:40:11 +0930 |
parents | 5c47f9361d93 |
children | 3925ac56d99e |
comparison
equal
deleted
inserted
replaced
40:5c47f9361d93 | 41:21da8af1cdd2 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 from IPython.core.debugger import Tracer | 3 from IPython.core.debugger import Tracer |
4 import ConfigParser | |
4 import exceptions | 5 import exceptions |
5 import itertools | 6 import itertools |
6 import math | 7 import math |
7 import mido | 8 import mido |
8 import os.path | 9 import os.path |
33 # +---+---+---+ lowest note | 34 # +---+---+---+ lowest note |
34 # | | | | | 35 # | | | | |
35 # +---+---+---+ highest note | 36 # +---+---+---+ highest note |
36 # | 37 # |
37 m = Midi2PDF(**{ | 38 m = Midi2PDF(**{ |
38 'notefile' : 'notes', | 39 'config' : 'orgues-de-barbarie-27.ini', |
39 'pagewidth' : 120, | |
40 'pageheight' : 155, | |
41 'pitch' : 5.5, | |
42 'slotsize' : 3.3, | |
43 'heel' : 6.0, | |
44 'leadin' : 50, | 40 'leadin' : 50, |
45 'timemarks' : False, | 41 'timemarks' : False, |
46 'trytranspose' : True, | 42 'trytranspose' : True, |
47 'drawrect' : False, | 43 'drawrect' : False, |
48 'notenames' : False, | 44 'notenames' : False, |
57 base, ext = os.path.splitext(filename) | 53 base, ext = os.path.splitext(filename) |
58 base = os.path.basename(base) | 54 base = os.path.basename(base) |
59 m.processMidi(filename, base + '-%02d.pdf') | 55 m.processMidi(filename, base + '-%02d.pdf') |
60 | 56 |
61 class Midi2PDF(object): | 57 class Midi2PDF(object): |
62 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize): | 58 def __init__(self, config, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize): |
63 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() | 59 cp = ConfigParser.ConfigParser() |
64 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi) | 60 cp.read(config) |
65 self.pagewidth = pagewidth # Dimensions are in millimetres | 61 self.pagewidth = cp.getfloat('default', 'pagewidth') |
66 self.pageheight = pageheight | 62 self.pageheight = cp.getfloat('default', 'pageheight') |
67 self.pitch = pitch # Distance between each slot | 63 self.pitch = cp.getfloat('default', 'pitch') |
68 self.slotsize = slotsize # Size of each slot cut out | 64 self.slotsize = cp.getfloat('default', 'slotsize') |
69 self.heel = heel # Bottom margin (from bottom of page to centre of slot) | 65 self.heel = cp.getfloat('default', 'heel') |
70 self.leadin = leadin # Extra at the start | 66 self.leadin = leadin # Extra at the start |
71 self.timemarks = timemarks # Draw vertical time lines | 67 self.timemarks = timemarks # Draw vertical time lines |
72 self.trytranspose = trytranspose # Attempt to tranpose unplayable notes | 68 self.trytranspose = trytranspose # Attempt to tranpose unplayable notes |
73 self.drawrect = drawrect # Draw rectangle around each page | 69 self.drawrect = drawrect # Draw rectangle around each page |
74 self.notenames = notenames # Draw note names on the right edge | 70 self.notenames = notenames # Draw note names on the right edge |
79 self.notescale = notescale # Multiply all note lengths by this (to get rearticulation) | 75 self.notescale = notescale # Multiply all note lengths by this (to get rearticulation) |
80 self.fontname = fontname | 76 self.fontname = fontname |
81 self.fontsize = fontsize # Points | 77 self.fontsize = fontsize # Points |
82 | 78 |
83 self.pdfwidth = self.pagewidth * self.pagesperpdf | 79 self.pdfwidth = self.pagewidth * self.pagesperpdf |
80 | |
81 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() | |
82 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(cp.get('default', 'notes').split(), self.note2midi) | |
84 | 83 |
85 def processMidi(self, midifile, outpat): | 84 def processMidi(self, midifile, outpat): |
86 stats = Stats() | 85 stats = Stats() |
87 stats.playablecount = 0 | 86 stats.playablecount = 0 |
88 stats.unplayablecount = 0 | 87 stats.unplayablecount = 0 |
264 note2midi[name] = midi | 263 note2midi[name] = midi |
265 | 264 |
266 return midi2note, note2midi | 265 return midi2note, note2midi |
267 | 266 |
268 @staticmethod | 267 @staticmethod |
269 def loadnote2slot(fname, note2midi): | 268 def loadnote2slot(notelist, note2midi): |
270 note2slot = {} | 269 note2slot = {} |
271 slot2note = {} | 270 slot2note = {} |
272 index = 0 | 271 index = 0 |
273 | 272 |
274 for note in file(fname): | 273 for note in notelist: |
275 note = note.strip() | 274 note = note.strip() |
276 if note[0] == '#': | 275 if note[0] == '#': |
277 continue | 276 continue |
278 if note not in note2midi: | 277 if note not in note2midi: |
279 raise exceptions.ValueError('Note \'%s\' not valid' % note) | 278 raise exceptions.ValueError('Note \'%s\' not valid' % note) |