comparison musiccutter.py @ 16:20337b22977d

Add left margin and slot size support.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 14 Apr 2016 09:48:08 +0930
parents 43b5d8d62df6
children c50427f1da2d
comparison
equal deleted inserted replaced
15:b9fb9dee39e4 16:20337b22977d
11 import sys 11 import sys
12 12
13 CUT_COLOUR = reportlab.lib.colors.red 13 CUT_COLOUR = reportlab.lib.colors.red
14 ENGRAVE_COLOUR = reportlab.lib.colors.black 14 ENGRAVE_COLOUR = reportlab.lib.colors.black
15 15
16
17 ## XXX: PDF origin bottom left, SVG origin top left
18 def test(filename = None): 16 def test(filename = None):
19 if filename == None: 17 if filename == None:
20 filename = 'test.midi' 18 filename = 'test.midi'
21 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf 19 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
22 m = Midi2PDF('notes', 200, 155, 5.5, 6.0, 20, 10, 'Helvetica', 12) 20 m = Midi2PDF('notes', 200, 155, 5.5, 3.0, 6.0, 20, 10, 'Helvetica', 12)
23 base, ext = os.path.splitext(filename) 21 base, ext = os.path.splitext(filename)
24 m.processMidi(filename, base + '-%02d.pdf') 22 m.processMidi(filename, base + '-%02d.pdf')
25 23
26 class Midi2PDF(object): 24 class Midi2PDF(object):
27 def __init__(self, notefile, pagewidth, pageheight, pitch, offset, margin, timescale, fontname, fontsize): 25 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, offset, lmargin, timescale, fontname, fontsize):
28 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() 26 self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
29 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi) 27 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
30 self.pagewidth = pagewidth # Dimensions are in millimetres 28 self.pagewidth = pagewidth # Dimensions are in millimetres
31 self.pageheight = pageheight 29 self.pageheight = pageheight
32 self.pitch = pitch # Distance between each slot 30 self.pitch = pitch # Distance between each slot
31 self.slotsize = slotsize # Size of each slot cut out
33 self.offset = offset # Bottom margin 32 self.offset = offset # Bottom margin
34 self.margin = margin # Left margin 33 self.lmargin = lmargin # Left margin
35 self.timescale = timescale 34 self.timescale = timescale # Width per second
36 self.fontname = fontname 35 self.fontname = fontname
37 self.fontsize = fontsize 36 self.fontsize = fontsize # Points
38 37
39 def processMidi(self, midifile, outpat): 38 def processMidi(self, midifile, outpat):
40 playablecount = 0 39 playablecount = 0
41 unplayablecount = 0 40 unplayablecount = 0
42 midi = mido.MidiFile(midifile) 41 midi = mido.MidiFile(midifile)
43 ctime = 0 42 ctime = 0
44 channels = [] 43 channels = []
45 for i in range(16): 44 for i in range(16):
46 channels.append({}) 45 channels.append({})
47 46
48 npages = int(math.ceil(midi.length * self.timescale / self.pagewidth)) 47 npages = int(math.ceil(((midi.length * self.timescale) + self.lmargin) / self.pagewidth))
49 print 'npages', npages 48 print 'npages', npages
50 pdfs = [] 49 pdfs = []
51 for i in range(npages): 50 for i in range(npages):
52 pdf = reportlab.pdfgen.canvas.Canvas(file(outpat % (i + 1), 'w'), pagesize = (self.pagewidth * mm, self.pageheight * mm)) 51 pdf = reportlab.pdfgen.canvas.Canvas(file(outpat % (i + 1), 'w'), pagesize = (self.pagewidth * mm, self.pageheight * mm))
53 pdfs.append(pdf) 52 pdfs.append(pdf)
154 index += 1 153 index += 1
155 154
156 return note2slot, slot2note 155 return note2slot, slot2note
157 156
158 def emitnote(self, pdfs, slot, start, notelen): 157 def emitnote(self, pdfs, slot, start, notelen):
159 x = start * self.timescale + self.margin 158 x = start * self.timescale + self.lmargin
160 pageidx = int(x / self.pagewidth) 159 pageidx = int(x / self.pagewidth)
161 x = x % self.pagewidth 160 x = x % self.pagewidth
162 y = self.offset + slot * self.pitch 161 y = self.offset + slot * self.pitch + (self.pitch - self.slotsize) / 2
163 w = notelen * self.timescale 162 w = notelen * self.timescale
164 h = self.pitch 163 h = self.slotsize
165 164
166 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h) 165 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
167 w1 = w 166 w1 = w
168 # Check if the note crosses a page 167 # Check if the note crosses a page
169 if x + w > self.pagewidth: 168 if x + w > self.pagewidth: